]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/clock.vala
Notes about remotising devices
[gregoa/zavai.git] / src / clock.vala
index d4d390860880fe8ca16beb48b8d0f76b0af19bbe..d08717bcd9ad82e9e5562c4d8e49ec38a4d6bd1a 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * clock - clock resource for zavai
  *
- * 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
@@ -136,12 +136,85 @@ public class ZavaiClock : Object {
     }
 }
 
+public class AlarmTriggerInfo
+{
+    public uint id;
+    public string label;
+    public bool acked;
+    public bool canceled;
+
+    public AlarmTriggerInfo(string label)
+    {
+        id = 0;
+        this.label = label;
+        acked = false;
+        canceled = false;
+    }
+}
+
+public class AlarmTriggerQueue : zavai.Service
+{
+    protected List<AlarmTriggerInfo> queue;
+
+    public signal void triggered(AlarmTriggerInfo info);
+    public signal void acked(AlarmTriggerInfo info);
+    public signal void canceled(AlarmTriggerInfo info);
+
+    public AlarmTriggerQueue()
+    {
+        queue = new List<AlarmTriggerInfo>();
+    }
+
+    public uint enqueue_trigger(AlarmTriggerInfo info)
+    {
+        // Reuse IDs from the associated logger object
+        info.id = zavai.log.log.start("alarm", "Alarm " + info.label);
+        queue.append(info);
+        if (queue.data.id == info.id)
+            triggered(queue.data);
+        return info.id;
+    }
+
+    protected void done_with_first()
+    {
+        var first = queue.data;
+        queue.remove_link(queue);
+        if (queue != null)
+            triggered(queue.data);
+    }
+
+    public void ack(AlarmTriggerInfo info)
+    {
+        if (queue == null || info.id != queue.data.id) return;
+        if (!info.acked && !info.canceled)
+        {
+            info.acked = true;
+            acked(info);
+            zavai.log.log.add(info.id, "alarm acknowledged");
+            zavai.log.log.end(info.id);
+        }
+        done_with_first();
+    }
+
+    public void cancel(AlarmTriggerInfo info)
+    {
+        if (queue == null || info.id != queue.data.id) return;
+        if (!info.acked && !info.canceled)
+        {
+            info.canceled = true;
+            canceled(info);
+            zavai.log.log.add(info.id, "alarm canceled");
+            zavai.log.log.end(info.id);
+        }
+        done_with_first();
+    }
+}
+
 public class Clock: zavai.Service
 {
     protected time_t last_gps_time;
     protected time_t last_gps_time_system_time;
     protected time_t last_system_time;
-    protected dynamic DBus.Object gps_time;
     protected uint system_time_timeout;
     protected time_t last_minute;
     protected time_t chosen_time;
@@ -155,7 +228,6 @@ public class Clock: zavai.Service
     // Ticks once a minute
     public signal void minute_changed(long time, SourceType source);
     public signal void schedule_changed(Alarm? next);
-    public signal void alarm_triggered(string label);
 
     public Clock()
     {
@@ -168,11 +240,6 @@ public class Clock: zavai.Service
         last_system_time = time_t();
         chosen_time = last_system_time;
 
-        gps_time = zavai.registry.sbus.get_object(
-                "org.freesmartphone.ogpsd",
-                "/org/freedesktop/Gypsy",
-                "org.freedesktop.Gypsy.Time");
-
         // FSO alarm system
         otimed_alarm = zavai.registry.sbus.get_object(
                 "org.freesmartphone.otimed",
@@ -190,7 +257,8 @@ public class Clock: zavai.Service
     public void notify_alarm(string label)
     {
         stderr.printf("Notifying %s\n", label);
-        alarm_triggered(label);
+        AlarmTriggerInfo info = new AlarmTriggerInfo(label);
+        alarm_trigger_queue.enqueue_trigger(info);
         schedule_changed(next_alarm());
     }
 
@@ -213,7 +281,7 @@ public class Clock: zavai.Service
         schedule_changed(next_alarm());
     }
 
-    private void on_gps_time(dynamic DBus.Object pos, int t)
+    private void on_gps_time(uint t)
     {
         if (t == 0)
         {
@@ -258,7 +326,7 @@ public class Clock: zavai.Service
         if (started) return;
 
         system_time_timeout = Timeout.add(5000, on_system_time);
-        gps_time.TimeChanged += on_gps_time;
+        zavai.gps.gps.time_changed += on_gps_time;
         last_system_time = time_t();
         update_time();
 
@@ -270,17 +338,19 @@ public class Clock: zavai.Service
         if (!started) return;
 
         Source.remove(system_time_timeout);
-        gps_time.TimeChanged -= on_gps_time;
+        zavai.gps.gps.time_changed -= on_gps_time;
 
         base.stop();
     }
 }
 
 public Clock clock = null;
+public AlarmTriggerQueue alarm_trigger_queue = null;
 
 public void init()
 {
     clock = new Clock();
+    alarm_trigger_queue = new AlarmTriggerQueue();
 }
 
 }