2016-02-18 21:28:03 +00:00
|
|
|
import logging
|
|
|
|
from ogn.commands.dbutils import session
|
2017-05-26 20:56:38 +00:00
|
|
|
from ogn.model import AircraftBeacon, ReceiverBeacon, Location
|
2016-03-18 21:54:22 +00:00
|
|
|
from ogn.parser import parse_aprs, parse_ogn_receiver_beacon, parse_ogn_aircraft_beacon, ParseError
|
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:
|
|
|
|
message = parse_aprs(raw_message, reference_date)
|
|
|
|
# symboltable / symbolcodes used by OGN:
|
|
|
|
# I&: used as receiver
|
|
|
|
# /X: helicopter_rotorcraft
|
|
|
|
# /': glider_or_motorglider
|
|
|
|
# \^: powered_aircraft
|
|
|
|
# /g: para_glider
|
|
|
|
# /O: ?
|
|
|
|
# /^: ?
|
|
|
|
# \n: ?
|
|
|
|
# /z: ?
|
|
|
|
# /o: ?
|
|
|
|
if 'symboltable' not in message and 'symbolcode' not in message:
|
|
|
|
# we have a receiver_beacon (status message)
|
|
|
|
message.update(parse_ogn_receiver_beacon(message['comment']))
|
|
|
|
beacon = ReceiverBeacon(**message)
|
|
|
|
elif message['symboltable'] == "I" and message['symbolcode'] == '&':
|
|
|
|
# ... we have a receiver_beacon
|
2017-06-03 13:24:17 +00:00
|
|
|
if message['comment']:
|
|
|
|
message.update(parse_ogn_receiver_beacon(message['comment']))
|
2016-10-31 07:58:19 +00:00
|
|
|
message = replace_lonlat_with_wkt(message)
|
|
|
|
beacon = ReceiverBeacon(**message)
|
|
|
|
else:
|
|
|
|
# ... we have a aircraft_beacon
|
|
|
|
message.update(parse_ogn_aircraft_beacon(message['comment']))
|
|
|
|
message = replace_lonlat_with_wkt(message)
|
|
|
|
beacon = AircraftBeacon(**message)
|
|
|
|
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
|
|
|
|
|
|
|
|
2016-10-31 07:58:19 +00:00
|
|
|
def process_beacon(raw_message, reference_date=None):
|
|
|
|
beacon = message_to_beacon(raw_message, reference_date)
|
|
|
|
if beacon is not None:
|
2017-05-26 20:56:38 +00:00
|
|
|
session.add(beacon)
|
|
|
|
session.commit()
|
2016-02-18 21:28:03 +00:00
|
|
|
logger.debug('Received message: {}'.format(raw_message))
|