def string_to_message(raw_string, reference_date): global receivers try: message = parse(raw_string, reference_date) except NotImplementedError as e: app.logger.error( "No parser implemented for message: {}".format(raw_string)) return None except ParseError as e: app.logger.error("Parsing error with message: {}".format(raw_string)) return None except TypeError as e: app.logger.error("TypeError with message: {}".format(raw_string)) return None except Exception as e: app.logger.error("Other Exception with string: {}".format(raw_string)) return None # update reference receivers and distance to the receiver if message["aprs_type"] == "position": if message[ "beacon_type"] in AIRCRAFT_BEACON_TYPES + RECEIVER_BEACON_TYPES: latitude = message["latitude"] longitude = message["longitude"] location = Location(longitude, latitude) message["location"] = location.to_wkt() location_mgrs = myMGRS.toMGRS(latitude, longitude).decode("utf-8") message["location_mgrs"] = location_mgrs message["location_mgrs_short"] = location_mgrs[ 0:5] + location_mgrs[5:7] + location_mgrs[10:12] if message[ "beacon_type"] in AIRCRAFT_BEACON_TYPES and "gps_quality" in message: if message["gps_quality"] is not None and "horizontal" in message[ "gps_quality"]: message["gps_quality_horizontal"] = message["gps_quality"][ "horizontal"] message["gps_quality_vertical"] = message["gps_quality"][ "vertical"] del message["gps_quality"] # TODO: Fix python-ogn-client 0.91 if "senders_messages" in message and message[ "senders_messages"] is not None: message["senders_messages"] = int(message["senders_messages"]) if "good_senders" in message and message["good_senders"] is not None: message["good_senders"] = int(message["good_senders"]) if "good_and_bad_senders" in message and message[ "good_and_bad_senders"] is not None: message["good_and_bad_senders"] = int(message["good_and_bad_senders"]) return message
def add(self, raw_string): try: message = parse(raw_string, reference_timestamp=self.reference_timestamp) except NotImplementedError as e: current_app.logger.error( "No parser implemented for message: {}".format(raw_string)) return except ParseError as e: current_app.logger.error( "Parsing error with message: {}".format(raw_string)) return except TypeError as e: current_app.logger.error( "TypeError with message: {}".format(raw_string)) return except Exception as e: current_app.logger.error( "Other Exception with string: {}".format(raw_string)) return if message['aprs_type'] not in ('server', 'position'): return elif message[ 'aprs_type'] == 'server' and self.auto_update_timestamp is True: self.reference_timestamp = message['timestamp'] return elif message['aprs_type'] == 'position': latitude = message["latitude"] longitude = message["longitude"] location = Location(longitude, latitude) message["location"] = location.to_wkt() location_mgrs = self.mgrs.toMGRS(latitude, longitude).decode("utf-8") message["location_mgrs"] = location_mgrs message["location_mgrs_short"] = location_mgrs[ 0:5] + location_mgrs[5:7] + location_mgrs[10:12] if "aircraft_type" in message: message["aircraft_type"] = AircraftType( message["aircraft_type"] ).name if message["aircraft_type"] in AircraftType.list( ) else AircraftType.UNKNOWN.name if "gps_quality" in message: if message[ "gps_quality"] is not None and "horizontal" in message[ "gps_quality"]: message["gps_quality_horizontal"] = message["gps_quality"][ "horizontal"] message["gps_quality_vertical"] = message["gps_quality"][ "vertical"] del message["gps_quality"] if message["beacon_type"] in RECEIVER_BEACON_TYPES: complete_message = ",".join([ str(message[k]) if k in message and message[k] is not None else "\\N" for k in BEACON_KEY_FIELDS + RECEIVER_BEACON_FIELDS ]) self.receiver_buffer.write(complete_message) self.receiver_buffer.write("\n") elif message["beacon_type"] in AIRCRAFT_BEACON_TYPES: complete_message = ",".join([ str(message[k]) if k in message and message[k] is not None else "\\N" for k in BEACON_KEY_FIELDS + AIRCRAFT_BEACON_FIELDS ]) self.aircraft_buffer.write(complete_message) self.aircraft_buffer.write("\n") else: current_app.logger.error("Ignore beacon_type: {}".format( message["beacon_type"])) return if datetime.utcnow() - self.last_flush >= timedelta(seconds=5): self._flush() self.last_flush = datetime.utcnow()