From 180880d2859eb2eb33bd3c24a086552cf0835974 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Thu, 18 Jul 2024 12:12:01 +0200 Subject: [PATCH] Simulator config, kwargs -> parameters. --- API_changes.rst | 1 + doc/source/library/simulator/config.rst | 4 +-- pymodbus/datastore/simulator.py | 48 ++++++++++++------------- pymodbus/server/simulator/setup.json | 12 +++---- test/sub_server/test_simulator.py | 18 +++++----- 5 files changed, 42 insertions(+), 41 deletions(-) diff --git a/API_changes.rst b/API_changes.rst index 8c3fd1de8..bcbd9dfb8 100644 --- a/API_changes.rst +++ b/API_changes.rst @@ -12,6 +12,7 @@ API changes 3.7.0 - binary framer no longer supported - Framer. renamed to FramerType. - PDU classes moved to pymodbus/pdu +- Simulator config custom actions kwargs -> parameters API changes 3.6.0 diff --git a/doc/source/library/simulator/config.rst b/doc/source/library/simulator/config.rst index 700f9a89f..99766f2e0 100644 --- a/doc/source/library/simulator/config.rst +++ b/doc/source/library/simulator/config.rst @@ -288,8 +288,8 @@ In case of **"increment"**, the counter is reset to the minimum value, if the ma .. code-block:: - {"addr": 9, "value": 7, "action": "random", "kwargs": {"minval": 0, "maxval": 12} }, - {"addr": 10, "value": 100, "action": "increment", "kwargs": {"minval": 50} } + {"addr": 9, "value": 7, "action": "random", "parameters": {"minval": 0, "maxval": 12} }, + {"addr": 10, "value": 100, "action": "increment", "parameters": {"minval": 50} } Invalid section diff --git a/pymodbus/datastore/simulator.py b/pymodbus/datastore/simulator.py index 31b61d856..69ecb2a26 100644 --- a/pymodbus/datastore/simulator.py +++ b/pymodbus/datastore/simulator.py @@ -35,7 +35,7 @@ class Cell: access: bool = False value: int = 0 action: int = 0 - action_kwargs: dict[str, Any] | None = None + action_parameters: dict[str, Any] | None = None count_read: int = 0 count_write: int = 0 @@ -47,7 +47,7 @@ class TextCell: # pylint: disable=too-few-public-methods access: str value: str action: str - action_kwargs: str + action_parameters: str count_read: str count_write: str @@ -69,7 +69,7 @@ class Label: # pylint: disable=too-many-instance-attributes increment: str = "increment" invalid: str = "invalid" ir_size: str = "ir size" - kwargs: str = "kwargs" + parameters: str = "parameters" method: str = "method" next: str = "next" none: str = "none" @@ -148,7 +148,7 @@ def __init__(self, runtime): }, } - def handle_type_bits(self, start, stop, value, action, action_kwargs): + def handle_type_bits(self, start, stop, value, action, action_parameters): """Handle type bits.""" for reg in self.runtime.registers[start:stop]: if reg.type != CellType.INVALID: @@ -156,9 +156,9 @@ def handle_type_bits(self, start, stop, value, action, action_kwargs): reg.value = value reg.type = CellType.BITS reg.action = action - reg.action_kwargs = action_kwargs + reg.action_parameters = action_parameters - def handle_type_uint16(self, start, stop, value, action, action_kwargs): + def handle_type_uint16(self, start, stop, value, action, action_parameters): """Handle type uint16.""" for reg in self.runtime.registers[start:stop]: if reg.type != CellType.INVALID: @@ -166,9 +166,9 @@ def handle_type_uint16(self, start, stop, value, action, action_kwargs): reg.value = value reg.type = CellType.UINT16 reg.action = action - reg.action_kwargs = action_kwargs + reg.action_parameters = action_parameters - def handle_type_uint32(self, start, stop, value, action, action_kwargs): + def handle_type_uint32(self, start, stop, value, action, action_parameters): """Handle type uint32.""" regs_value = ModbusSimulatorContext.build_registers_from_value(value, True) for i in range(start, stop, 2): @@ -178,11 +178,11 @@ def handle_type_uint32(self, start, stop, value, action, action_kwargs): regs[0].value = regs_value[0] regs[0].type = CellType.UINT32 regs[0].action = action - regs[0].action_kwargs = action_kwargs + regs[0].action_parameters = action_parameters regs[1].value = regs_value[1] regs[1].type = CellType.NEXT - def handle_type_float32(self, start, stop, value, action, action_kwargs): + def handle_type_float32(self, start, stop, value, action, action_parameters): """Handle type uint32.""" regs_value = ModbusSimulatorContext.build_registers_from_value(value, False) for i in range(start, stop, 2): @@ -192,11 +192,11 @@ def handle_type_float32(self, start, stop, value, action, action_kwargs): regs[0].value = regs_value[0] regs[0].type = CellType.FLOAT32 regs[0].action = action - regs[0].action_kwargs = action_kwargs + regs[0].action_parameters = action_parameters regs[1].value = regs_value[1] regs[1].type = CellType.NEXT - def handle_type_string(self, start, stop, value, action, action_kwargs): + def handle_type_string(self, start, stop, value, action, action_parameters): """Handle type string.""" regs = stop - start reg_len = regs * 2 @@ -214,7 +214,7 @@ def handle_type_string(self, start, stop, value, action, action_kwargs): reg.type = CellType.NEXT self.runtime.registers[start].type = CellType.STRING self.runtime.registers[start].action = action - self.runtime.registers[start].action_kwargs = action_kwargs + self.runtime.registers[start].action_parameters = action_parameters def handle_setup_section(self): """Load setup section.""" @@ -305,7 +305,7 @@ def handle_types(self): self.runtime.action_name_to_id[ entry.get(Label.action, type_entry[Label.action]) ], - entry.get(Label.kwargs, None), + entry.get(Label.parameters, None), ) del self.config[section] @@ -440,7 +440,7 @@ class ModbusSimulatorContext(ModbusBaseSlaveContext): {"addr": [32, 34], "value": 0xF1}, --> with value {"addr": [35, 36], "action": "increment"}, --> with action {"addr": [37, 38], "action": "increment", "value": 0xF1} --> with action and value - {"addr": [37, 38], "action": "increment", "kwargs": {"min": 0, "max": 100}} --> with action with arguments + {"addr": [37, 38], "action": "increment", "parameters": {"min": 0, "max": 100}} --> with action with arguments ], "uint16": [ --> Define uint16 (1 register == 2 bytes) --> same as type_bits @@ -495,8 +495,8 @@ def get_text_register(self, register): text_cell.count_read = str(reg.count_read) text_cell.count_write = str(reg.count_write) text_cell.action = self.action_id_to_name[reg.action] - if reg.action_kwargs: - text_cell.action = f"{text_cell.action}({reg.action_kwargs})" + if reg.action_parameters: + text_cell.action = f"{text_cell.action}({reg.action_parameters})" if reg.type in (CellType.INVALID, CellType.UINT16, CellType.NEXT): text_cell.value = str(reg.value) build_len = 0 @@ -589,9 +589,9 @@ def getValues(self, func_code, address, count=1): real_address = self.fc_offset[func_code] + address for i in range(real_address, real_address + count): reg = self.registers[i] - kwargs = reg.action_kwargs if reg.action_kwargs else {} + parameters = reg.action_parameters if reg.action_parameters else {} if reg.action: - self.action_methods[reg.action](self.registers, i, reg, **kwargs) + self.action_methods[reg.action](self.registers, i, reg, **parameters) self.registers[i].count_read += 1 result.append(reg.value) else: @@ -602,9 +602,9 @@ def getValues(self, func_code, address, count=1): for i in range(real_address, real_address + reg_count): reg = self.registers[i] if reg.action: - kwargs = reg.action_kwargs or {} + parameters = reg.action_parameters or {} self.action_methods[reg.action]( - self.registers, i, reg, **kwargs + self.registers, i, reg, **parameters ) self.registers[i].count_read += 1 while count and bit_index < 16: @@ -707,7 +707,7 @@ def action_increment(cls, registers, inx, cell, minval=None, maxval=None): reg2.value = new_regs[1] @classmethod - def action_timestamp(cls, registers, inx, _cell, **_kwargs): + def action_timestamp(cls, registers, inx, _cell, **_parameters): """Set current time. :meta private: @@ -722,7 +722,7 @@ def action_timestamp(cls, registers, inx, _cell, **_kwargs): registers[inx + 6].value = system_time.second @classmethod - def action_reset(cls, _registers, _inx, _cell, **_kwargs): + def action_reset(cls, _registers, _inx, _cell, **_parameters): """Reboot server. :meta private: @@ -730,7 +730,7 @@ def action_reset(cls, _registers, _inx, _cell, **_kwargs): raise RuntimeError("RESET server") @classmethod - def action_uptime(cls, registers, inx, cell, **_kwargs): + def action_uptime(cls, registers, inx, cell, **_parameters): """Return uptime in seconds. :meta private: diff --git a/pymodbus/server/simulator/setup.json b/pymodbus/server/simulator/setup.json index ea3213b62..94c63f4fb 100644 --- a/pymodbus/server/simulator/setup.json +++ b/pymodbus/server/simulator/setup.json @@ -171,12 +171,12 @@ {"addr": 2305, "value": 50, "action": "increment", - "kwargs": {"minval": 45, "maxval": 155} + "parameters": {"minval": 45, "maxval": 155} }, {"addr": 2306, "value": 50, "action": "random", - "kwargs": {"minval": 45, "maxval": 55} + "parameters": {"minval": 45, "maxval": 55} } ], "uint32": [ @@ -190,12 +190,12 @@ {"addr": [3876, 3877], "value": 50000, "action": "increment", - "kwargs": {"minval": 45000, "maxval": 55000} + "parameters": {"minval": 45000, "maxval": 55000} }, {"addr": [3878, 3879], "value": 50000, "action": "random", - "kwargs": {"minval": 45000, "maxval": 55000} + "parameters": {"minval": 45000, "maxval": 55000} } ], "float32": [ @@ -209,12 +209,12 @@ {"addr": [4876, 4877], "value": 50000.0, "action": "increment", - "kwargs": {"minval": 45000.0, "maxval": 55000.0} + "parameters": {"minval": 45000.0, "maxval": 55000.0} }, {"addr": [4878, 48779], "value": 50000.0, "action": "random", - "kwargs": {"minval": 45000.0, "maxval": 55000.0} + "parameters": {"minval": 45000.0, "maxval": 55000.0} } ], "string": [ diff --git a/test/sub_server/test_simulator.py b/test/sub_server/test_simulator.py index aeefed8b7..a870d942e 100644 --- a/test/sub_server/test_simulator.py +++ b/test/sub_server/test_simulator.py @@ -85,7 +85,7 @@ class TestSimulator: "addr": [31, 32], "value": 50, "action": "random", - "kwargs": {"minval": 10, "maxval": 80}, + "parameters": {"minval": 10, "maxval": 80}, }, ], "float32": [ @@ -151,7 +151,7 @@ class TestSimulator: Cell(type=CellType.UINT32, value=5, action=1), Cell(type=CellType.NEXT, value=17320), # 30 Cell( - type=CellType.UINT32, action=2, action_kwargs={"minval": 10, "maxval": 80} + type=CellType.UINT32, action=2, action_parameters={"minval": 10, "maxval": 80} ), Cell(type=CellType.NEXT, value=50), Cell(type=CellType.FLOAT32, access=True, value=17731), @@ -215,7 +215,7 @@ def test_simulator_config_verify(self): assert reg.value == test_cell.value, f"at index {i} - {offset}" assert reg.action == test_cell.action, f"at index {i} - {offset}" assert ( - reg.action_kwargs == test_cell.action_kwargs + reg.action_parameters == test_cell.action_parameters ), f"at index {i} - {offset}" assert ( reg.count_read == test_cell.count_read @@ -404,10 +404,10 @@ def test_simulator_set_values(self): exc_simulator.setValues(FX_WRITE_BIT, 84, [True]) exc_simulator.setValues(FX_WRITE_BIT, 86, [True, False, True]) result = exc_simulator.getValues(FX_READ_BIT, 80, 8) - assert [True, False] * 4 == result + assert result == [True, False] * 4 exc_simulator.setValues(FX_WRITE_BIT, 88, [False]) result = exc_simulator.getValues(FX_READ_BIT, 86, 3) - assert [True, False, False] == result + assert result == [True, False, False] exc_simulator.setValues(FX_WRITE_BIT, 80, [True] * 17) def test_simulator_get_text(self): @@ -509,13 +509,13 @@ def test_simulator_action_increment( exc_setup = copy.deepcopy(self.default_config) exc_simulator = ModbusSimulatorContext(exc_setup, None) action = exc_simulator.action_name_to_id[Label.increment] - kwargs = { + parameters = { "minval": minval, "maxval": maxval, } exc_simulator.registers[30].type = celltype exc_simulator.registers[30].action = action - exc_simulator.registers[30].action_kwargs = kwargs + exc_simulator.registers[30].action_parameters = parameters exc_simulator.registers[31].type = CellType.NEXT is_int = celltype != CellType.FLOAT32 @@ -557,13 +557,13 @@ def test_simulator_action_random(self, celltype, minval, maxval): exc_setup = copy.deepcopy(self.default_config) exc_simulator = ModbusSimulatorContext(exc_setup, None) action = exc_simulator.action_name_to_id[Label.random] - kwargs = { + parameters = { "minval": minval, "maxval": maxval, } exc_simulator.registers[30].type = celltype exc_simulator.registers[30].action = action - exc_simulator.registers[30].action_kwargs = kwargs + exc_simulator.registers[30].action_parameters = parameters exc_simulator.registers[31].type = CellType.NEXT is_int = celltype != CellType.FLOAT32 reg_count = 1 if celltype in (CellType.BITS, CellType.UINT16) else 2