-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodbus_dynamic_slave_context.py
More file actions
61 lines (45 loc) · 1.83 KB
/
Copy pathmodbus_dynamic_slave_context.py
File metadata and controls
61 lines (45 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import datetime
from typing import Iterable
from pymodbus.interfaces import IModbusSlaveContext
class ModbusDynamicSlaveContext(IModbusSlaveContext):
def __init__(self):
self.store = None
self.zero_mode = False
self.create_empty_stores()
def create_empty_stores(self):
self.store = {'d': {}, 'c': {}, 'i': {}, 'h': {}}
def reset(self):
self.create_empty_stores()
def validate(self, fx, address, count=1):
if not self.zero_mode:
address = address + 1
for i in range(address, address + count):
if i not in self.store[self.decode(fx)]:
return False
return True
def getValues(self, fx, address, count=1):
if not self.validate(fx, address, count):
raise IndexError("One or more addresses are not available")
if not self.zero_mode:
address = address + 1
result = []
for i in range(address, address + count):
result.append(self.store[self.decode(fx)][i](datetime.datetime.now()))
return result
def setValues(self, fx, address, values):
raise NotImplementedError("Values cannot be set on a ModbusDynamicSlaveContext")
def setLambda(self, fx, address, lambda_functions):
"""
:param fx:
:param address:
:param lambda_functions: a lamda function or a list of lambda functions, the lambda function should require one
parameter which is the current time
:return:
"""
if not self.zero_mode:
address = address + 1
if not isinstance(lambda_functions, Iterable):
lambda_functions = [lambda_functions]
for i, f in zip(range(address, address + len(lambda_functions)), lambda_functions):
self.store[self.decode(fx)][i] = f
return self