-
-import location
-
try :
- import hildon
+ import location
except :
- hildon = False
+ import hildongtk.location as location
+
+
import gobject
class GPSObject ( gobject.GObject ) :
- def __init__ ( self , widget=None ) :
+ def __init__ ( self ) :
gobject.GObject.__init__( self )
- self._parent = widget
- self._debug = False
+
self.control = location.GPSDControl.get_default()
- # properties : maincontext_pointer preferred_interval preferred_method
- self.method = location.METHOD_GNSS
- self.device = None
- self.gps_state = False
- self.gps_info = "NO_FIX 0 0 NaN NaN NaN NaN NaN NaN NaN"
- self.update_handler = None
+ self.control.set_properties(preferred_method=location.METHOD_GNSS,
+ preferred_interval=location.INTERVAL_DEFAULT)
+ self.device = location.GPSDevice()
+ self.device.connect_object("changed", GPSObject.update , self)
+
+ # Values to be set by GPSDevice changed events
+ self.info = None, 0, 0, None, None, None, None
self.satellites = None
- self.cell_info = None
+ self.cells = None
self.ngps = 0
- self.status = None
def set_method ( self , method="gps" ) :
if method == "agps" :
- self.method = location.METHOD_GNSS | location.METHOD_AGNSS
+ self.control.set_properties(preferred_method=location.METHOD_GNSS|location.METHOD_AGNSS)
else :
- self.method = location.METHOD_GNSS
-
- def set_interval ( self , interval="1" ) :
- if interval == "1" :
- self.interval = location.INTERVAL_1S
- elif interval == "2" :
- self.interval = location.INTERVAL_2S
- elif interval == "5" :
- self.interval = location.INTERVAL_5S
- elif interval == "10" :
- self.interval = location.INTERVAL_10S
- elif interval == "20" :
- self.interval = location.INTERVAL_20S
- elif interval == "30" :
- self.interval = location.INTERVAL_30S
- elif interval == "60" :
- self.interval = location.INTERVAL_60S
- elif interval == "120" :
- self.interval = location.INTERVAL_120S
-
- def do_start ( self ) :
- if not self.device :
- self.control.set_properties(preferred_method=self.method)
- self.device = location.GPSDevice()
- if self.update_handler :
- if hildon :
- hildon.hildon_banner_show_information( self._parent , "icon_path" , "GPS already started" )
+ control.set_properties(preferred_method=location.METHOD_GNSS)
+
+ def set_interval ( self , interval=0 ) :
+ if interval <= 0 :
+ self.control.set_properties(preferred_interval=location.INTERVAL_DEFAULT)
+ elif interval <= 1 :
+ self.control.set_properties(preferred_interval=location.INTERVAL_1S)
+ elif interval <= 2 :
+ self.control.set_properties(preferred_interval=location.INTERVAL_2S)
+ elif interval <= 5 :
+ self.control.set_properties(preferred_interval=location.INTERVAL_5S)
+ elif interval <= 10 :
+ self.control.set_properties(preferred_interval=location.INTERVAL_10S)
+ elif interval <= 20 :
+ self.control.set_properties(preferred_interval=location.INTERVAL_20S)
+ elif interval <= 30 :
+ self.control.set_properties(preferred_interval=location.INTERVAL_30S)
+ elif interval <= 60 :
+ self.control.set_properties(preferred_interval=location.INTERVAL_60S)
else :
- self.update_handler = self.device.connect_object("changed", GPSObject.do_update , self)
+ self.control.set_properties(preferred_interval=location.INTERVAL_120S)
+
+ def start ( self ) :
+ if not self.device.online :
self.control.start()
# FIXME : Stopping does not work, at least while getting fix
- def do_stop ( self ) :
- if self.update_handler :
- # FIXME : Is this removal OK?
- del self.update_handler
- self.update_handler = None
- self.device.stop()
- self.control.stop()
-
- def do_update ( self ) :
-
- if self.device :
- if self.device.status == location.GPS_DEVICE_STATUS_NO_FIX :
- # if self.gps_state == "FIX" or self.gps_state == "DGPS" :
- # if hildon :
- # banner = hildon.hildon_banner_show_information( self._parent , "icon_path" , "Lost GPS fix" )
- # banner.set_timeout( 1500 )
- self.gps_state = "NO_FIX"
- elif self.device.status == location.GPS_DEVICE_STATUS_FIX :
- # if self.gps_state == "NO_FIX" :
- # if hildon :
- # banner = hildon.hildon_banner_show_information( self._parent , "icon_path" , "Got GPS fix" )
- # banner.set_timeout( 1500 )
- self.gps_state = "FIX"
+ def stop ( self ) :
+ if self.device.online :
+ self.control.stop()
+
+ def update ( self ) :
+
+ if self.device.online :
+ if self.device.status == location.GPS_DEVICE_STATUS_FIX :
+ state = "FIX"
self.ngps += 1
- self.refresh_infowin()
elif self.device.status == location.GPS_DEVICE_STATUS_DGPS_FIX :
- # if self.gps_state == "NO_FIX" :
- # if hildon :
- # banner = hildon.hildon_banner_show_information( self._parent , "icon_path" , "Got differential GPS fix" )
- # banner.set_timeout( 1500 )
- self.gps_state = "DGPS"
-
- 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] )
+ state = "DGPS"
+ # FIXME : Increase also ngps here ???
+ else :
+ state = None
+
+ lat , lon , alt = None , None , None
+ if self.device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
+ lat , lon = self.device.fix[4:6]
+ if self.device.fix[1] & location.GPS_DEVICE_ALTITUDE_SET:
+ alt = self.device.fix[7]
+ # FIXME : get time from GPS fix
+ self.info = state , self.device.satellites_in_view , self.device.satellites_in_use , self.device.fix[2] , lat , lon , alt
self.satellites = self.device.satellites
- self.cell_info = self.device.cell_info
-
- # if self._debug :
- # if hildon :
- # banner = hildon.hildon_banner_show_information( self._parent , "icon_path" , "GPS info : %s" % self.gps_info )
- # banner.set_timeout( 300 )
-
- def set_infowin ( self , statuswin ) :
- self.status = statuswin
-
- def refresh_infowin ( self ) :
- if self.status :
- self.status.set_label( "%d gps" % self.ngps )
-
-
-# mode = device.fix[0]
-# if mode == location.GPS_DEVICE_MODE_NOT_SEEN : # This means ??
-# print "mode is NOSEEN"
-# if mode == location.GPS_DEVICE_MODE_NO_FIX : # This implies device.status == location.GPS_DEVICE_STATUS_NO_FIX
-# # and probably device.fix[1] == location.GPS_DEVICE_NONE_SET
-# print "mode is NOFIX"
-# if mode == location.GPS_DEVICE_MODE_2D :
-# print "mode is 2D"
-# if mode == location.GPS_DEVICE_MODE_3D :
-# print "mode is 3D"
-
-# if flags & location.GPS_DEVICE_SPEED_SET :
-# print "GPS_DEVICE_SPEED_SET"
-# if flags & location.GPS_DEVICE_TRACK_SET :
-# print "GPS_DEVICE_TRACK_SET"
-# if flags & location.GPS_DEVICE_ALTITUDE_SET :
-# print "GPS_DEVICE_ALTITUDE_SET"
-# if flags & location.GPS_DEVICE_CLIMB_SET :
-# print "GPS_DEVICE_CLIMB_SET"
+ self.cells = self.device.cell_info
+ else :
+ self.info = None, 0, 0, None, None, None, None
+
+ def report ( self ) :
+ return "%d gps" % self.ngps
gobject.type_register(GPSObject)
+if __name__ == "__main__" :
+ loop = gobject.MainLoop()
+ sample = GPSObject()
+ def on_stop(control, mainloop):
+ mainloop.quit()
+ sample.control.connect("gpsd-stopped", on_stop, loop)
+ sample.start()
+ loop.run()
+ sample.stop()