]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/log.vala
Notes about remotising devices
[gregoa/zavai.git] / src / log.vala
index a3ef9a05b6fd69b9937bb7364f0aea6e7885a939..eb78363990ba2f2e26a2df82b115c9f355abd83d 100644 (file)
@@ -47,7 +47,7 @@ public class Waypoint : Object
     public void writeInside(FileStream outfd)
     {
         var t = Time.gm(ts);
-        outfd.printf("   <time>%s</time>", t.format("%Y-%m-%dT%H:%M:%SZ"));
+        outfd.printf("   <time>%s</time>\n", t.format("%Y-%m-%dT%H:%M:%SZ"));
     }
 }
 
@@ -58,14 +58,15 @@ public class LogEntry : Waypoint
 
     public LogEntry()
     {
-        Object();
+        base();
     }
 
     public void write(FileStream outfd)
     {
-        outfd.printf("   <wpt lat=\"%f\" lon=\"%f\">", lat, lon);
+        outfd.printf("   <wpt lat=\"%f\" lon=\"%f\">\n", lat, lon);
         writeInside(outfd);
-        outfd.puts("   </wpt>");
+        outfd.printf("   <name>%s</name>\n", Markup.escape_text(msg));
+        outfd.puts("   </wpt>\n");
     }
 }
 
@@ -73,14 +74,14 @@ public class TrackEntry : Waypoint
 {
     public TrackEntry()
     {
-        Object();
+        base();
     }
 
     public void write(FileStream outfd)
     {
-        outfd.printf("   <trkpt lat=\"%f\" lon=\"%f\">", lat, lon);
+        outfd.printf("   <trkpt lat=\"%f\" lon=\"%f\">\n", lat, lon);
         writeInside(outfd);
-        outfd.puts("   </trkpt>");
+        outfd.puts("   </trkpt>\n");
     }
 }
 
@@ -89,13 +90,15 @@ public class Log : Object
 {
     public uint id;
     public string tag;
+    public string title;
     public List<LogEntry> entries;
     public List<TrackEntry> track;
 
-    public Log(uint id, string tag)
+    public Log(uint id, string tag, string title)
     {
         this.id = id;
         this.tag = tag;
+        this.title = title;
         entries = null;
         track = null;
     }
@@ -107,60 +110,73 @@ public class Log : Object
         entries.append(entry);
     }
 
+    public void add_trackpoint()
+    {
+        track.append(new TrackEntry());
+    }
+
     public void save()
     {
         if (entries == null) return;
 
         // Directory where we save the log
         string dir = config.homedir + "/log-" + tag;
+        DirUtils.create(dir, 0777);
 
         // First try with a plain name
         var t = Time.local(entries.data.ts);
-        string basename = t.format("%Y%m%d-%H%M%S");
+        string basename = dir + "/" + t.format("%Y%m%d-%H%M%S");
 
-        string name = basename + ".gpx";
+        string pathname = 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);
+        for (int i = 1; FileUtils.test(pathname, FileTest.EXISTS); ++i)
+            pathname = "%s-%d.gpx".printf(basename, i);
 
         // Write out
         var outfd = FileStream.open(pathname, "w");
+        if (outfd == null)
+        {
+            zavai.log.error("opening " + pathname + ": " + strerror(errno));
+            return;
+        }
+
         write(outfd);
         outfd.flush();
     }
 
     protected void writeTrack(FileStream outfd)
     {
-        outfd.puts("   <trk>");
-        outfd.puts("     <trkseg>");
+        outfd.puts("   <trk>\n");
+        outfd.puts("     <trkseg>\n");
         for (weak List<TrackEntry> i = track; i != null; i = i.next)
             i.data.write(outfd);
-        outfd.puts("     </trkseg>");
-        outfd.puts("   </trk>");
+        outfd.puts("     </trkseg>\n");
+        outfd.puts("   </trk>\n");
     }
 
     protected void writeEntries(FileStream outfd)
     {
-        outfd.puts("   <wpt>");
+        outfd.puts("   <wpt>\n");
         for (weak List<LogEntry> i = entries; i != null; i = i.next)
             i.data.write(outfd);
-        outfd.puts("   </wpt>");
+        outfd.puts("   </wpt>\n");
     }
 
     protected void write(FileStream outfd)
     {
-        outfd.puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
-        outfd.puts(" <gpx");
-        outfd.puts("     version=\"1.0\"");
+        outfd.puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
+        outfd.puts("<gpx version=\"1.0\"\n");
         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\">");
+        outfd.puts("     xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
+        outfd.puts("     xmlns=\"http://www.topografix.com/GPX/1/0\"\n");
+        outfd.puts("     xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
+        outfd.puts("  <metadata>\n");
+        outfd.printf("    <name>%s</name>\n", Markup.escape_text(title));
+        outfd.puts("  </metadata>\n");
         if (track != null) writeTrack(outfd);
         if (entries != null) writeEntries(outfd);
-        outfd.puts(" </gpx>");
+        outfd.puts(" </gpx>\n");
     }
 }
 
@@ -177,6 +193,22 @@ public class Logger : Resource, Object
         zavai.registry.register(this);
     }
 
+    protected void start_trace()
+    {
+        gps.gps.pos_changed += on_pos_changed;
+    }
+
+    protected void end_trace()
+    {
+        gps.gps.pos_changed -= on_pos_changed;
+    }
+
+    protected void on_pos_changed()
+    {
+        for (weak List<Log> i = logs; i != null; i = i.next)
+            i.data.add_trackpoint();
+    }
+
     protected uint gen_seq()
     {
         // Increase avoiding 0 on rollover
@@ -217,10 +249,12 @@ public class Logger : Resource, Object
         return null;
     }
 
-    public uint start(string tag)
+    public uint start(string tag, string title)
     {
+        bool was_empty = (logs == null);
         uint id = gen_seq();
-        logs.append(new Log(id, tag));
+        logs.append(new Log(id, tag, title));
+        if (was_empty) start_trace();
         return id;
     }
 
@@ -235,11 +269,12 @@ public class Logger : Resource, Object
     {
         Log log = pop(id);
         log.save();
+        if (log == null) end_trace();
     }
 
     public void instant(string tag, string msg)
     {
-        var log = new Log(0, tag);
+        var log = new Log(0, tag, msg);
         log.add(msg);
         log.save();
     }