mqtt/payload: fix bitfields

This commit is contained in:
Evgeny Zinoviev 2022-12-25 03:01:00 +03:00
parent ed590fdd74
commit e7362bd832
2 changed files with 27 additions and 31 deletions

View File

@ -1,7 +1,8 @@
import abc import abc
import struct import struct
import re
from typing import Generic, TypeVar from typing import Optional, Tuple
class MQTTPayload(abc.ABC): class MQTTPayload(abc.ABC):
@ -20,15 +21,9 @@ class MQTTPayload(abc.ABC):
bf_progress = 0 bf_progress = 0
for field, field_type in self.__class__.__annotations__.items(): for field, field_type in self.__class__.__annotations__.items():
field_type_origin = None bfp = _bit_field_params(field_type)
if hasattr(field_type, '__extra__') or hasattr(field_type, '__origin__'): if bfp:
try: n, s, b = bfp
field_type_origin = field_type.__extra__
except AttributeError:
field_type_origin = field_type.__origin__
if field_type_origin is not None and issubclass(field_type_origin, MQTTPayloadBitField):
n, s, b = field_type.__args__
if n != bf_number: if n != bf_number:
if bf_number != -1: if bf_number != -1:
args.append(bf_arg) args.append(bf_arg)
@ -61,15 +56,9 @@ class MQTTPayload(abc.ABC):
bf_progress = 0 bf_progress = 0
for field, field_type in cls.__annotations__.items(): for field, field_type in cls.__annotations__.items():
field_type_origin = None bfp = _bit_field_params(field_type)
if hasattr(field_type, '__extra__') or hasattr(field_type, '__origin__'): if bfp:
try: n, s, b = bfp
field_type_origin = field_type.__extra__
except AttributeError:
field_type_origin = field_type.__origin__
if field_type_origin is not None and issubclass(field_type_origin, MQTTPayloadBitField):
n, s, b = field_type.__args__
if n != bf_number: if n != bf_number:
bf_number = n bf_number = n
bf_progress = 0 bf_progress = 0
@ -86,6 +75,7 @@ class MQTTPayload(abc.ABC):
else: else:
kwargs[field] = cls._unpack_field(field, data[i]) kwargs[field] = cls._unpack_field(field, data[i])
i += 1 i += 1
return cls(**kwargs) return cls(**kwargs)
def _pack_field(self, name): def _pack_field(self, name):
@ -120,10 +110,16 @@ class MQTTPayloadCustomField(abc.ABC):
pass pass
NT = TypeVar('NT') # number of bit field def bit_field(seq_no: int, total_bits: int, bits: int):
ST = TypeVar('ST') # size in bytes return type(f'MQTTPayloadBitField_{seq_no}_{total_bits}_{bits}', (object,), {
BT = TypeVar('BT') # size in bits of particular value 'seq_no': seq_no,
'total_bits': total_bits,
'bits': bits
})
class MQTTPayloadBitField(int, Generic[NT, ST, BT]): def _bit_field_params(cl) -> Optional[Tuple[int, ...]]:
pass match = re.match(r'MQTTPayloadBitField_(\d+)_(\d+)_(\d)$', cl.__name__)
if match is not None:
return tuple([int(match.group(i)) for i in range(1, 4)])
return None

View File

@ -1,6 +1,6 @@
import struct import struct
from .base_payload import MQTTPayload, MQTTPayloadBitField from .base_payload import MQTTPayload, bit_field
from typing import Tuple from typing import Tuple
_mult_10 = lambda n: int(n*10) _mult_10 = lambda n: int(n*10)
@ -57,12 +57,12 @@ class Status(MQTTPayload):
pv2_input_voltage: float pv2_input_voltage: float
# H # H
mppt1_charger_status: MQTTPayloadBitField[0, 16, 2] mppt1_charger_status: bit_field(0, 16, 2)
mppt2_charger_status: MQTTPayloadBitField[0, 16, 2] mppt2_charger_status: bit_field(0, 16, 2)
battery_power_direction: MQTTPayloadBitField[0, 16, 2] battery_power_direction: bit_field(0, 16, 2)
dc_ac_power_direction: MQTTPayloadBitField[0, 16, 2] dc_ac_power_direction: bit_field(0, 16, 2)
line_power_direction: MQTTPayloadBitField[0, 16, 2] line_power_direction: bit_field(0, 16, 2)
load_connected: MQTTPayloadBitField[0, 16, 1] load_connected: bit_field(0, 16, 1)
class Generation(MQTTPayload): class Generation(MQTTPayload):