client-xmpp.py: import XMPP/Jabber client
authorDavid Adam (zanchey) <[email protected]>
Sat, 10 Oct 2009 14:48:53 +0000 (22:48 +0800)
committerDavid Adam (zanchey) <[email protected]>
Thu, 15 Oct 2009 12:26:43 +0000 (20:26 +0800)
client-xmpp.py [new file with mode: 0755]

diff --git a/client-xmpp.py b/client-xmpp.py
new file mode 100755 (executable)
index 0000000..b9af4cb
--- /dev/null
@@ -0,0 +1,77 @@
+#! /usr/bin/env python
+# UCC Door Server - Jabber/XMPP door client
+# David Adam <[email protected]>
+# Released under an MIT-style license; see COPYING for details.
+
+import dbus
+import gobject
+import xmpp
+import os
+
+server = 'ucc.asn.au'
+password = open(os.environ['HOME']+ '/.jabberpw', 'r').readline().strip()
+
+class Door(object):
+    # XXX: if the server process is not running, this client will terminate
+    # XXX: if the server process stops, there is not notification, and old
+    #      statuses will be persisted until it is restarted
+    
+    def __init__(self, doorname):
+        self.presences = {-1: {'show': 'dnd', 'status': 'busted'},
+            0: {'show': 'away', 'status': 'closed'},
+            1: {'show': 'chat', 'status': 'open'},}
+        
+        if 'pir' in doorname:
+            self.presences = {-1: {'show': 'dnd', 'status': 'busted'},
+                0: {'show': 'away', 'status': 'nothing moves'},
+                1: {'show': 'chat', 'status': 'there is movement'},}
+        
+        self.state = -1
+        
+        # connect to hardware server over D-BUS
+        self.remoteobj = system_bus.get_object('au.asn.ucc.DoorServer',
+                                               '/au/asn/ucc/doors/' + doorname)
+        self.remoteobj.connect_to_signal('status_changed', self.state_changed,
+                                         dbus_interface='au.asn.ucc.DoorInterface')
+        
+        # connect to the Jabber server
+        self.jabberclient = xmpp.Client(server, debug=None)
+        self.jabberclient.connect()
+        self.jabberclient.auth(doorname, password)
+        # make self available via jabber, don't bother getting the contact list
+        self.jabberclient.sendInitPresence(requestRoster = 0)
+        
+        try:
+            new_state = self.remoteobj.get_status(dbus_interface='au.asn.ucc.DoorInterface')
+        except:
+            pass
+        
+        self.state_changed(new_state)
+    
+    def state_changed(self, new_state):
+        self.state = new_state
+        # look up the dictionary object for the current state, then construct
+        # a presence object using that as a keyword argument
+        new_presence = xmpp.dispatcher.Presence(**self.presences[new_state])
+        self.jabberclient.send(new_presence)
+
+if __name__ == '__main__':
+    
+    from dbus.mainloop.glib import DBusGMainLoop
+    DBusGMainLoop(set_as_default=True)
+    # get on the bus
+    system_bus = dbus.SystemBus()
+    
+    # dictionary to hold door names and objects
+    doors = {'uccdoor': None, 'unisfadoor': None, 'chdoor': None, 'mrdoor': None, 'uccpir': None}
+    
+    # create and save door objects
+    for doorname in doors.keys():
+        doors[doorname] = Door(doorname)
+    
+    # engage!
+    loop = gobject.MainLoop()
+    try:
+        loop.run()
+    except KeyboardInterrupt:
+        loop.quit()

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