View Single Post
Posts: 27 | Thanked: 17 times | Joined on Jun 2010
#1
Hello!

I've followed the guide on http://wiki.maemo.org/PyMaemo/Using_Location_API. The purpose is to every 3rd hour connect with the GPS, fetch 10 coordinates and send them to my server. If I lose my phone I can always go to my server and find the coordinates of the phone. The script works fine, but the problem occurs when I enter a closed building without GPS-reception, then my phone tries forever until it finds GPS-reception which ofcourse drains the battery. So I'd like to implement a function that cancels the update if the script takes longer than 5 minutes. How can this be made?

Thanks!

Code:
import location
import gobject


x=0
 
def on_error(control, error, data):
    print "location error: %d... quitting" % error
    data.quit()
 
def on_changed(device, data):
    global x

    if not device:
        return
    if device.fix:
        if device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
	    #http://maps.google.com/maps?q=62.62991%2C17.040443
            print "http://maps.google.com/maps?q=%f,%f" % device.fix[4:6]
            if x>8:
		#commented out to allow continuous loop for a reliable fix - press ctrl c to break the loop, or program your own way of exiting)
            	data.stop() 
            else:
		x=x+1
		
 
def on_stop(control, data):
    data.quit()
 
def start_location(data):
    data.start()
    return False


 
loop = gobject.MainLoop()
control = location.GPSDControl.get_default()
device = location.GPSDevice()
control.set_properties(preferred_method=location.METHOD_USER_SELECTED,
                       preferred_interval=location.INTERVAL_5S)
 
control.connect("error-verbose", on_error, loop)
device.connect("changed", on_changed, control)
control.connect("gpsd-stopped", on_stop, loop)
 
gobject.idle_add(start_location, control)
 
loop.run()