0965bfd897cc72a7f03a43779dc7f9525a59ff71
[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         self.status = None
25
26     def set_method ( self , method="gps" ) :
27         if method == "agps" :
28             self.control.set_properties(preferred_method=location.METHOD_GNSS|location.METHOD_AGNSS)
29         else :
30             control.set_properties(preferred_method=location.METHOD_GNSS)
31
32     def set_interval ( self , interval=0 ) :
33         if interval <= 0 :
34             self.control.set_properties(preferred_interval=location.INTERVAL_DEFAULT)
35         elif interval <= 1 :
36             self.control.set_properties(preferred_interval=location.INTERVAL_1S)
37         elif interval <= 2 :
38             self.control.set_properties(preferred_interval=location.INTERVAL_2S)
39         elif interval <= 5 :
40             self.control.set_properties(preferred_interval=location.INTERVAL_5S)
41         elif interval <= 10 :
42             self.control.set_properties(preferred_interval=location.INTERVAL_10S)
43         elif interval <= 20 :
44             self.control.set_properties(preferred_interval=location.INTERVAL_20S)
45         elif interval <= 30 :
46             self.control.set_properties(preferred_interval=location.INTERVAL_30S)
47         elif interval <= 60 :
48             self.control.set_properties(preferred_interval=location.INTERVAL_60S)
49         else :
50             self.control.set_properties(preferred_interval=location.INTERVAL_120S)
51
52     def start ( self ) :
53         if not self.device.online :
54             self.control.start()
55
56     # FIXME : Stopping does not work, at least while getting fix
57     def stop ( self ) :
58         if self.device.online :
59             self.control.stop()
60
61     def update ( self ) :
62
63         self.state = None
64         if self.device.online :
65             if self.device.status == location.GPS_DEVICE_STATUS_NO_FIX :
66                 self.gps_state = "NO_FIX"
67             elif self.device.status == location.GPS_DEVICE_STATUS_FIX :
68                 self.gps_state = "FIX"
69                 self.ngps += 1
70                 self.refresh_infowin()
71             elif self.device.status == location.GPS_DEVICE_STATUS_DGPS_FIX :
72                 self.gps_state = "DGPS"
73
74             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] )
75             self.satellites = self.device.satellites
76             self.cell_info = self.device.cell_info
77
78     def set_infowin ( self , statuswin ) :
79         self.status = statuswin
80
81     def refresh_infowin ( self ) :
82         if self.status :
83             self.status.set_label( "%d gps" % self.ngps )
84
85
86 #    mode = device.fix[0]
87 #    if mode == location.GPS_DEVICE_MODE_NOT_SEEN : # This means ??
88 #        print "mode is NOSEEN"
89 #    if mode == location.GPS_DEVICE_MODE_NO_FIX : # This implies device.status == location.GPS_DEVICE_STATUS_NO_FIX
90 #                                                 # and probably device.fix[1] == location.GPS_DEVICE_NONE_SET
91 #        print "mode is NOFIX"
92 #    if mode == location.GPS_DEVICE_MODE_2D :
93 #        print "mode is 2D"
94 #    if mode == location.GPS_DEVICE_MODE_3D :
95 #        print "mode is 3D"
96
97 #    if flags & location.GPS_DEVICE_SPEED_SET :
98 #        print "GPS_DEVICE_SPEED_SET"
99 #    if flags & location.GPS_DEVICE_TRACK_SET :
100 #        print "GPS_DEVICE_TRACK_SET"
101 #    if flags & location.GPS_DEVICE_ALTITUDE_SET :
102 #        print "GPS_DEVICE_ALTITUDE_SET"
103 #    if flags & location.GPS_DEVICE_CLIMB_SET :
104 #        print "GPS_DEVICE_CLIMB_SET"
105
106 gobject.type_register(GPSObject)
107
108 if __name__ == "__main__" :
109     loop = gobject.MainLoop()
110     sample = GPSObject()
111     print dir(sample)
112     def on_stop(control, mainloop):
113         mainloop.quit()
114     sample.control.connect("gpsd-stopped", on_stop, loop)
115     sample.start()                                                        
116     loop.run()                                       
117     sample.stop()