S allius/issue102 (#110)

* hotfix: don't send two MODBUS commands together

* fix unit tests

* remove read loop

* optional sleep between msg read and sending rsp

* wait after read 0.5s before sending a response

* add pending state

* fix state definitions

* determine the connection timeout by the conn state

* avoid sending MODBUS cmds in the inverter's reporting phase

* update changelog
This commit is contained in:
Stefan Allius
2024-06-23 15:06:43 +02:00
committed by GitHub
parent cc233dcb17
commit 6332976c4a
7 changed files with 53 additions and 16 deletions

View File

@@ -15,7 +15,9 @@ class AsyncStream():
_ids = count(0)
MAX_PROC_TIME = 2
'''maximum processing time for a received msg in sec'''
MAX_IDLE_TIME = 400
MAX_START_TIME = 400
'''maximum time without a received msg in sec'''
MAX_IDLE_TIME = 90
'''maximum time without a received msg in sec'''
def __init__(self, reader: StreamReader, writer: StreamWriter,
@@ -30,6 +32,12 @@ class AsyncStream():
self.proc_start = None # start processing start timestamp
self.proc_max = 0
def __timeout(self):
if self.state == State.init:
self.MAX_START_TIME
else:
self.MAX_IDLE_TIME
async def server_loop(self, addr: str) -> None:
'''Loop for receiving messages from the inverter (server-side)'''
logger.info(f'[{self.node_id}:{self.conn_no}] '
@@ -85,7 +93,8 @@ class AsyncStream():
self.proc_max = proc
self.proc_start = None
await asyncio.wait_for(self.__async_read(), self.MAX_IDLE_TIME)
await asyncio.wait_for(self.__async_read(),
self.__timeout())
if self.unique_id:
await self.async_write()
@@ -169,7 +178,9 @@ class AsyncStream():
if data:
self.proc_start = time.time()
self._recv_buffer += data
self.read() # call read in parent class
wait = self.read() # call read in parent class
if wait > 0:
await asyncio.sleep(wait)
else:
raise RuntimeError("Peer closed.")