Try to do the GPSMonitor
authorEnrico Zini <enrico@enricozini.org>
Tue, 14 Jul 2009 16:43:30 +0000 (17:43 +0100)
committerEnrico Zini <enrico@enricozini.org>
Tue, 14 Jul 2009 16:43:30 +0000 (17:43 +0100)
vala/gps.vala

index 57e6b36431244680c1c817a6e6b31cb5809042ff..d8a823bf5c89cb533e23796fedc7e49bd81e6dd1 100644 (file)
@@ -22,80 +22,109 @@ using GLib;
 
 namespace zavai {
 
-/*
 public class GPSMonitor : zavai.Service
 {
-//     def __init__(self, gps):
-//         super(GPSMonitor, self).__init__(["satellites"])
-// 
-//         self.gps = gps
-//         self.gps_ubx = gps.gps_ubx
-// 
-//         # This piece of machinery is taken from Zhone
-//         self.debug_busy = None
-//         self.debug_want = set( ["NAV-STATUS", "NAV-SVINFO"] )
-//         self.debug_have = set()
-//         self.debug_error = set()
+       static const int NAV_STATUS=0;
+       static const int NAV_SVINFO=1;
+       static const int NAV_MAX=2;
+
+       zavai.GPS* gps;
+       dynamic DBus.Object ubx;
+       string[] filters = { "NAV-STATUS", "NAV_SVINFO" };
+
+       int debug_busy;
+       int debug_want;
+       int debug_have;
+       int debug_error;
+
+
+       public GPSMonitor(zavai.Registry r)
+       {
+               gps = (zavai.GPS)r.gets("gps");
+               ubx = r.sbus.get_object(
+                       "org.freesmartphone.ogpsd",
+                       "/org/freedesktop/Gypsy",
+                       "org.freesmartphone.GPS.UBX");
+
+               // This piece of machinery is taken from Zhone
+               debug_busy = -1;
+               debug_want = (1 << NAV_STATUS) | (1 << NAV_SVINFO);
+               debug_have = 0;
+               debug_error = 0;
+       }
 
-       protected void debug_update()
+       protected void debug_update() throws GLib.Error
        {
-//         if self.debug_busy is None:
-//             pending = self.debug_want - self.debug_have - self.debug_error
-//             if pending:
-//                 self.debug_busy = pending.pop()
-//                 self.gps_ubx.SetDebugFilter(
-//                     self.debug_busy,
-//                     True,
-//                     reply_handler=self.on_debug_reply,
-//                     error_handler=self.on_debug_error,
-//                 )
+               if (debug_busy != -1)
+                       return;
+               int pending = debug_want & (~debug_have) & (~debug_error);
+               if (pending == 0)
+                       return;
+               for (int i = 0; i < NAV_MAX; ++i)
+                       if ((pending & (1<<i)) != 0)
+                       {
+                               debug_busy = i;
+                               break;
+                       }
+
+               ubx.SetDebugFilter(
+                       filters[debug_busy],
+                       true,
+                       on_debug_reply
+//                     on_debug_error
+               );
        }
 
-       protected void debug_request()
+       protected void debug_request() throws GLib.Error
        {
-//         self.debug_have = set()
-//         self.debug_update()
+               debug_have = 0;
+               debug_update();
        }
 
-       protected void on_debug_reply()
+       protected void on_debug_reply() throws GLib.Error
        {
-//         self.debug_have.add(self.debug_busy)
-//         self.debug_busy = None
-//         self.debug_update()
+               debug_have |= (1<<debug_busy);
+               debug_busy = -1;
+               debug_update();
        }
 
-       protected void on_debug_error(e):
+       protected void on_debug_error(string e) throws GLib.Error
        {
-//         zavai.info(e, "error while requesting debug packet %s" % self.debug_busy)
-//         self.debug_error.add(self.debug_busy)
-//         self.debug_busy = None
-//         self.debug_update()
+               string name = debug_busy == -1 ? "none" : filters[debug_busy];
+               zavai.log.error("error while requesting debug packet " + name + ": " + e);
+               debug_error |= (1<<debug_busy);
+               debug_busy = -1;
+               debug_update();
        }
 
+       /*
        protected void on_satellites_changed(satellites)
        {
 //         zavai.info("gps monitor: satellites changed")
 //         self.debug_request()
        }
+       */
 
-       protected void on_ubxdebug_packet(clid, length, data)
+       protected void on_ubxdebug_packet(string clid, int length, DBus.Object data)
        {
-//         zavai.info("gps monitor: UBX debug packet")
+               zavai.log.info("gps monitor: UBX debug packet");
 //         self.notify("satellites", clid, length, data)
        }
 
        protected override void start()
        {
-//         self.gps.connect("gps", self)
+               gps->request("GPSMonitor");
 //         # 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',
 //             'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-//         self.gps.bus.add_signal_receiver(
-//             self.on_ubxdebug_packet, 'DebugPacket', 'org.freesmartphone.GPS.UBX',
-//             'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-//         self.debug_request()
-               base.start();
+               ubx.DebugPacket += on_ubxdebug_packet;
+               try {
+                       debug_request();
+                       base.start();
+               } catch (GLib.Error e) {
+                       zavai.log.error(e.message);
+               }
        }
 
        protected override void stop()
@@ -103,14 +132,11 @@ public class GPSMonitor : zavai.Service
 //         self.gps.bus.remove_signal_receiver(
 //             self.on_satellites_changed, 'SatellitesChanged', 'org.freedesktop.Gypsy.Satellite',
 //             'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-//         self.gps.bus.remove_signal_receiver(
-//             self.on_ubxdebug_packet, 'DebugPacket', 'org.freesmartphone.GPS.UBX',
-//             'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-//         self.gps.disconnect("gps", self)
+               ubx.DebugPacket -= on_ubxdebug_packet;
+               gps->release("GPSMonitor");
                base.stop();
        }
 }
-*/
 
 
 public class GPSPosition : zavai.Service