Improve getting gps data
[gigfinder] / gig_finder.py
index e2f1c19..f9e9625 100644 (file)
@@ -18,6 +18,7 @@ import gtk
 import hildon
 import location
 import time
+import gobject
 
 class GigParser:
 
@@ -95,6 +96,7 @@ class GigFinder:
         self.url_base = "http://ws.audioscrobbler.com/2.0/"
         self.api_key = "1928a14bdf51369505530949d8b7e1ee"
         self.distance = '10'
+        self.loop = gobject.MainLoop()
 
         program = hildon.Program.get_instance()
         self.win = hildon.StackableWindow()
@@ -110,7 +112,6 @@ class GigFinder:
         pannable_area.add_with_viewport(self.table)
 
         self.win.add(pannable_area)
-
         self.update_gigs()
 
     def main(self):
@@ -119,10 +120,11 @@ class GigFinder:
 
     def update_gigs(self):
         """ Get gig info """
-        parser = GigParser()
         self.get_lat_long()
+        parser = GigParser()
         xml = self.get_xml()
         events = parser.parse_xml(xml, self.lat, self.long)
+        self.win.set_title('Gig Finder (%s)' % len(events))
         self.add_events(events)
         
     def get_xml(self):
@@ -136,15 +138,39 @@ class GigFinder:
         response = urllib.urlopen(self.url_base, params)
         return response.read()
 
+    def on_error(self, control, error, data):
+        print "location error: %d... quitting" % error
+        data.quit()
+
+    def on_changed(self, device, data):
+        if not device:
+            return
+        if device.fix:
+            if device.fix[1] & device.status:
+                self.lat, self.long = device.fix[4:6]
+                data.stop()
+
+    def on_stop(self, control, data):
+        print "quitting"
+        data.quit()
+
+    def start_location(self, data):
+        data.start()
+        return False
+
     def get_lat_long(self):
         """ Access gps and return current long lats """
         # TODO: Improve geolocation code, very crude atm
         control = location.GPSDControl.get_default()
         device = location.GPSDevice()
-        control.start()
-        fix = device.fix
-        control.stop()
-        self.lat, self.long = fix[4:6]
+        device.reset_last_known()
+        control.set_properties(preferred_method=location.METHOD_USER_SELECTED,
+                               preferred_interval=location.INTERVAL_DEFAULT)
+        control.connect("error-verbose", self.on_error, self.loop)
+        device.connect("changed", self.on_changed, control)
+        control.connect("gpsd-stopped", self.on_stop, self.loop)
+        gobject.idle_add(self.start_location, control)
+        self.loop.run()
         #self.lat, self.long = ('51.546228', '-0.075016')
 
     def show_details(self, widget, data):
@@ -167,7 +193,7 @@ class GigFinder:
         buffer.insert(end, '%s\n' % data['title'])
         buffer.insert(end, 'Artists: %s\n' % data['artists'])
         buffer.insert(end, 'Venue: %s\n' % data['venue'])
-        buffer.insert(end, data['address'])
+        buffer.insert(end, '%s\n' % data['address'])
         buffer.insert(end, 'When: %s\n' % data['date'].strftime('%H:%M'))
         buffer.insert(end, '\n')
         scroll.add_with_viewport(view)