Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulator config, kwargs -> parameters. #2235

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions API_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ API changes 3.7.0
- binary framer no longer supported
- Framer.<type> renamed to FramerType.<type>
- PDU classes moved to pymodbus/pdu
- Simulator config custom actions kwargs -> parameters


API changes 3.6.0
Expand Down
4 changes: 2 additions & 2 deletions doc/source/library/simulator/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 24 additions & 24 deletions pymodbus/datastore/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -148,27 +148,27 @@ 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:
raise RuntimeError(f'ERROR "{Label.type_bits}" {reg} used')
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:
raise RuntimeError(f'ERROR "{Label.type_uint16}" {reg} used')
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):
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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."""
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -722,15 +722,15 @@ 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:
"""
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:
Expand Down
12 changes: 6 additions & 6 deletions pymodbus/server/simulator/setup.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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": [
Expand All @@ -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": [
Expand Down
18 changes: 9 additions & 9 deletions test/sub_server/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class TestSimulator:
"addr": [31, 32],
"value": 50,
"action": "random",
"kwargs": {"minval": 10, "maxval": 80},
"parameters": {"minval": 10, "maxval": 80},
},
],
"float32": [
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down