resolution of connection classes

- remove ConnectionG3Client
- remove ConnectionG3Server
- remove ConnectionG3PClient
- remove ConnectionG3PServer
This commit is contained in:
Stefan Allius
2024-09-29 20:08:04 +02:00
parent 5a0ef30ceb
commit 41aeac4168
19 changed files with 679 additions and 774 deletions

File diff suppressed because it is too large Load Diff

Before

Width:  |  Height:  |  Size: 50 KiB

After

Width:  |  Height:  |  Size: 47 KiB

View File

@@ -23,35 +23,27 @@
[AsyncStream]<-[AsyncStreamClient]
[ConnectionG3|remote.stream:ConnectionG3|healthy()]
[ConnectionG3Client|_ifc:AsyncStreamClient;conn_no;addr|close()]
[ConnectionG3Server|_ifc:AsyncStreamServer;conn_no;addr|;close()]
[ConnectionG3]^[ConnectionG3Client]
[ConnectionG3]^[ConnectionG3Server]
[ConnectionG3Client]-[InverterG3]
[ConnectionG3Client]++-1>[AsyncStreamClient]
[ConnectionG3Server]-[InverterG3]
[ConnectionG3Server]++-1>[AsyncStreamServer]
[ConnectionG3||]
[ConnectionG3]<remote-[InverterG3]
[InverterG3]-remote>[AsyncStreamClient]
[ConnectionG3]<-local++[InverterG3]
[InverterG3]++local->[AsyncStreamServer]
[ConnectionG3P|remote.stream:ConnectionG3P|healthy();close()]
[ConnectionG3PClient|_ifc:AsyncStreamClient;conn_no;addr|close()]
[ConnectionG3PServer|_ifc:AsyncStreamServer;conn_no;addr|;close()]
[ConnectionG3P]^[ConnectionG3PClient]
[ConnectionG3P]^[ConnectionG3PServer]
[ConnectionG3PClient]-[InverterG3P]
[ConnectionG3PClient]++-1>[AsyncStreamClient]
[ConnectionG3PServer]-[InverterG3P]
[ConnectionG3PServer]++-1>[AsyncStreamServer]
[ConnectionG3P||]
[ConnectionG3P]<remote-[InverterG3P]
[InverterG3P]-remote>[AsyncStreamClient]
[ConnectionG3P]<-local++[InverterG3P]
[InverterG3P]++local->[AsyncStreamServer]
[Infos|stat;new_stat_data;info_dev|static_init();dev_value();inc_counter();dec_counter();ha_proxy_conf;ha_conf;ha_remove;update_db;set_db_def_value;get_db_value;ignore_this_device]
[Infos]^[InfosG3||ha_confs();parse()]
[Infos]^[InfosG3P||ha_confs();parse()]
[Talent|await_conn_resp_cnt;id_str;contact_name;contact_mail;db:InfosG3;mb:Modbus;switch|msg_contact_info();msg_ota_update();msg_get_time();msg_collector_data();msg_inverter_data();msg_unknown();;close()]
[Talent|ifc:AsyncIfc;conn_no;addr;;await_conn_resp_cnt;id_str;contact_name;contact_mail;db:InfosG3;mb:Modbus;switch|msg_contact_info();msg_ota_update();msg_get_time();msg_collector_data();msg_inverter_data();msg_unknown();;healthy();close()]
[Talent]^[ConnectionG3]
[Talent]use->[<<AsyncIfc>>]
[Talent]->[InfosG3]
[SolarmanV5|control;serial;snr;db:InfosG3P;mb:Modbus;switch|msg_unknown();;close()]
[SolarmanV5|ifc:AsyncIfc;conn_no;addr;;control;serial;snr;db:InfosG3P;mb:Modbus;switch|msg_unknown();;healthy();close()]
[SolarmanV5]^[ConnectionG3P]
[SolarmanV5]use->[<<AsyncIfc>>]
[SolarmanV5]->[InfosG3P]

View File

@@ -1,55 +1,16 @@
import logging
from asyncio import StreamReader, StreamWriter
if __name__ == "app.src.gen3.connection_g3":
from app.src.async_stream import AsyncStreamServer
from app.src.async_stream import AsyncStreamClient
from app.src.inverter import Inverter
from app.src.gen3.talent import Talent
else: # pragma: no cover
from async_stream import AsyncStreamServer
from async_stream import AsyncStreamClient
from inverter import Inverter
from gen3.talent import Talent
logger = logging.getLogger('conn')
class ConnectionG3(Talent):
def healthy(self) -> bool:
logger.debug('ConnectionG3 healthy()')
return self._ifc.healthy()
def __init__(self, addr, ifc, server_side, id_str=b'') -> None:
super().__init__(addr, server_side, ifc, id_str)
def close(self):
self._ifc.close()
Talent.close(self)
# logger.info(f'AsyncStream refs: {gc.get_referrers(self)}')
class ConnectionG3Server(ConnectionG3):
def __init__(self, inverter: "Inverter",
reader: StreamReader, writer: StreamWriter,
addr, id_str=b'') -> None:
server_side = True
self._ifc = AsyncStreamServer(reader, writer,
inverter.async_publ_mqtt,
inverter.async_create_remote,
inverter.remote)
self.conn_no = self._ifc.get_conn_no()
self.addr = addr
Talent.__init__(self, server_side, self._ifc, id_str)
class ConnectionG3Client(ConnectionG3):
def __init__(self, inverter: "Inverter",
reader: StreamReader, writer: StreamWriter,
addr, id_str=b'') -> None:
server_side = False
self._ifc = AsyncStreamClient(reader, writer,
inverter.remote)
self.conn_no = self._ifc.get_conn_no()
self.addr = addr
Talent.__init__(self, server_side, self._ifc, id_str)
super().close()

View File

@@ -2,32 +2,38 @@ import logging
from asyncio import StreamReader, StreamWriter
if __name__ == "app.src.gen3.inverter_g3":
from app.src.inverter import Inverter
from app.src.inverter_base import InverterBase
from app.src.async_stream import StreamPtr
from app.src.gen3.connection_g3 import ConnectionG3Server
from app.src.gen3.connection_g3 import ConnectionG3Client
from app.src.async_stream import AsyncStreamServer
from app.src.gen3.connection_g3 import ConnectionG3
else: # pragma: no cover
from inverter import Inverter
from inverter_base import InverterBase
from async_stream import StreamPtr
from gen3.connection_g3 import ConnectionG3Server
from gen3.connection_g3 import ConnectionG3Client
from async_stream import AsyncStreamServer
from gen3.connection_g3 import ConnectionG3
logger_mqtt = logging.getLogger('mqtt')
class InverterG3(Inverter):
class InverterG3(InverterBase):
def __init__(self, reader: StreamReader, writer: StreamWriter, addr):
super().__init__()
self.addr = addr
self.remote = StreamPtr(None)
ifc = AsyncStreamServer(reader, writer,
self.async_publ_mqtt,
self.async_create_remote,
self.remote)
self.remote = StreamPtr(None)
self.local = StreamPtr(
ConnectionG3Server(self, reader, writer, addr)
ConnectionG3(addr, ifc, True)
)
async def async_create_remote(self) -> None:
await Inverter.async_create_remote(
self, 'tsun', ConnectionG3Client)
await InverterBase.async_create_remote(
self, 'tsun', ConnectionG3)
def close(self) -> None:
logging.debug(f'InverterG3.close() {self.addr}')

View File

@@ -46,13 +46,15 @@ class Talent(Message):
MB_REGULAR_TIMEOUT = 60
TXT_UNKNOWN_CTRL = 'Unknown Ctrl'
def __init__(self, server_side: bool, ifc: "AsyncIfc", id_str=b''):
def __init__(self, addr, server_side: bool, ifc: "AsyncIfc", id_str=b''):
super().__init__(server_side, self.send_modbus_cb, mb_timeout=15)
ifc.rx_set_cb(self.read)
ifc.prot_set_timeout_cb(self._timeout)
ifc.prot_set_init_new_client_conn_cb(self._init_new_client_conn)
ifc.prot_set_update_header_cb(self._update_header)
self.addr = addr
self.ifc = ifc
self.conn_no = ifc.get_conn_no()
self.await_conn_resp_cnt = 0
self.id_str = id_str
self.contact_name = b''
@@ -93,6 +95,10 @@ class Talent(Message):
'''
Our puplic methods
'''
def healthy(self) -> bool:
logger.debug('Talent healthy()')
return self.ifc.healthy()
def close(self) -> None:
logging.debug('Talent.close()')
if self.server_side:
@@ -110,6 +116,7 @@ class Talent(Message):
self.log_lvl.clear()
self.state = State.closed
self.mb_timer.close()
self.ifc.close()
self.ifc.rx_set_cb(None)
self.ifc.prot_set_timeout_cb(None)
self.ifc.prot_set_init_new_client_conn_cb(None)

View File

@@ -1,56 +1,18 @@
import logging
from asyncio import StreamReader, StreamWriter
if __name__ == "app.src.gen3plus.connection_g3p":
from app.src.async_stream import AsyncStreamServer
from app.src.async_stream import AsyncStreamClient
from app.src.inverter import Inverter
from app.src.gen3plus.solarman_v5 import SolarmanV5
else: # pragma: no cover
from async_stream import AsyncStreamServer
from async_stream import AsyncStreamClient
from inverter import Inverter
from gen3plus.solarman_v5 import SolarmanV5
logger = logging.getLogger('conn')
class ConnectionG3P(SolarmanV5):
def healthy(self) -> bool:
logger.debug('ConnectionG3P healthy()')
return self._ifc.healthy()
def __init__(self, addr, ifc, server_side,
client_mode: bool = False) -> None:
super().__init__(addr, server_side, client_mode, ifc)
def close(self):
self._ifc.close()
SolarmanV5.close(self)
super().close()
# logger.info(f'AsyncStream refs: {gc.get_referrers(self)}')
class ConnectionG3PServer(ConnectionG3P):
def __init__(self, inverter: "Inverter",
reader: StreamReader, writer: StreamWriter,
addr, client_mode: bool) -> None:
server_side = True
self._ifc = AsyncStreamServer(reader, writer,
inverter.async_publ_mqtt,
inverter.async_create_remote,
inverter.remote)
self.conn_no = self._ifc.get_conn_no()
self.addr = addr
SolarmanV5.__init__(self, server_side, client_mode, self._ifc)
class ConnectionG3PClient(ConnectionG3P):
def __init__(self, inverter: "Inverter",
reader: StreamReader, writer: StreamWriter,
addr) -> None:
server_side = False
client_mode = False
self._ifc = AsyncStreamClient(reader, writer, inverter.remote)
self.conn_no = self._ifc.get_conn_no()
self.addr = addr
SolarmanV5.__init__(self, server_side, client_mode, self._ifc)

View File

@@ -2,33 +2,38 @@ import logging
from asyncio import StreamReader, StreamWriter
if __name__ == "app.src.gen3plus.inverter_g3p":
from app.src.inverter import Inverter
from app.src.inverter_base import InverterBase
from app.src.async_stream import StreamPtr
from app.src.gen3plus.connection_g3p import ConnectionG3PServer
from app.src.gen3plus.connection_g3p import ConnectionG3PClient
from app.src.async_stream import AsyncStreamServer
from app.src.gen3plus.connection_g3p import ConnectionG3P
else: # pragma: no cover
from inverter import Inverter
from inverter_base import InverterBase
from async_stream import StreamPtr
from gen3plus.connection_g3p import ConnectionG3PServer
from gen3plus.connection_g3p import ConnectionG3PClient
from async_stream import AsyncStreamServer
from gen3plus.connection_g3p import ConnectionG3P
logger_mqtt = logging.getLogger('mqtt')
class InverterG3P(Inverter):
class InverterG3P(InverterBase):
def __init__(self, reader: StreamReader, writer: StreamWriter, addr,
client_mode: bool = False):
super().__init__()
self.addr = addr
self.remote = StreamPtr(None)
ifc = AsyncStreamServer(reader, writer,
self.async_publ_mqtt,
self.async_create_remote,
self.remote)
self.local = StreamPtr(
ConnectionG3PServer(self, reader, writer, addr, client_mode)
ConnectionG3P(addr, ifc, True, client_mode)
)
async def async_create_remote(self) -> None:
await Inverter.async_create_remote(
self, 'solarman', ConnectionG3PClient)
await InverterBase.async_create_remote(
self, 'solarman', ConnectionG3P)
def close(self) -> None:
logging.debug(f'InverterG3P.close() {self.addr}')

View File

@@ -62,14 +62,17 @@ class SolarmanV5(Message):
HDR_FMT = '<BLLL'
'''format string for packing of the header'''
def __init__(self, server_side: bool, client_mode: bool, ifc: "AsyncIfc"):
def __init__(self, addr, server_side: bool, client_mode: bool,
ifc: "AsyncIfc"):
super().__init__(server_side, self.send_modbus_cb, mb_timeout=8)
ifc.rx_set_cb(self.read)
ifc.prot_set_timeout_cb(self._timeout)
ifc.prot_set_init_new_client_conn_cb(self._init_new_client_conn)
ifc.prot_set_update_header_cb(self._update_header)
self.addr = addr
self.ifc = ifc
self.conn_no = ifc.get_conn_no()
self.header_len = 11 # overwrite construcor in class Message
self.control = 0
self.seq = Sequence(server_side)
@@ -150,6 +153,10 @@ class SolarmanV5(Message):
'''
Our puplic methods
'''
def healthy(self) -> bool:
logger.debug('SolarmanV5 healthy()')
return self.ifc.healthy()
def close(self) -> None:
logging.debug('Solarman.close()')
if self.server_side:
@@ -167,6 +174,7 @@ class SolarmanV5(Message):
self.log_lvl.clear()
self.state = State.closed
self.mb_timer.close()
self.ifc.close()
self.ifc.rx_set_cb(None)
self.ifc.prot_set_timeout_cb(None)
self.ifc.prot_set_init_new_client_conn_cb(None)

View File

@@ -1,8 +1,6 @@
import asyncio
import logging
import traceback
import json
from aiomqtt import MqttCodeError
if __name__ == "app.src.inverter":
from app.src.config import Config
@@ -106,88 +104,3 @@ class Inverter():
logging.info('Close MQTT Task')
loop.run_until_complete(cls.mqtt.close())
cls.mqtt = None
def __init__(self):
self.__ha_restarts = -1
async def async_create_remote(self, inv_prot: str, conn_class) -> None:
'''Establish a client connection to the TSUN cloud'''
tsun = Config.get(inv_prot)
host = tsun['host']
port = tsun['port']
addr = (host, port)
stream = self.local.stream
try:
logging.info(f'[{stream.node_id}] Connect to {addr}')
connect = asyncio.open_connection(host, port)
reader, writer = await connect
if hasattr(stream, 'id_str'):
self.remote.stream = conn_class(
self, reader, writer, addr, stream.id_str)
else:
self.remote.stream = conn_class(
self, reader, writer, addr)
logging.info(f'[{self.remote.stream.node_id}:'
f'{self.remote.stream.conn_no}] '
f'Connected to {addr}')
asyncio.create_task(self.remote.ifc.client_loop(addr))
except (ConnectionRefusedError, TimeoutError) as error:
logging.info(f'{error}')
except Exception:
Infos.inc_counter('SW_Exception')
logging.error(
f"Inverter: Exception for {addr}:\n"
f"{traceback.format_exc()}")
async def async_publ_mqtt(self) -> None:
'''publish data to MQTT broker'''
stream = self.local.stream
if not stream.unique_id:
return
# check if new inverter or collector infos are available or when the
# home assistant has changed the status back to online
try:
if (('inverter' in stream.new_data and stream.new_data['inverter'])
or ('collector' in stream.new_data and
stream.new_data['collector'])
or self.mqtt.ha_restarts != self.__ha_restarts):
await self._register_proxy_stat_home_assistant()
await self.__register_home_assistant(stream)
self.__ha_restarts = self.mqtt.ha_restarts
for key in stream.new_data:
await self.__async_publ_mqtt_packet(stream, key)
for key in Infos.new_stat_data:
await Inverter._async_publ_mqtt_proxy_stat(key)
except MqttCodeError as error:
logging.error(f'Mqtt except: {error}')
except Exception:
Infos.inc_counter('SW_Exception')
logging.error(
f"Inverter: Exception:\n"
f"{traceback.format_exc()}")
async def __async_publ_mqtt_packet(self, stream, key):
db = stream.db.db
if key in db and stream.new_data[key]:
data_json = json.dumps(db[key])
node_id = stream.node_id
logger_mqtt.debug(f'{key}: {data_json}')
await self.mqtt.publish(f'{self.entity_prfx}{node_id}{key}', data_json) # noqa: E501
stream.new_data[key] = False
async def __register_home_assistant(self, stream) -> None:
'''register all our topics at home assistant'''
for data_json, component, node_id, id in stream.db.ha_confs(
self.entity_prfx, stream.node_id, stream.unique_id,
stream.sug_area):
logger_mqtt.debug(f"MQTT Register: cmp:'{component}'"
f" node_id:'{node_id}' {data_json}")
await self.mqtt.publish(f"{self.discovery_prfx}{component}"
f"/{node_id}{id}/config", data_json)
stream.db.reg_clr_at_midnight(f'{self.entity_prfx}{stream.node_id}')

108
app/src/inverter_base.py Normal file
View File

@@ -0,0 +1,108 @@
import asyncio
import logging
import traceback
import json
from aiomqtt import MqttCodeError
if __name__ == "app.src.inverter_base":
from app.src.inverter import Inverter
from app.src.async_stream import AsyncStreamClient
from app.src.config import Config
from app.src.infos import Infos
else: # pragma: no cover
from inverter import Inverter
from async_stream import AsyncStreamClient
from config import Config
from infos import Infos
logger_mqtt = logging.getLogger('mqtt')
class InverterBase(Inverter):
def __init__(self):
self.__ha_restarts = -1
async def async_create_remote(self, inv_prot: str, conn_class) -> None:
'''Establish a client connection to the TSUN cloud'''
tsun = Config.get(inv_prot)
host = tsun['host']
port = tsun['port']
addr = (host, port)
stream = self.local.stream
try:
logging.info(f'[{stream.node_id}] Connect to {addr}')
connect = asyncio.open_connection(host, port)
reader, writer = await connect
ifc = AsyncStreamClient(reader, writer,
self.remote)
if hasattr(stream, 'id_str'):
self.remote.stream = conn_class(
addr, ifc, False, stream.id_str)
else:
self.remote.stream = conn_class(
addr, ifc, False)
logging.info(f'[{self.remote.stream.node_id}:'
f'{self.remote.stream.conn_no}] '
f'Connected to {addr}')
asyncio.create_task(self.remote.ifc.client_loop(addr))
except (ConnectionRefusedError, TimeoutError) as error:
logging.info(f'{error}')
except Exception:
Infos.inc_counter('SW_Exception')
logging.error(
f"Inverter: Exception for {addr}:\n"
f"{traceback.format_exc()}")
async def async_publ_mqtt(self) -> None:
'''publish data to MQTT broker'''
stream = self.local.stream
if not stream.unique_id:
return
# check if new inverter or collector infos are available or when the
# home assistant has changed the status back to online
try:
if (('inverter' in stream.new_data and stream.new_data['inverter'])
or ('collector' in stream.new_data and
stream.new_data['collector'])
or self.mqtt.ha_restarts != self.__ha_restarts):
await self._register_proxy_stat_home_assistant()
await self.__register_home_assistant(stream)
self.__ha_restarts = self.mqtt.ha_restarts
for key in stream.new_data:
await self.__async_publ_mqtt_packet(stream, key)
for key in Infos.new_stat_data:
await Inverter._async_publ_mqtt_proxy_stat(key)
except MqttCodeError as error:
logging.error(f'Mqtt except: {error}')
except Exception:
Infos.inc_counter('SW_Exception')
logging.error(
f"Inverter: Exception:\n"
f"{traceback.format_exc()}")
async def __async_publ_mqtt_packet(self, stream, key):
db = stream.db.db
if key in db and stream.new_data[key]:
data_json = json.dumps(db[key])
node_id = stream.node_id
logger_mqtt.debug(f'{key}: {data_json}')
await self.mqtt.publish(f'{self.entity_prfx}{node_id}{key}', data_json) # noqa: E501
stream.new_data[key] = False
async def __register_home_assistant(self, stream) -> None:
'''register all our topics at home assistant'''
for data_json, component, node_id, id in stream.db.ha_confs(
self.entity_prfx, stream.node_id, stream.unique_id,
stream.sug_area):
logger_mqtt.debug(f"MQTT Register: cmp:'{component}'"
f" node_id:'{node_id}' {data_json}")
await self.mqtt.publish(f"{self.discovery_prfx}{component}"
f"/{node_id}{id}/config", data_json)
stream.db.reg_clr_at_midnight(f'{self.entity_prfx}{stream.node_id}')

View File

@@ -31,12 +31,12 @@ class ModbusConn():
logging.info(f'[{stream.node_id}:{stream.conn_no}] '
f'Connected to {self.addr}')
Infos.inc_counter('Inverter_Cnt')
await self.inverter.local._ifc.publish_outstanding_mqtt()
await self.inverter.local.ifc.publish_outstanding_mqtt()
return self.inverter
async def __aexit__(self, exc_type, exc, tb):
Infos.dec_counter('Inverter_Cnt')
await self.inverter.local._ifc.publish_outstanding_mqtt()
await self.inverter.local.ifc.publish_outstanding_mqtt()
self.inverter.close()
@@ -65,7 +65,7 @@ class ModbusTcp():
async with ModbusConn(host, port) as inverter:
stream = inverter.local.stream
await stream.send_start_cmd(snr, host)
await stream._ifc.loop()
await stream.ifc.loop()
logger.info(f'[{stream.node_id}:{stream.conn_no}] '
f'Connection closed - Shutdown: '
f'{stream.shutdown_started}')

View File

@@ -5,8 +5,8 @@ import asyncio
from itertools import count
from mock import patch
from app.src.async_stream import StreamPtr
from app.src.async_stream import AsyncStream, AsyncIfcImpl
from app.src.gen3.connection_g3 import ConnectionG3Server
from app.src.async_stream import AsyncStream, AsyncStreamServer, AsyncIfcImpl
from app.src.gen3.connection_g3 import ConnectionG3
from app.src.gen3.talent import Talent
@@ -75,24 +75,26 @@ class FakeWriter():
def test_method_calls(patch_talent_init, patch_healthy, patch_async_close, patch_talent_close):
def test_method_calls(patch_healthy, patch_async_close):
AsyncIfcImpl._ids = count(5)
spy2 = patch_talent_init
spy3 = patch_healthy
spy4 = patch_async_close
spy5 = patch_talent_close
reader = FakeReader()
writer = FakeWriter()
id_str = "id_string"
addr = ('proxy.local', 10000)
conn = ConnectionG3Server(FakeInverter(), reader, writer, addr,
id_str=id_str)
assert 5 == conn._ifc.get_conn_no()
spy2.assert_called_once_with(conn, True, conn._ifc, id_str)
inv = FakeInverter()
ifc = AsyncStreamServer(reader, writer,
inv.async_publ_mqtt,
inv.async_create_remote,
inv.remote)
conn = ConnectionG3(addr, ifc, server_side=True, id_str=id_str)
assert 5 == conn.conn_no
assert 5 == conn.ifc.get_conn_no()
conn.healthy()
spy3.assert_called_once()
conn.close()
spy4.assert_called_once()
spy5.assert_called_once()

View File

@@ -5,8 +5,9 @@ import asyncio
from itertools import count
from mock import patch
from app.src.singleton import Singleton
from app.src.async_stream import AsyncStream, AsyncIfcImpl, StreamPtr
from app.src.gen3plus.connection_g3p import ConnectionG3PServer
from app.src.async_stream import StreamPtr
from app.src.async_stream import AsyncStream, AsyncStreamServer, AsyncIfcImpl
from app.src.gen3plus.connection_g3p import ConnectionG3P
from app.src.gen3plus.solarman_v5 import SolarmanV5
@@ -80,24 +81,25 @@ class FakeWriter():
def test_method_calls(patch_solarman_init, patch_healthy, patch_async_close, patch_solarman_close):
def test_method_calls(patch_healthy, patch_async_close):
AsyncIfcImpl._ids = count(5)
spy2 = patch_solarman_init
spy3 = patch_healthy
spy4 = patch_async_close
spy5 = patch_solarman_close
reader = FakeReader()
writer = FakeWriter()
addr = ('proxy.local', 10000)
conn = ConnectionG3PServer(FakeInverter(), reader, writer, addr,
client_mode=False)
assert 5 == conn._ifc.get_conn_no()
spy2.assert_called_once_with(conn, True, False, conn._ifc)
inv = FakeInverter()
ifc = AsyncStreamServer(reader, writer,
inv.async_publ_mqtt,
inv.async_create_remote,
inv.remote)
conn = ConnectionG3P(addr, ifc, server_side=True, client_mode=False)
assert 5 == conn.conn_no
assert 5 == conn.ifc.get_conn_no()
conn.healthy()
spy3.assert_called_once()
conn.close()
spy4.assert_called_once()
spy5.assert_called_once()

View File

@@ -8,7 +8,7 @@ from app.src.infos import Infos
from app.src.config import Config
from app.src.inverter import Inverter
from app.src.singleton import Singleton
from app.src.gen3.connection_g3 import ConnectionG3Server
from app.src.gen3.connection_g3 import ConnectionG3
from app.src.gen3.inverter_g3 import InverterG3
from app.tests.test_modbus_tcp import patch_mqtt_err, patch_mqtt_except, test_port, test_hostname
@@ -44,12 +44,12 @@ def module_init():
@pytest.fixture
def patch_conn_init():
with patch.object(ConnectionG3Server, '__init__', return_value= None) as conn:
with patch.object(ConnectionG3, '__init__', return_value= None) as conn:
yield conn
@pytest.fixture
def patch_conn_close():
with patch.object(ConnectionG3Server, 'close') as conn:
with patch.object(ConnectionG3, 'close') as conn:
yield conn
class FakeReader():

View File

@@ -8,7 +8,7 @@ from app.src.infos import Infos
from app.src.config import Config
from app.src.inverter import Inverter
from app.src.singleton import Singleton
from app.src.gen3plus.connection_g3p import ConnectionG3PServer
from app.src.gen3plus.connection_g3p import ConnectionG3P
from app.src.gen3plus.inverter_g3p import InverterG3P
from app.tests.test_modbus_tcp import patch_mqtt_err, patch_mqtt_except, test_port, test_hostname
@@ -45,12 +45,12 @@ def module_init():
@pytest.fixture
def patch_conn_init():
with patch.object(ConnectionG3PServer, '__init__', return_value= None) as conn:
with patch.object(ConnectionG3P, '__init__', return_value= None) as conn:
yield conn
@pytest.fixture
def patch_conn_close():
with patch.object(ConnectionG3PServer, 'close') as conn:
with patch.object(ConnectionG3P, 'close') as conn:
yield conn
class FakeReader():

View File

@@ -154,8 +154,8 @@ async def test_modbus_conn(patch_open):
stream = inverter.local.stream
assert stream.node_id == 'G3P'
assert stream.addr == ('test.local', 1234)
assert type(stream._ifc._reader) is FakeReader
assert type(stream._ifc._writer) is FakeWriter
assert type(stream.ifc._reader) is FakeReader
assert type(stream.ifc._writer) is FakeWriter
assert Infos.stat['proxy']['Inverter_Cnt'] == 1
assert Infos.stat['proxy']['Inverter_Cnt'] == 0
@@ -206,7 +206,7 @@ async def test_modbus_cnf2(config_conn, patch_no_mqtt, patch_open):
test += 1
assert Infos.stat['proxy']['Inverter_Cnt'] == 1
m.shutdown_started = True
m._ifc._reader.on_recv.set()
m.ifc._reader.on_recv.set()
del m
assert 1 == test
@@ -266,14 +266,14 @@ async def test_mqtt_err(config_conn, patch_mqtt_err, patch_open):
test += 1
if test == 1:
m.shutdown_started = False
m._ifc._reader.on_recv.set()
m.ifc._reader.on_recv.set()
await asyncio.sleep(0.1)
assert m.state == State.closed
await asyncio.sleep(0.1)
await asyncio.sleep(0.1)
else:
m.shutdown_started = True
m._ifc._reader.on_recv.set()
m.ifc._reader.on_recv.set()
del m
await asyncio.sleep(0.01)
@@ -299,13 +299,13 @@ async def test_mqtt_except(config_conn, patch_mqtt_except, patch_open):
test += 1
if test == 1:
m.shutdown_started = False
m._ifc._reader.on_recv.set()
m.ifc._reader.on_recv.set()
await asyncio.sleep(0.1)
assert m.state == State.closed
await asyncio.sleep(0.1)
else:
m.shutdown_started = True
m._ifc._reader.on_recv.set()
m.ifc._reader.on_recv.set()
del m
await asyncio.sleep(0.01)

View File

@@ -45,7 +45,7 @@ def config_no_conn(test_port):
@pytest.fixture
def spy_at_cmd():
conn = SolarmanV5(server_side=True, client_mode= False, ifc=AsyncIfcImpl())
conn = SolarmanV5(('test.local', 1234), server_side=True, client_mode= False, ifc=AsyncIfcImpl())
conn.node_id = 'inv_2/'
with patch.object(conn, 'send_at_cmd', wraps=conn.send_at_cmd) as wrapped_conn:
yield wrapped_conn
@@ -53,7 +53,7 @@ def spy_at_cmd():
@pytest.fixture
def spy_modbus_cmd():
conn = SolarmanV5(server_side=True, client_mode= False, ifc=AsyncIfcImpl())
conn = SolarmanV5(('test.local', 1234), server_side=True, client_mode= False, ifc=AsyncIfcImpl())
conn.node_id = 'inv_1/'
with patch.object(conn, 'send_modbus_cmd', wraps=conn.send_modbus_cmd) as wrapped_conn:
yield wrapped_conn
@@ -61,7 +61,7 @@ def spy_modbus_cmd():
@pytest.fixture
def spy_modbus_cmd_client():
conn = SolarmanV5(server_side=False, client_mode= False, ifc=AsyncIfcImpl())
conn = SolarmanV5(('test.local', 1234), server_side=False, client_mode= False, ifc=AsyncIfcImpl())
conn.node_id = 'inv_1/'
with patch.object(conn, 'send_modbus_cmd', wraps=conn.send_modbus_cmd) as wrapped_conn:
yield wrapped_conn

View File

@@ -35,7 +35,7 @@ class Mqtt():
class MemoryStream(SolarmanV5):
def __init__(self, msg, chunks = (0,), server_side: bool = True):
_ifc = AsyncIfcImpl()
super().__init__(server_side, client_mode=False, ifc=_ifc)
super().__init__(('test.local', 1234), server_side, client_mode=False, ifc=_ifc)
if server_side:
self.mb.timeout = 0.4 # overwrite for faster testing
self.remote = StreamPtr(None)
@@ -1236,9 +1236,9 @@ def test_build_logger_modell(config_tsun_allow_all, device_ind_msg):
def test_msg_iterator():
Message._registry.clear()
m1 = SolarmanV5(server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m2 = SolarmanV5(server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m3 = SolarmanV5(server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m1 = SolarmanV5(('test1.local', 1234), server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m2 = SolarmanV5(('test2.local', 1234), server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m3 = SolarmanV5(('test3.local', 1234), server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m3.close()
del m3
test1 = 0
@@ -1256,7 +1256,7 @@ def test_msg_iterator():
assert test2 == 1
def test_proxy_counter():
m = SolarmanV5(server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m = SolarmanV5(('test.local', 1234), server_side=True, client_mode=False, ifc=AsyncIfcImpl())
assert m.new_data == {}
m.db.stat['proxy']['Unknown_Msg'] = 0
Infos.new_stat_data['proxy'] = False

View File

@@ -20,7 +20,7 @@ tracer = logging.getLogger('tracer')
class MemoryStream(Talent):
def __init__(self, msg, chunks = (0,), server_side: bool = True):
self.ifc = AsyncIfcImpl()
super().__init__(server_side, self.ifc)
super().__init__(('test.local', 1234), server_side, self.ifc)
if server_side:
self.mb.timeout = 0.4 # overwrite for faster testing
self.remote = StreamPtr(None)
@@ -1639,9 +1639,9 @@ def test_ctrl_byte():
def test_msg_iterator():
m1 = Talent(server_side=True, ifc=AsyncIfcImpl())
m2 = Talent(server_side=True, ifc=AsyncIfcImpl())
m3 = Talent(server_side=True, ifc=AsyncIfcImpl())
m1 = Talent(('test1.local', 1234), server_side=True, ifc=AsyncIfcImpl())
m2 = Talent(('test2.local', 1234), server_side=True, ifc=AsyncIfcImpl())
m3 = Talent(('test3.local', 1234), server_side=True, ifc=AsyncIfcImpl())
m3.close()
del m3
test1 = 0