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/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