bluez/test/test-thermometer

98 lines
2.6 KiB
Plaintext
Raw Normal View History

2011-11-22 20:10:15 +08:00
#!/usr/bin/python
from __future__ import absolute_import, print_function, unicode_literals
2011-11-22 20:10:15 +08:00
'''
Thermometer test script
'''
import gobject
import sys
import dbus
import dbus.service
import dbus.mainloop.glib
from optparse import OptionParser, make_option
import bluezutils
2011-11-22 20:10:15 +08:00
BUS_NAME = 'org.bluez'
THERMOMETER_MANAGER_INTERFACE = 'org.bluez.ThermometerManager1'
THERMOMETER_WATCHER_INTERFACE = 'org.bluez.ThermometerWatcher1'
THERMOMETER_INTERFACE = 'org.bluez.Thermometer1'
2011-11-22 20:10:15 +08:00
class Watcher(dbus.service.Object):
@dbus.service.method(THERMOMETER_WATCHER_INTERFACE,
2012-10-09 19:19:49 +08:00
in_signature="oa{sv}", out_signature="")
def MeasurementReceived(self, device, measure):
print("%s measurement received from %s" % (measure["Measurement"], device))
print("Exponent: ", measure["Exponent"])
print("Mantissa: ", measure["Mantissa"])
print("Unit: ", measure["Unit"])
2011-11-22 20:10:15 +08:00
if "Time" in measure:
print("Time: ", measure["Time"])
2011-11-22 20:10:15 +08:00
if "Type" in measure:
print("Type: ", measure["Type"])
2011-11-22 20:10:15 +08:00
def properties_changed(interface, changed, invalidated):
if interface != THERMOMETER_INTERFACE:
return
for name, value in changed.iteritems():
print("Property %s changed: %s" % (name, str(value)))
2011-11-22 20:10:15 +08:00
if __name__ == "__main__":
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
option_list = [
make_option("-i", "--adapter", action="store",
type="string", dest="adapter"),
make_option("-b", "--device", action="store",
type="string", dest="address"),
]
parser = OptionParser(option_list=option_list)
(options, args) = parser.parse_args()
if not options.address:
print("Usage: %s [-i <adapter>] -b <bdaddr> [command]" % (sys.argv[0]))
print("Possible commands:")
print("\tEnableIntermediateMeasurement")
2011-11-22 20:10:15 +08:00
sys.exit(1)
managed_objects = bluezutils.get_managed_objects()
adapter = bluezutils.find_adapter_in_objects(managed_objects,
options.adapter)
adapter_path = adapter.object_path
thermometer_manager = dbus.Interface(bus.get_object(BUS_NAME,
adapter_path), THERMOMETER_MANAGER_INTERFACE)
2012-10-09 19:19:49 +08:00
device = bluezutils.find_device_in_objects(managed_objects,
options.address,
options.adapter)
device_path = device.object_path
2011-11-22 20:10:15 +08:00
bus.add_signal_receiver(properties_changed, bus_name=BUS_NAME,
path=device_path,
dbus_interface="org.freedesktop.DBus.Properties",
signal_name="PropertiesChanged")
2011-11-22 20:10:15 +08:00
path = "/test/watcher"
watcher = Watcher(bus, path)
2012-10-09 19:19:49 +08:00
thermometer_manager.RegisterWatcher(path)
2011-11-22 20:10:15 +08:00
if len(args) > 0:
if args[0] == "EnableIntermediateMeasurement":
2012-10-09 19:19:49 +08:00
thermometer_manager.EnableIntermediateMeasurement(path)
2011-11-22 20:10:15 +08:00
else:
print("unknown command")
2011-11-22 20:10:15 +08:00
sys.exit(1)
mainloop = gobject.MainLoop()
mainloop.run()