fix MQTT paket transmitting (#309)

This commit is contained in:
Stefan Allius
2025-03-15 13:52:49 +01:00
committed by GitHub
parent 88cb01f613
commit 3489e8997d
2 changed files with 44 additions and 21 deletions

View File

@@ -5,6 +5,7 @@ import asyncio
from itertools import chain from itertools import chain
from datetime import datetime from datetime import datetime
from proxy import Proxy
from async_ifc import AsyncIfc from async_ifc import AsyncIfc
from messages import hex_dump_memory, Message, State from messages import hex_dump_memory, Message, State
from cnf.config import Config from cnf.config import Config
@@ -511,7 +512,7 @@ class SolarmanV5(SolarmanBase):
node_id = self.node_id node_id = self.node_id
key = 'at_resp' key = 'at_resp'
logger.info(f'{key}: {data_json}') logger.info(f'{key}: {data_json}')
await self.mqtt.publish(f'{self.entity_prfx}{node_id}{key}', data_json) # noqa: E501 await Proxy.mqtt.publish(f'{Proxy.entity_prfx}{node_id}{key}', data_json) # noqa: E501
return return
self.forward_at_cmd_resp = False self.forward_at_cmd_resp = False
@@ -655,7 +656,7 @@ class SolarmanV5(SolarmanBase):
def publish_mqtt(self, key, data): # pragma: no cover def publish_mqtt(self, key, data): # pragma: no cover
asyncio.ensure_future( asyncio.ensure_future(
self.mqtt.publish(key, data)) Proxy.mqtt.publish(key, data))
def get_cmd_rsp_log_lvl(self) -> int: def get_cmd_rsp_log_lvl(self) -> int:
ftype = self.ifc.rx_peek()[self.header_len] ftype = self.ifc.rx_peek()[self.header_len]
@@ -679,7 +680,7 @@ class SolarmanV5(SolarmanBase):
node_id = self.node_id node_id = self.node_id
key = 'at_resp' key = 'at_resp'
logger.info(f'{key}: {data_json}') logger.info(f'{key}: {data_json}')
self.publish_mqtt(f'{self.entity_prfx}{node_id}{key}', data_json) # noqa: E501 self.publish_mqtt(f'{Proxy.entity_prfx}{node_id}{key}', data_json) # noqa: E501
return return
elif ftype == self.MB_RTU_CMD: elif ftype == self.MB_RTU_CMD:
self.__modbus_command_rsp(data) self.__modbus_command_rsp(data)

View File

@@ -11,6 +11,7 @@ from cnf.config import Config
from infos import Infos, Register from infos import Infos, Register
from modbus import Modbus from modbus import Modbus
from messages import State, Message from messages import State, Message
from proxy import Proxy
pytest_plugins = ('pytest_asyncio',) pytest_plugins = ('pytest_asyncio',)
@@ -24,6 +25,8 @@ heartbeat = 60
class Mqtt(): class Mqtt():
def __init__(self): def __init__(self):
self.clear()
def clear(self):
self.key = '' self.key = ''
self.data = '' self.data = ''
@@ -50,7 +53,6 @@ class MemoryStream(SolarmanV5):
self.mb_timeout = 0.5 self.mb_timeout = 0.5
self.sent_pdu = b'' self.sent_pdu = b''
self.ifc.tx_fifo.reg_trigger(self.write_cb) self.ifc.tx_fifo.reg_trigger(self.write_cb)
self.mqtt = Mqtt()
self.__msg = msg self.__msg = msg
self.__msg_len = len(msg) self.__msg_len = len(msg)
self.__chunks = chunks self.__chunks = chunks
@@ -62,7 +64,6 @@ class MemoryStream(SolarmanV5):
self.db.stat['proxy']['AT_Command'] = 0 self.db.stat['proxy']['AT_Command'] = 0
self.db.stat['proxy']['AT_Command_Blocked'] = 0 self.db.stat['proxy']['AT_Command_Blocked'] = 0
self.test_exception_async_write = False self.test_exception_async_write = False
self.entity_prfx = ''
self.at_acl = {'mqtt': {'allow': ['AT+'], 'block': ['AT+WEBU']}, 'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE', 'AT+TIME'], 'block': ['AT+WEBU']}} self.at_acl = {'mqtt': {'allow': ['AT+'], 'block': ['AT+WEBU']}, 'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE', 'AT+TIME'], 'block': ['AT+WEBU']}}
self.key = '' self.key = ''
self.data = '' self.data = ''
@@ -85,8 +86,8 @@ class MemoryStream(SolarmanV5):
self.__chunk_idx = 0 self.__chunk_idx = 0
def publish_mqtt(self, key, data): def publish_mqtt(self, key, data):
self.key = key Proxy.mqtt.key = key
self.data = data Proxy.mqtt.data = data
def _read(self) -> int: def _read(self) -> int:
copied_bytes = 0 copied_bytes = 0
@@ -768,7 +769,17 @@ def dcu_data_rsp_msg(): # 0x1210
@pytest.fixture @pytest.fixture
def config_tsun_allow_all(): def config_tsun_allow_all():
Config.act_config = {'solarman':{'enabled': True}, 'inverters':{'allow_all':True}} Config.act_config = {
'ha':{
'auto_conf_prefix': 'homeassistant',
'discovery_prefix': 'homeassistant',
'entity_prefix': 'tsun',
'proxy_node_id': 'test_1',
'proxy_unique_id': ''
},
'solarman':{'enabled': True}, 'inverters':{'allow_all':True}}
Proxy.class_init()
Proxy.mqtt = Mqtt() # set dummy mqtt instance
@pytest.fixture @pytest.fixture
def config_no_tsun_inv1(): def config_no_tsun_inv1():
@@ -776,7 +787,17 @@ def config_no_tsun_inv1():
@pytest.fixture @pytest.fixture
def config_tsun_inv1(): def config_tsun_inv1():
Config.act_config = {'solarman':{'enabled': True},'inverters':{'Y170000000000001':{'monitor_sn': 2070233889, 'node_id':'inv1', 'modbus_polling': True, 'suggested_area':'roof', 'sensor_list': 0}}} Config.act_config = {
'ha':{
'auto_conf_prefix': 'homeassistant',
'discovery_prefix': 'homeassistant',
'entity_prefix': 'tsun',
'proxy_node_id': 'test_1',
'proxy_unique_id': ''
},
'solarman':{'enabled': True},'inverters':{'Y170000000000001':{'monitor_sn': 2070233889, 'node_id':'inv1', 'modbus_polling': True, 'suggested_area':'roof', 'sensor_list': 0}}}
Proxy.class_init()
Proxy.mqtt = Mqtt()
@pytest.fixture @pytest.fixture
def config_tsun_scan(): def config_tsun_scan():
@@ -1465,8 +1486,8 @@ async def test_at_cmd(config_tsun_allow_all, device_ind_msg, device_rsp_msg, inv
assert m.ifc.fwd_fifo.get()==b'' assert m.ifc.fwd_fifo.get()==b''
assert m.sent_pdu == b'' assert m.sent_pdu == b''
assert str(m.seq) == '01:01' assert str(m.seq) == '01:01'
assert m.mqtt.key == '' assert Proxy.mqtt.key == ''
assert m.mqtt.data == "" assert Proxy.mqtt.data == ""
m.append_msg(inverter_ind_msg) m.append_msg(inverter_ind_msg)
m.read() # read inverter ind m.read() # read inverter ind
@@ -1482,8 +1503,8 @@ async def test_at_cmd(config_tsun_allow_all, device_ind_msg, device_rsp_msg, inv
m.sent_pdu = bytearray() m.sent_pdu = bytearray()
assert str(m.seq) == '02:03' assert str(m.seq) == '02:03'
assert m.mqtt.key == '' assert Proxy.mqtt.key == ''
assert m.mqtt.data == "" assert Proxy.mqtt.data == ""
m.append_msg(at_command_rsp_msg) m.append_msg(at_command_rsp_msg)
m.read() # read at resp m.read() # read at resp
@@ -1492,8 +1513,9 @@ async def test_at_cmd(config_tsun_allow_all, device_ind_msg, device_rsp_msg, inv
assert m.ifc.rx_get()==b'' assert m.ifc.rx_get()==b''
assert m.ifc.tx_fifo.get()==b'' assert m.ifc.tx_fifo.get()==b''
assert m.ifc.fwd_fifo.get()==b'' assert m.ifc.fwd_fifo.get()==b''
assert m.key == 'at_resp' assert Proxy.mqtt.key == 'tsun/at_resp'
assert m.data == "+ok" assert Proxy.mqtt.data == "+ok"
Proxy.mqtt.clear() # clear last test result
m.sent_pdu = bytearray() m.sent_pdu = bytearray()
m.test_exception_async_write = True m.test_exception_async_write = True
@@ -1505,8 +1527,8 @@ async def test_at_cmd(config_tsun_allow_all, device_ind_msg, device_rsp_msg, inv
assert m.sent_pdu == b'' assert m.sent_pdu == b''
assert str(m.seq) == '03:04' assert str(m.seq) == '03:04'
assert m.forward_at_cmd_resp == False assert m.forward_at_cmd_resp == False
assert m.mqtt.key == '' assert Proxy.mqtt.key == ''
assert m.mqtt.data == "" assert Proxy.mqtt.data == ""
m.close() m.close()
@pytest.mark.asyncio @pytest.mark.asyncio
@@ -1523,8 +1545,8 @@ async def test_at_cmd_blocked(config_tsun_allow_all, device_ind_msg, device_rsp_
assert m.ifc.tx_fifo.get()==b'' assert m.ifc.tx_fifo.get()==b''
assert m.ifc.fwd_fifo.get()==b'' assert m.ifc.fwd_fifo.get()==b''
assert str(m.seq) == '01:01' assert str(m.seq) == '01:01'
assert m.mqtt.key == '' assert Proxy.mqtt.key == ''
assert m.mqtt.data == "" assert Proxy.mqtt.data == ""
m.append_msg(inverter_ind_msg) m.append_msg(inverter_ind_msg)
m.read() m.read()
@@ -1540,8 +1562,8 @@ async def test_at_cmd_blocked(config_tsun_allow_all, device_ind_msg, device_rsp_
assert m.ifc.fwd_fifo.get()==b'' assert m.ifc.fwd_fifo.get()==b''
assert str(m.seq) == '02:02' assert str(m.seq) == '02:02'
assert m.forward_at_cmd_resp == False assert m.forward_at_cmd_resp == False
assert m.mqtt.key == 'at_resp' assert Proxy.mqtt.key == 'tsun/at_resp'
assert m.mqtt.data == "'AT+WEBU' is forbidden" assert Proxy.mqtt.data == "'AT+WEBU' is forbidden"
m.close() m.close()
def test_at_cmd_ind(config_tsun_inv1, at_command_ind_msg): def test_at_cmd_ind(config_tsun_inv1, at_command_ind_msg):