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/inverter_mqtt_sender.py | |
parent | 09a6b8d1ed6ac97a044023987cb40dbd16d45bcb (diff) |
mqtt refactoring (wip)
Diffstat (limited to 'src/inverter_mqtt_sender.py')
-rwxr-xr-x | src/inverter_mqtt_sender.py | 40 |
1 files changed, 17 insertions, 23 deletions
diff --git a/src/inverter_mqtt_sender.py b/src/inverter_mqtt_sender.py index 4e06436..74191a2 100755 --- a/src/inverter_mqtt_sender.py +++ b/src/inverter_mqtt_sender.py @@ -1,6 +1,4 @@ #!/usr/bin/env python3 -import paho.mqtt.client as mqtt -import logging import time import datetime import json @@ -8,21 +6,18 @@ import inverterd from home.config import config from home.mqtt import MQTTBase, poll_tick -from home.mqtt.message import Status, Generation - -logger = logging.getLogger(__name__) +from home.mqtt.payload.inverter import Status, Generation class MQTTClient(MQTTBase): def __init__(self): super().__init__() - self.inverter = inverterd.Client() - self.inverter.connect() - self.inverter.format(inverterd.Format.SIMPLE_JSON) + self._home_id = config['mqtt']['home_id'] - def on_connect(self, client: mqtt.Client, userdata, flags, rc): - super().on_connect(client, userdata, flags, rc) + self._inverter = inverterd.Client() + self._inverter.connect() + self._inverter.format(inverterd.Format.SIMPLE_JSON) def poll_inverter(self): freq = int(config['mqtt']['inverter']['poll_freq']) @@ -36,18 +31,18 @@ class MQTTClient(MQTTBase): # read status now = time.time() try: - raw = self.inverter.exec('get-status') + raw = self._inverter.exec('get-status') except inverterd.InverterError as e: - logger.error(f'inverter error: {str(e)}') + self._logger.error(f'inverter error: {str(e)}') # TODO send to server continue data = json.loads(raw)['data'] + status = Status(time=round(now), **data) # FIXME this will crash with 99% probability - packer = Status() - self.client.publish(f'home/{self.home_id}/status', - payload=packer.pack(round(now), data), - qos=1) + self._client.publish(f'hk/{self._home_id}/status', + payload=status.pack(), + qos=1) # read today's generation stat now = time.time() @@ -55,18 +50,17 @@ class MQTTClient(MQTTBase): gen_prev = now today = datetime.date.today() try: - raw = self.inverter.exec('get-day-generated', (today.year, today.month, today.day)) + raw = self._inverter.exec('get-day-generated', (today.year, today.month, today.day)) except inverterd.InverterError as e: - logger.error(f'inverter error: {str(e)}') + self._logger.error(f'inverter error: {str(e)}') # TODO send to server continue - # print('raw:', raw, type(raw)) data = json.loads(raw)['data'] - packer = Generation() - self.client.publish(f'home/{self.home_id}/gen', - payload=packer.pack(round(now), data['wh']), - qos=1) + gen = Generation(time=round(now), wh=data['wh']) + self._client.publish(f'hk/{self._home_id}/gen', + payload=gen.pack(), + qos=1) if __name__ == '__main__': |