From 9852f44dfa9094004b9e6395140f7132abd5feae Mon Sep 17 00:00:00 2001 From: Stefan Allius Date: Sat, 5 Oct 2024 21:11:42 +0200 Subject: [PATCH] use ProtocolIfc class --- app/proxy.svg | 60 +++++++------- app/proxy.yuml | 6 +- app/proxy_2.svg | 136 ++++++++++++++++---------------- app/src/iter_registry.py | 8 -- app/src/messages.py | 7 +- app/src/protocol_ifc.py | 28 +------ app/tests/test_inverter_base.py | 17 +--- 7 files changed, 108 insertions(+), 154 deletions(-) diff --git a/app/proxy.svg b/app/proxy.svg index a8e328d..649fe8e 100644 --- a/app/proxy.svg +++ b/app/proxy.svg @@ -119,19 +119,19 @@ A6 - -StreamPtr - - -stream:MessageProt -ifc:AsyncIfc + +StreamPtr + + +stream:ProtocolIfc +ifc:AsyncIfc A5->A6 - - -2 + + +2 @@ -160,44 +160,44 @@ A11 - -<<AsyncIfc>> + +<<AsyncIfc>> A6->A11 - - -1 + + +1 A12 - -<<MessageProt>> + +<<ProtocolIfc>> A6->A12 - - -1 + + +1 A8 - - - -Creates an GEN3 -inverter instance -with -prot_class:Talent + + + +Creates an GEN3 +inverter instance +with +prot_class:Talent A7->A8 - + @@ -218,9 +218,9 @@ A12->A11 - - -use + + +use diff --git a/app/proxy.yuml b/app/proxy.yuml index 9d007cb..21c7d11 100644 --- a/app/proxy.yuml +++ b/app/proxy.yuml @@ -11,7 +11,7 @@ [<>||healthy()->bool;disc(shutdown_started=False);create_remote();] [<>]^-.-[<>] [InverterBase|_registry;__ha_restarts;;addr;config_id:str;prot_class:MessageProt;remote:StreamPtr;local:StreamPtr;|healthy()->bool;disc(shutdown_started=False);create_remote();async_publ_mqtt()] -[StreamPtr||stream:MessageProt;ifc:AsyncIfc] +[StreamPtr||stream:ProtocolIfc;ifc:AsyncIfc] [<>]^-.-[InverterBase] [InverterG3]-[note: Creates an GEN3 inverter instance with prot_class:Talent{bg:cornsilk}] [InverterG3P]-[note: Creates an GEN3PLUS inverter instance with prot_class:SolarmanV5{bg:cornsilk}] @@ -24,11 +24,11 @@ [<>] -[StreamPtr]-1>[<>] +[StreamPtr]-1>[<>] [StreamPtr]-1>[<>] -[<>]use-.->[<>] +[<>]use-.->[<>] [ModbusConn|host;port;addr;stream:InverterG3P;|]has-1>[InverterG3P] diff --git a/app/proxy_2.svg b/app/proxy_2.svg index 2aab16e..35c5479 100644 --- a/app/proxy_2.svg +++ b/app/proxy_2.svg @@ -4,11 +4,11 @@ - + G - + A0 @@ -47,81 +47,81 @@ A2 - -InverterG3 - -addr -remote:StreamPtr -local:StreamPtr - -create_remote() -close() + +InverterG3 + +addr +remote:StreamPtr +local:StreamPtr + +create_remote() +close() A7 - -AsyncStreamServer - -create_remote - -<async>server_loop() -<async>_async_forward() -<async>publish_outstanding_mqtt() -close() + +AsyncStreamServer + +create_remote + +<async>server_loop() +<async>_async_forward() +<async>publish_outstanding_mqtt() +close() A2->A7 - - - -local + + + +local A8 - -AsyncStreamClient - - -<async>client_loop() -<async>_async_forward()) + +AsyncStreamClient + + +<async>client_loop() +<async>_async_forward()) A2->A8 - - -remote + + +remote A3 - -InverterG3P - -addr -remote:StreamPtr -local:StreamPtr - -create_remote() -close() + +InverterG3P + +addr +remote:StreamPtr +local:StreamPtr + +create_remote() +close() A3->A7 - - - -local + + + +local A3->A8 - - -remote + + +remote @@ -199,14 +199,14 @@ A6->A7 - - + + A6->A8 - - + + @@ -237,17 +237,17 @@ A9->A2 - - -remote + + +remote A9->A2 - - - -local + + + +local @@ -295,17 +295,17 @@ A10->A3 - - -remote + + +remote A10->A3 - - - -local + + + +local diff --git a/app/src/iter_registry.py b/app/src/iter_registry.py index 34ebb25..ea0cd73 100644 --- a/app/src/iter_registry.py +++ b/app/src/iter_registry.py @@ -1,14 +1,6 @@ from abc import ABCMeta -class IterRegistry(type): - def __iter__(cls): - for ref in cls._registry: - obj = ref() - if obj is not None: - yield obj - - class AbstractIterMeta(ABCMeta): def __iter__(cls): for ref in cls._registry: diff --git a/app/src/messages.py b/app/src/messages.py index c68b125..bbff315 100644 --- a/app/src/messages.py +++ b/app/src/messages.py @@ -5,11 +5,11 @@ from enum import Enum if __name__ == "app.src.messages": - from app.src.iter_registry import IterRegistry + from app.src.protocol_ifc import ProtocolIfc from app.src.infos import Infos, Register from app.src.modbus import Modbus else: # pragma: no cover - from iter_registry import IterRegistry + from protocol_ifc import ProtocolIfc from infos import Infos, Register from modbus import Modbus @@ -82,8 +82,7 @@ class State(Enum): '''connection closed''' -class Message(metaclass=IterRegistry): - _registry = [] +class Message(ProtocolIfc): MAX_START_TIME = 400 '''maximum time without a received msg in sec''' MAX_INV_IDLE_TIME = 120 diff --git a/app/src/protocol_ifc.py b/app/src/protocol_ifc.py index 90ee8f1..46795e4 100644 --- a/app/src/protocol_ifc.py +++ b/app/src/protocol_ifc.py @@ -1,5 +1,4 @@ from abc import abstractmethod -import weakref if __name__ == "app.src.protocol_ifc": from app.src.iter_registry import AbstractIterMeta @@ -10,34 +9,13 @@ else: # pragma: no cover class ProtocolIfc(metaclass=AbstractIterMeta): - - @abstractmethod - def __init__(self, addr, ifc: "AsyncIfc", server_side: bool, - client_mode: bool = False, id_str=b''): - pass # pragma: no cover - - @abstractmethod - def close(self): - pass # pragma: no cover - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc, tb): - self.close() - - -class Test(): - def test_method(self): - return self - - -class ProtocolIfcImpl(ProtocolIfc, Test): _registry = [] + @abstractmethod def __init__(self, addr, ifc: "AsyncIfc", server_side: bool, client_mode: bool = False, id_str=b''): - self._registry.append(weakref.ref(self)) + pass # pragma: no cover + @abstractmethod def close(self): pass # pragma: no cover diff --git a/app/tests/test_inverter_base.py b/app/tests/test_inverter_base.py index f944d43..4e3ad92 100644 --- a/app/tests/test_inverter_base.py +++ b/app/tests/test_inverter_base.py @@ -10,8 +10,7 @@ from app.src.config import Config from app.src.gen3.talent import Talent from app.src.inverter_base import InverterBase from app.src.singleton import Singleton -from app.src.protocol_ifc import ProtocolIfcImpl -from app.src.async_stream import AsyncStream, AsyncIfcImpl, AsyncStreamClient +from app.src.async_stream import AsyncStream, AsyncStreamClient from app.tests.test_modbus_tcp import patch_mqtt_err, patch_mqtt_except, test_port, test_hostname @@ -114,20 +113,6 @@ def patch_unhealthy_remote(): with patch.object(AsyncStreamClient, 'healthy', new_healthy) as conn: yield conn -def test_protocol_iter(): - ProtocolIfcImpl._registry.clear() - cnt = 0 - ifc = AsyncIfcImpl() - prot = ProtocolIfcImpl(('test.intern', 123), ifc, True) - for p in ProtocolIfcImpl: - assert p == prot - cnt += 1 - del p - del prot - assert cnt == 1 - for p in ProtocolIfcImpl: - assert False - def test_inverter_iter(): InverterBase._registry.clear() cnt = 0