ogn-python/ogn_python/gateway/process.py

72 wiersze
2.4 KiB
Python
Czysty Zwykły widok Historia

import logging
2017-10-03 11:31:24 +00:00
from mgrs import MGRS
from ogn_python.commands.dbutils import session
from ogn_python.model import Location
from ogn_python.parser import parse, ParseError
from ogn_python.gateway.process_tools import DbSaver, AIRCRAFT_BEACON_TYPES, RECEIVER_BEACON_TYPES
2017-10-03 11:31:24 +00:00
logger = logging.getLogger(__name__)
myMGRS = MGRS()
2018-11-28 06:37:35 +00:00
def _replace_lonlat_with_wkt(message):
2018-01-19 18:14:57 +00:00
latitude = message['latitude']
longitude = message['longitude']
location = Location(longitude, latitude)
message['location_wkt'] = 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]
del message['latitude']
del message['longitude']
return message
2018-09-03 17:58:35 +00:00
def string_to_message(raw_string, reference_date):
global receivers
2016-10-03 13:14:44 +00:00
2018-09-03 17:58:35 +00:00
try:
message = parse(raw_string, reference_date)
except NotImplementedError as e:
2019-01-28 21:06:38 +00:00
logger.w('No parser implemented for message: {}'.format(raw_string))
2018-09-03 17:58:35 +00:00
return None
except ParseError as e:
2019-01-28 21:06:38 +00:00
logger.error('Parsing error with message: {}'.format(raw_string))
2018-09-03 17:58:35 +00:00
return None
except TypeError as e:
2019-01-28 21:06:38 +00:00
logger.error('TypeError with message: {}'.format(raw_string))
2018-09-03 17:58:35 +00:00
return None
except Exception as e:
2019-01-28 21:06:38 +00:00
logger.error(raw_string)
logger.error(e)
2018-09-03 17:58:35 +00:00
return None
# update reference receivers and distance to the receiver
if message['aprs_type'] == 'position':
2019-01-28 21:06:38 +00:00
if message['beacon_type'] in AIRCRAFT_BEACON_TYPES + RECEIVER_BEACON_TYPES:
2018-09-03 17:58:35 +00:00
message = _replace_lonlat_with_wkt(message)
2019-01-28 21:06:38 +00:00
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']
2018-09-03 17:58:35 +00:00
# update raw_message
message['raw_message'] = raw_string
return message
saver = DbSaver(session=session)
2019-01-28 21:06:38 +00:00
def process_raw_message(raw_message, reference_date=None, saver=saver):
2018-09-03 17:58:35 +00:00
logger.debug('Received message: {}'.format(raw_message))
message = string_to_message(raw_message, reference_date)
2019-01-28 21:06:38 +00:00
saver.add_message(message)