]> ToastFreeware Gitweb - gregoa/zavai.git/commitdiff
Draft gpx-based logging module
authorEnrico Zini <enrico@enricozini.org>
Tue, 9 Mar 2010 21:36:16 +0000 (22:36 +0100)
committerEnrico Zini <enrico@enricozini.org>
Tue, 9 Mar 2010 21:36:16 +0000 (22:36 +0100)
README
src/gps.vala
src/log.vala
src/zavai.vala

diff --git a/README b/README
index b11f1f38172778fb3ffdf7748704c85a655c7140..fdc2f213a9f1b96da066181cb9ff7c7f926e8e04 100644 (file)
--- a/README
+++ b/README
@@ -170,6 +170,14 @@ TODO list / wish list
     - remember alarm names (on request, maybe with an add feature) and how
       often they are triggered, and show them most frequent first
     - show active alarms and allow to delete them
+ - audio notes
+    - record audio notes, logging start and stop so it gets an associated GPX
+    - shortcut icon in main screen
+ - turn on/off wireless
+    - turn on/off chip
+    - start/stop wicd
+    - start/stop wicd-client
+ - turn on/off bluetooth
  - turn on/off gsm
     - start frameworkd as a subprocess, configured to only do phone
     - go through the dbus motions of turning on this and that, and entering PIN
@@ -213,8 +221,9 @@ TODO list / wish list
     - lua functions to read things
  - zavai-calendar as a separate app
  - zavai-contacts as a separate app
- - next30: don't update if not shown currently on the notebook
- - gtk_calendar_set_detail_func
+ - calendar
+    - next30: don't update if not shown currently on the notebook
+    - gtk_calendar_set_detail_func
  - GPS FixNow mode for sleeping
  - GSM power and network
     - link to open SHR-dialer
@@ -227,12 +236,6 @@ TODO list / wish list
  - GPX waypoint using AUX button
  - GPX + Audio track
  - track EPV (and similar) in gpx and kill trackpoints with bad accuracy
- - Audio note
-    - shortcut icon in main screen
- - Toggle wireless
-    - turn on/off chip
-    - start/stop wicd
-    - start/stop wicd-client
  - Simple wireless scanner (code from guessnet, or minimal wicd client)
  - Suspend phone if not calling and gps is not on and backlight is not on
    (maybe with idle notifier?)
index 50c722f5652e5568e411d6bbf6b65553b90a0c96..c591a16a28cb243b9da4609ad9f2c127429fda25 100644 (file)
@@ -44,6 +44,7 @@ public class GPS: zavai.Service
 
     public int fix_status() { return old_fix_status; }
     public double time() { return old_time; }
+    public weak libgps.data_t info() { return data; }
 
     protected bool on_input_data(IOChannel source, IOCondition condition)
     {
index e9499395ef04a118212ef7e1d298e632b751eec0..a3ef9a05b6fd69b9937bb7364f0aea6e7885a939 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * log - logging functions
  *
- * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
+ * Copyright (C) 2009--2010  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
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+using GLib;
+
 namespace zavai {
 namespace log {
 
+public class Waypoint : Object
+{
+    public time_t ts;
+    public double lat;
+    public double lon;
+
+    public Waypoint()
+    {
+        if (gps.gps.fix_status() != libgps.STATUS_NO_FIX)
+        {
+            lat = gps.gps.info().fix.latitude;
+            lon = gps.gps.info().fix.longitude;
+            ts = (time_t)gps.gps.info().fix.time;
+        } else {
+            // Use 1000 as missing values
+            lat = 1000;
+            lon = 1000;
+            ts = time_t();
+        }
+    }
+
+    public void writeInside(FileStream outfd)
+    {
+        var t = Time.gm(ts);
+        outfd.printf("   <time>%s</time>", t.format("%Y-%m-%dT%H:%M:%SZ"));
+    }
+}
+
+
+public class LogEntry : Waypoint
+{
+    public string msg;
+
+    public LogEntry()
+    {
+        Object();
+    }
+
+    public void write(FileStream outfd)
+    {
+        outfd.printf("   <wpt lat=\"%f\" lon=\"%f\">", lat, lon);
+        writeInside(outfd);
+        outfd.puts("   </wpt>");
+    }
+}
+
+public class TrackEntry : Waypoint
+{
+    public TrackEntry()
+    {
+        Object();
+    }
+
+    public void write(FileStream outfd)
+    {
+        outfd.printf("   <trkpt lat=\"%f\" lon=\"%f\">", lat, lon);
+        writeInside(outfd);
+        outfd.puts("   </trkpt>");
+    }
+}
+
+
+public class Log : Object
+{
+    public uint id;
+    public string tag;
+    public List<LogEntry> entries;
+    public List<TrackEntry> track;
+
+    public Log(uint id, string tag)
+    {
+        this.id = id;
+        this.tag = tag;
+        entries = null;
+        track = null;
+    }
+
+    public void add(string msg)
+    {
+        var entry = new LogEntry();
+        entry.msg = msg;
+        entries.append(entry);
+    }
+
+    public void save()
+    {
+        if (entries == null) return;
+
+        // Directory where we save the log
+        string dir = config.homedir + "/log-" + tag;
+
+        // First try with a plain name
+        var t = Time.local(entries.data.ts);
+        string basename = t.format("%Y%m%d-%H%M%S");
+
+        string name = basename + ".gpx";
+
+        // Find a pathname that does not exist already
+        string pathname = dir + "/" + name;
+        for (int i = 1; FileUtils.test(dir + "/" + name, FileTest.EXISTS); ++i)
+            name = "%s-%d.gpx".printf(basename, i);
+
+        // Write out
+        var outfd = FileStream.open(pathname, "w");
+        write(outfd);
+        outfd.flush();
+    }
+
+    protected void writeTrack(FileStream outfd)
+    {
+        outfd.puts("   <trk>");
+        outfd.puts("     <trkseg>");
+        for (weak List<TrackEntry> i = track; i != null; i = i.next)
+            i.data.write(outfd);
+        outfd.puts("     </trkseg>");
+        outfd.puts("   </trk>");
+    }
+
+    protected void writeEntries(FileStream outfd)
+    {
+        outfd.puts("   <wpt>");
+        for (weak List<LogEntry> i = entries; i != null; i = i.next)
+            i.data.write(outfd);
+        outfd.puts("   </wpt>");
+    }
+
+    protected void write(FileStream outfd)
+    {
+        outfd.puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+        outfd.puts(" <gpx");
+        outfd.puts("     version=\"1.0\"");
+        outfd.printf("     creator=\"zavai %s\"\n", zavai.config.version);
+        outfd.puts("     xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
+        outfd.puts("     xmlns=\"http://www.topografix.com/GPX/1/0\"");
+        outfd.puts("     xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">");
+        if (track != null) writeTrack(outfd);
+        if (entries != null) writeEntries(outfd);
+        outfd.puts(" </gpx>");
+    }
+}
+
+public class Logger : Resource, Object
+{
+    protected List<Log> logs;
+    protected uint seq;
+
+    public Logger()
+    {
+        logs = null;
+        seq = 0;
+
+        zavai.registry.register(this);
+    }
+
+    protected uint gen_seq()
+    {
+        // Increase avoiding 0 on rollover
+        while (true)
+        {
+            if (++seq == 0) ++seq;
+            bool found = false;
+            for (weak List<Log> i = logs; i != null; i = i.next)
+            {
+                if (i.data.id == seq)
+                {
+                    found = true;
+                    break;
+                }
+            }
+            if (!found) break;
+        }
+        return seq;
+    }
+
+    protected weak Log? find(uint id)
+    {
+        for (weak List<Log> i = logs; i != null; i = i.next)
+            if (i.data.id == id)
+                return i.data;
+        return null;
+    }
+
+    protected Log? pop(uint id)
+    {
+        for (weak List<Log> i = logs; i != null; i = i.next)
+            if (i.data.id == id)
+            {
+                Log res = i.data;
+                logs.delete_link(i);
+                return res;
+            }
+        return null;
+    }
+
+    public uint start(string tag)
+    {
+        uint id = gen_seq();
+        logs.append(new Log(id, tag));
+        return id;
+    }
+
+    public void add(uint id, string msg)
+    {
+        Log log = find(id);
+        if (log == null) return;
+        log.add(msg);
+    }
+
+    public void end(uint id)
+    {
+        Log log = pop(id);
+        log.save();
+    }
+
+    public void instant(string tag, string msg)
+    {
+        var log = new Log(0, tag);
+        log.add(msg);
+        log.save();
+    }
+
+    public void shutdown()
+    {
+        while (logs != null)
+        {
+            var log = pop(logs.data.id);
+            log.save();
+        }
+    }
+}
+
 public void error(string s)
 {
        stderr.printf("%s\n", s);
@@ -38,5 +271,12 @@ public void debug(string s)
        stderr.printf("%s\n", s);
 }
 
+Logger log = null;
+
+public void init()
+{
+    log = new Logger();
+}
+
 }
 }
index 0a33c9442fec189ed1f2adbbacbb84f09523513a..968b84c64da55c87b0d3c77d439a8888a420f09f 100644 (file)
@@ -100,6 +100,7 @@ static int main (string[] args) {
        zavai.gsm.init();
        zavai.clock.init();
        zavai.audio.init();
+       zavai.log.init();
 
        /*
        zavai.clock.clock.schedule(new zavai.clock.Alarm(123456, "Second"));