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/sensors_mqtt_sender.py | |
parent | 09a6b8d1ed6ac97a044023987cb40dbd16d45bcb (diff) |
mqtt refactoring (wip)
Diffstat (limited to 'src/sensors_mqtt_sender.py')
-rwxr-xr-x | src/sensors_mqtt_sender.py | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/src/sensors_mqtt_sender.py b/src/sensors_mqtt_sender.py index f4f8ec9..2cf2717 100755 --- a/src/sensors_mqtt_sender.py +++ b/src/sensors_mqtt_sender.py @@ -1,24 +1,21 @@ #!/usr/bin/env python3 -import paho.mqtt.client as mqtt -import logging import time import json from home.util import parse_addr, MySimpleSocketClient from home.mqtt import MQTTBase, poll_tick -from home.mqtt.message import Temperature +from home.mqtt.payload.sensors import Temperature from home.config import config -logger = logging.getLogger(__name__) - class MQTTClient(MQTTBase): - def on_connect(self, client: mqtt.Client, userdata, flags, rc): - super().on_connect(client, userdata, flags, rc) + def __init__(self): + super().__init__(self) + self._home_id = config['mqtt']['home_id'] def poll(self): freq = int(config['mqtt']['sensors']['poll_freq']) - logger.debug(f'freq={freq}') + self._logger.debug(f'freq={freq}') g = poll_tick(freq) while True: @@ -28,7 +25,7 @@ class MQTTClient(MQTTBase): self.publish_si7021(host, port, k) def publish_si7021(self, host: str, port: int, name: str): - logging.debug(f"publish_si7021/{name}: {host}:{port}") + self._logger.debug(f"publish_si7021/{name}: {host}:{port}") try: now = time.time() @@ -40,14 +37,16 @@ class MQTTClient(MQTTBase): temp = response['temp'] humidity = response['humidity'] - logging.debug(f'publish_si7021/{name}: temp={temp} humidity={humidity}') + self._logger.debug(f'publish_si7021/{name}: temp={temp} humidity={humidity}') - packer = Temperature() - self.client.publish(f'home/{self.home_id}/si7021/{name}', - payload=packer.pack(round(now), temp, humidity), - qos=1) + pld = Temperature(time=round(now), + temp=temp, + rh=humidity) + self._client.publish(f'hk/{self._home_id}/si7021/{name}', + payload=pld.pack(), + qos=1) except Exception as e: - logger.exception(e) + self._logger.exception(e) if __name__ == '__main__': |