-
Notifications
You must be signed in to change notification settings - Fork 1k
add 'change_rate' randomization option #1229
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,6 +113,7 @@ def __init__( | |
| holding_registers: BaseModbusDataBlock = None, | ||
| zero_mode: bool = False, | ||
| randomize: int = 0, | ||
| change_rate: int = 0, | ||
| **kwargs, | ||
| ): | ||
| """Reactive Modbus slave context supporting simulating data. | ||
|
|
@@ -124,6 +125,8 @@ def __init__( | |
| :param zero_mode: Enable zero mode for data blocks | ||
| :param randomize: Randomize reads every <n> reads for DI and IR, | ||
| default is disabled (0) | ||
| :param change_rate: Rate in % of registers to change for DI and IR, | ||
| default is disabled (0) | ||
| :param min_binary_value: Minimum value for coils and discrete inputs | ||
| :param max_binary_value: Max value for discrete inputs | ||
| :param min_register_value: Minimum value for input registers | ||
|
|
@@ -142,6 +145,9 @@ def __init__( | |
| min_register_value = kwargs.get("min_register_value", 0) | ||
| max_register_value = kwargs.get("max_register_value", 65535) | ||
| self._randomize = randomize | ||
| self._change_rate = change_rate | ||
| if self._randomize > 0: | ||
| self._change_rate = 0 | ||
| self._lock = threading.Lock() | ||
| self._read_counter = {"d": 0, "i": 0} | ||
| self._min_binary_value = min_binary_value | ||
|
|
@@ -177,6 +183,20 @@ def getValues(self, fc_as_hex, address, count=1): | |
| # "'%s'", _block_type, values, address) | ||
| self.store[_block_type].setValues(address, values) | ||
| self._read_counter[_block_type] += 1 | ||
| elif self._change_rate > 0 and _block_type in {"d", "i"}: | ||
| for offset in range(count): | ||
| if random.randint(1, 100) <= self._change_rate: | ||
janiversen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| with self._lock: | ||
| # Update values | ||
| if _block_type == "d": | ||
| min_val = self._min_binary_value | ||
| max_val = self._max_binary_value | ||
| else: | ||
| min_val = self._min_register_value | ||
| max_val = self._max_register_value | ||
| self.store[_block_type].setValues( | ||
| address + offset, random.randint(min_val, max_val) | ||
| ) | ||
| values = self.store[_block_type].getValues(address, count) | ||
| return values | ||
|
|
||
|
|
@@ -385,13 +405,15 @@ def create_context( | |
| unit: list[int] = [1], | ||
| single: bool = False, | ||
| randomize: int = 0, | ||
| change_rate: int = 0, | ||
| ): # pylint: disable=dangerous-default-value | ||
| """Create Modbus context. | ||
|
|
||
| :param data_block_settings: Datablock (dict) Refer DEFAULT_DATA_BLOCK | ||
| :param unit: Unit id for the slave | ||
| :param single: To run as a single slave | ||
| :param randomize: Randomize every <n> reads for DI and IR. | ||
| :param change_rate: Rate in % of registers to change for DI and IR. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your code do not work in % but random, that is every run will change a different number of registers some will change more registers than -r % and others fewer.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I push a new commit per @dhoomakethu's suggestion and I think that solved the problem |
||
| :return: ModbusServerContext object | ||
| """ | ||
| data_block = data_block_settings.pop("data_block", DEFAULT_DATA_BLOCK) | ||
|
|
@@ -422,7 +444,11 @@ def create_context( | |
| block[modbus_entity] = db(start_address, default_values) | ||
|
|
||
| slave_context = ReactiveModbusSlaveContext( | ||
| **block, randomize=randomize, zero_mode=True, **data_block_settings | ||
| **block, | ||
| randomize=randomize, | ||
| change_rate=change_rate, | ||
| zero_mode=True, | ||
| **data_block_settings, | ||
| ) | ||
| if not single: | ||
| slaves[i] = slave_context | ||
|
|
@@ -471,6 +497,7 @@ def factory( # pylint: disable=dangerous-default-value,too-many-arguments | |
| sys.exit(1) | ||
| server = SERVER_MAPPER.get(server) | ||
| randomize = kwargs.pop("randomize", 0) | ||
| change_rate = kwargs.pop("change_rate", 0) | ||
| if not framer: | ||
| framer = DEFAULT_FRAMER.get(server) | ||
| if not context: | ||
|
|
@@ -479,6 +506,7 @@ def factory( # pylint: disable=dangerous-default-value,too-many-arguments | |
| unit=unit, | ||
| single=single, | ||
| randomize=randomize, | ||
| change_rate=change_rate, | ||
| ) | ||
| if not identity: | ||
| identity = cls.create_identity() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will potentially confuse a user, please add a logger message so the user knows why -r did not work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the new commit, do you have any suggestion on how to handle the case that user provide both flags ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My personal preference is to terminate with an error message, but at the very least the user needs to be aware that the -c is voided.