Exemplo n.º 1
0
    def on_message(self, message):
        try:
            parsed_data = parse_optiplex_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))
        except Exception as e:
            logging.exception(e)

        value = parsed_data['value']
        unit = parsed_data['unit']

        if unit == "cm":

            try:
                water_depth_sentence = format_water_depth_sentence(value / 100)
                self.concentrator_server.send(water_depth_sentence)
            except ValueError as e:
                msg = ("Unable to convert '{!r}' from {} to NMEA water depth "
                       "sentence. Error was: {}")
                log.error(msg.format(message, self.peername, e))
            except Exception as e:
                logging.exception(e)

        if unit == "hPa":
            try:
                value *= 100
                pressure_sentence = format_pressure_sentence(value)
                self.concentrator_server.send(pressure_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)
Exemplo n.º 2
0
    def on_message(self, message):
        try:
            parsed_data = parse_optiplex_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))
        except Exception as e:
            logging.exception(e)

        value = parsed_data['value']
        unit = parsed_data['unit']

        if unit == "cm":

            try:
                water_depth_sentence = format_water_depth_sentence(value / 100)
                self.concentrator_server.send(water_depth_sentence)
            except ValueError as e:
                msg = ("Unable to convert '{!r}' from {} to NMEA water depth "
                       "sentence. Error was: {}")
                log.error(msg.format(message, self.peername, e))
            except Exception as e:
                logging.exception(e)

        if unit == "hPa":
            try:
                value *= 100
                pressure_sentence = format_pressure_sentence(value)
                self.concentrator_server.send(pressure_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)
Exemplo n.º 3
0
def test_parse_optiplex_message():

    optiplex_messages = [
        b'20101217150000+0543.8cm0\r\n',
        b'20101217150001+0544.0cm0\r\n',
        b'201012171500011001.6hPa\r\n',
        b'20101217150002+0544.1cm0\r\n',
        b'20160203 145313 +0365.0 cm 0\r\n',
        b'20160203 145314 +0364.4 cm 0\r\n',
        b'20160203 145315 +0364.8 cm 0\r\n',
        b'20160203 145316 +0363.8 cm 0\r\n',
    ]

    expected_data = [
        {
            'timestamp': datetime(2010, 12, 17, 15, 0),
            'alert': 0,
            'unit': 'cm',
            'value': 543.8
        },
        {
            'timestamp': datetime(2010, 12, 17, 15, 0, 1),
            'alert': 0,
            'unit': 'cm',
            'value': 544.0
        },
        {
            'timestamp': datetime(2010, 12, 17, 15, 0, 1),
            'alert': None,
            'unit': 'hPa',
            'value': 1001.6
        },
        {
            'timestamp': datetime(2010, 12, 17, 15, 0, 2),
            'alert': 0,
            'unit': 'cm',
            'value': 544.1
        },
        {
            'timestamp': datetime(2016, 2, 3, 14, 53, 13),
            'alert': 0,
            'unit': 'cm',
            'value': 365.0
        },
        {
            'timestamp': datetime(2016, 2, 3, 14, 53, 14),
            'alert': 0,
            'unit': 'cm',
            'value': 364.4
        },
        {
            'timestamp': datetime(2016, 2, 3, 14, 53, 15),
            'alert': 0,
            'unit': 'cm',
            'value': 364.8
        },
        {
            'timestamp': datetime(2016, 2, 3, 14, 53, 16),
            'alert': 0,
            'unit': 'cm',
            'value': 363.8
        },
    ]

    for msg, data in zip(optiplex_messages, expected_data):
        assert parse_optiplex_message(msg) == data