diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aeea99..070f4a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- fix issue [#18](https://github.com/s-allius/tsun-gen3-proxy/issues/18) +- initialize the proxy statistics + ## [0.4.0] - 2023-10-16 - fix issue [#8](https://github.com/s-allius/tsun-gen3-proxy/issues/8) diff --git a/app/src/infos.py b/app/src/infos.py index c179614..9130cd4 100644 --- a/app/src/infos.py +++ b/app/src/infos.py @@ -4,14 +4,28 @@ import struct, json, logging, os class Infos: stat = {} + app_name = os.getenv('SERVICE_NAME', 'proxy') + version = os.getenv('VERSION', 'unknown') + + @classmethod + def static_init(cls): + logging.info('Initialize proxy statistics') + # init proxy counter in the class.stat dictionary + cls.stat['proxy'] = {} + for key in cls.__info_defs: + name = cls.__info_defs[key]['name'] + if name[0]=='proxy': + cls.stat['proxy'][name[1]] = 0 + + # add values from the environment to the device definition table + prxy = cls.__info_devs['proxy'] + prxy['sw'] = cls.version + prxy['mdl'] = cls.app_name + + def __init__(self): self.db = {} - self.app_name = os.getenv('SERVICE_NAME', 'proxy') - self.version = os.getenv('VERSION', 'unknown') self.tracer = logging.getLogger('data') - prxy = self.__info_devs['proxy'] - prxy['sw'] = self.version - prxy['mdl'] = self.app_name __info_devs={ 'proxy': {'singleton': True, 'name':'Proxy', 'mf':'Stefan Allius'}, @@ -236,24 +250,15 @@ class Infos: yield json.dumps (attr), component, node_id, attr['uniq_id'] - - def __init_counter (self, counter:str) -> dict: - '''init proxy statistic counter, when its missing''' - if not 'proxy' in self.stat: - self.stat['proxy'] = {} - dict = self.stat['proxy'] - if not counter in dict: - dict[counter] = 0 - return dict def inc_counter (self, counter:str) -> None: '''inc proxy statistic counter''' - dict = self.__init_counter (counter) + dict = self.stat['proxy'] dict[counter] += 1 def dec_counter (self, counter:str) -> None: '''dec proxy statistic counter''' - dict = self.__init_counter (counter) + dict = self.stat['proxy'] dict[counter] -= 1 diff --git a/app/src/inverter.py b/app/src/inverter.py index f9f70f2..183ec9c 100644 --- a/app/src/inverter.py +++ b/app/src/inverter.py @@ -35,7 +35,7 @@ class Inverter(AsyncStream): logging.debug ("disconnect client connection") self.remoteStream.disc() - # await self.async_publ_mqtt() + await self.__async_publ_mqtt_packet('proxy') async def client_loop(self, addr): '''Loop for receiving messages from the TSUN cloud (client-side)''' @@ -73,8 +73,6 @@ class Inverter(AsyncStream): async def async_publ_mqtt(self) -> None: '''puplish data to MQTT broker''' - db = self.db.db - stat = self.db.stat # check if new inverter or collector infos are available or when the home assistant has changed the status back to online if (('inverter' in self.new_data and self.new_data['inverter']) or ('collector' in self.new_data and self.new_data['collector']) or @@ -83,18 +81,23 @@ class Inverter(AsyncStream): self.ha_restarts = self.mqtt.ha_restarts for key in self.new_data: - if self.new_data[key]: - if key in db: - data_json = json.dumps(db[key]) - node_id = self.node_id - elif key in stat: - data_json = json.dumps(stat[key]) - node_id = self.proxy_node_id - else: - continue - logger_mqtt.debug(f'{key}: {data_json}') - await self.mqtt.publish(f"{self.entity_prfx}{node_id}{key}", data_json) - self.new_data[key] = False + await self.__async_publ_mqtt_packet(key) + + async def __async_publ_mqtt_packet(self, key): + db = self.db.db + stat = self.db.stat + if self.new_data[key]: + if key in db: + data_json = json.dumps(db[key]) + node_id = self.node_id + elif key in stat: + data_json = json.dumps(stat[key]) + node_id = self.proxy_node_id + else: + return + logger_mqtt.debug(f'{key}: {data_json}') + await self.mqtt.publish(f"{self.entity_prfx}{node_id}{key}", data_json) + self.new_data[key] = False async def __register_home_assistant(self) -> None: '''register all our topics at home assistant''' diff --git a/app/src/server.py b/app/src/server.py index ddf9e95..3a74a44 100644 --- a/app/src/server.py +++ b/app/src/server.py @@ -4,6 +4,7 @@ from async_stream import AsyncStream from inverter import Inverter from config import Config from mqtt import Mqtt +from infos import Infos async def handle_client(reader, writer): @@ -69,6 +70,8 @@ if __name__ == "__main__": # call Mqtt singleton to establisch the connection to the mqtt broker mqtt = Mqtt() + # initialize the proxy statistics + Infos.static_init() # # Register some UNIX Signal handler for a gracefully server shutdown on Docker restart and stop diff --git a/app/tests/test_messages.py b/app/tests/test_messages.py index 65a98a4..05f5ef1 100644 --- a/app/tests/test_messages.py +++ b/app/tests/test_messages.py @@ -2,7 +2,10 @@ import pytest from app.src.messages import Message, Control from app.src.config import Config +from app.src.infos import Infos +# initialize the proxy statistics +Infos.static_init() class MemoryStream(Message): def __init__(self, msg, chunks = (0,)):