Skip to content

Commit

Permalink
Allow omitted rule constraints to match any value
Browse files Browse the repository at this point in the history
  • Loading branch information
rgov committed Jul 24, 2020
1 parent dcf35a1 commit 3937df2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 5 additions & 6 deletions umodbus/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ def __init__(self, endpoint, slave_ids, function_codes, addresses):
self.addresses = addresses

def match(self, slave_id, function_code, address):
if slave_id in self.slave_ids and\
function_code in self.function_codes and \
address in self.addresses:
return True

return False
# A constraint of None matches any value
matches = lambda values, v: values is None or v in values
return matches(self.slave_ids, slave_id) and \
matches(self.function_codes, function_code) and \
matches(self.addresses, address)
8 changes: 5 additions & 3 deletions umodbus/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ def route(self, slave_ids=None, function_codes=None, addresses=None):
def read_single_bit_values(slave_id, address):
return random.choise([0, 1])
:param slave_ids: A list or set with slave id's.
:param function_codes: A list or set with function codes.
:param addresses: A list or set with addresses.
Any argument can be omitted to match any value.
:param slave_ids: A list (or iterable) of slave ids.
:param function_codes: A list (or iterable) of function codes.
:param addresses: A list (or iterable) of addresses.
"""
def inner(f):
self.route_map.add_rule(f, slave_ids, function_codes, addresses)
Expand Down

0 comments on commit 3937df2

Please sign in to comment.