class GeoTracer: def __init__(self): self.geo = Geolocation(self.on_location, self.on_status) self.updating = False def _fix_read(self): '''Intercept data read back from PpsFile to tweak the result value so it's the expected string instead of bytes.''' resp = self._f_read() try: value = resp[Geolocation._KEY_RES] resp[Geolocation._KEY_RES] = value.decode() except AttributeError: pass # print('data', resp) return resp def run(self, duration=60, period=1.0): self.geo.open_connection() # hotpatch the Geolocation's PpsFile object to fix the data read back self._f_read = self.geo.f.read self.geo.f.read = self._fix_read try: self.geo.start_location_updates(period) start = time.time() while time.time() - start < duration: time.sleep(5) if not self.updating: print('waiting', self.geo.status_of_location_updates()) self.geo.cancel_location_updates() finally: self.geo.f.read = self._f_read # undo hotpatching self.geo.close_connection() def on_location(self, latitude, longitude, accuracy, altitude, altitude_accuracy, heading, speed, *args): if not self.updating: self.notify = qn.SimpleNotification('Geotrace: location found!', showTimeFlag=True, ) self.updating = True used = len(list(x for x in args[-1] if x['used'])) tracked = len(list(x for x in args[-1] if x['tracked'])) satlist = ','.join(str(x['id']) for x in args[-1] if x['used']) print('(%.6f, %.6f) acc=%5.1fm elev=%4.0fm heading=%3.0f speed=%4.1fm/s sats=%s/%s (%s)' % (latitude, longitude, accuracy, altitude, heading, speed, used, tracked, satlist)) sys.stdout.flush() def on_status(self, status, *args): print('status', status)
def __init__(self): self.geo = Geolocation(self.on_location, self.on_status) self.updating = False