From c779e908afe5379909c8e4a0eadac5da6a412787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Gru=CC=88ndger?= Date: Thu, 3 May 2018 07:47:38 +0200 Subject: [PATCH] Modified example code --- README.md | 9 +++---- ogn/parser/parse.py | 63 +++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 6d82f05..fca808d 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ Parse APRS/OGN packet. ``` from ogn.parser import parse -from datetime import date, time +from datetime import datetime beacon = parse("FLRDDDEAD>APRS,qAS,EDER:/114500h5029.86N/00956.98E'342/049/A=005524 id0ADDDEAD -454fpm -1.1rot 8.8dB 0e +51.2kHz gps4x5", - reference_date=date(2016,1,1), reference_time=time(11,46)) + reference_timestamp=datetime(2015, 07, 31, 12, 34, 56)) ``` Connect to OGN and display all incoming beacons. @@ -37,12 +37,9 @@ from ogn.client import AprsClient from ogn.parser import parse, ParseError def process_beacon(raw_message): - if raw_message[0] == '#': - print('Server Status: {}'.format(raw_message)) - return try: beacon = parse(raw_message) - print('Received {beacon_type} from {name}'.format(**beacon)) + print('Received {beacon_type}: {raw_message}'.format(**beacon)) except ParseError as e: print('Error, {}'.format(e.message)) diff --git a/ogn/parser/parse.py b/ogn/parser/parse.py index 916089d..37f3774 100644 --- a/ogn/parser/parse.py +++ b/ogn/parser/parse.py @@ -33,27 +33,35 @@ def parse_aprs(message, reference_timestamp=None): if reference_timestamp is None: reference_timestamp = datetime.utcnow() + result = {'raw_message': message, + 'reference_timestamp': reference_timestamp} + if message and message[0] == '#': match_server = re.search(PATTERN_SERVER, message) if match_server: - return {'version': match_server.group('version'), - 'timestamp': datetime.strptime(match_server.group('timestamp'), "%d %b %Y %H:%M:%S %Z"), - 'server': match_server.group('server'), - 'ip_address': match_server.group('ip_address'), - 'port': match_server.group('port'), - 'aprs_type': 'server'} + result.update({ + 'version': match_server.group('version'), + 'timestamp': datetime.strptime(match_server.group('timestamp'), "%d %b %Y %H:%M:%S %Z"), + 'server': match_server.group('server'), + 'ip_address': match_server.group('ip_address'), + 'port': match_server.group('port'), + 'aprs_type': 'server'}) else: - return {'comment': message, - 'aprs_type': 'comment'} + result.update({ + 'comment': message, + 'aprs_type': 'comment'}) - match = re.search(PATTERN_APRS, message) - if match: - aprs_type = 'position' if match.group('aprs_type') == '/' else 'status' - aprs_body = match.group('aprs_body') - if aprs_type == 'position': - match_position = re.search(PATTERN_APRS_POSITION, aprs_body) - if match_position: - return {'name': match.group('callsign'), + else: + match = re.search(PATTERN_APRS, message) + if match: + aprs_type = 'position' if match.group('aprs_type') == '/' else 'status' + result.update({'aprs_type': aprs_type}) + aprs_body = match.group('aprs_body') + if aprs_type == 'position': + match_position = re.search(PATTERN_APRS_POSITION, aprs_body) + if match_position: + result.update({ + 'name': match.group('callsign'), 'dstcall': match.group('dstcall'), 'relay': match.group('relay') if match.group('relay') else None, 'receiver_name': match.group('receiver'), @@ -67,19 +75,24 @@ def parse_aprs(message, reference_timestamp=None): 'track': int(match_position.group('course')) if match_position.group('course_extension') else None, 'ground_speed': int(match_position.group('ground_speed')) * KNOTS_TO_MS / KPH_TO_MS if match_position.group('ground_speed') else None, 'altitude': int(match_position.group('altitude')) * FEETS_TO_METER, - 'comment': match_position.group('comment') if match_position.group('comment') else "", - 'aprs_type': aprs_type} - elif aprs_type == 'status': - match_status = re.search(PATTERN_APRS_STATUS, aprs_body) - if match_status: - return {'name': match.group('callsign'), + 'comment': match_position.group('comment') if match_position.group('comment') else ""}) + else: + raise AprsParseError(message) + elif aprs_type == 'status': + match_status = re.search(PATTERN_APRS_STATUS, aprs_body) + if match_status: + result.update({ + 'name': match.group('callsign'), 'dstcall': match.group('dstcall'), 'receiver_name': match.group('receiver'), 'timestamp': createTimestamp(match_status.group('time'), reference_timestamp), - 'comment': match_status.group('comment') if match_status.group('comment') else "", - 'aprs_type': aprs_type} + 'comment': match_status.group('comment') if match_status.group('comment') else ""}) + else: + raise AprsParseError(message) + else: + raise AprsParseError(message) - raise AprsParseError(message) + return result dstcall_parser_mapping = {'APRS': OgnParser(),