diff options
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__': |