diff --git a/pyblnet/blnet_parser.py b/pyblnet/blnet_parser.py index 441c362..953db9c 100644 --- a/pyblnet/blnet_parser.py +++ b/pyblnet/blnet_parser.py @@ -79,30 +79,18 @@ def __init__(self, data): self.digital[channel + 1] = self._convert_digital(digital, channel) self.speed = {} - for channel in range(0, 4): - speed = self._convert_speed(speed[channel]) - if speed: - self.speed[channel + 1] = round(speed, 3) - else: - break + for idx, value in enumerate(speed): + self.speed[idx + 1] = self._convert_speed(value) self.energy = {} for channel in range(0, 2): energy = self._convert_energy(MWh[channel], kWh[channel], active, channel) - if energy: - self.energy[channel + 1] = round(energy, 3) - else: - break + self.energy[channel + 1] = energy self.power = {} - for channel in range(0, 2): - power = self._convert_power(power[channel], active, channel) - if power: - self.power[channel + 1] = round( - self._convert_power(power[channel], active, channel), 3 - ) - else: - break + for idx, value in enumerate(power): + power = self._convert_power(value, active, idx) + self.power[idx + 1] = power def to_dict(self): """ @@ -149,7 +137,7 @@ def _convert_speed(self, value): if value & SPEED_ACTIVE: return None else: - return value & SPEED_MASK + return round(value & SPEED_MASK, 3) def _convert_energy(self, mwh, kwh, active, position): """ @@ -158,7 +146,7 @@ def _convert_energy(self, mwh, kwh, active, position): """ if active & position: kwh = self._calculate_value(kwh, 0.1, INT16_POSITIVE_MASK) - return mwh * 1000 + kwh + return round(mwh * 1000 + kwh, 3) else: return None @@ -168,12 +156,14 @@ def _convert_power(self, value, active, position): @return its power """ if active & position: - return self._calculate_value(value, 1 / 2560, INT32_MASK, INT32_SIGN) + value = self._calculate_value(value, 1 / 2560, INT32_MASK, INT32_SIGN) + value = round(value, 3) + return value else: return None def _calculate_value( - self, value, multiplier=1, positive_mask=POSITIVE_VALUE_MASK, signbit=SIGN_BIT + self, value, multiplier=1.0, positive_mask=POSITIVE_VALUE_MASK, signbit=SIGN_BIT ): result = value & positive_mask if value & signbit: diff --git a/pyblnet/tests/test_parser.py b/pyblnet/tests/test_parser.py index 831795d..2e9bc3e 100644 --- a/pyblnet/tests/test_parser.py +++ b/pyblnet/tests/test_parser.py @@ -11,12 +11,16 @@ def test_parser_ok(self): expected_values = { 'analog': {1: 12.5, 2: 69.3, 3: 61.4, 4: 51.5, 5: 38.2, 6: 65.4, 7: 27.8, 8: 22.4, 9: 25.5, 10: 24.3, 11: 24.8, 12: 0, 13: 31.7, 14: 1, 15: 0, 16: 48.2}, 'digital': {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0}, - 'speed': {}, - 'energy': {}, - 'power': {} + 'speed': {1: None, 2: 0, 3: 0, 4: 0}, + 'energy': {1: None, 2: None}, + 'power': {1: None, 2: None} } - assert parsed_values == expected_values + assert parsed_values['analog'] == expected_values['analog'] + assert parsed_values['digital'] == expected_values['digital'] + assert parsed_values['speed'] == expected_values['speed'] + assert parsed_values['energy'] == expected_values['energy'] + assert parsed_values['power'] == expected_values['power'] def test_parser_not_ok(self): data = b'broken} \xb5"f"\x03"~!\x8e"\x16!\xe0 \xff \xf3 \xf8 \x00\x00=!\x01\x00\x00`\xe2!\x00\x00\x80\x00\x00\x00\x00,\x00\xa4(\x06\x00DhI\x82\x1d\x04Ce\xc0\x00'