BUGFIX : clear reference to removed 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             elif self.device.status == location.GPS_DEVICE_STATUS_DGPS_FIX :
70                 self.gps_state = "DGPS"
71
72             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] )
73             self.satellites = self.device.satellites
74             self.cell_info = self.device.cell_info
75
76     def report ( self ) :
77         return "%d gps" % self.ngps
78
79 #    mode = device.fix[0]
80 #    if mode == location.GPS_DEVICE_MODE_NOT_SEEN : # This means ??
81 #        print "mode is NOSEEN"
82 #    if mode == location.GPS_DEVICE_MODE_NO_FIX : # This implies device.status == location.GPS_DEVICE_STATUS_NO_FIX
83 #                                                 # and probably device.fix[1] == location.GPS_DEVICE_NONE_SET
84 #        print "mode is NOFIX"
85 #    if mode == location.GPS_DEVICE_MODE_2D :
86 #        print "mode is 2D"
87 #    if mode == location.GPS_DEVICE_MODE_3D :
88 #        print "mode is 3D"
89
90 #    if flags & location.GPS_DEVICE_SPEED_SET :
91 #        print "GPS_DEVICE_SPEED_SET"
92 #    if flags & location.GPS_DEVICE_TRACK_SET :
93 #        print "GPS_DEVICE_TRACK_SET"
94 #    if flags & location.GPS_DEVICE_ALTITUDE_SET :
95 #        print "GPS_DEVICE_ALTITUDE_SET"
96 #    if flags & location.GPS_DEVICE_CLIMB_SET :
97 #        print "GPS_DEVICE_CLIMB_SET"
98
99 gobject.type_register(GPSObject)
100
101 if __name__ == "__main__" :
102     loop = gobject.MainLoop()
103     sample = GPSObject()
104     print dir(sample)
105     def on_stop(control, mainloop):
106         mainloop.quit()
107     sample.control.connect("gpsd-stopped", on_stop, loop)
108     sample.start()                                                        
109     loop.run()                                       
110     sample.stop()