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