-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
165 lines (139 loc) · 6.08 KB
/
main.py
File metadata and controls
165 lines (139 loc) · 6.08 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
import logging
import random
from pymodbus.datastore import ModbusServerContext
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.server.sync import StartTcpServer
from modbus_dynamic_slave_context import ModbusDynamicSlaveContext
from math import floor, sin
FORMAT = ('%(asctime)-15s %(threadName)-15s'
' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
logging.getLogger().setLevel(logging.INFO)
HOST = 'localhost'
PORT = 5020
def run_server(host, port):
store = ModbusDynamicSlaveContext()
# around 1300 rpm
lambda_functions_rpm = [
lambda t: floor(1240 + (t.timestamp() %
120 + random.randint(-2, 5))) * 1000 >> 16,
lambda t: floor(1240 + (t.timestamp() %
120 + random.randint(-2, 5))) * 1000 & 0xFFFF
]
store.setLambda(4, 51300, lambda_functions_rpm)
# around 60c
lambda_functions_oiltemp = [
lambda t: floor(55 + (t.timestamp() %
120 + random.randint(-2, 5)) / 12) * 1000 >> 16,
lambda t: floor(55 + (t.timestamp() %
120 + random.randint(-2, 5)) / 12) * 1000 & 0xFFFF
]
store.setLambda(4, 51460, lambda_functions_oiltemp)
# around 5 bar
lambda_functions_oilpressure = [
lambda t: floor(490 + (t.timestamp() %
120 + random.randint(-2, 5) / 10)) * 1000 >> 16,
lambda t: floor(490 + (t.timestamp() %
120 + random.randint(-2, 5) / 10)) * 1000 & 0xFFFF
]
store.setLambda(4, 51426, lambda_functions_oilpressure)
# constantly running since 2019-11-04
lambda_functions_engine_hours = [
lambda t: floor((t.timestamp() - 1572869165) / 36) >> 16,
lambda t: floor((t.timestamp() - 1572869165) / 36) & 0xFFFF
]
store.setLambda(4, 51360, lambda_functions_engine_hours)
# around 80c
lambda_functions_coolanttemp = [
lambda t: floor(90 - (t.timestamp() %
120 + random.randint(-2, 5)) / 12) * 1000 >> 16,
lambda t: floor(90 - (t.timestamp() %
120 + random.randint(-2, 5)) / 12) * 1000 & 0xFFFF
]
store.setLambda(4, 51414, lambda_functions_coolanttemp)
# around 1 bar
lambda_functions_coolantpressure = [
lambda t: floor(110 - (t.timestamp() %
120 + random.randint(-2, 5)) / 10) * 1000 >> 16,
lambda t: floor(110 - (t.timestamp() %
120 + random.randint(-2, 5)) / 10) * 1000 & 0xFFFF
]
store.setLambda(4, 51606, lambda_functions_coolantpressure)
# around 2 bar
lambda_functions_boostpressure = [
lambda t: floor(190 + (t.timestamp() %
120 + random.randint(-2, 5) / 10)) * 1000 >> 16,
lambda t: floor(190 + (t.timestamp() %
120 + random.randint(-2, 5) / 10)) * 1000 & 0xFFFF
]
store.setLambda(4, 51408, lambda_functions_boostpressure)
# around 20c
lambda_functions_intaketemp = [
lambda t: 0,
lambda t: floor(19500 + t.timestamp() % 1000)
]
store.setLambda(4, 51440, lambda_functions_intaketemp)
# around 50%
lambda_functions_load = [
lambda t: 0,
lambda t: floor((1240 + (t.timestamp() % 120)) / 2600 * 1000)
]
store.setLambda(4, 51422, lambda_functions_load)
# around 350c
lambda_functions_exhaust_temp = [
lambda t: floor(340 + (t.timestamp() %
120 + random.randint(-2, 5)) / 10) * 1000 >> 16,
lambda t: floor(340 + (t.timestamp() %
120 + random.randint(-2, 5)) / 10) * 1000 & 0xFFFF
]
store.setLambda(4, 51442, lambda_functions_exhaust_temp)
# around 60c
lambda_functions_transmissionoiltemp = [
lambda t: floor(50 + (t.timestamp() %
240 + random.randint(-2, 5)) / 12) * 1000 >> 16,
lambda t: floor(50 + (t.timestamp() %
240 + random.randint(-2, 5)) / 12) * 1000 & 0xFFFF
]
store.setLambda(4, 51454, lambda_functions_transmissionoiltemp)
# around 15bar
lambda_functions_transmissionoilpressure = [
lambda t: floor(1490 + (t.timestamp() %
120 + random.randint(-2, 5) / 10)) * 1000 >> 16,
lambda t: floor(1490 + (t.timestamp() %
120 + random.randint(-2, 5) / 10)) * 1000 & 0xFFFF
]
store.setLambda(4, 51452, lambda_functions_transmissionoilpressure)
# using 100l/h since 2019-11-04
lambda_functions_fuelused = [
lambda t: floor((t.timestamp() - 1572869165) / 36 * 1000) >> 16,
lambda t: floor((t.timestamp() - 1572869165) / 36 * 1000) & 0xFFFF
]
store.setLambda(4, 51372, lambda_functions_fuelused)
# around 100l/h
lambda_functions_fuelrate = [
lambda t: floor(97 + (t.timestamp() %
120 + random.randint(-2, 5)) / 20) * 1000 >> 16,
lambda t: floor(97 + (t.timestamp() %
120 + random.randint(-2, 5)) / 20) * 1000 & 0xFFFF
]
store.setLambda(4, 51436, lambda_functions_fuelrate)
# around 2.8 bar
lambda_functions_fuelpressure = [
lambda t: floor(290 - (t.timestamp() %
120 + random.randint(-2, 5) / 10)) * 1000 >> 16,
lambda t: floor(290 - (t.timestamp() %
120 + random.randint(-2, 5) / 10)) * 1000 & 0xFFFF
]
store.setLambda(4, 51432, lambda_functions_fuelpressure)
context = ModbusServerContext(slaves=store, single=True)
identity = ModbusDeviceIdentification()
identity.VendorName = 'Pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/riptideio/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = '2.2.0'
StartTcpServer(context, identity=identity, address=(host, port))
if __name__ == "__main__":
run_server(HOST, PORT)