From f2a7cafc9be2e1e2ad01c77917108b76624e4a66 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Fri, 12 May 2023 04:33:32 +0300 Subject: temphum: refactoring in progress --- src/home/mqtt/temphum.py | 21 ++++++++++++++++++--- src/home/util.py | 12 +++++++++--- src/temphum.py | 20 -------------------- src/temphum_nodes_util.py | 17 +++++++++++++++++ src/temphum_smbus_util.py | 20 ++++++++++++++++++++ 5 files changed, 64 insertions(+), 26 deletions(-) delete mode 100755 src/temphum.py create mode 100755 src/temphum_nodes_util.py create mode 100755 src/temphum_smbus_util.py 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.py b/src/temphum.py deleted file mode 100755 index 0f90835..0000000 --- a/src/temphum.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python3 -from argparse import ArgumentParser -from home.temphum import SensorType, create_sensor - - -if __name__ == '__main__': - parser = ArgumentParser() - parser.add_argument('-t', '--type', choices=[item.value for item in SensorType], - required=True, - help='Sensor type') - parser.add_argument('-b', '--bus', type=int, default=0, - help='I2C bus number') - arg = parser.parse_args() - - sensor = create_sensor(SensorType(arg.type), arg.bus) - temp = sensor.temperature() - hum = sensor.humidity() - - print(f'temperature: {temp}') - print(f'rel. humidity: {hum}') 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_smbus_util.py b/src/temphum_smbus_util.py new file mode 100755 index 0000000..0f90835 --- /dev/null +++ b/src/temphum_smbus_util.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +from argparse import ArgumentParser +from home.temphum import SensorType, create_sensor + + +if __name__ == '__main__': + parser = ArgumentParser() + parser.add_argument('-t', '--type', choices=[item.value for item in SensorType], + required=True, + help='Sensor type') + parser.add_argument('-b', '--bus', type=int, default=0, + help='I2C bus number') + arg = parser.parse_args() + + sensor = create_sensor(SensorType(arg.type), arg.bus) + temp = sensor.temperature() + hum = sensor.humidity() + + print(f'temperature: {temp}') + print(f'rel. humidity: {hum}') -- cgit v1.2.3