client-json: produce slightly more valid JSON
[uccdoor.git] / server.py
index 25e8ff8..91fa630 100755 (executable)
--- a/server.py
+++ b/server.py
@@ -7,10 +7,12 @@
 
 # statuses: 1 means open circuit, 0 means closed circuit, -1 means error
 
+import syslog
 import dbus, dbus.service
 import gobject
 from LATClient import LATClient
 from select import select
+from threading import Timer
 
 def check_LAT_service(servicename, retries=3):
     latclient = LATClient(service=servicename.upper())
@@ -56,8 +58,12 @@ class Door(dbus.service.Object):
         object_path = '/au/asn/ucc/doors/%s' % doorname
         dbus.service.Object.__init__(self, bus, object_path)
         
-        # get initial state
-        self.poll()
+        # get initial state in a new thread
+        self.timeout = Timer(0, self.poll)
+        # daemon threads will be killed when the mainloop exits
+        # the timeout threads will inherit this value
+        self.timeout.setDaemon(True)
+        self.timeout.start()
     
     def poll(self):
         try:
@@ -66,12 +72,16 @@ class Door(dbus.service.Object):
             newstatus = -1
         
         if newstatus != self.status:
+            syslog.syslog(142, "status changed detected for %s; status: %i, newstatus: %i" % self.service, self.status, newstatus)
             self.status = newstatus
             # emit signal
             self.status_changed(newstatus)
+            # back off if broken
+            self.interval = [10,10,20][newstatus]
         
         # set up timeout again
-        gobject.timeout_add_seconds(self.interval, self.poll)
+        self.timeout = Timer(self.interval, self.poll)
+        self.timeout.start()
     
     @dbus.service.signal('au.asn.ucc.DoorInterface', signature='n')
     def status_changed(self, newstatus):
@@ -83,6 +93,7 @@ class Door(dbus.service.Object):
         return self.status
 
 if __name__ == '__main__':
+    gobject.threads_init()
     doors = ('uccdoor', 'unisfadoor', 'chdoor', 'mrdoor', 'uccpir')
     
     from dbus.mainloop.glib import DBusGMainLoop

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