From 02d9f01947f4eea7fbfe2054bb3da8ce1bb08f4c Mon Sep 17 00:00:00 2001 From: Stefan Allius Date: Tue, 7 May 2024 18:32:56 +0200 Subject: [PATCH] don't send AT or Modbus cmds on closed connections --- app/src/gen3/talent.py | 2 ++ app/src/gen3plus/solarman_v5.py | 6 ++++-- app/src/mqtt.py | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/src/gen3/talent.py b/app/src/gen3/talent.py index e6d3b67..1534446 100644 --- a/app/src/gen3/talent.py +++ b/app/src/gen3/talent.py @@ -45,6 +45,7 @@ class Talent(Message): self.db = InfosG3() self.mb = Modbus() self.forward_modbus_resp = False + self.closed = False self.switch = { 0x00: self.msg_contact_info, 0x13: self.msg_ota_update, @@ -66,6 +67,7 @@ class Talent(Message): # so we have to erase self.switch, otherwise this instance can't be # deallocated by the garbage collector ==> we get a memory leak self.switch.clear() + self.closed = True def __set_serial_no(self, serial_no: str): diff --git a/app/src/gen3plus/solarman_v5.py b/app/src/gen3plus/solarman_v5.py index 5805361..126e06e 100644 --- a/app/src/gen3plus/solarman_v5.py +++ b/app/src/gen3plus/solarman_v5.py @@ -62,6 +62,7 @@ class SolarmanV5(Message): self.time_ofs = 0 self.mb = Modbus() self.forward_modbus_resp = False + self.closed = False self.switch = { 0x4210: self.msg_data_ind, # real time data @@ -102,6 +103,7 @@ class SolarmanV5(Message): # so we have to erase self.switch, otherwise this instance can't be # deallocated by the garbage collector ==> we get a memory leak self.switch.clear() + self.closed = True def __set_serial_no(self, snr: int): serial_no = str(snr) @@ -434,9 +436,9 @@ class SolarmanV5(Message): elif ftype == self.MB_RTU_CMD: valid = data[1] modbus_msg_len = self.data_len - 14 - logger.debug(f'modbus_len:{modbus_msg_len} accepted:{valid}') + # logger.debug(f'modbus_len:{modbus_msg_len} accepted:{valid}') if valid == 1 and modbus_msg_len > 4: - logger.info(f'first byte modbus:{data[14]}') + # logger.info(f'first byte modbus:{data[14]}') inv_update = False for key, update in self.mb.recv_resp(self.db, data[14:-2], self.node_id): diff --git a/app/src/mqtt.py b/app/src/mqtt.py index 7257038..3469201 100644 --- a/app/src/mqtt.py +++ b/app/src/mqtt.py @@ -130,7 +130,7 @@ class Mqtt(metaclass=Singleton): topic = str(message.topic) node_id = topic.split('/')[1] + '/' for m in Message: - if m.server_side and m.node_id == node_id: + if m.server_side and not m.closed and (m.node_id == node_id): logger_mqtt.debug(f'Found: {node_id}') fnc = getattr(m, func_name, None) if callable(fnc): @@ -148,7 +148,7 @@ class Mqtt(metaclass=Singleton): payload = message.payload.decode("UTF-8") logger_mqtt.info(f'InvCnf: {node_id}:{payload}') for m in Message: - if m.server_side and m.node_id == node_id: + if m.server_side and not m.closed and (m.node_id == node_id): logger_mqtt.info(f'Found: {node_id}') fnc = getattr(m, "send_modbus_cmd", None) res = payload.split(',')