summaryrefslogtreecommitdiff
path: root/test/test-thermometer
blob: 7e67c234d65cc74283e24d1e827b099d0d709d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python

from __future__ import absolute_import, print_function, unicode_literals

'''
Thermometer test script
'''

from optparse import OptionParser, make_option
import sys
import dbus
import dbus.service
import dbus.mainloop.glib
try:
  from gi.repository import GObject
except ImportError:
  import gobject as GObject
import bluezutils

BUS_NAME = 'org.bluez'
THERMOMETER_MANAGER_INTERFACE = 'org.bluez.ThermometerManager1'
THERMOMETER_WATCHER_INTERFACE = 'org.bluez.ThermometerWatcher1'
THERMOMETER_INTERFACE = 'org.bluez.Thermometer1'

class Watcher(dbus.service.Object):
	@dbus.service.method(THERMOMETER_WATCHER_INTERFACE,
					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"])

		if "Time" in measure:
			print("Time: ", measure["Time"])

		if "Type" in measure:
			print("Type: ", measure["Type"])

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)))

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")
		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)

	device = bluezutils.find_device_in_objects(managed_objects,
								options.address,
								options.adapter)
	device_path = device.object_path

	bus.add_signal_receiver(properties_changed, bus_name=BUS_NAME,
			path=device_path,
			dbus_interface="org.freedesktop.DBus.Properties",
			signal_name="PropertiesChanged")

	path = "/test/watcher"
	watcher = Watcher(bus, path)

	thermometer_manager.RegisterWatcher(path)

	if len(args) > 0:
		if args[0] == "EnableIntermediateMeasurement":
			thermometer_manager.EnableIntermediateMeasurement(path)
		else:
			print("unknown command")
			sys.exit(1)

	mainloop = GObject.MainLoop()
	mainloop.run()