Exemple #1
0
def parse_aprs(message, reference_date=None, reference_time=None):
    if reference_date is None:
        now = datetime.utcnow()
        reference_date = now.date()
        reference_time = now.time()

    match_position = re.search(PATTERN_APRS_POSITION, message)
    if match_position:
        return {'name': match_position.group('callsign'),
                'receiver_name': match_position.group('receiver'),
                'dstcall': match_position.group('dstcall'),
                'timestamp': createTimestamp(match_position.group('time'), reference_date, reference_time),
                'latitude': parseAngle('0' + match_position.group('latitude') + (match_position.group('latitude_enhancement') or '0')) *
                (-1 if match_position.group('latitude_sign') == 'S' else 1),
                'symboltable': match_position.group('symbol_table'),
                'longitude': parseAngle(match_position.group('longitude') + (match_position.group('longitude_enhancement') or '0')) *
                (-1 if match_position.group('longitude_sign') == 'W' else 1),
                'symbolcode': match_position.group('symbol'),
                'track': int(match_position.group('course')) if match_position.group('course_extension') else None,
                'ground_speed': int(match_position.group('ground_speed')) * kts2kmh if match_position.group('ground_speed') else None,
                'altitude': int(match_position.group('altitude')) * feet2m,
                'comment': match_position.group('comment')}

    match_status = re.search(PATTERN_APRS_STATUS, message)
    if match_status:
        return {'name': match_status.group('callsign'),
                'receiver_name': match_status.group('receiver'),
                'dstcall': match_status.group('dstcall'),
                'timestamp': createTimestamp(match_status.group('time'), reference_date, reference_time),
                'comment': match_status.group('comment')}

    raise AprsParseError(message)
Exemple #2
0
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:
            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:
            result.update({'comment': message, 'aprs_type': 'comment'})

    else:
        match = re.search(PATTERN_APRS, message)
        if match:
            aprs_type = 'position' if match.group(
                'aprs_type') == '/' else 'status' if match.group(
                    'aprs_type') == '>' else 'unknown'
            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'),
                        'timestamp':
                        createTimestamp(match_position.group('time'),
                                        reference_timestamp),
                        'latitude':
                        parseAngle('0' + match_position.group('latitude') +
                                   (match_position.group(
                                       'latitude_enhancement') or '0'))
                        *  # noqa: W504
                        (-1 if match_position.group('latitude_sign') == 'S'
                         else 1),
                        'symboltable':
                        match_position.group('symbol_table'),
                        'longitude':
                        parseAngle(
                            match_position.group('longitude') +
                            (match_position.group('longitude_enhancement')
                             or '0')) *  # noqa: W504
                        (-1 if match_position.group('longitude_sign') == 'W'
                         else 1),
                        'symbolcode':
                        match_position.group('symbol'),
                        '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
                        if match_position.group('altitude') else None,
                        'aircrafttype':
                        get_aircraft_type(match_position.group('symbol_table'),
                                          match_position.group('symbol')),
                        '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 ""
                    })
                else:
                    raise NotImplementedError(message)
        else:
            raise AprsParseError(message)

    return result
 def test_parseAngle(self):
     self.assertAlmostEqual(parseAngle('05048.30'), 50.805, 5)
 def test_parseAngle(self):
     self.assertAlmostEqual(parseAngle('05048.30'), 50.805, 5)
Exemple #5
0
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:
            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:
            result.update({'comment': message, 'aprs_type': 'comment'})
    else:
        match = re.search(PATTERN_APRS, message)
        if match:
            aprs_type = 'position' if match.group(
                'aprs_type') == '/' else 'status' if match.group(
                    'aprs_type') == '>' else 'unknown'
            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'),
                        'timestamp':
                        createTimestamp(match_position.group('time'),
                                        reference_timestamp),
                        'latitude':
                        parseAngle('0' + match_position.group('latitude') +
                                   (match_position.group(
                                       'latitude_enhancement') or '0'))
                        *  # noqa: W504
                        (-1 if match_position.group('latitude_sign') == 'S'
                         else 1),
                        'symboltable':
                        match_position.group('symbol_table'),
                        'longitude':
                        parseAngle(
                            match_position.group('longitude') +
                            (match_position.group('longitude_enhancement')
                             or '0')) *  # noqa: W504
                        (-1 if match_position.group('longitude_sign') == 'W'
                         else 1),
                        'symbolcode':
                        match_position.group('symbol'),
                        '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
                        if match_position.group('altitude') else None,
                        'comment':
                        match_position.group('comment')
                        if match_position.group('comment') else "",
                    })
                    return result

                match_position_weather = re.search(
                    PATTERN_APRS_POSITION_WEATHER, aprs_body)
                if match_position_weather:
                    result.update({
                        'aprs_type':
                        'position_weather',
                        'name':
                        match.group('callsign'),
                        'dstcall':
                        match.group('dstcall'),
                        'relay':
                        match.group('relay') if match.group('relay') else None,
                        'receiver_name':
                        match.group('receiver'),
                        'timestamp':
                        createTimestamp(match_position_weather.group('time'),
                                        reference_timestamp),
                        'latitude':
                        parseAngle('0' +
                                   match_position_weather.group('latitude'))
                        *  # noqa: W504
                        (-1 if match_position_weather.group('latitude_sign')
                         == 'S' else 1),
                        'symboltable':
                        match_position_weather.group('symbol_table'),
                        'longitude':
                        parseAngle(match_position_weather.group('longitude'))
                        *  # noqa: W504
                        (-1 if match_position_weather.group('longitude_sign')
                         == 'W' else 1),
                        'symbolcode':
                        match_position_weather.group('symbol'),
                        'wind_direction':
                        int(match_position_weather.group('wind_direction')) if
                        match_position_weather.group('wind_direction') != '...'
                        else None,
                        'wind_speed':
                        int(match_position_weather.group('wind_speed')) *
                        KNOTS_TO_MS / KPH_TO_MS
                        if match_position_weather.group('wind_speed') != '...'
                        else None,
                        'wind_speed_peak':
                        int(match_position_weather.group('wind_speed_peak')) *
                        KNOTS_TO_MS / KPH_TO_MS
                        if match_position_weather.group('wind_speed_peak') !=
                        '...' else None,
                        'temperature':
                        fahrenheit_to_celsius(
                            float(match_position_weather.group('temperature')))
                        if match_position_weather.group('temperature') != '...'
                        else None,
                        'rainfall_1h':
                        int(match_position_weather.group('rainfall_1h')) /
                        100.0 * INCH_TO_MM if
                        match_position_weather.group('rainfall_1h') else None,
                        'rainfall_24h':
                        int(match_position_weather.group('rainfall_24h')) /
                        100.0 * INCH_TO_MM if
                        match_position_weather.group('rainfall_24h') else None,
                        'humidity':
                        int(match_position_weather.group('humidity')) * 0.01
                        if match_position_weather.group('humidity') else None,
                        'barometric_pressure':
                        int(match_position_weather.group(
                            'barometric_pressure'))
                        if match_position_weather.group('barometric_pressure')
                        else None,
                        'comment':
                        match_position_weather.group('comment')
                        if match_position_weather.group('comment') else "",
                    })
                    return result

                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 ""
                    })
                else:
                    raise NotImplementedError(message)
        else:
            raise AprsParseError(message)

    return result
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:
            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:
            result.update({
                'comment': message,
                'aprs_type': 'comment'})

    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'),
                        'timestamp': createTimestamp(match_position.group('time'), reference_timestamp),
                        'latitude': parseAngle('0' + match_position.group('latitude') + (match_position.group('latitude_enhancement') or '0')) *
                        (-1 if match_position.group('latitude_sign') == 'S' else 1),
                        'symboltable': match_position.group('symbol_table'),
                        'longitude': parseAngle(match_position.group('longitude') + (match_position.group('longitude_enhancement') or '0')) *
                        (-1 if match_position.group('longitude_sign') == 'W' else 1),
                        'symbolcode': match_position.group('symbol'),
                        '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 ""})
                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 ""})
                else:
                    raise AprsParseError(message)
        else:
            raise AprsParseError(message)

    return result