0dc76299f50247802a00431a75e97521fad5df59
[gigfinder] / src / opt / gigfinder / locator.py
1 import gobject
2 import location
3 import thread
4
5 class LocationUpdater:
6
7     def __init__(self):
8         self.lat = None
9         self.long = None
10
11         self.control = location.GPSDControl.get_default()
12         self.control.set_properties(preferred_method=location\
13                                             .METHOD_USER_SELECTED,
14                                     preferred_interval=location\
15                                             .INTERVAL_DEFAULT)
16         self.control.connect("error-verbose", self.on_error, self.control)
17         self.control.connect("gpsd-stopped", self.stop, None)
18
19         self.device = location.GPSDevice()
20         self.device.connect("changed", self.on_changed, self.control)
21
22     def update_location(self):
23         """ Run the loop and update lat and long """
24         self.reset()
25         gobject.idle_add(self.start_location, self.control)
26
27     def on_error(self, control, error, data):
28         """ Handle errors """
29         print "location error: %d... quitting" % error
30         data.quit()
31
32     def on_changed(self, device, data):
33         """ Set long and lat """
34         if not device:
35             return
36         if device.fix:
37             # once fix is found and horizontal accuracy is 1km
38             if location.GPS_DEVICE_LATLONG_SET:
39                 if device.fix[6] <= 100000:
40                     self.lat, self.long = device.fix[4:6]
41                     data.stop()
42
43     def stop(self, widget, data):
44         """ Stop the location service """
45         self.control.stop()
46
47     def start_location(self, data):
48         """ Start the location service """
49         data.start()
50         return False
51
52     def reset(self):
53         """ Reset coordinates """
54         self.lat = None
55         self.long = None
56         self.device.reset_last_known()
57