def report(timestamp, vin, data): logger.info('Tracking Callback Server: Report request: Time: %s, VIN: %s, Data: %s.', timestamp, vin, data) # get the vehicle record from the database try: vehicle = Vehicle.objects.get(veh_vin = vin) except Exception as e: logger.error("Tracking Callback Server: Cannot retrieve vehicle '%s' from database. Error: %s", vin, e) return {u'status': 0} location = Location() location.loc_vehicle = vehicle location.loc_time = timestamp for channel in data: key = channel['channel'] value = channel['value'] if key == 'location': location.loc_latitude = value['lat'] location.loc_longitude = value['lon'] location.loc_altitude = value['alt'] elif key == 'speed': location.loc_speed = float(value) elif key == 'odometer': location.loc_odometer = float(value) location.save() return {u'status': 0}
def run(self): # get the vehicle record try: vehicle = Vehicle.objects.get(veh_name=self.vehicle_name) except: logger.error("%s: Vehicle '%s' does not exist in database. Add it first.", MY_NAME, self.vehicle_name) sys.exit(2) # start GPS polling thread self.gps_poller.start() # catch signals for proper shutdown for sig in (SIGABRT, SIGTERM, SIGINT): signal(sig, self.cleanup) # main execution loop while True: try: time.sleep(self.interval) # If we are idle too long the database server may # close the connection on us, ping the server to check if # the connection is still up. if connection.connection is not None: if connection.is_usable(): logger.debug("%s: Database connection is up.", MY_NAME) else: logger.error("%s: Database connection is down.", MY_NAME) connection.close() else: logger.error("%s: Database connection is closed.", MY_NAME) # process GPS data session = self.gps_poller.session if (session.fix.mode == MODE_NO_FIX) and not self.nofix: logger.info("%s: Waiting for GPS to fix...", MY_NAME) continue if not isnan(session.fix.time): if (session.fix.speed < 0.1) and (self.last_speed < 0.1): continue self.last_speed = session.fix.speed # if the time is valid the data record is valid location = Location() location.loc_vehicle = vehicle location.loc_time = session.utc location.loc_latitude = session.fix.latitude location.loc_longitude = session.fix.longitude if session.fix.mode == MODE_3D: location.loc_altitude = session.fix.altitude location.loc_speed = session.fix.speed location.loc_climb = session.fix.climb location.loc_track = session.fix.track location.save() logger.info("%s: Valid location: %s", MY_NAME, location) else: logger.debug("%s: Invalid location: %s", MY_NAME) except KeyboardInterrupt: print ("\n") break
def report(timestamp, vin, data): logger.info( 'Tracking Callback Server: Report request: Time: %s, VIN: %s, Data: %s.', timestamp, vin, data) # get the vehicle record from the database try: vehicle = Vehicle.objects.get(veh_vin=vin) except Exception as e: logger.error( "Tracking Callback Server: Cannot retrieve vehicle '%s' from database. Error: %s", vin, e) return {u'status': 0} location = Location() location.loc_vehicle = vehicle try: location.loc_time = datetime.fromtimestamp(float(timestamp)) except: location.loc_time = timestamp if isinstance(data, str) or isinstance(data, unicode): logger.info('data is string') data = ast.literal_eval(data) for channel in data: key = channel['channel'] value = channel['value'] logger.info('%s: %s', key, value) if key == 'location': location.loc_latitude = value['lat'] location.loc_longitude = value['lon'] location.loc_altitude = value['alt'] elif key == 'speed': location.loc_speed = float(value) elif key == 'odometer': location.loc_odometer = float(value) print 'Saving', location location.save() logger.info('Saved') return {u'status': 0}
def run(self): # get the vehicle record try: vehicle = Vehicle.objects.get(veh_name=self.vehicle_name) except: logger.error( "%s: Vehicle '%s' does not exist in database. Add it first.", MY_NAME, self.vehicle_name) sys.exit(2) # start GPS polling thread self.gps_poller.start() # catch signals for proper shutdown for sig in (SIGABRT, SIGTERM, SIGINT): signal(sig, self.cleanup) # main execution loop while True: try: time.sleep(self.interval) # If we are idle too long the database server may # close the connection on us, ping the server to check if # the connection is still up. if (connection.connection is not None): if (connection.is_usable()): logger.debug('%s: Database connection is up.', MY_NAME) else: logger.error('%s: Database connection is down.', MY_NAME) connection.close() else: logger.error('%s: Database connection is closed.', MY_NAME) # process GPS data session = self.gps_poller.session if (session.fix.mode == MODE_NO_FIX) and not self.nofix: logger.info("%s: Waiting for GPS to fix...", MY_NAME) continue if not isnan(session.fix.time): if (session.fix.speed < 0.1) and (self.last_speed < 0.1): continue self.last_speed = session.fix.speed # if the time is valid the data record is valid location = Location() location.loc_vehicle = vehicle location.loc_time = session.utc location.loc_latitude = session.fix.latitude location.loc_longitude = session.fix.longitude if (session.fix.mode == MODE_3D): location.loc_altitude = session.fix.altitude location.loc_speed = session.fix.speed location.loc_climb = session.fix.climb location.loc_track = session.fix.track location.save() logger.info("%s: Valid location: %s", MY_NAME, location) else: logger.debug("%s: Invalid location: %s", MY_NAME) except KeyboardInterrupt: print('\n') break