Try to acquire the GPS
authorEnrico Zini <enrico@enricozini.org>
Tue, 14 Jul 2009 15:30:54 +0000 (16:30 +0100)
committerEnrico Zini <enrico@enricozini.org>
Tue, 14 Jul 2009 15:30:54 +0000 (16:30 +0100)
vala/Makefile
vala/gps.vala [new file with mode: 0644]
vala/registry.vala
vala/zavai.vala

index 326e64492caa64de881e4077c728b2204910bb25..1735c600c02f6ae1e0c0cbb8daa2306a33db3508 100644 (file)
@@ -1,5 +1,5 @@
-SOURCES=registry.vala zavai.vala
-VFLAGS=--pkg gee-1.0
+SOURCES=log.vala registry.vala gps.vala zavai.vala
+VFLAGS=-g --pkg gee-1.0 --pkg dbus-glib-1
 LDFLAGS=$(shell pkg-config --libs gee-1.0 glib-2.0)
 
 all: zavai
diff --git a/vala/gps.vala b/vala/gps.vala
new file mode 100644 (file)
index 0000000..94cc7b0
--- /dev/null
@@ -0,0 +1,402 @@
+/*
+ * gps - gps resource for zavai
+ *
+ * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+using GLib;
+
+namespace zavai {
+
+// #import sys
+// import os
+// import os.path
+// import time
+// import dbus
+// import 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()
+
+       protected void debug_update()
+       {
+//         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,
+//                 )
+       }
+
+       protected void debug_request()
+       {
+//         self.debug_have = set()
+//         self.debug_update()
+       }
+
+       protected void on_debug_reply()
+       {
+//         self.debug_have.add(self.debug_busy)
+//         self.debug_busy = None
+//         self.debug_update()
+       }
+
+       protected void on_debug_error(e):
+       {
+//         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()
+       }
+
+       protected void on_satellites_changed(satellites)
+       {
+//         zavai.info("gps monitor: satellites changed")
+//         self.debug_request()
+       }
+
+       protected void on_ubxdebug_packet(clid, length, data)
+       {
+//         zavai.info("gps monitor: UBX debug packet")
+//         self.notify("satellites", clid, length, data)
+       }
+
+       protected override void start()
+       {
+//         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',
+//             '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();
+       }
+
+       protected override void stop()
+       {
+//         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)
+               base.stop();
+       }
+}
+*/
+
+
+public 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("position", fields, tstamp, lat, lon, alt)
+// 
+       public override void start()
+       {
+//         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')
+               base.start();
+       }
+
+       public override void stop()
+       {
+//         self.gps.bus.remove_signal_receiver(
+//             self.on_position_changed, 'PositionChanged', 'org.freedesktop.Gypsy.Position',
+//             'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
+//         self.gps.disconnect("gps", self)
+               base.stop();
+       }
+}
+
+// For a list of dbus services, look in /etc/dbus-1/system.d/
+public class GPS: zavai.Service
+{
+       dynamic DBus.Object usage;
+
+       public GPS(zavai.Registry r)
+       {
+               // see mdbus -s org.freesmartphone.ousaged /org/freesmartphone/Usage
+               r.sbus.get_object(
+                       "org.freesmartphone.ousaged",
+                       "/org/freesmartphone/Usage",
+                       "org.freesmartphone.Usage");
+
+//         # see mdbus -s org.freesmartphone.ogpsd /org/freedesktop/Gypsy
+//         gps = self.bus.get_object('org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy') 
+//         self.gps = dbus.Interface(gps, "org.freedesktop.Gypsy.Device")
+//         self.gps_time = dbus.Interface(gps, "org.freedesktop.Gypsy.Time")
+//         self.gps_position = dbus.Interface(gps, 'org.freedesktop.Gypsy.Position')
+//         self.gps_ubx = dbus.Interface(gps, 'org.freesmartphone.GPS.UBX')
+// 
+//         self.monitor = GPSMonitor(self)
+//         self.position = GPSPosition(self)
+       }
+
+       /// Request GPS resource
+       public override void start()
+       {
+               try {
+                       usage.RequestResource("GPS");
+                       zavai.log.info("Acquired GPS");
+                       base.start();
+               } catch (GLib.Error e) {
+                       zavai.log.error(e.message);
+               }
+       }
+
+       // Release usage of GPS
+       public override void stop()
+       {
+               try {
+                       usage.ReleaseResource("GPS");
+                       zavai.log.info("Released GPS");
+                       base.stop();
+               } catch (GLib.Error e) {
+                       zavai.log.error(e.message);
+               }
+       }
+
+// #    def wait_for_fix(self, callback):
+// #        status = self.gps.GetFixStatus()
+// #        if status in [2, 3]:
+// #            zavai.info("We already have a fix, good.")
+// #            callback()
+// #            return True
+// #        else:
+// #            zavai.info("Waiting for a fix...")
+// #            self.waiting_for_fix = callback
+// #            self.bus.add_signal_receiver(
+// #                self.on_fix_status_changed, 'FixStatusChanged', 'org.freedesktop.Gypsy.Device',
+// #                'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
+// #            return False
+// #
+// #    def on_fix_status_changed(self, status):
+// #        if status not in [2, 3]: return
+// #
+// #        zavai.info("Got GPS fix")
+// #        self.bus.remove_signal_receiver(
+// #            self.on_fix_status_changed, 'FixStatusChanged', 'org.freedesktop.Gypsy.Device',
+// #            'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
+// #
+// #        if self.waiting_for_fix:
+// #            self.waiting_for_fix()
+// #            self.waiting_for_fix = None
+// #
+// 
+// #    def start_recording(self):
+// #        if self.gps_monitor:
+// #            self.gps_monitor.stop()
+// #            self.gps_monitor = None
+// #
+// #        if not self.audio:
+// #            return
+// #
+// #        # Sync system time
+// #        gpstime = self.gps.gps_time.GetTime()
+// #        subprocess.call(["date", "-s", "@%d" % gpstime])
+// #        subprocess.call(["hwclock", "--systohc"])
+// #
+// #        # Compute basename for output files
+// #        self.basename = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(gpstime))
+// #        self.basename = os.path.join(AUDIODIR, self.basename)
+// #
+// #        # Start recording the GPX track
+// #        self.gpx = GPX(self.basename)
+// #        self.gps.track_position(self.on_position_changed)
+// #
+// #        # Start recording in background forking arecord
+// #        self.audio.set_basename(self.basename)
+// #        self.audio.start_recording()
+// #
+// 
+// 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
+//         self.last_pos = None
+//         conf = registry.resource("conf")
+//         self.trackdir = conf.homedir
+//         self.activity_monitors = set()
+// 
+//     def add_activity_monitor(self, cb):
+//         self.activity_monitors.add(cb)
+//         cb(self, self.last_pos is not None)
+// 
+//     def del_activity_monitor(self, cb):
+//         self.activity_monitors.discard(cb)
+// 
+//     def notify_activity_monitors(self):
+//         for mon in self.activity_monitors:
+//             mon(self, self.last_pos is not None)
+// 
+//     def start(self):
+//         zavai.info("Starting GPX trace subsystem")
+//         gps = self.registry.resource("gps")
+//         gps.position.connect("position", self.on_position_changed)
+// 
+//     def stop(self):
+//         zavai.info("Stopping GPX trace subsystem")
+//         gps = self.registry.resource("gps")
+//         gps.position.disconnect("position", self.on_position_changed)
+//         self.stop_track()
+// 
+//     def on_position_changed(self, fields, tstamp, lat, lon, alt):
+//         self.last_pos = (fields, tstamp, lat, lon, alt)
+//         self.trackpoint()
+// 
+//     def start_track(self, tstamp = None, basename = None):
+//         if basename is not None:
+//             self.basename = basename
+//         elif tstamp is not None:
+//             # Compute basename for output files
+//             self.basename = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(tstamp))
+//             self.basename = os.path.join(self.trackdir, self.basename)
+// 
+//         self.trk = open(self.basename + "-trk.gpx", "wt")
+//         print >>self.trk, """<?xml version="1.0" encoding="UTF-8"?>
+// <gpx
+//     version="1.0"
+//     creator="audiomap %s"
+//     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+//     xmlns="http://www.topografix.com/GPX/1/0"
+//     xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
+//   <trk>
+//     <trkseg>""" % zavai.VERSION
+// 
+//         self.wpt = open(self.basename + "-wpt.gpx", "wt")
+//         print >>self.wpt, """<?xml version="1.0" encoding="UTF-8"?>
+// <gpx
+//     version="1.0"
+//     creator="audiomap %s"
+//     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+//     xmlns="http://www.topografix.com/GPX/1/0"
+//     xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">""" % zavai.VERSION
+// 
+//         self.wpt_seq = 1;
+//         self.notify_activity_monitors()
+// 
+//     def stop_track(self):
+//         if self.trk is not None:
+//             print >>self.trk, "</trkseg></trk></gpx>"
+//             self.trk.close()
+//             self.trk = None
+//         if self.wpt is not None:
+//             print >>self.wpt, "</gpx>"
+//             self.wpt.close()
+//             self.wpt = None
+//         self.last_pos = None
+//         self.notify_activity_monitors()
+// 
+//     def trackpoint(self):
+//         "Mark a track point"
+//         if self.last_pos is None:
+//             return
+// 
+//         fields, tstamp, lat, lon, ele = self.last_pos
+// 
+//         if not self.trk:
+//             self.start_track(tstamp)
+// 
+//         print >>self.trk, """<trkpt lat="%f" lon="%f">
+//   <time>%s</time>
+//   <ele>%f</ele>""" % (lat, lon, time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(tstamp)), ele)
+//         #if course is not None: print >>self.trk, "    <course>%f</course>" % course
+//         #if speed is not None: print >>self.trk, "    <speed>%f</speed>" % speed
+//         #if fix is not None: print >>self.trk, "    <fix>%f</fix>" % fix
+//         #if hdop is not None: print >>self.trk, "    <hdop>%f</hdop>" % hdop
+//         print >>self.trk, "</trkpt>"
+// 
+//     def waypoint(self, name = None):
+//         "Mark a waypoint"
+//         if self.last_pos is None:
+//             return
+// 
+//         fields, tstamp, lat, lon, ele = self.last_pos
+// 
+//         if not self.wpt:
+//             self.start_track(tstamp)
+// 
+//         if name is None:
+//             name = "wpt_%d" % self.wpt_seq
+//             self.wpt_seq += 1
+// 
+//         print >>self.wpt, """<wpt lat="%f" lon="%f">
+//     <name>%s</name>
+//     <time>%s</time>
+//     <ele>%f</ele>
+// </wpt>""" % (
+//             lat, lon, name, time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(tstamp)), ele)
+// 
+// 
+// #    def record(self):
+// #        self.audio = Audio(self.make_waypoint)
+// #        self.gps = GPS()
+// #        # Get a fix and start recording
+// #        if not self.gps.wait_for_fix(self.start_recording):
+// #            self.gps_monitor = GPSMonitor(self.gps)
+// #            self.gps_monitor.start()
+// #
+// #    def monitor(self):
+// #        self.audio = None
+// #        self.gps = GPS()
+// #        self.gps_monitor = GPSMonitor(self.gps)
+// #        self.gps_monitor.start()
+// #
+// #
+// #    def make_waypoint(self):
+// #        if self.gpx is None:
+// #            return
+// #        if self.last_pos is None:
+// #            self.last_pos = self.gps.gps_position.GetPosition()
+// #        (fields, tstamp, lat, lon, alt) = self.last_pos
+// #        self.gpx.waypoint(tstamp, lat, lon, alt)
+// #        zavai.info("Making waypoint at %s: %f, %f, %f" % (
+// #            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(tstamp)), lat, lon, alt))
+
+}
+
+}
index c3f337a6d77e96d336b88a843b00419a18a80029..c420972949379ecad36833e94d92285dc7ae66d0 100644 (file)
@@ -124,9 +124,11 @@ public abstract class Service : Resource {
 public class Registry : Resource
 {
        HashMap<string, Resource> members;
+       public DBus.Connection sbus;
 
        construct {
-               members = new HashMap<string, Resource>();
+               members = new HashMap<string, Resource>(str_hash, str_equal);
+               sbus = DBus.Bus.get(DBus.BusType.SYSTEM);
        }
 
        public override void shutdown()
index 43617044ac936fe6801ccec41ae5f9209114170f..64c4a8c8c6c0ec4b34ed91c6ce86570d7853fdb2 100644 (file)
@@ -44,6 +44,10 @@ static int main (string[] args) {
        // Set up zavai
        zavai.Registry registry = new zavai.Registry();
 
+       registry.register("gps", new zavai.GPS(registry));
+
+       registry.gets("gps").start();
+
        // # Register main factories
        // registry.register(dbus_system_bus, "dbus.system_bus")
        // registry.register(conf, "conf")