X-Git-Url: https://git.ucc.asn.au/?p=uccdoor.git;a=blobdiff_plain;f=server.py;h=69a0d7e295e344e5543ccd267398d4243dc854eb;hp=25e8ff8edfe6b21199c6bb51f5318f3742b3f833;hb=617af409792df2be2bcc87660cdf605bf3008f8d;hpb=71000ffa8a503792eeac8b19f86c9ff7dd69baab diff --git a/server.py b/server.py index 25e8ff8..69a0d7e 100755 --- a/server.py +++ b/server.py @@ -7,39 +7,50 @@ # 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()) rfh, wfh = latclient.get_fh() results = {'error': retries, 'closed': retries, 'open': retries} test_string = "got wombles?" + syslog.syslog(142, "start check_LAT_service:" + servicename) # Only return after n(retries) consistent results, although not necessarily # in order. This means we poll at least (retries) and up to (retries ** 2) times. # This is mtearle's original code. No idea if it's the Right Thing. while True: + syslog.syslog(142, "running c_L_s loop:" + servicename) try: wfh.write(test_string) wfh.flush() except: results['error'] -= 1 if results['error'] == 0: return -1 + syslog.syslog(142, "c_L_s write error:" + servicename) continue + syslog.syslog(142, "c_Ls written data to " + servicename) rr, wr, er = select([rfh], [], [], 3.0) if rfh not in rr: results['open'] -= 1 if results['open'] == 0: return 1 + syslog.syslog(142, "c_L_s open: " + servicename) continue + syslog.syslog(142, "c_L_s select() succeeded: " + servicename) recv = rfh.read(len(test_string)) + syslog.syslog(142, "c_L_s read() succeeded: " + servicename) if recv <> test_string: + syslog.syslog(142, "c_L_s not the data we sent " + servicename) results['error'] -= 1 if results['error'] == 0: return -1 continue results['closed'] -= 1 + syslog.syslog(142, "c_L_s closed " + servicename) if results['closed'] == 0: return 0 class Door(dbus.service.Object): @@ -56,8 +67,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: @@ -71,7 +86,8 @@ class Door(dbus.service.Object): self.status_changed(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 +99,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