Modify reporting method, giving a simple string. Fixed bug on update method
[wifihood] / wifimap / gps.py
1
2 import location
3
4
5 import gobject
6
7 class GPSObject ( gobject.GObject ) :
8
9     def __init__ ( self ) :
10         gobject.GObject.__init__( self )
11
12         self.control = location.GPSDControl.get_default()
13         self.control.set_properties(preferred_method=location.METHOD_GNSS,                                                  
14                                     preferred_interval=location.INTERVAL_DEFAULT)                                              
15         self.device = location.GPSDevice()
16         self.device.connect_object("changed", GPSObject.update , self)
17
18         # Values to be set by GPSDevice changed events
19         self.gps_state = False
20         self.gps_info = "NO_FIX 0 0 NaN NaN NaN NaN NaN NaN NaN"
21         self.satellites = None
22         self.cell_info = None
23         self.ngps = 0
24
25     def set_method ( self , method="gps" ) :
26         if method == "agps" :
27             self.control.set_properties(preferred_method=location.METHOD_GNSS|location.METHOD_AGNSS)
28         else :
29             control.set_properties(preferred_method=location.METHOD_GNSS)
30
31     def set_interval ( self , interval=0 ) :
32         if interval <= 0 :
33             self.control.set_properties(preferred_interval=location.INTERVAL_DEFAULT)
34         elif interval <= 1 :
35             self.control.set_properties(preferred_interval=location.INTERVAL_1S)
36         elif interval <= 2 :
37             self.control.set_properties(preferred_interval=location.INTERVAL_2S)
38         elif interval <= 5 :
39             self.control.set_properties(preferred_interval=location.INTERVAL_5S)
40         elif interval <= 10 :
41             self.control.set_properties(preferred_interval=location.INTERVAL_10S)
42         elif interval <= 20 :
43             self.control.set_properties(preferred_interval=location.INTERVAL_20S)
44         elif interval <= 30 :
45             self.control.set_properties(preferred_interval=location.INTERVAL_30S)
46         elif interval <= 60 :
47             self.control.set_properties(preferred_interval=location.INTERVAL_60S)
48         else :
49             self.control.set_properties(preferred_interval=location.INTERVAL_120S)
50
51     def start ( self ) :
52         if not self.device.online :
53             self.control.start()
54
55     # FIXME : Stopping does not work, at least while getting fix
56     def stop ( self ) :
57         if self.device.online :
58             self.control.stop()
59
60     def update ( self ) :
61
62         self.gps_state = None
63         if self.device.online :
64             if self.device.status == location.GPS_DEVICE_STATUS_NO_FIX :
65                 self.gps_state = "NO_FIX"
66             elif self.device.status == location.GPS_DEVICE_STATUS_FIX :
67                 self.gps_state = "FIX"
68                 self.ngps += 1
69                 self.refresh_infowin()
70             elif self.device.status == location.GPS_DEVICE_STATUS_DGPS_FIX :
71                 self.gps_state = "DGPS"
72
73             self.gps_info = "%s %d %d %s %s %s %s %s %s %s" % ( self.gps_state , self.device.satellites_in_use , self.device.satellites_in_view , self.device.fix[2] , self.device.fix[4] , self.device.fix[5] , self.device.fix[7] , self.device.fix[9] , self.device.fix[11] , self.device.fix[13] )
74             self.satellites = self.device.satellites
75             self.cell_info = self.device.cell_info
76
77     def report ( self ) :
78         return "%d gps" % self.ngps
79
80 #    mode = device.fix[0]
81 #    if mode == location.GPS_DEVICE_MODE_NOT_SEEN : # This means ??
82 #        print "mode is NOSEEN"
83 #    if mode == location.GPS_DEVICE_MODE_NO_FIX : # This implies device.status == location.GPS_DEVICE_STATUS_NO_FIX
84 #                                                 # and probably device.fix[1] == location.GPS_DEVICE_NONE_SET
85 #        print "mode is NOFIX"
86 #    if mode == location.GPS_DEVICE_MODE_2D :
87 #        print "mode is 2D"
88 #    if mode == location.GPS_DEVICE_MODE_3D :
89 #        print "mode is 3D"
90
91 #    if flags & location.GPS_DEVICE_SPEED_SET :
92 #        print "GPS_DEVICE_SPEED_SET"
93 #    if flags & location.GPS_DEVICE_TRACK_SET :
94 #        print "GPS_DEVICE_TRACK_SET"
95 #    if flags & location.GPS_DEVICE_ALTITUDE_SET :
96 #        print "GPS_DEVICE_ALTITUDE_SET"
97 #    if flags & location.GPS_DEVICE_CLIMB_SET :
98 #        print "GPS_DEVICE_CLIMB_SET"
99
100 gobject.type_register(GPSObject)
101
102 if __name__ == "__main__" :
103     loop = gobject.MainLoop()
104     sample = GPSObject()
105     print dir(sample)
106     def on_stop(control, mainloop):
107         mainloop.quit()
108     sample.control.connect("gpsd-stopped", on_stop, loop)
109     sample.start()                                                        
110     loop.run()                                       
111     sample.stop()