2016-02-18 21:28:03 +00:00
|
|
|
import logging
|
2017-10-03 11:31:24 +00:00
|
|
|
|
2016-02-18 21:28:03 +00:00
|
|
|
from ogn.commands.dbutils import session
|
2017-05-26 20:56:38 +00:00
|
|
|
from ogn.model import AircraftBeacon, ReceiverBeacon, Location
|
2017-10-03 11:31:24 +00:00
|
|
|
from ogn.parser import parse, ParseError
|
2017-12-13 19:19:11 +00:00
|
|
|
from datetime import datetime, timedelta
|
2017-10-03 11:31:24 +00:00
|
|
|
|
2016-02-18 21:28:03 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-04-24 17:34:25 +00:00
|
|
|
def replace_lonlat_with_wkt(message):
|
|
|
|
location = Location(message['longitude'], message['latitude'])
|
|
|
|
message['location_wkt'] = location.to_wkt()
|
|
|
|
del message['latitude']
|
|
|
|
del message['longitude']
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
2016-10-31 07:58:19 +00:00
|
|
|
def message_to_beacon(raw_message, reference_date):
|
|
|
|
beacon = None
|
2016-10-03 13:14:44 +00:00
|
|
|
|
2016-10-31 07:58:19 +00:00
|
|
|
if raw_message[0] != '#':
|
|
|
|
try:
|
2017-10-03 11:31:24 +00:00
|
|
|
message = parse(raw_message, reference_date)
|
|
|
|
if message['aprs_type'] == 'position':
|
2016-10-31 07:58:19 +00:00
|
|
|
message = replace_lonlat_with_wkt(message)
|
2017-10-03 11:31:24 +00:00
|
|
|
|
|
|
|
if message['beacon_type'] == 'aircraft_beacon':
|
|
|
|
beacon = AircraftBeacon(**message)
|
|
|
|
elif message['beacon_type'] == 'receiver_beacon':
|
2016-10-31 07:58:19 +00:00
|
|
|
beacon = ReceiverBeacon(**message)
|
|
|
|
else:
|
2017-10-03 11:31:24 +00:00
|
|
|
print("Whoops: what is this: {}".format(message))
|
2017-12-02 11:44:57 +00:00
|
|
|
except NotImplementedError as e:
|
|
|
|
logger.error('Received message: {}'.format(raw_message))
|
|
|
|
logger.error(e)
|
2016-10-31 07:58:19 +00:00
|
|
|
except ParseError as e:
|
|
|
|
logger.error('Received message: {}'.format(raw_message))
|
|
|
|
logger.error('Drop packet, {}'.format(e.message))
|
|
|
|
except TypeError as e:
|
|
|
|
logger.error('TypeError: {}'.format(raw_message))
|
2016-05-22 05:23:22 +00:00
|
|
|
|
2016-10-31 07:58:19 +00:00
|
|
|
return beacon
|
2016-05-22 05:23:22 +00:00
|
|
|
|
2017-12-13 19:19:11 +00:00
|
|
|
beacons = list()
|
|
|
|
last_commit = datetime.utcnow()
|
|
|
|
|
2016-05-22 05:23:22 +00:00
|
|
|
|
2016-10-31 07:58:19 +00:00
|
|
|
def process_beacon(raw_message, reference_date=None):
|
2017-12-13 19:19:11 +00:00
|
|
|
global beacons
|
|
|
|
global last_commit
|
|
|
|
|
2016-10-31 07:58:19 +00:00
|
|
|
beacon = message_to_beacon(raw_message, reference_date)
|
|
|
|
if beacon is not None:
|
2017-12-13 19:19:11 +00:00
|
|
|
beacons.append(beacon)
|
2016-02-18 21:28:03 +00:00
|
|
|
logger.debug('Received message: {}'.format(raw_message))
|
2017-12-13 19:19:11 +00:00
|
|
|
|
|
|
|
current_time = datetime.utcnow()
|
|
|
|
elapsed_time = current_time - last_commit
|
|
|
|
if elapsed_time >= timedelta(seconds=1):
|
|
|
|
session.bulk_save_objects(beacons)
|
|
|
|
session.commit()
|
|
|
|
logger.debug('Commited beacons')
|
|
|
|
beacons = list()
|
|
|
|
last_commit = current_time
|