client-signal.py: example client that listens for signals
[uccdoor.git] / server.py
1 #! /usr/bin/env python
2 # UCC Door Server - hardware interface server
3 # reads status of reed switches and provides a D-BUS interface to read them
4 # David Adam <[email protected]>
5 # Released under an MIT-style license; see COPYING for details.
6
7 # statuses: 1 means open circuit, 0 means closed circuit, -1 means error
8
9 import dbus, dbus.service
10 import gobject
11
12 class Door(dbus.service.Object):
13     
14     def __init__(self, doorname, bus):
15         self.interval = 10 # seconds
16         self.service = doorname
17         self.status = -1
18         
19         # set up D-BUS service name
20         object_path = '/au/asn/ucc/doors/%s' % doorname
21         dbus.service.Object.__init__(self, bus, object_path)
22         
23         # get initial state
24         self.poll()
25     
26     def poll(self):
27         # check LAT
28         # XXX to be added
29         
30         newstatus = -1
31         if newstatus != self.status:
32             self.status = newstatus
33             # emit signal
34             self.status_changed(newstatus)
35         
36         # set up timeout again
37         gobject.timeout_add_seconds(self.interval, self.poll)
38     
39     @dbus.service.signal('au.asn.ucc.DoorInterface', signature='n')
40     def status_changed(self, newstatus):
41         pass
42     
43     @dbus.service.method('au.asn.ucc.DoorInterface', in_signature='',
44                          out_signature='n')
45     def get_status(self):
46         return self.status
47
48 if __name__ == '__main__':
49     doors = ('uccdoor', 'unisfadoor', 'chdoor', 'mrdoor', 'uccpir')
50     
51     from dbus.mainloop.glib import DBusGMainLoop
52     DBusGMainLoop(set_as_default=True)
53     # get on the bus
54     system_bus = dbus.SystemBus()
55     system_bus.request_name('au.asn.ucc.DoorServer')
56     
57     door_objects = []
58
59     for door in doors:
60         door_objects.append(Door(door, system_bus))
61
62     # engage!
63     loop = gobject.MainLoop()
64     try:
65         loop.run()
66     except KeyboardInterrupt:
67         loop.quit()
68     except:
69         pass

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