Try to use fso for scheduling alarms
[gregoa/zavai.git] / src / clock.vala
index 90c8785245de1d52b58b6c481a24e3cfa67b0c41..8dec2e334c7fc242dd909f3627b9fe96a5ea2f16 100644 (file)
@@ -59,6 +59,8 @@ public class Clock: zavai.Service
     protected time_t chosen_time;
     protected SourceType chosen_type;
 
+       protected dynamic DBus.Object otimed_alarm;
+       protected dynamic DBus.Object notification;
     protected SList<Alarm> alarms;
 
     // Ticks once a minute
@@ -78,12 +80,42 @@ public class Clock: zavai.Service
                        "org.freesmartphone.ogpsd",
                        "/org/freedesktop/Gypsy",
                        "org.freedesktop.Gypsy.Time");
+
+        // FSO alarm system
+           otimed_alarm = zavai.registry.sbus.get_object(
+                       "org.freesmartphone.otimed",
+                       "/org/freesmartphone/Time/Alarm",
+                       "org.freesmartphone.Time.Alarm");
+        notification = zavai.registry.sbus.get_object(
+            "org.freesmartphone",
+            "org/freesmartphone/Notification",
+            "org.freesmartphone.Notification");
+        notification.Alarm += on_alarm;
        }
 
     public void schedule(Alarm a)
     {
         alarms.insert_sorted(a, alarm_compare);
         zavai.log.info("Next alarm: " + alarms.data.label + " at " + Time.local(alarms.data.deadline).to_string());
+        otimed_reschedule();
+    }
+
+    private void otimed_reschedule()
+    {
+        if (alarms != null)
+        {
+            try {
+                otimed_alarm.ClearAlarm();
+                otimed_alarm.SetAlarm(alarms.data.deadline);
+            } catch (Error e) {
+                zavai.log.error("Cannot reschedule alarms: " + e.message);
+            }
+        }
+    }
+
+    private void on_alarm()
+    {
+        check_alarms();
     }
 
     public void check_alarms()
@@ -97,6 +129,8 @@ public class Clock: zavai.Service
             zavai.log.info("Triggering " + a.label);
             a.trigger();
         }
+
+        otimed_reschedule();
     }
 
        private void on_gps_time(dynamic DBus.Object pos, int t)