Initial implementation of the new hildon wrappers
[wifihood] / wifiscanner / wifimap / gps.py
1
2 try :
3     import location
4 except :
5     import hildongtk.location as location
6
7
8 import gobject
9
10 class GPSObject ( gobject.GObject ) :
11
12     def __init__ ( self ) :
13         gobject.GObject.__init__( self )
14
15         self.control = location.GPSDControl.get_default()
16         self.control.set_properties(preferred_method=location.METHOD_GNSS,                                                  
17                                     preferred_interval=location.INTERVAL_DEFAULT)                                              
18         self.device = location.GPSDevice()
19         self.device.connect_object("changed", GPSObject.update , self)
20
21         # Values to be set by GPSDevice changed events
22         self.info = None, 0, 0, None, None, None, None
23         self.satellites = None
24         self.cells = None
25         self.ngps = 0
26
27     def set_method ( self , method="gps" ) :
28         if method == "agps" :
29             self.control.set_properties(preferred_method=location.METHOD_GNSS|location.METHOD_AGNSS)
30         else :
31             control.set_properties(preferred_method=location.METHOD_GNSS)
32
33     def set_interval ( self , interval=0 ) :
34         if interval <= 0 :
35             self.control.set_properties(preferred_interval=location.INTERVAL_DEFAULT)
36         elif interval <= 1 :
37             self.control.set_properties(preferred_interval=location.INTERVAL_1S)
38         elif interval <= 2 :
39             self.control.set_properties(preferred_interval=location.INTERVAL_2S)
40         elif interval <= 5 :
41             self.control.set_properties(preferred_interval=location.INTERVAL_5S)
42         elif interval <= 10 :
43             self.control.set_properties(preferred_interval=location.INTERVAL_10S)
44         elif interval <= 20 :
45             self.control.set_properties(preferred_interval=location.INTERVAL_20S)
46         elif interval <= 30 :
47             self.control.set_properties(preferred_interval=location.INTERVAL_30S)
48         elif interval <= 60 :
49             self.control.set_properties(preferred_interval=location.INTERVAL_60S)
50         else :
51             self.control.set_properties(preferred_interval=location.INTERVAL_120S)
52
53     def start ( self ) :
54         if not self.device.online :
55             self.control.start()
56
57     # FIXME : Stopping does not work, at least while getting fix
58     def stop ( self ) :
59         if self.device.online :
60             self.control.stop()
61
62     def update ( self ) :
63
64         if self.device.online :
65             if self.device.status == location.GPS_DEVICE_STATUS_FIX :
66                 state = "FIX"
67                 self.ngps += 1
68             elif self.device.status == location.GPS_DEVICE_STATUS_DGPS_FIX :
69                 state = "DGPS"
70                 # FIXME : Increase also ngps here ???
71             else :
72                 state = None
73
74             lat , lon , alt = None , None , None
75             if self.device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
76                 lat , lon = self.device.fix[4:6]
77             if self.device.fix[1] & location.GPS_DEVICE_ALTITUDE_SET:
78                 alt = self.device.fix[7]
79             # FIXME : get time from GPS fix
80             self.info = state , self.device.satellites_in_view , self.device.satellites_in_use , self.device.fix[2] , lat , lon , alt
81             self.satellites = self.device.satellites
82             self.cells = self.device.cell_info
83         else :
84             self.info = None, 0, 0, None, None, None, None
85
86     def report ( self ) :
87         return "%d gps" % self.ngps
88
89 gobject.type_register(GPSObject)
90
91 if __name__ == "__main__" :
92     loop = gobject.MainLoop()
93     sample = GPSObject()
94     def on_stop(control, mainloop):
95         mainloop.quit()
96     sample.control.connect("gpsd-stopped", on_stop, loop)
97     sample.start()                                                        
98     loop.run()                                       
99     sample.stop()