Skip to content

Commit 6f98569

Browse files
authored
Clean pdu lookup in simulator. (#2751)
1 parent 8ce8001 commit 6f98569

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

pymodbus/pdu/decoders.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def lookupPduClass(self, data: bytes) -> type[ModbusPDU] | None:
3737
return self.sub_lookup[func_code].get(sub_func_code, None)
3838
return self.lookup.get(func_code, None)
3939

40+
def list_function_codes(self):
41+
"""Return list of function codes."""
42+
return list(self.lookup)
43+
4044
@classmethod
4145
def add_pdu(cls, req: type[ModbusPDU], resp: type[ModbusPDU]):
4246
"""Register request/response."""

pymodbus/server/simulator/http_server.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class CallTypeResponse:
7171
clear_after: int = 1
7272

7373

74-
class ModbusSimulatorServer: # pylint: disable=too-many-instance-attributes
74+
class ModbusSimulatorServer:
7575
"""**ModbusSimulatorServer**.
7676
7777
:param modbus_server: Server name in json file (default: "server")
@@ -201,7 +201,6 @@ def __init__(
201201
self.refresh_rate = 0
202202
self.register_filter: list[int] = []
203203
self.call_list: list[CallTracer] = []
204-
self.request_lookup = DecodePDU(True).lookup
205204
self.call_monitor = CallTypeMonitor()
206205
self.call_response = CallTypeResponse()
207206
app_key = getattr(web, 'AppKey', str) # fall back to str for aiohttp < 3.9.0
@@ -376,7 +375,7 @@ def build_html_calls(self, params: dict, html: str) -> str:
376375
else ""
377376
)
378377
function_codes = ""
379-
for function in self.request_lookup.values():
378+
for function in DecodePDU(True).list_function_codes():
380379
selected = (
381380
"selected"
382381
if function.function_code == self.call_monitor.function
@@ -392,9 +391,7 @@ def build_html_calls(self, params: dict, html: str) -> str:
392391
del self.call_list[0]
393392
call_rows = ""
394393
for entry in reversed(self.call_list):
395-
# req_obj = self.request_lookup[entry[1]]
396394
call_rows += f"<tr><td>{entry.call} - {entry.fc}</td><td>{entry.address}</td><td>{entry.count}</td><td>{entry.data.decode()}</td></tr>"
397-
# line += req_obj.funcion_code_name
398395
new_html = (
399396
html.replace("<!--SIMULATION_ACTIVE-->", simulation_action)
400397
.replace("FUNCTION_RANGE_START", range_start_html)
@@ -543,11 +540,11 @@ def build_json_calls(self, params: dict) -> dict:
543540
)
544541

545542
function_codes = []
546-
for function in self.request_lookup.values():
543+
for function in DecodePDU(True).list_function_codes():
547544
function_codes.append({
548-
"value": function.function_code,
545+
"value": function,
549546
"text": "function code name",
550-
"selected": function.function_code == self.call_monitor.function
547+
"selected": function == self.call_monitor.function
551548
})
552549

553550
simulation_action = "ACTIVE" if self.call_response.active != RESPONSE_INACTIVE else ""

0 commit comments

Comments
 (0)