add abstract inverter interface class

This commit is contained in:
Stefan Allius
2024-10-04 01:35:44 +02:00
parent 84034127e3
commit cd2f41a713
6 changed files with 212 additions and 19 deletions

View File

@@ -1,15 +1,12 @@
import asyncio
import weakref
import logging
import json
if __name__ == "app.src.inverter":
from app.src.iter_registry import IterRegistry
from app.src.config import Config
from app.src.mqtt import Mqtt
from app.src.infos import Infos
else: # pragma: no cover
from iter_registry import IterRegistry
from config import Config
from mqtt import Mqtt
from infos import Infos
@@ -17,7 +14,7 @@ else: # pragma: no cover
logger_mqtt = logging.getLogger('mqtt')
class Inverter(metaclass=IterRegistry):
class Inverter():
'''class Inverter is a baseclass
The class has some class method for managing common resources like a
@@ -40,8 +37,6 @@ class Inverter(metaclass=IterRegistry):
async_create_remote(): Establish a client connection to the TSUN cloud
async_publ_mqtt(): Publish data to MQTT broker
'''
_registry = []
@classmethod
def class_init(cls) -> None:
logging.debug('Inverter.class_init')
@@ -109,6 +104,3 @@ class Inverter(metaclass=IterRegistry):
logging.info('Close MQTT Task')
loop.run_until_complete(cls.mqtt.close())
cls.mqtt = None
def __init__(self):
self._registry.append(weakref.ref(self))

View File

@@ -1,3 +1,5 @@
from abc import abstractmethod
import weakref
import asyncio
import logging
import traceback
@@ -6,6 +8,7 @@ from aiomqtt import MqttCodeError
from asyncio import StreamReader, StreamWriter
if __name__ == "app.src.inverter_base":
from app.src.iter_registry import AbstractIterMeta
from app.src.inverter import Inverter
from app.src.async_stream import StreamPtr
from app.src.async_stream import AsyncStreamClient
@@ -13,6 +16,7 @@ if __name__ == "app.src.inverter_base":
from app.src.config import Config
from app.src.infos import Infos
else: # pragma: no cover
from iter_registry import AbstractIterMeta
from inverter import Inverter
from async_stream import StreamPtr
from async_stream import AsyncStreamClient
@@ -23,11 +27,43 @@ else: # pragma: no cover
logger_mqtt = logging.getLogger('mqtt')
class InverterBase(Inverter):
class InverterIfc(metaclass=AbstractIterMeta):
@abstractmethod
def __init__(self, reader: StreamReader, writer: StreamWriter,
config_id: str, prot_class,
client_mode: bool):
pass # pragma: no cover
@abstractmethod
def __enter__(self):
pass # pragma: no cover
@abstractmethod
def __exit__(self, exc_type, exc, tb):
pass # pragma: no cover
@abstractmethod
def healthy(self) -> bool:
pass # pragma: no cover
@abstractmethod
async def disc(self, shutdown_started=False) -> None:
pass # pragma: no cover
@abstractmethod
async def async_create_remote(self) -> None:
pass # pragma: no cover
class InverterBase(InverterIfc, Inverter):
_registry = []
def __init__(self, reader: StreamReader, writer: StreamWriter,
config_id: str, prot_class,
client_mode: bool = False):
super().__init__()
Inverter.__init__(self)
self._registry.append(weakref.ref(self))
self.addr = writer.get_extra_info('peername')
self.config_id = config_id
self.prot_class = prot_class

View File

@@ -1,3 +1,4 @@
from abc import ABCMeta
class IterRegistry(type):
@@ -6,3 +7,12 @@ class IterRegistry(type):
obj = ref()
if obj is not None:
yield obj
class AbstractIterMeta(ABCMeta):
def __iter__(cls):
for ref in cls._registry:
obj = ref()
print(f'obj: {obj}')
if obj is not None:
yield obj