move StremPtr instances into Inverter class

This commit is contained in:
Stefan Allius
2024-09-29 15:31:14 +02:00
parent 0c824b4a2a
commit 5a0ef30ceb
13 changed files with 548 additions and 550 deletions

View File

@@ -19,24 +19,25 @@ class ModbusConn():
self.host = host
self.port = port
self.addr = (host, port)
self.stream = None
self.inverter = None
async def __aenter__(self) -> 'InverterG3P':
'''Establish a client connection to the TSUN cloud'''
connection = asyncio.open_connection(self.host, self.port)
reader, writer = await connection
self.stream = InverterG3P(reader, writer, self.addr,
client_mode=True)
logging.info(f'[{self.stream.node_id}:{self.stream.conn_no}] '
self.inverter = InverterG3P(reader, writer, self.addr,
client_mode=True)
stream = self.inverter.local.stream
logging.info(f'[{stream.node_id}:{stream.conn_no}] '
f'Connected to {self.addr}')
Infos.inc_counter('Inverter_Cnt')
await self.stream._ifc.publish_outstanding_mqtt()
return self.stream
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.stream._ifc.publish_outstanding_mqtt()
self.stream.close()
await self.inverter.local._ifc.publish_outstanding_mqtt()
self.inverter.close()
class ModbusTcp():
@@ -61,7 +62,8 @@ class ModbusTcp():
'''Loop for receiving messages from the TSUN cloud (client-side)'''
while True:
try:
async with ModbusConn(host, port) as stream:
async with ModbusConn(host, port) as inverter:
stream = inverter.local.stream
await stream.send_start_cmd(snr, host)
await stream._ifc.loop()
logger.info(f'[{stream.node_id}:{stream.conn_no}] '