]> ToastFreeware Gitweb - gregoa/zavai.git/blob - src/clock.vala
Fixes to alarm scheduling
[gregoa/zavai.git] / src / clock.vala
1 /*
2  * clock - clock resource for zavai
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 using GLib;
22
23 namespace zavai {
24 namespace clock {
25
26 public enum SourceType
27 {
28     SYSTEM,
29     GPS
30 }
31
32 public class Alarm : Object
33 {
34     public signal void trigger();
35
36     public time_t deadline;
37     public string label;
38
39     public Alarm(time_t deadline, string label)
40     {
41         this.deadline = deadline;
42         this.label = label;
43     }
44 }
45
46 private int alarm_compare(void* a, void* b)
47 {
48     return (int)(((Alarm*)a)->deadline - ((Alarm*)b)->deadline);
49 }
50
51 [DBus (name = "org.freesmartphone.Notification")]
52 public class AlarmNotification : Object {
53     public void Alarm () {
54         clock.check_alarms();
55     }
56 }
57
58 public class Clock: zavai.Service
59 {
60     protected time_t last_gps_time;
61     protected time_t last_gps_time_system_time;
62     protected time_t last_system_time;
63         protected dynamic DBus.Object gps_time;
64     protected uint system_time_timeout;
65     protected time_t last_minute;
66     protected time_t chosen_time;
67     protected SourceType chosen_type;
68     protected AlarmNotification listener;
69
70         protected dynamic DBus.Object otimed_alarm;
71         protected dynamic DBus.Object notification;
72     protected SList<Alarm> alarms;
73
74     // Ticks once a minute
75     public signal void minute_changed(long time, SourceType source);
76     public signal void schedule_changed();
77
78         public Clock()
79         {
80                 name = "clock";
81         alarms = null;
82         listener = new AlarmNotification();
83         last_minute = 0;
84         last_gps_time = 0;
85         last_gps_time_system_time = 0;
86         last_system_time = time_t();
87         chosen_time = last_system_time;
88         
89                 gps_time = zavai.registry.sbus.get_object(
90                         "org.freesmartphone.ogpsd",
91                         "/org/freedesktop/Gypsy",
92                         "org.freedesktop.Gypsy.Time");
93
94         // FSO alarm system
95             otimed_alarm = zavai.registry.sbus.get_object(
96                         "org.freesmartphone.otimed",
97                         "/org/freesmartphone/Time/Alarm",
98                         "org.freesmartphone.Time.Alarm");
99         if (otimed_alarm == null)
100             zavai.log.error("ALARM IS NULL");
101
102         zavai.registry.sbus.register_object("/", listener);
103
104         //notification = zavai.registry.sbus.get_object(
105         //    "org.freesmartphone",
106         //    "org/freesmartphone/Notification",
107         //    "org.freesmartphone.Notification");
108         //notification.Alarm += on_alarm;
109         }
110
111     public Alarm? next_alarm()
112     {
113         if (alarms == null)
114             return null;
115         return alarms.data;
116     }
117
118     public void schedule(Alarm a)
119     {
120         alarms.insert_sorted(a, alarm_compare);
121         otimed_reschedule();
122     }
123
124     private void otimed_reschedule()
125     {
126         if (alarms != null)
127         {
128             zavai.log.info("Scheduling next alarm: " + alarms.data.label + " at " + Time.local(alarms.data.deadline).to_string());
129             
130             try {
131                 otimed_alarm.ClearAlarm(zavai.registry.bus_name);
132             } catch (Error e) {
133                 zavai.log.error("Cannot clear alarms: " + e.message);
134             }
135             try {
136                 otimed_alarm.SetAlarm(zavai.registry.bus_name, (int)alarms.data.deadline);
137             } catch (Error e) {
138                 zavai.log.error("Cannot reschedule alarms: " + e.message);
139             }
140         } else
141             zavai.log.info("No alarms left to reschedule");
142         schedule_changed();
143     }
144
145     public void check_alarms()
146     {
147         last_system_time = time_t();
148         update_time();
149         while (alarms != null && alarms.data.deadline <= chosen_time)
150         {
151             Alarm a = alarms.data;
152             alarms.remove(a);
153             zavai.log.info("Triggering " + a.label);
154             a.trigger();
155         }
156
157         otimed_reschedule();
158     }
159
160         private void on_gps_time(dynamic DBus.Object pos, int t)
161         {
162         if (t == 0)
163         {
164             last_gps_time_system_time = 0;
165             update_time();
166         } else {
167             last_gps_time = (time_t)t;
168             last_gps_time_system_time = time_t();
169             update_time();
170         }
171         }
172
173     private bool on_system_time()
174     {
175         last_system_time = time_t();
176         update_time();
177         return true;
178     }
179
180     private void update_time()
181     {
182         if (last_gps_time_system_time + 10 > last_system_time)
183         {
184             chosen_time = last_gps_time;
185             chosen_type = SourceType.GPS;
186         }
187         else
188         {
189             chosen_time = last_system_time;
190             chosen_type = SourceType.SYSTEM;
191         }
192         if (chosen_time / 60 != last_minute)
193         {
194             last_minute = chosen_time / 60;
195             minute_changed(chosen_time, chosen_type);
196         }
197     }
198
199         /// Request GPS resource
200         public override void start()
201         {
202                 if (started) return;
203
204         system_time_timeout = Timeout.add(5000, on_system_time);
205                 gps_time.TimeChanged += on_gps_time;
206         last_system_time = time_t();
207         update_time();
208
209                 base.start();
210         }
211
212         public override void stop()
213         {
214                 if (!started) return;
215
216         Source.remove(system_time_timeout);
217                 gps_time.TimeChanged -= on_gps_time;
218
219                 base.stop();
220         }
221 }
222
223 public Clock clock = null;
224
225 public void init()
226 {
227     clock = new Clock();
228
229         zavai.registry.register_service(clock);
230 }
231
232 }
233 }