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