diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-12-18 05:33:54 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-12-18 05:33:54 +0300 |
commit | 9af0e28b94fd44d2340b0b9ddf2dcccc11d48271 (patch) | |
tree | b27dea0aee4226a802e641070b202eaa88b9455a /src/home/mqtt/payload/relay.py | |
parent | 09a6b8d1ed6ac97a044023987cb40dbd16d45bcb (diff) |
mqtt refactoring (wip)
Diffstat (limited to 'src/home/mqtt/payload/relay.py')
-rw-r--r-- | src/home/mqtt/payload/relay.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/home/mqtt/payload/relay.py b/src/home/mqtt/payload/relay.py new file mode 100644 index 0000000..2a327ba --- /dev/null +++ b/src/home/mqtt/payload/relay.py @@ -0,0 +1,54 @@ +from .base_payload import MQTTPayload, MQTTPayloadCustomField + + +class StatFlags(MQTTPayloadCustomField): + state: bool + config_changed_value_present: bool + config_changed: bool + + @staticmethod + def unpack(flags: int): + state = flags & 0x1 + ccvp = (flags >> 1) & 0x1 + cc = (flags >> 2) & 0x1 + return StatFlags(state=(state == 1), + config_changed_value_present=(ccvp == 1), + config_changed=(cc == 1)) + + def __index__(self): + bits = 0 + bits |= (int(self.state) & 0x1) + bits |= (int(self.config_changed_value_present) & 0x1) << 1 + bits |= (int(self.config_changed) & 0x1) << 2 + return bits + + +class InitialStatPayload(MQTTPayload): + FORMAT = 'IBbIB' + + ip: int + fw_version: int + rssi: int + free_heap: int + flags: StatFlags + + +class StatPayload(MQTTPayload): + FORMAT = 'bIB' + + rssi: int + free_heap: int + flags: StatFlags + + +class PowerPayload(MQTTPayload): + FORMAT = '12sB' + PACKER = { + 'state': lambda n: int(n) + } + UNPACKER = { + 'state': lambda n: bool(n) + } + + secret: str + state: bool |