def on_message(self, message): message = message.replace(b'\x00', b' ') try: parsed_data = parse_aanderaa_message(message) log.debug("Extracted %s" % parsed_data) except ValueError as e: msg = "Unable to parse '{!r}' from {}. Error was: {}" log.error(msg.format(message, self.peername, e)) return except Exception as e: logging.exception(e) return try: args = { 'magnetic_degrees': parsed_data['direction'], 'true_degrees': parsed_data['direction'] - self.magnetic_declination, 'speed_in_knots': parsed_data['speed'] * 0.01944 } water_flow_sentence = format_water_flow_sentence(**args) self.concentrator_server.send(water_flow_sentence) except ValueError as e: msg = ("Unable to convert '{!r}' from {} to NMEA water flow " "sentence. Error was: {}") log.error(msg.format(message, self.peername, e)) except Exception as e: logging.exception(e) try: temperature = parsed_data['temperature'] temperature_sentence = format_temperature_sentence(temperature) self.concentrator_server.send(temperature_sentence) except ValueError as e: msg = ("Unable to convert '{!r}' from {} to NMEA temperature " "sentence. Error was: {}") log.error(msg.format(message, self.peername, e)) except Exception as e: logging.exception(e)
def test_parse_aanderaa_message(): aanderaa_messages = [ b'0701 0116 0906 0366\r\t', b'0701 0106 0912 0366\r\t', b'0699 0111 0915 0366\r\t', b'0702 0116 0919 0366\r\t', b'0704 0097 0938 0366\r\t', b'0701 0089 0928 0366\r\t', b'0699 0087 0945 0366\r\t', b'0701 0080 0954 0366\r\t', ] expected_data = [ { 'direction': 318.5496, 'temperature': 18.96246, 'reference': 701, 'speed': 34.022800000000004 }, { 'direction': 320.6592, 'temperature': 18.96246, 'reference': 701, 'speed': 31.0898 }, { 'direction': 321.714, 'temperature': 18.96246, 'reference': 699, 'speed': 32.5563 }, { 'direction': 323.1204, 'temperature': 18.96246, 'reference': 702, 'speed': 34.022800000000004 }, { 'direction': 329.80080000000004, 'temperature': 18.96246, 'reference': 704, 'speed': 28.4501 }, { 'direction': 326.2848, 'temperature': 18.96246, 'reference': 701, 'speed': 26.1037 }, { 'direction': 332.262, 'temperature': 18.96246, 'reference': 699, 'speed': 25.5171 }, { 'direction': 335.4264, 'temperature': 18.96246, 'reference': 701, 'speed': 23.464 }, ] for msg, data in zip(aanderaa_messages, expected_data): assert parse_aanderaa_message(msg) == data