Allow to have more than one notification type in Service
authorEnrico Zini <enrico@enricozini.org>
Mon, 15 Jun 2009 15:18:10 +0000 (23:18 +0800)
committerEnrico Zini <enrico@enricozini.org>
Mon, 15 Jun 2009 15:18:10 +0000 (23:18 +0800)
plugins/50_sat_monitor.py
zavai/gps.py
zavai/registry.py

index 41be4b0bf4a0b5787664f51344211b25d59c8965..e44108463806dbf3b5c363fddecf2586d8f11318 100644 (file)
@@ -58,10 +58,10 @@ class SatelliteMonitor(zavai.Applet):
     def start(self, *args):
         if self.gps is None:
             self.init()
-        self.gps.monitor.connect(self.on_ubxdebug_packet)
+        self.gps.monitor.connect("satellites", self.on_ubxdebug_packet)
 
     def stop(self, *args):
-        self.gps.monitor.disconnect(self.on_ubxdebug_packet)
+        self.gps.monitor.disconnect("satellites", self.on_ubxdebug_packet)
 
     def on_ubxdebug_packet(self, clid, length, data):
         # In zhone it is cbUBXDebugPacket
index f0d11f244f81d0f34235be16363b1c1499829612..bb551097979ec2eb3a247c8c593a3ddb73461c37 100755 (executable)
@@ -25,7 +25,7 @@ import zavai
 
 class GPSMonitor(zavai.Service):
     def __init__(self, gps):
-        super(GPSMonitor, self).__init__()
+        super(GPSMonitor, self).__init__(["satellites"])
 
         self.gps = gps
         self.gps_ubx = gps.gps_ubx
@@ -69,10 +69,10 @@ class GPSMonitor(zavai.Service):
 
     def on_ubxdebug_packet(self, clid, length, data):
         zavai.info("gps monitor: UBX debug packet")
-        self.notify(clid, length, data)
+        self.notify("satellites", clid, length, data)
 
     def start(self):
-        self.gps.request(self)
+        self.gps.connect("gps", self)
         # TODO: find out how come sometimes these events are not sent
         self.gps.bus.add_signal_receiver(
             self.on_satellites_changed, 'SatellitesChanged', 'org.freedesktop.Gypsy.Satellite',
@@ -89,19 +89,20 @@ class GPSMonitor(zavai.Service):
         self.gps.bus.remove_signal_receiver(
             self.on_ubxdebug_packet, 'DebugPacket', 'org.freesmartphone.GPS.UBX',
             'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-        self.gps.release(self)
+        self.gps.disconnect("gps", self)
 
 
 class GPSPosition(zavai.Service):
     def __init__(self, gps):
+        super(GPSPosition, self).__init__(["position"])
         self.gps = gps
 
     def on_position_changed(self, fields, tstamp, lat, lon, alt):
         zavai.info("gps position: position changed")
-        self.notify(fields, tstamp, lat, lon, alt)
+        self.notify("position", fields, tstamp, lat, lon, alt)
 
     def start(self):
-        self.gps.request(self)
+        self.gps.connect("gps", self)
         self.gps.bus.add_signal_receiver(
             self.on_position_changed, 'PositionChanged', 'org.freedesktop.Gypsy.Position',
             'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
@@ -110,12 +111,12 @@ class GPSPosition(zavai.Service):
         self.gps.bus.remove_signal_receiver(
             self.on_position_changed, 'PositionChanged', 'org.freedesktop.Gypsy.Position',
             'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-        self.gps.release(self)
+        self.gps.disconnect("gps", self)
 
 # For a list of dbus services, look in /etc/dbus-1/system.d/
 class GPS(zavai.Service):
     def __init__(self, registry, name):
-        super(GPS, self).__init__()
+        super(GPS, self).__init__(["gps"])
 
         self.bus = registry.resource("dbus.system_bus")
 
@@ -199,6 +200,7 @@ class GPS(zavai.Service):
 class GPX(zavai.Service):
     "Write GPX track and waypoint files"
     def __init__(self, registry, name):
+        super(GPX, self).__init__(["gpx"])
         self.registry = registry
         self.trk = None
         self.wpt = None
@@ -223,13 +225,13 @@ class GPX(zavai.Service):
         zavai.info("Starting GPX trace subsystem")
         self.started = True
         gps = self.registry.resource("gps")
-        gps.position.connect(self.on_position_changed)
+        gps.position.connect("gps", self.on_position_changed)
 
     def stop(self):
         if not self.started: return
         zavai.info("Stopping GPX trace subsystem")
         gps = self.registry.resource("gps")
-        gps.position.disconnect(self.on_position_changed)
+        gps.position.disconnect("gps", self.on_position_changed)
         self.stop_track()
         self.started = False
 
index 285045f507ac3165f3e85221e0c4fe7041330fcf..ec0a4a2c4f3b29c9bc497de4c6b1bedecc63bf77 100644 (file)
@@ -162,8 +162,13 @@ class Resource(object):
 class Service(Resource):
     "Service that is activated only when someone is listening"
     def __init__(self):
-        super(Service, self).__init__()
-        self.callbacks = set()
+        """
+        Initialise a service that can emit signals for the given event types
+        """
+        super(Service, self).__init__(types = [])
+        self.callbacks = dict()
+        for t in types:
+            self.callbacks[t] = set()
 
     def shutdown(self):
         self.stop()
@@ -176,21 +181,26 @@ class Service(Resource):
         "Deactivate the service"
         pass
 
-    def notify(self, *args, **kw):
+    def notify(self, type, *args, **kw):
         "Call all callbacks with the given parameters"
-        for cb in self.callbacks:
+        for cb in self.callbacks[type]:
             cb(*args, **kw)
 
-    def connect(self, callback):
+    def has_callbacks(self):
+        for i in self.callbacks.values():
+            if i: return True
+        return False
+
+    def connect(self, type, callback):
         "Connect a callback to this resource, activating it if needed"
-        do_start = not self.callbacks
-        self.callbacks.add(callback)
+        do_start = not self.has_callbacks()
+        self.callbacks[type].add(callback)
         if do_start:
             self.start()
 
-    def disconnect(self, callback):
+    def disconnect(self, type, callback):
         "Disconnect a callback to this resource, activating it if needed"
-        if not self.callbacks: return
+        if not self.has_callbacks(): return
         self.callbacks.discard(callback)
-        if not self.callbacks:
+        if not self.has_callbacks():
             self.stop()