Perform some cleanup
[wifihood] / wifiscanner / wifimap / gps.py
1
2
3 import location
4
5 import gobject
6
7 class GPSObject ( gobject.GObject ) :
8
9     def __init__ ( self , widget=None ) :
10         gobject.GObject.__init__( self )
11         self._parent = widget
12         self._debug = False
13         self.control = location.GPSDControl.get_default()
14         self.device = location.GPSDevice()
15         # properties : maincontext_pointer preferred_interval preferred_method
16         self.method = location.METHOD_GNSS
17         self.gps_state = False
18         self.gps_info = "NO_FIX 0 0 NaN NaN NaN NaN NaN NaN NaN"
19         self.update_handler = None
20         self.satellites = None
21         self.cell_info = None
22         self.ngps = 0
23         self.status = None
24
25     def set_method ( self , method="gps" ) :
26         if method == "agps" :
27             self.method = location.METHOD_GNSS | location.METHOD_AGNSS
28         else :
29             self.method = location.METHOD_GNSS
30
31     def set_interval ( self , interval="1" ) :
32         if interval == "1" :
33             self.interval = location.INTERVAL_1S
34         elif interval == "2" :
35             self.interval = location.INTERVAL_2S
36         elif interval == "5" :
37             self.interval = location.INTERVAL_5S
38         elif interval == "10" :
39             self.interval = location.INTERVAL_10S
40         elif interval == "20" :
41             self.interval = location.INTERVAL_20S
42         elif interval == "30" :
43             self.interval = location.INTERVAL_30S
44         elif interval == "60" :
45             self.interval = location.INTERVAL_60S
46         elif interval == "120" :
47             self.interval = location.INTERVAL_120S
48         else :
49             self.interval = location.INTERVAL_DEFAULT
50
51     def do_start ( self ) :
52         self.control.set_properties(preferred_method=self.method,                                                  
53                                     preferred_interval=self.interval)                                              
54         if not self.update_handler :
55             self.update_handler = self.device.connect_object("changed", GPSObject.do_update , self)
56             self.control.start()
57
58     # FIXME : Stopping does not work, at least while getting fix
59     def do_stop ( self ) :
60         if self.update_handler :
61             # FIXME : Is this removal OK?
62             del self.update_handler
63             self.update_handler = None
64         self.control.stop()
65
66     def do_update ( self ) :
67
68         if self.device :
69             if self.device.status == location.GPS_DEVICE_STATUS_NO_FIX :
70                 self.gps_state = "NO_FIX"
71             elif self.device.status == location.GPS_DEVICE_STATUS_FIX :
72                 self.gps_state = "FIX"
73                 self.ngps += 1
74                 self.refresh_infowin()
75             elif self.device.status == location.GPS_DEVICE_STATUS_DGPS_FIX :
76                 self.gps_state = "DGPS"
77
78             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] )
79             self.satellites = self.device.satellites
80             self.cell_info = self.device.cell_info
81
82     def set_infowin ( self , statuswin ) :
83         self.status = statuswin
84
85     def refresh_infowin ( self ) :
86         if self.status :
87             self.status.set_label( "%d gps" % self.ngps )
88
89
90 #    mode = device.fix[0]
91 #    if mode == location.GPS_DEVICE_MODE_NOT_SEEN : # This means ??
92 #        print "mode is NOSEEN"
93 #    if mode == location.GPS_DEVICE_MODE_NO_FIX : # This implies device.status == location.GPS_DEVICE_STATUS_NO_FIX
94 #                                                 # and probably device.fix[1] == location.GPS_DEVICE_NONE_SET
95 #        print "mode is NOFIX"
96 #    if mode == location.GPS_DEVICE_MODE_2D :
97 #        print "mode is 2D"
98 #    if mode == location.GPS_DEVICE_MODE_3D :
99 #        print "mode is 3D"
100
101 #    if flags & location.GPS_DEVICE_SPEED_SET :
102 #        print "GPS_DEVICE_SPEED_SET"
103 #    if flags & location.GPS_DEVICE_TRACK_SET :
104 #        print "GPS_DEVICE_TRACK_SET"
105 #    if flags & location.GPS_DEVICE_ALTITUDE_SET :
106 #        print "GPS_DEVICE_ALTITUDE_SET"
107 #    if flags & location.GPS_DEVICE_CLIMB_SET :
108 #        print "GPS_DEVICE_CLIMB_SET"
109
110 gobject.type_register(GPSObject)
111