Modified example code

pull/62/head
Konstantin Gründger 2018-05-03 07:47:38 +02:00
rodzic a1f84c6cb7
commit c779e908af
2 zmienionych plików z 41 dodań i 31 usunięć

Wyświetl plik

@ -24,10 +24,10 @@ Parse APRS/OGN packet.
``` ```
from ogn.parser import parse 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", 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. Connect to OGN and display all incoming beacons.
@ -37,12 +37,9 @@ from ogn.client import AprsClient
from ogn.parser import parse, ParseError from ogn.parser import parse, ParseError
def process_beacon(raw_message): def process_beacon(raw_message):
if raw_message[0] == '#':
print('Server Status: {}'.format(raw_message))
return
try: try:
beacon = parse(raw_message) beacon = parse(raw_message)
print('Received {beacon_type} from {name}'.format(**beacon)) print('Received {beacon_type}: {raw_message}'.format(**beacon))
except ParseError as e: except ParseError as e:
print('Error, {}'.format(e.message)) print('Error, {}'.format(e.message))

Wyświetl plik

@ -33,27 +33,35 @@ def parse_aprs(message, reference_timestamp=None):
if reference_timestamp is None: if reference_timestamp is None:
reference_timestamp = datetime.utcnow() reference_timestamp = datetime.utcnow()
result = {'raw_message': message,
'reference_timestamp': reference_timestamp}
if message and message[0] == '#': if message and message[0] == '#':
match_server = re.search(PATTERN_SERVER, message) match_server = re.search(PATTERN_SERVER, message)
if match_server: if match_server:
return {'version': match_server.group('version'), result.update({
'timestamp': datetime.strptime(match_server.group('timestamp'), "%d %b %Y %H:%M:%S %Z"), 'version': match_server.group('version'),
'server': match_server.group('server'), 'timestamp': datetime.strptime(match_server.group('timestamp'), "%d %b %Y %H:%M:%S %Z"),
'ip_address': match_server.group('ip_address'), 'server': match_server.group('server'),
'port': match_server.group('port'), 'ip_address': match_server.group('ip_address'),
'aprs_type': 'server'} 'port': match_server.group('port'),
'aprs_type': 'server'})
else: else:
return {'comment': message, result.update({
'aprs_type': 'comment'} 'comment': message,
'aprs_type': 'comment'})
match = re.search(PATTERN_APRS, message) else:
if match: match = re.search(PATTERN_APRS, message)
aprs_type = 'position' if match.group('aprs_type') == '/' else 'status' if match:
aprs_body = match.group('aprs_body') aprs_type = 'position' if match.group('aprs_type') == '/' else 'status'
if aprs_type == 'position': result.update({'aprs_type': aprs_type})
match_position = re.search(PATTERN_APRS_POSITION, aprs_body) aprs_body = match.group('aprs_body')
if match_position: if aprs_type == 'position':
return {'name': match.group('callsign'), match_position = re.search(PATTERN_APRS_POSITION, aprs_body)
if match_position:
result.update({
'name': match.group('callsign'),
'dstcall': match.group('dstcall'), 'dstcall': match.group('dstcall'),
'relay': match.group('relay') if match.group('relay') else None, 'relay': match.group('relay') if match.group('relay') else None,
'receiver_name': match.group('receiver'), '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, '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, '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, 'altitude': int(match_position.group('altitude')) * FEETS_TO_METER,
'comment': match_position.group('comment') if match_position.group('comment') else "", 'comment': match_position.group('comment') if match_position.group('comment') else ""})
'aprs_type': aprs_type} else:
elif aprs_type == 'status': raise AprsParseError(message)
match_status = re.search(PATTERN_APRS_STATUS, aprs_body) elif aprs_type == 'status':
if match_status: match_status = re.search(PATTERN_APRS_STATUS, aprs_body)
return {'name': match.group('callsign'), if match_status:
result.update({
'name': match.group('callsign'),
'dstcall': match.group('dstcall'), 'dstcall': match.group('dstcall'),
'receiver_name': match.group('receiver'), 'receiver_name': match.group('receiver'),
'timestamp': createTimestamp(match_status.group('time'), reference_timestamp), 'timestamp': createTimestamp(match_status.group('time'), reference_timestamp),
'comment': match_status.group('comment') if match_status.group('comment') else "", 'comment': match_status.group('comment') if match_status.group('comment') else ""})
'aprs_type': aprs_type} else:
raise AprsParseError(message)
else:
raise AprsParseError(message)
raise AprsParseError(message) return result
dstcall_parser_mapping = {'APRS': OgnParser(), dstcall_parser_mapping = {'APRS': OgnParser(),