8f82d1f49ea3bafd2d5d53595e374354ac1178dd
[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 from gettext import gettext as _
22 import zavai
23
24 SAT_QI_NAMES = {
25     0: _("idle"),
26     1: _("searching"),
27     2: _("signal acquired"),
28     3: _("signal unusable"),
29     4: _("code lock"),
30     5: _("code&carrier lock"),
31     6: _("code&carrier lock"),
32     7: _("receiving data")
33 }
34
35 class SatelliteMonitor(zavai.Applet):
36     def __init__(self, registry, name, **kw):
37         super(SatelliteMonitor, self).__init__(registry, name, _("Satellite monitor"))
38
39         self.gps = None
40
41         self.store = gtk.ListStore(str, str, str, str, str, str, str, str, str, str, str)
42         self.view = gtk.TreeView(self.store)
43         renderer = gtk.CellRendererText()
44         for idx, name in enumerate((_("CH"), _("ID"), _("SN"), _("ELE"), _("AZI"),
45                      _("Used"), _("Diff"), _("Alm"), _("Eph"),
46                      _("Bad"), _("Status"))):
47             col = gtk.TreeViewColumn(name)
48             col.pack_start(renderer, False)
49             col.add_attribute(renderer, "text", idx)
50             self.view.append_column(col)
51         self.add(self.view)
52
53     def init(self):
54         self.gps = self.zavai_registry.resource("gps")
55
56     def start(self, *args):
57         if self.gps is None:
58             self.init()
59         self.gps.monitor.connect(self.on_ubxdebug_packet)
60
61     def stop(self, *args):
62         self.gps.monitor.disconnect(self.on_ubxdebug_packet)
63
64     def on_ubxdebug_packet(self, clid, length, data):
65         # In zhone it is cbUBXDebugPacket
66         #if clid == "NAV-STATUS" and data:
67         #    i = ["%s: %d" % (k, data[0][k]) for k in sorted(data[0].keys())]
68         #    zavai.info("Status:", " ".join(i))
69         ##    if data[0]['TTFF']:
70         ##        zavai.info("TTFF: %f", data[0]['TTFF']/1000.0)
71         if clid == "NAV-SVINFO":
72             self.handle_ubx_sat_data(data[1:])
73         #else:
74         #    zavai.info("gps got ubxdebug packet", clid)
75         #    zavai.info("DATA:", data)
76
77     def handle_ubx_sat_data(self, ubxinfo):
78         #zavai.info("CH ID SN ELE AZI Used Diff Alm Eph Bad Status")
79         self.store.clear()
80         for sv in ubxinfo:
81             if sv["CNO"] == 0: continue
82             svid = sv["SVID"]
83             used = sv["Flags"] & 0x01
84             diff = sv["Flags"] & 0x02
85             almoreph = sv["Flags"] & 0x04
86             eph = sv["Flags"] & 0x08
87             bad = sv["Flags"] & 0x10
88             qi = ("%i: " % sv["QI"]) + SAT_QI_NAMES.get(sv["QI"], _("Unknown"))
89             self.store.append([
90                 "%2d" % sv["chn"],
91                 "%2d" % sv["SVID"],
92                 "%2d" % sv["CNO"],
93                 "%3d" % sv["Elev"],
94                 "%3d" % sv["Azim"],
95                 used and "used" or "",
96                 diff and "diff" or "",
97                 almoreph and "alm" or "",
98                 eph and "eph" or "",
99                 bad and "bad" or "",
100                 qi])
101
102 def init(conf = None, registry = None, **kw):
103     name = "menu.main.gps.satellite_monitor"
104     registry.register(name, SatelliteMonitor(registry, name))