aaaf4feff2309853dc1bcf097c44441097f6aefc
[wifihood] / wifiscanner / 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.info = None, 0, 0, None, None, None, None
20         self.satellites = None
21         self.cells = None
22         self.ngps = 0
23
24     def set_method ( self , method="gps" ) :
25         if method == "agps" :
26             self.control.set_properties(preferred_method=location.METHOD_GNSS|location.METHOD_AGNSS)
27         else :
28             control.set_properties(preferred_method=location.METHOD_GNSS)
29
30     def set_interval ( self , interval=0 ) :
31         if interval <= 0 :
32             self.control.set_properties(preferred_interval=location.INTERVAL_DEFAULT)
33         elif interval <= 1 :
34             self.control.set_properties(preferred_interval=location.INTERVAL_1S)
35         elif interval <= 2 :
36             self.control.set_properties(preferred_interval=location.INTERVAL_2S)
37         elif interval <= 5 :
38             self.control.set_properties(preferred_interval=location.INTERVAL_5S)
39         elif interval <= 10 :
40             self.control.set_properties(preferred_interval=location.INTERVAL_10S)
41         elif interval <= 20 :
42             self.control.set_properties(preferred_interval=location.INTERVAL_20S)
43         elif interval <= 30 :
44             self.control.set_properties(preferred_interval=location.INTERVAL_30S)
45         elif interval <= 60 :
46             self.control.set_properties(preferred_interval=location.INTERVAL_60S)
47         else :
48             self.control.set_properties(preferred_interval=location.INTERVAL_120S)
49
50     def start ( self ) :
51         if not self.device.online :
52             self.control.start()
53
54     # FIXME : Stopping does not work, at least while getting fix
55     def stop ( self ) :
56         if self.device.online :
57             self.control.stop()
58
59     def update ( self ) :
60
61         if self.device.online :
62             if self.device.status == location.GPS_DEVICE_STATUS_FIX :
63                 state = "FIX"
64                 self.ngps += 1
65             elif self.device.status == location.GPS_DEVICE_STATUS_DGPS_FIX :
66                 state = "DGPS"
67                 # FIXME : Increase also ngps here ???
68             else :
69                 state = None
70
71             lat , lon , alt = None , None , None
72             if self.device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
73                 lat , lon = device.fix[4:6]
74             if self.device.fix[1] & location.GPS_DEVICE_ALTITUDE_SET:
75                 alt = device.fix[7]
76             # FIXME : get time from GPS fix
77             self.info = state , self.device.satellites_in_view , self.device.satellites_in_use , self.device.fix[2] , lat , lon , alt
78             self.satellites = self.device.satellites
79             self.cells = self.device.cell_info
80         else :
81             self.info = None, 0, 0, None, None, None, None
82
83     def report ( self ) :
84         return "%d gps" % self.ngps
85
86 gobject.type_register(GPSObject)
87
88 if __name__ == "__main__" :
89     loop = gobject.MainLoop()
90     sample = GPSObject()
91     def on_stop(control, mainloop):
92         mainloop.quit()
93     sample.control.connect("gpsd-stopped", on_stop, loop)
94     sample.start()                                                        
95     loop.run()                                       
96     sample.stop()