client-signal.py: example client that listens for signals
[uccdoor.git] / client-signal.py
diff --git a/client-signal.py b/client-signal.py
new file mode 100644 (file)
index 0000000..ba0da59
--- /dev/null
@@ -0,0 +1,36 @@
+# UCC Door Server - example synchronous client
+# David Adam <[email protected]>
+# Released under an MIT-style license; see COPYING for details.
+
+import dbus
+import gobject
+
+def signal_handler(new_status, path):
+    door = path.split('/')[-1:][0]
+    # globals!
+    if door in doors:
+        print "Received new status %d from %s" % (new_status, door)
+
+if __name__ == '__main__':
+    
+    from dbus.mainloop.glib import DBusGMainLoop
+    DBusGMainLoop(set_as_default=True)
+    # get on the bus
+    system_bus = dbus.SystemBus()
+    
+    doors = {'uccdoor': None, 'unisfadoor': None, 'chdoor': None, 'mrdoor': None, 'uccpir': None}
+    
+    # According to the D-BUS documentation, you shouldn't create an object just
+    # to listen to a signal, as this may cause server activation. However, your
+    # client might like to do more clever things with the object.
+    for door in doors.keys():
+        doors[door] = system_bus.get_object('au.asn.ucc.DoorServer', '/au/asn/ucc/doors/%s' % door)
+    
+    for door, remote_object in doors.items():
+        remote_object.connect_to_signal('status_changed', signal_handler,
+                                        dbus_interface='au.asn.ucc.DoorInterface',
+                                        path_keyword='path')
+    
+    # engage!
+    loop = gobject.MainLoop()
+    loop.run()
\ No newline at end of file

UCC git Repository :: git.ucc.asn.au