More debugging
[gregoa/zavai.git] / src / app_gps.vala
1 /*
2  * gpx_trace - zavai GPX trace functions
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 ui {
25 namespace gps {
26
27 /*
28 class GPSOn(gtk.ToggleAction):
29     states = [_("GPS always on"), _("GPS on when needed")]
30
31     def __init__(self, registry, **kw):
32         self.state = 0
33         super(GPSOn, self).__init__("menu.main.gps.alwayson", self.states[self.state], None, None)
34
35         self.registry = registry
36         self.set_active(False)
37
38         self.connect("toggled", self.on_toggle)
39
40     def on_toggle(self, *args):
41         self.state = (self.state + 1) % len(self.states)
42         self.set_property("label", self.states[self.state])
43         if self.get_active():
44             self.start()
45         else:
46             self.stop()
47
48     def start(self):
49         self.registry.resource("gps").connect("gps", self)
50
51     def stop(self):
52         self.registry.resource("gps").disconnect("gps", self)
53
54 class GPXTracer(gtk.ToggleAction):
55     states = [_("Start GPX trace"), _("Stop GPX trace")]
56
57     def __init__(self, registry, **kw):
58         self.state = 0
59         super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
60
61         self.registry = registry
62         self.set_active(False)
63
64         self.connect("toggled", self.on_toggle)
65
66     def on_toggle(self, *args):
67         self.state = (self.state + 1) % len(self.states)
68         self.set_property("label", self.states[self.state])
69         if self.get_active():
70             self.start()
71         else:
72             self.stop()
73
74     def start(self):
75         zavai.info("GPX trace started")
76         self.registry.resource("gpx").connect("gpx", self)
77
78     def stop(self):
79         zavai.info("GPX trace ended")
80         self.registry.resource("gpx").disconnect("gpx", self)
81 */
82
83 public class Waypoint : BigButton
84 {
85         public Waypoint()
86         {
87                 set_label("Take waypoint");
88                 zavai.gps.gpx.tracking_changed += on_gpx_tracking_changed;
89                 clicked += on_clicked;
90                 set_sensitive(zavai.gps.gpx.tracking);
91         }
92
93         protected void on_gpx_tracking_changed(zavai.gps.GPX gpx, bool new_state)
94         {
95                 stderr.printf("Toggled %d\n", (int)new_state);
96                 set_sensitive(new_state);
97         }
98
99         protected void on_clicked()
100         {
101                 stderr.printf("Activate\n");
102                 zavai.gps.gpx.waypoint();
103         }
104 }
105
106 /*
107 class GPXAudioTracer(gtk.ToggleAction):
108     states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
109
110     def __init__(self, registry, **kw):
111         self.state = 0
112         super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
113
114         self.registry = registry
115         self.recorder = zavai.Recorder(registry)
116         self.set_active(False)
117
118         self.connect("toggled", self.on_toggle)
119
120     def shutdown(self):
121         self.recorder.stop()
122         super(GPXAudioTracer, self).shutdown()
123
124     def on_toggle(self, *args):
125         self.state = (self.state + 1) % len(self.states)
126         self.set_property("label", self.states[self.state])
127         if self.get_active():
128             self.start()
129         else:
130             self.stop()
131
132     def start(self):
133         zavai.info("GPX trace started")
134         gpx = self.registry.resource("gpx")
135         gpx.connect("gpx", self)
136         gpx.add_activity_monitor(self.on_gpx_activity_changed)
137
138     def stop(self):
139         zavai.info("GPX trace ended")
140         gpx = self.registry.resource("gpx")
141         gpx.disconnect("gpx", self)
142         self.recorder.stop()
143         gpx.del_activity_monitor(self.on_gpx_activity_changed)
144
145     def on_gpx_activity_changed(self, gpx, state):
146         if state:
147             self.recorder.start(gpx.basename + ".wav")
148         else:
149             self.recorder.stop()
150 */
151
152 public class GPSRequestLink : Gtk.ToggleButton
153 {
154         protected string service_name;
155         protected string label_start;
156         protected string label_stop;
157         protected Gtk.StatusIcon status_icon;
158         protected int fix_status = 0;
159
160         public GPSRequestLink()
161         {
162                 service_name = "gps";
163                 label_start = "Keep GPS on";
164                 label_stop = "Stop keeping GPS on";
165                 set_size_request(0, zavai.config.min_button_height);
166                 toggled += on_toggled;
167
168                 set_label(get_active() ? label_stop : label_start);
169
170                 //tooltip = "GPS status";
171                 try {
172                         fix_status = zavai.gps.gps.device.GetFixStatus();
173                 } catch (Error e) {
174                         fix_status = 0;
175                 }
176                 zavai.gps.gps.device.FixStatusChanged += on_fix_status_changed;
177
178                 // GPS status icon
179                 status_icon = new Gtk.StatusIcon();
180                 status_icon.set_visible(true);
181                 status_icon.activate += on_status_activate;
182                 update_icon();
183         }
184
185         protected void update_icon()
186         {
187                 string name;
188                 if (fix_status == 2 || fix_status == 3)
189                         name = zavai.config.icondir + "/" + (get_active() ? "gps_fix_on.png" : "gps_fix_off.png");
190                 else
191                         name = zavai.config.icondir + "/" + (get_active() ? "gps_nofix_on.png" : "gps_nofix_off.png");
192                 stderr.printf("load icon from %s\n", name);
193                 status_icon.set_from_file(name);
194         }
195
196         private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
197         {
198                 this.fix_status = fix_status;
199                 update_icon();
200         }
201
202         private void on_toggled(Gtk.Button src)
203         {
204                 Service s = zavai.registry.gets(service_name);
205                 if (get_active())
206                         s.request("servicerequestlink");
207                 else
208                         s.release("servicerequestlink");
209                 set_label(get_active() ? label_stop : label_start);
210                 update_icon();
211         }
212
213         private void on_status_activate()
214         {
215                 set_active(!get_active());
216         }
217 }
218
219 static string[] SAT_QI_NAMES;
220
221 public class SatelliteMonitor : Applet
222 {
223         Gtk.ListStore store;
224         Gtk.TreeView view;
225
226         public SatelliteMonitor()
227         {
228                 _label = "Satellite monitor";
229
230                 store = new Gtk.ListStore(11, typeof(string), typeof(string), typeof(string),
231                               typeof(string), typeof(string), typeof(string), typeof(string),
232                               typeof(string), typeof(string), typeof(string), typeof(string));
233                 view = new Gtk.TreeView();
234                 view.set_model(store);
235
236                 var renderer = new Gtk.CellRendererText();
237                 view.insert_column_with_attributes(-1, "CH", renderer, "text", 0, null);
238                 view.insert_column_with_attributes(-1, "ID", renderer, "text", 1, null);
239                 view.insert_column_with_attributes(-1, "SN", renderer, "text", 2, null);
240                 view.insert_column_with_attributes(-1, "ELE", renderer, "text", 3, null);
241                 view.insert_column_with_attributes(-1, "AZI", renderer, "text", 4, null);
242                 view.insert_column_with_attributes(-1, "Used", renderer, "text", 5, null);
243                 view.insert_column_with_attributes(-1, "Diff", renderer, "text", 6, null);
244                 view.insert_column_with_attributes(-1, "Alm", renderer, "text", 7, null);
245                 view.insert_column_with_attributes(-1, "Eph", renderer, "text", 8, null);
246                 view.insert_column_with_attributes(-1, "Bad", renderer, "text", 9, null);
247                 view.insert_column_with_attributes(-1, "Status", renderer, "text", 10, null);
248                 pack_start(view, true, true, 0);
249         }
250
251         public override void start()
252         {
253                 zavai.gps.monitor.sat_info += on_sat_info;
254                 zavai.gps.monitor.request("app.satmonitor");
255         }
256
257         public override void stop()
258         {
259                 zavai.gps.monitor.release("app.satmonitor");
260                 zavai.gps.monitor.sat_info -= on_sat_info;
261         }
262
263         public void on_sat_info(PtrArray< HashTable<string, Value?> > data)
264         {
265                 store.clear();
266 stderr.printf("CH ID SN ELE AZI Used Diff Alm Eph Bad Status\n");
267                 for (int i = 0; i < data.len; ++i)
268                 {
269                         HashTable<string, Value?> sv  = (HashTable<string, Value?>)data.pdata[i];
270
271                         Value v = sv.lookup("Flags");
272                         int flags = (int)v;
273
274                         v = sv.lookup("chn");
275                         int chn = (int)v;
276
277                         v = sv.lookup("SVID");
278                         int svid = (int)v;
279
280                         v = sv.lookup("CNO");
281                         int cno = (int)v;
282
283                         v = sv.lookup("Elev");
284                         int elev = (int)v;
285
286                         v = sv.lookup("Azim");
287                         int azim = (int)v;
288
289                         bool used = (flags & 0x01) != 0;
290                         bool diff = (flags & 0x02) != 0;
291                         bool almoreph = (flags & 0x04) != 0;
292                         bool eph = (flags & 0x08) != 0;
293                         bool bad = (flags & 0x10) != 0;
294                         v = sv.lookup("QI");
295                         string sqi = "Unknown";
296
297                         int qi = (int)v;
298                         if (qi < SAT_QI_NAMES.length)
299                                 sqi = "%i: %s".printf(qi, SAT_QI_NAMES[qi]);
300
301                         Gtk.TreeIter iter;
302                         store.append(out iter);
303                         store.set(iter, "%2d".printf(chn),
304                                         "%2d".printf(svid),
305                                         "%2d".printf(cno),
306                                         "%3d".printf(elev),
307                                         "%3d".printf(azim),
308                                         used ? "used" : "",
309                                         diff ? "diff" : "",
310                                         almoreph ? "alm" : "",
311                                         eph ? "eph" : "",
312                                         bad ? "bad" : "", sqi);
313 stderr.printf("%2d %2d %2d %3d %3d %d %d %d %d %d %s\n", chn, svid, cno, elev, azim, (int)used, (int)diff, (int)almoreph, (int)eph, (int)bad, sqi);
314                 }
315     /*
316     def on_ubxdebug_packet(self, clid, length, data):
317         # In zhone it is cbUBXDebugPacket
318         #if clid == "NAV-STATUS" and data:
319         #    i = ["%s: %d" % (k, data[0][k]) for k in sorted(data[0].keys())]
320         #    zavai.info("Status:", " ".join(i))
321         ##    if data[0]['TTFF']:
322         ##        zavai.info("TTFF: %f", data[0]['TTFF']/1000.0)
323         if clid == "NAV-SVINFO":
324             self.handle_ubx_sat_data(data[1:])
325         #else:
326         #    zavai.info("gps got ubxdebug packet", clid)
327         #    zavai.info("DATA:", data)
328     */
329
330         }
331 }
332
333
334 public void init()
335 {
336         SAT_QI_NAMES = new string[] {
337                 "idle",
338                 "searching",
339                 "signal acquired",
340                 "signal unusable",
341                 "code lock",
342                 "code&carrier lock",
343                 "code&carrier lock",
344                 "receiving data"
345         };
346
347     /*
348     registry.register(GPXAudioTracer(registry))
349     registry.register(GPXWaypoint(registry))
350     */
351
352     // Apps
353     var window_list = new SatelliteMonitor();
354     zavai.registry.register_applet("ui.gps.monitor", window_list);
355
356     var menu_waypoint = new Waypoint();
357     var menu_gpsrequest = new GPSRequestLink();
358                 //label_on = "Stop GPX trace";
359                 //label_off = "Start GPX trace";
360                 //label_on = "Stop GPS monitor";
361                 //label_off = "Start GPS monitor";
362                 //label_on = "Stop GPS position tracking";
363                 //label_off = "Start GPS position tracking";
364                 //label_on = "Stop keeping GPS on";
365                 //label_off = "Keep GPS on";
366
367     // Menus
368     var menu_gps = new zavai.Menu("GPS");
369     //menu_gps.add_applet("app.debug.useless");
370     menu_gps.add_service_toggle("gps.gpx", "Start GPX trace", "Stop GPX trace");
371     menu_gps.add_applet("ui.gps.monitor");
372     menu_gps.add_widget(menu_waypoint);
373     menu_gps.add_widget(menu_gpsrequest);
374
375     zavai.registry.register_menu("menu.gps", menu_gps);
376     zavai.registry.getmenu("menu.main").add_applet("menu.gps");
377 }
378
379 }
380 }
381 }