Skip to content

Commit

Permalink
Merge 850dfce into d173ab5
Browse files Browse the repository at this point in the history
  • Loading branch information
piertoni committed Sep 5, 2019
2 parents d173ab5 + 850dfce commit a5f5f15
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
29 changes: 21 additions & 8 deletions umodbus/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ def execute(self, slave_id, route_map):
for address in range(self.starting_address,
self.starting_address + self.quantity):
endpoint = route_map.match(slave_id, self.function_code,
address)
address, self.starting_address,
self.quantity)
values.append(endpoint(slave_id=slave_id, address=address,
function_code=self.function_code))

Expand Down Expand Up @@ -575,7 +576,8 @@ def execute(self, slave_id, route_map):
for address in range(self.starting_address,
self.starting_address + self.quantity):
endpoint = route_map.match(slave_id, self.function_code,
address)
address, self.starting_address,
self.quantity)
values.append(endpoint(slave_id=slave_id, address=address,
function_code=self.function_code))

Expand Down Expand Up @@ -755,7 +757,8 @@ def execute(self, slave_id, route_map):
for address in range(self.starting_address,
self.starting_address + self.quantity):
endpoint = route_map.match(slave_id, self.function_code,
address)
address, self.starting_address,
self.quantity)
values.append(endpoint(slave_id=slave_id, address=address,
function_code=self.function_code))

Expand Down Expand Up @@ -933,7 +936,8 @@ def execute(self, slave_id, route_map):
for address in range(self.starting_address,
self.starting_address + self.quantity):
endpoint = route_map.match(slave_id, self.function_code,
address)
address, self.starting_address,
self.quantity)
values.append(endpoint(slave_id=slave_id, address=address,
function_code=self.function_code))

Expand Down Expand Up @@ -1093,7 +1097,10 @@ def execute(self, slave_id, route_map):
:param slave_id: Slave id.
:param eindpoint: Instance of modbus.route.Map.
"""
endpoint = route_map.match(slave_id, self.function_code, self.address)
starting_address = self.address
quantity = 1
endpoint = route_map.match(slave_id, self.function_code, self.address,
starting_address, quantity)
try:
endpoint(slave_id=slave_id, address=self.address, value=self.value,
function_code=self.function_code)
Expand Down Expand Up @@ -1237,7 +1244,10 @@ def execute(self, slave_id, route_map):
:param slave_id: Slave id.
:param eindpoint: Instance of modbus.route.Map.
"""
endpoint = route_map.match(slave_id, self.function_code, self.address)
starting_address = self.address
quantity = 1
endpoint = route_map.match(slave_id, self.function_code, self.address,
starting_address, quantity=1)
try:
endpoint(slave_id=slave_id, address=self.address, value=self.value,
function_code=self.function_code)
Expand Down Expand Up @@ -1453,7 +1463,8 @@ def execute(self, slave_id, route_map):
"""
for index, value in enumerate(self.values):
address = self.starting_address + index
endpoint = route_map.match(slave_id, self.function_code, address)
endpoint = route_map.match(slave_id, self.function_code, address,
self.starting_address, self.quantity)

try:
endpoint(slave_id=slave_id, address=address, value=value,
Expand Down Expand Up @@ -1605,7 +1616,9 @@ def execute(self, slave_id, route_map):
"""
for index, value in enumerate(self.values):
address = self.starting_address + index
endpoint = route_map.match(slave_id, self.function_code, address)
quantity = len(self.values)
endpoint = route_map.match(slave_id, self.function_code, address,
self.starting_address, quantity)

try:
endpoint(slave_id=slave_id, address=address, value=value,
Expand Down
18 changes: 11 additions & 7 deletions umodbus/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@ class Map:
def __init__(self):
self._rules = []

def add_rule(self, endpoint, slave_ids, function_codes, addresses):
def add_rule(self, endpoint, slave_ids, function_codes, addresses, starting_address=None, quantity=None):
self._rules.append(DataRule(endpoint, slave_ids, function_codes,
addresses))
addresses, starting_address, quantity))

def match(self, slave_id, function_code, address):
def match(self, slave_id, function_code, address, starting_address, quantity):
for rule in self._rules:
if rule.match(slave_id, function_code, address):
if rule.match(slave_id, function_code, address, starting_address, quantity):
return rule.endpoint


class DataRule:
def __init__(self, endpoint, slave_ids, function_codes, addresses):
def __init__(self, endpoint, slave_ids, function_codes, addresses, starting_address, quantity):
self.endpoint = endpoint
self.slave_ids = slave_ids
self.function_codes = function_codes
self.addresses = addresses
self.starting_address = starting_address
self.quantity = quantity

def match(self, slave_id, function_code, address):
if slave_id in self.slave_ids and\
def match(self, slave_id, function_code, address, starting_address, quantity):
if slave_id in self.slave_ids and \
function_code in self.function_codes and \
(True if self.starting_address is None else starting_address == self.starting_address) and \
(True if self.quantity is None else quantity == self.quantity) and \
address in self.addresses:
return True

Expand Down
4 changes: 2 additions & 2 deletions umodbus/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
pack_exception_pdu, recv_exactly)


def route(self, slave_ids=None, function_codes=None, addresses=None):
def route(self, slave_ids=None, function_codes=None, addresses=None, starting_address=None, quantity=None):
""" A decorator that is used to register an endpoint for a given
rule::
Expand All @@ -24,7 +24,7 @@ def read_single_bit_values(slave_id, address):
:param addresses: A list or set with addresses.
"""
def inner(f):
self.route_map.add_rule(f, slave_ids, function_codes, addresses)
self.route_map.add_rule(f, slave_ids, function_codes, addresses, starting_address, quantity)
return f

return inner
Expand Down

0 comments on commit a5f5f15

Please sign in to comment.