bluez/test/test-heartrate

109 lines
2.7 KiB
Plaintext
Raw Normal View History

2012-09-25 22:27:46 +08:00
#!/usr/bin/python
from __future__ import absolute_import, print_function, unicode_literals
'''
Heart Rate Monitor test script
'''
from optparse import OptionParser, make_option
2012-09-25 22:27:46 +08:00
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
2012-09-25 22:27:46 +08:00
BUS_NAME = 'org.bluez'
HEARTRATE_MANAGER_INTERFACE = 'org.bluez.HeartRateManager1'
HEARTRATE_WATCHER_INTERFACE = 'org.bluez.HeartRateWatcher1'
HEARTRATE_INTERFACE = 'org.bluez.HeartRate1'
2012-09-25 22:27:46 +08:00
class Watcher(dbus.service.Object):
@dbus.service.method(HEARTRATE_WATCHER_INTERFACE,
2012-09-25 22:27:46 +08:00
in_signature="oa{sv}", out_signature="")
def MeasurementReceived(self, device, measure):
print("Measurement received from %s" % device)
print("Value: ", measure["Value"])
if "Energy" in measure:
print("Energy: ", measure["Energy"])
if "Contact" in measure:
print("Contact: ", measure["Contact"])
if "Interval" in measure:
for i in measure["Interval"]:
print("Interval: ", i)
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> [cmd]" % (sys.argv[0]))
print("Possible commands:")
print("\tReset")
sys.exit(1)
managed_objects = bluezutils.get_managed_objects()
adapter = bluezutils.find_adapter_in_objects(managed_objects,
options.adapter)
adapter_path = adapter.object_path
heartrateManager = dbus.Interface(bus.get_object(BUS_NAME,
adapter_path), HEARTRATE_MANAGER_INTERFACE)
2012-09-25 22:27:46 +08:00
path = "/test/watcher"
heartrateManager.RegisterWatcher(path)
device = bluezutils.find_device_in_objects(managed_objects,
options.address,
options.adapter)
device_path = device.object_path
2012-09-25 22:27:46 +08:00
heartrate = dbus.Interface(bus.get_object(BUS_NAME, device_path),
HEARTRATE_INTERFACE)
2012-09-25 22:27:46 +08:00
watcher = Watcher(bus, path)
dev_prop = dbus.Interface(bus.get_object(BUS_NAME, device_path),
2012-10-14 00:56:19 +08:00
"org.freedesktop.DBus.Properties")
properties = dev_prop.GetAll(HEARTRATE_INTERFACE)
2012-09-25 22:27:46 +08:00
if "Location" in properties:
print("Sensor location: %s" % properties["Location"])
else:
print("Sensor location is not supported")
if len(args) > 0:
if args[0] == "Reset":
reset_sup = properties["ResetSupported"]
if reset_sup:
heartrate.Reset()
else:
print("Reset not supported")
sys.exit(1)
else:
print("unknown command")
sys.exit(1)
mainloop = GObject.MainLoop()
2012-09-25 22:27:46 +08:00
mainloop.run()