1594a80830d3dddf8466b23abc9247e7c2f5edc5
[gregoa/zavai.git] / plugins / 50_sat_monitor.py
1 # sat_monitor - zavai satellite monitor
2 #
3 # Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19 import gtk
20 import sys
21 import gettext
22 _ = gettext.gettext
23 import zavai
24
25 SAT_QI_NAMES = {
26     0: _("idle"),
27     1: _("searching"),
28     2: _("signal acquired"),
29     3: _("signal unusable"),
30     4: _("code lock"),
31     5: _("code&carrier lock"),
32     6: _("code&carrier lock"),
33     7: _("receiving data")
34 }
35
36 class SatelliteMonitor(gtk.VBox, zavai.Resource):
37     def __init__(self, registry, name, **kw):
38         super(SatelliteMonitor, self).__init__()
39
40         self.gps = registry.resource("gps")
41
42         self.store = gtk.ListStore(str, str, str, str, str, str, str, str, str, str, str)
43         self.view = gtk.TreeView(self.store)
44
45         renderer = gtk.CellRendererText()
46         for idx, name in enumerate((_("CH"), _("ID"), _("SN"), _("ELE"), _("AZI"),
47                      _("Used"), _("Diff"), _("Alm"), _("Eph"),
48                      _("Bad"), _("Status"))):
49             col = gtk.TreeViewColumn(name)
50             col.pack_start(renderer, False)
51             col.add_attribute(renderer, "text", idx)
52             self.view.append_column(col)
53
54         self.back = registry.menu_link("gps", _("Back"))
55         self.back.connect("clicked", self.stop)
56
57         self.pack_start(self.view, True, True)
58         self.pack_start(self.back, False, False)
59
60     def shutdown(self):
61         self.stop()
62
63     def start(self, *args):
64         self.gps.monitor.connect(self.on_ubxdebug_packet)
65
66     def stop(self, *args):
67         self.gps.monitor.disconnect(self.on_ubxdebug_packet)
68
69     def on_ubxdebug_packet(self, clid, length, data):
70         # In zhone it is cbUBXDebugPacket
71         #if clid == "NAV-STATUS" and data:
72         #    i = ["%s: %d" % (k, data[0][k]) for k in sorted(data[0].keys())]
73         #    zavai.info("Status:", " ".join(i))
74         ##    if data[0]['TTFF']:
75         ##        zavai.info("TTFF: %f", data[0]['TTFF']/1000.0)
76         if clid == "NAV-SVINFO":
77             self.handle_ubx_sat_data(data[1:])
78         #else:
79         #    zavai.info("gps got ubxdebug packet", clid)
80         #    zavai.info("DATA:", data)
81
82     def handle_ubx_sat_data(self, ubxinfo):
83         #zavai.info("CH ID SN ELE AZI Used Diff Alm Eph Bad Status")
84         self.store.clear()
85         for sv in ubxinfo:
86             if sv["CNO"] == 0: continue
87             svid = sv["SVID"]
88             used = sv["Flags"] & 0x01
89             diff = sv["Flags"] & 0x02
90             almoreph = sv["Flags"] & 0x04
91             eph = sv["Flags"] & 0x08
92             bad = sv["Flags"] & 0x10
93             qi = ("%i: " % sv["QI"]) + SAT_QI_NAMES.get(sv["QI"], _("Unknown"))
94             self.store.append([
95                 "%2d" % sv["chn"],
96                 "%2d" % sv["SVID"],
97                 "%2d" % sv["CNO"],
98                 "%3d" % sv["Elev"],
99                 "%3d" % sv["Azim"],
100                 used and "used" or "",
101                 diff and "diff" or "",
102                 almoreph and "alm" or "",
103                 eph and "eph" or "",
104                 bad and "bad" or "",
105                 qi])
106
107 def start_monitor(registry):
108     monitor = registry.resource("app.satellite_monitor")
109     registry.resource("app").show_widget("app.satellite_monitor")
110     monitor.start()
111
112 def init(conf = None, registry = None, **kw):
113     registry.register("app.satellite_monitor", SatelliteMonitor)
114     menu_gps = registry.menu("main.gps")
115
116     monitor = gtk.Button(_("Monitor"))
117     monitor.connect("clicked", lambda *args: start_monitor(registry))
118     menu_gps.add_child(monitor)
119
120     # TODO: automate this in registry
121     registry.menu("main").add_child(registry.menu_link("main.gps", _("GPS")))
122