improve logging output

This commit is contained in:
Stefan Allius
2024-06-03 19:52:37 +02:00
parent 8baa68e615
commit 8204cae2b1
7 changed files with 28 additions and 8 deletions

View File

@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
- improve logging: add protocol or node_id to connection logs
- improve logging: log ignored AT+ or MODBUS commands
- fix typo in docker-compose.yaml
- trace heartbeat and regular modbus pakets witl log level DEBUG - trace heartbeat and regular modbus pakets witl log level DEBUG
- GEN3PLUS: don't forward ack paket from tsun to the inverter - GEN3PLUS: don't forward ack paket from tsun to the inverter
- add allow and block filter for AT+ commands - add allow and block filter for AT+ commands

View File

@@ -17,11 +17,12 @@ class AsyncStream():
async def server_loop(self, addr): async def server_loop(self, addr):
'''Loop for receiving messages from the inverter (server-side)''' '''Loop for receiving messages from the inverter (server-side)'''
logging.info(f'Accept connection from {addr}') logging.info(f'[{self.node_id}] Accept connection from {addr}')
self.inc_counter('Inverter_Cnt') self.inc_counter('Inverter_Cnt')
await self.loop() await self.loop()
self.dec_counter('Inverter_Cnt') self.dec_counter('Inverter_Cnt')
logging.info(f'Server loop stopped for r{self.r_addr}') logging.info(f'[{self.node_id}] Server loop stopped for'
f' r{self.r_addr}')
# if the server connection closes, we also have to disconnect # if the server connection closes, we also have to disconnect
# the connection to te TSUN cloud # the connection to te TSUN cloud
@@ -36,7 +37,8 @@ class AsyncStream():
async def client_loop(self, addr): async def client_loop(self, addr):
'''Loop for receiving messages from the TSUN cloud (client-side)''' '''Loop for receiving messages from the TSUN cloud (client-side)'''
clientStream = await self.remoteStream.loop() clientStream = await self.remoteStream.loop()
logging.info(f'Client loop stopped for l{clientStream.l_addr}') logging.info(f'[{self.node_id}] Client loop stopped for'
f' l{clientStream.l_addr}')
# if the client connection closes, we don't touch the server # if the client connection closes, we don't touch the server
# connection. Instead we erase the client connection stream, # connection. Instead we erase the client connection stream,
@@ -66,14 +68,14 @@ class AsyncStream():
await self.async_publ_mqtt() await self.async_publ_mqtt()
except OSError as error: except OSError as error:
logger.error(f'{error} for l{self.l_addr} | ' logger.error(f'[{self.node_id}] {error} for l{self.l_addr} | '
f'r{self.r_addr}') f'r{self.r_addr}')
await self.disc() await self.disc()
self.close() self.close()
return self return self
except RuntimeError as error: except RuntimeError as error:
logger.warning(f"{error} for {self.l_addr}") logger.info(f"[{self.node_id}] {error} for {self.l_addr}")
await self.disc() await self.disc()
self.close() self.close()
return self return self

View File

@@ -56,7 +56,7 @@ class InverterG3(Inverter, ConnectionG3):
addr = (host, port) addr = (host, port)
try: try:
logging.info(f'Connected to {addr}') logging.info(f'[{self.node_id}] Connected to {addr}')
connect = asyncio.open_connection(host, port) connect = asyncio.open_connection(host, port)
reader, writer = await connect reader, writer = await connect
self.remoteStream = ConnectionG3(reader, writer, addr, self, self.remoteStream = ConnectionG3(reader, writer, addr, self,

View File

@@ -63,6 +63,8 @@ class Talent(Message):
0x04: logging.INFO, 0x04: logging.INFO,
} }
self.modbus_elms = 0 # for unit tests self.modbus_elms = 0 # for unit tests
self.node_id = 'G3' # will be overwritten in __set_serial_no
# self.forwarding = Config.get('tsun')['enabled']
''' '''
Our puplic methods Our puplic methods
@@ -139,6 +141,8 @@ class Talent(Message):
def send_modbus_cb(self, modbus_pdu: bytearray, log_lvl: int, state: str): def send_modbus_cb(self, modbus_pdu: bytearray, log_lvl: int, state: str):
if self.state != self.STATE_UP: if self.state != self.STATE_UP:
logger.warn(f'[{self.node_id}] ignore MODBUS cmd,'
' as the state is not UP')
return return
self.__build_header(0x70, 0x77) self.__build_header(0x70, 0x77)
@@ -154,6 +158,8 @@ class Talent(Message):
async def send_modbus_cmd(self, func, addr, val, log_lvl) -> None: async def send_modbus_cmd(self, func, addr, val, log_lvl) -> None:
if self.state != self.STATE_UP: if self.state != self.STATE_UP:
logger.warn(f'[{self.node_id}] ignore MODBUS cmd,'
' as the state is not UP')
return return
self.mb.build_msg(Modbus.INV_ADDR, func, addr, val, log_lvl) self.mb.build_msg(Modbus.INV_ADDR, func, addr, val, log_lvl)

View File

@@ -56,7 +56,7 @@ class InverterG3P(Inverter, ConnectionG3P):
addr = (host, port) addr = (host, port)
try: try:
logging.info(f'Connected to {addr}') logging.info(f'[{self.node_id}] Connected to {addr}')
connect = asyncio.open_connection(host, port) connect = asyncio.open_connection(host, port)
reader, writer = await connect reader, writer = await connect
self.remoteStream = ConnectionG3P(reader, writer, addr, self, self.remoteStream = ConnectionG3P(reader, writer, addr, self,

View File

@@ -122,6 +122,9 @@ class SolarmanV5(Message):
if 'at_acl' in g3p_cnf: # pragma: no cover if 'at_acl' in g3p_cnf: # pragma: no cover
self.at_acl = g3p_cnf['at_acl'] self.at_acl = g3p_cnf['at_acl']
self.node_id = 'G3P' # will be overwritten in __set_serial_no
# self.forwarding = Config.get('solarman')['enabled']
''' '''
Our puplic methods Our puplic methods
''' '''
@@ -336,6 +339,8 @@ class SolarmanV5(Message):
def send_modbus_cb(self, pdu: bytearray, log_lvl: int, state: str): def send_modbus_cb(self, pdu: bytearray, log_lvl: int, state: str):
if self.state != self.STATE_UP: if self.state != self.STATE_UP:
logger.warn(f'[{self.node_id}] ignore MODBUS cmd,'
' as the state is not UP')
return return
self.__build_header(0x4510) self.__build_header(0x4510)
self._send_buffer += struct.pack('<BHLLL', self.MB_RTU_CMD, self._send_buffer += struct.pack('<BHLLL', self.MB_RTU_CMD,
@@ -349,6 +354,8 @@ class SolarmanV5(Message):
async def send_modbus_cmd(self, func, addr, val, log_lvl) -> None: async def send_modbus_cmd(self, func, addr, val, log_lvl) -> None:
if self.state != self.STATE_UP: if self.state != self.STATE_UP:
logger.warn(f'[{self.node_id}] ignore MODBUS cmd,'
' as the state is not UP')
return return
self.mb.build_msg(Modbus.INV_ADDR, func, addr, val, log_lvl) self.mb.build_msg(Modbus.INV_ADDR, func, addr, val, log_lvl)
@@ -358,6 +365,8 @@ class SolarmanV5(Message):
async def send_at_cmd(self, AT_cmd: str) -> None: async def send_at_cmd(self, AT_cmd: str) -> None:
if self.state != self.STATE_UP: if self.state != self.STATE_UP:
logger.warn(f'[{self.node_id}] ignore AT+ cmd,'
' as the state is not UP')
return return
AT_cmd = AT_cmd.strip() AT_cmd = AT_cmd.strip()

View File

@@ -72,7 +72,7 @@ class Message(metaclass=IterRegistry):
self.header_len = 0 self.header_len = 0
self.data_len = 0 self.data_len = 0
self.unique_id = 0 self.unique_id = 0
self.node_id = '' self.node_id = '' # will be overwritten in the child class's __init__
self.sug_area = '' self.sug_area = ''
self._recv_buffer = bytearray(0) self._recv_buffer = bytearray(0)
self._send_buffer = bytearray(0) self._send_buffer = bytearray(0)