3 from __future__ import absolute_import, print_function, unicode_literals
6 Thermometer test script
9 from optparse import OptionParser, make_option
13 import dbus.mainloop.glib
15 from gi.repository import GObject
17 import gobject as GObject
20 BUS_NAME = 'org.bluez'
21 THERMOMETER_MANAGER_INTERFACE = 'org.bluez.ThermometerManager1'
22 THERMOMETER_WATCHER_INTERFACE = 'org.bluez.ThermometerWatcher1'
23 THERMOMETER_INTERFACE = 'org.bluez.Thermometer1'
25 class Watcher(dbus.service.Object):
26 @dbus.service.method(THERMOMETER_WATCHER_INTERFACE,
27 in_signature="oa{sv}", out_signature="")
28 def MeasurementReceived(self, device, measure):
29 print("%s measurement received from %s" % (measure["Measurement"], device))
30 print("Exponent: ", measure["Exponent"])
31 print("Mantissa: ", measure["Mantissa"])
32 print("Unit: ", measure["Unit"])
35 print("Time: ", measure["Time"])
38 print("Type: ", measure["Type"])
40 def properties_changed(interface, changed, invalidated):
41 if interface != THERMOMETER_INTERFACE:
43 for name, value in changed.iteritems():
44 print("Property %s changed: %s" % (name, str(value)))
46 if __name__ == "__main__":
47 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
49 bus = dbus.SystemBus()
52 make_option("-i", "--adapter", action="store",
53 type="string", dest="adapter"),
54 make_option("-b", "--device", action="store",
55 type="string", dest="address"),
58 parser = OptionParser(option_list=option_list)
60 (options, args) = parser.parse_args()
62 if not options.address:
63 print("Usage: %s [-i <adapter>] -b <bdaddr> [command]" % (sys.argv[0]))
64 print("Possible commands:")
65 print("\tEnableIntermediateMeasurement")
68 managed_objects = bluezutils.get_managed_objects()
69 adapter = bluezutils.find_adapter_in_objects(managed_objects,
71 adapter_path = adapter.object_path
73 thermometer_manager = dbus.Interface(bus.get_object(BUS_NAME,
74 adapter_path), THERMOMETER_MANAGER_INTERFACE)
76 device = bluezutils.find_device_in_objects(managed_objects,
79 device_path = device.object_path
81 bus.add_signal_receiver(properties_changed, bus_name=BUS_NAME,
83 dbus_interface="org.freedesktop.DBus.Properties",
84 signal_name="PropertiesChanged")
86 path = "/test/watcher"
87 watcher = Watcher(bus, path)
89 thermometer_manager.RegisterWatcher(path)
92 if args[0] == "EnableIntermediateMeasurement":
93 thermometer_manager.EnableIntermediateMeasurement(path)
95 print("unknown command")
98 mainloop = GObject.MainLoop()