diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2023-05-12 04:33:32 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2023-05-12 04:33:32 +0300 |
commit | f2a7cafc9be2e1e2ad01c77917108b76624e4a66 (patch) | |
tree | e67646ebcdb5c6674bca26b1c8004740b0e00b26 | |
parent | 883f3181392b1f46c9299cf28396cf635c8ac492 (diff) |
temphum: refactoring in progress
-rw-r--r-- | src/home/mqtt/temphum.py | 21 | ||||
-rw-r--r-- | src/home/util.py | 12 | ||||
-rwxr-xr-x | src/temphum_nodes_util.py | 17 | ||||
-rwxr-xr-x | src/temphum_smbus_util.py (renamed from src/temphum.py) | 0 |
4 files changed, 44 insertions, 6 deletions
diff --git a/src/home/mqtt/temphum.py b/src/home/mqtt/temphum.py index b9b2eb9..83886ac 100644 --- a/src/home/mqtt/temphum.py +++ b/src/home/mqtt/temphum.py @@ -1,10 +1,25 @@ import paho.mqtt.client as mqtt import re -from .payload.temphum import ( - TempHumDataPayload -) +from enum import auto +from .payload.temphum import TempHumDataPayload from .esp import MqttEspBase +from ..util import HashableEnum + + +class MqttTempHumNodes(HashableEnum): + KBN_SH_HALL = auto() + KBN_SH_BATHROOM = auto() + KBN_SH_LIVINGROOM = auto() + KBN_SH_BEDROOM = auto() + + KBN_BH_2FL = auto() + KBN_BH_2FL_STREET = auto() + KBN_BH_1FL_LIVINGROOM = auto() + KBN_BH_1FL_BEDROOM = auto() + KBN_BH_1FL_BATHROOM = auto() + + SPB_FLAT120_CABINET = auto() class MqttTempHum(MqttEspBase): diff --git a/src/home/util.py b/src/home/util.py index 5050ebb..d5c4229 100644 --- a/src/home/util.py +++ b/src/home/util.py @@ -7,9 +7,10 @@ import logging import string import random -from enum import Enum +from enum import Enum, IntEnum from datetime import datetime -from typing import Tuple, Optional, List +from typing import Tuple, Optional, List, Union +from zlib import adler32 Addr = Tuple[str, int] # network address type (host, port) @@ -187,4 +188,9 @@ def filesize_fmt(num, suffix="B") -> str: if abs(num) < 1024.0: return f"{num:3.1f} {unit}{suffix}" num /= 1024.0 - return f"{num:.1f} Yi{suffix}"
\ No newline at end of file + return f"{num:.1f} Yi{suffix}" + + +class HashableEnum(Enum): + def hash(self) -> int: + return adler32(self.name.encode())
\ No newline at end of file diff --git a/src/temphum_nodes_util.py b/src/temphum_nodes_util.py new file mode 100755 index 0000000..c700ca8 --- /dev/null +++ b/src/temphum_nodes_util.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +from home.mqtt.temphum import MqttTempHumNodes + +if __name__ == '__main__': + max_name_len = 0 + for node in MqttTempHumNodes: + if len(node.name) > max_name_len: + max_name_len = len(node.name) + + values = [] + for node in MqttTempHumNodes: + hash = node.hash() + if hash in values: + raise ValueError(f'collision detected: {hash}') + values.append(values) + print(' '*(max_name_len-len(node.name)), end='') + print(f'{node.name}: {hash}') diff --git a/src/temphum.py b/src/temphum_smbus_util.py index 0f90835..0f90835 100755 --- a/src/temphum.py +++ b/src/temphum_smbus_util.py |