diff options
Diffstat (limited to 'src/sensors_mqtt_receiver.py')
-rwxr-xr-x | src/sensors_mqtt_receiver.py | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/src/sensors_mqtt_receiver.py b/src/sensors_mqtt_receiver.py index 011ee44..99b6af2 100755 --- a/src/sensors_mqtt_receiver.py +++ b/src/sensors_mqtt_receiver.py @@ -1,16 +1,13 @@ #!/usr/bin/env python3 import paho.mqtt.client as mqtt -import logging import re from home.mqtt import MQTTBase from home.config import config -from home.mqtt.message import Temperature +from home.mqtt.payload.sensors import Temperature from home.api.types import TemperatureSensorLocation from home.database import SensorsDatabase -logger = logging.getLogger(__name__) - def get_sensor_type(sensor: str) -> TemperatureSensorLocation: for item in TemperatureSensorLocation: @@ -26,27 +23,26 @@ class MQTTServer(MQTTBase): def on_connect(self, client: mqtt.Client, userdata, flags, rc): super().on_connect(client, userdata, flags, rc) - logger.info("subscribing to home/#") + self._logger.info("subscribing to home/#") client.subscribe('home/#', qos=1) def on_message(self, client: mqtt.Client, userdata, msg): try: variants = '|'.join([s.name.lower() for s in TemperatureSensorLocation]) - match = re.match(rf'home/(\d+)/si7021/({variants})', msg.topic) + match = re.match(rf'(?:home|hk)/(\d+)/si7021/({variants})', msg.topic) if not match: return + # FIXME string home_id must be supported home_id = int(match.group(1)) sensor = get_sensor_type(match.group(2)) - packer = Temperature() - client_time, temp, rh = packer.unpack(msg.payload) - - self.database.add_temperature(home_id, client_time, sensor, - temp=int(temp*100), - rh=int(rh*100)) + payload = Temperature.unpack(msg.payload) + self.database.add_temperature(home_id, payload.time, sensor, + temp=int(payload.temp*100), + rh=int(payload.rh*100)) except Exception as e: - logger.exception(str(e)) + self._logger.exception(str(e)) if __name__ == '__main__': |