-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEM24RTU_device.py
379 lines (296 loc) · 11.1 KB
/
EM24RTU_device.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from copy import copy
import dbus
from functools import partial
try:
from pymodbus.client.sync import *
except ImportError:
from pymodbus.client import *
from pymodbus.register_read_message import ReadHoldingRegistersResponse
import logging
import os
import time
import traceback
from settingsdevice import SettingsDevice
from vedbus import VeDbusService
import __main__
from register import Reg
from utils import *
log = logging.getLogger()
class ModbusDevice(object):
min_timeout = 0.1
def __init__(self, modbus, unit, model):
self.modbus = modbus.get()
self.unit = unit
self.model = model
self.role = None
self.info = {}
self.dbus = None
self.settings = None
self.err_count = 0
self.latency = modbus.timeout
self.need_reinit = False
def destroy(self):
self.info = {}
if self.dbus:
self.dbus.__del__()
self.dbus = None
if self.settings:
self.settings._settings = None
self.settings = None
self.modbus.put()
def __eq__(self, other):
if isinstance(other, type(self)):
return str(self) == str(other)
if isinstance(other, (str, type(u''))):
return str(self) == other
return False
def __hash__(self):
return hash(str(self))
def __str__(self):
if isinstance(self.modbus, ModbusTcpClient):
return 'tcp:%s:%d:%d' % (self.modbus.host,
self.modbus.port,
self.unit)
elif isinstance(self.modbus, ModbusUdpClient):
return 'udp:%s:%d:%d' % (self.modbus.host,
self.modbus.port,
self.unit)
elif isinstance(self.modbus, ModbusSerialClient):
return '%s:%s:%d:%d' % (self.modbus.method,
os.path.basename(self.modbus.port),
self.modbus.baudrate,
self.unit)
return str(self.modbus)
def connection(self):
if isinstance(self.modbus, (ModbusTcpClient, ModbusUdpClient)):
return 'Modbus %s %s' % (self.method.upper(),
self.modbus.socket.getpeername()[0])
elif isinstance(self.modbus, ModbusSerialClient):
return 'Modbus %s %s:%d' % (self.modbus.method.upper(),
os.path.basename(self.modbus.port),
self.unit)
return 'Modbus'
def read_register(self, reg):
rr = self.modbus.read_holding_registers(reg.base, reg.count,
unit=self.unit)
if not isinstance(rr, ReadHoldingRegistersResponse):
log.error('Error reading register %#04x: %s', reg.base, rr)
raise Exception(rr)
reg.decode(rr.registers)
return reg.value
def write_modbus(self, base, val):
if len(val) == 1:
self.modbus.write_register(base, val[0], unit=self.unit)
else:
self.modbus.write_registers(base, val, unit=self.unit)
def write_register(self, reg, val):
reg.value = val
self.write_modbus(reg.base, reg.encode())
def read_info_regs(self, d):
for reg in self.info_regs:
self.read_register(reg)
d[reg.name] = reg
def read_data_regs(self, regs, d):
now = time.time()
if all(now - r.time < r.max_age for r in regs):
return
start = regs[0].base
count = regs[-1].base + regs[-1].count - start
rr = self.modbus.read_holding_registers(start, count, unit=self.unit)
latency = time.time() - now
if not isinstance(rr, ReadHoldingRegistersResponse):
log.error('Error reading registers %#04x-%#04x: %s',
start, start + count - 1, rr)
raise Exception(rr)
for reg in regs:
base = reg.base - start
end = base + reg.count
if now - reg.time > reg.max_age:
if reg.decode(rr.registers[base:end]):
d[reg.name] = copy(reg) if reg.isvalid() else None
reg.time = now
return latency
def read_info(self):
if not self.info:
self.read_info_regs(self.info)
def init_device_settings(self, dbus):
if self.settings:
return
self.settings_dbus = dbus
path = '/Settings/Devices/' + self.get_ident()
role = self.role or self.default_role
def_inst = '%s:%s' % (self.default_role, self.default_instance)
SETTINGS = {
'instance': [path + '/ClassAndVrmInstance', def_inst, 0, 0],
'customname': [path + '/CustomName', '', 0, 0],
}
self.settings = SettingsDevice(dbus, SETTINGS, self.setting_changed)
self.role, self.devinst = self.get_role_instance()
if self.role == 'pvinverter':
self.settings.addSettings({
'position': [path + '/Position', 0, 0, 2]
})
def setting_changed(self, name, old, new):
if name == 'customname':
self.dbus['/CustomName'] = new
return
if name == 'instance':
role, inst = self.get_role_instance()
if role != self.role:
self.role = role
self.sched_reinit()
return
self.dbus['/DeviceInstance'] = inst
return
if name == 'position':
if self.role == 'pvinverter':
self.dbus['/Position'] = new
return
def get_role_instance(self):
val = self.settings['instance'].split(':')
return val[0], int(val[1])
def reinit(self):
self.destroy()
self.init(self.settings_dbus)
self.need_reinit = False
def sched_reinit(self):
self.need_reinit = True
def get_customname(self):
return self.settings['customname']
def set_customname(self, val):
self.settings['customname'] = val
def customname_changed(self, path, val):
self.set_customname(val)
return True
def role_changed(self, path, val):
if val not in self.allowed_roles:
return False
old, inst = self.get_role_instance()
self.settings['instance'] = '%s:%s' % (val, inst)
return True
def position_changed(self, path, val):
if not 0 <= val <= 2:
return False
self.settings['position'] = val
return True
def dbus_write_register(self, reg, path, val):
try:
val = get_super(Reg, reg)(val)
if callable(reg.write):
return reg.write(val)
if isinstance(reg.write, list):
if val not in reg.write:
return False
if isinstance(reg.write, tuple):
if not reg.write[0] <= val <= reg.write[1]:
return False
self.write_register(reg, val)
return True
except:
traceback.print_exc()
return False
def dbus_add_register(self, r):
v = r if r.isvalid() else None
if r.write:
cb = partial(self.dbus_write_register, r)
self.dbus.add_path(r.name, v, writeable=True, onchangecallback=cb)
else:
self.dbus.add_path(r.name, v)
def pack_regs(self, regs):
rr = []
for r in regs:
rr += r if isinstance(r, list) else [r]
rr.sort(key=lambda r: r.base)
overhead = 5 + 2 # request + response
if self.method == 'tcp':
overhead += 2 * (20 + 7) # TCP + MBAP
elif self.method == 'rtu':
overhead += 2 * (1 + 2) # address + crc
regs = []
rg = [rr.pop(0)]
for r in rr:
end = rg[-1].base + rg[-1].count
nr = r.base + r.count - rg[0].base
if nr > 10 or 2 * (r.base - end) > overhead:
regs.append(rg)
rg = []
rg.append(r)
if rg:
regs.append(rg)
return regs
def init(self, dbus):
self.device_init()
self.read_info()
self.init_device_settings(dbus)
self.data_regs = self.pack_regs(self.data_regs)
ident = self.get_ident()
svcname = 'com.victronenergy.%s.%s' % (self.role, ident)
self.dbus = VeDbusService(svcname, private_bus())
self.dbus.add_path('/Mgmt/ProcessName', __main__.NAME)
self.dbus.add_path('/Mgmt/ProcessVersion', __main__.VERSION)
self.dbus.add_path('/Mgmt/Connection', self.connection())
self.dbus.add_path('/DeviceInstance', self.devinst)
self.dbus.add_path('/ProductId', self.productid)
self.dbus.add_path('/ProductName', self.productname)
self.dbus.add_path('/Model', self.model)
self.dbus.add_path('/Connected', 1)
self.dbus.add_path('/CustomName', self.get_customname(),
writeable=True,
onchangecallback=self.customname_changed)
if self.allowed_roles:
self.dbus.add_path('/AllowedRoles', self.allowed_roles)
self.dbus.add_path('/Role', self.role, writeable=True,
onchangecallback=self.role_changed)
else:
self.dbus.add_path('/Role', self.role)
if self.role == 'pvinverter':
self.dbus.add_path('/Position', self.settings['position'],
writeable=True,
onchangecallback=self.position_changed)
for p in self.info:
self.dbus_add_register(self.info[p])
for r in self.data_regs:
for rr in r:
self.dbus_add_register(rr)
self.latfilt = LatencyFilter(self.latency)
self.device_init_late()
def device_init(self):
pass
def device_init_late(self):
pass
def update(self):
if self.need_reinit:
self.reinit()
latency = []
with self.dbus as d:
for r in self.data_regs:
t = self.read_data_regs(r, d)
if t:
latency.append(t)
if latency:
self.latency = self.latfilt.filter(latency)
self.modbus.timeout = max(self.min_timeout, self.latency * 4)
class LatencyFilter(object):
def __init__(self, val):
self.length = 8
self.pos = 0
self.val = val
self.values = [val] * self.length
def filter(self, values):
self.values[self.pos] = max(values)
self.pos += 1
self.pos &= self.length - 1
val = max(self.values)
if val > self.val:
self.val = 0.25 * self.val + 0.75 * val
else:
self.val = 0.75 * self.val + 0.25 * val
return self.val
class EnergyMeter(ModbusDevice):
allowed_roles = ['grid', 'pvinverter', 'genset', 'acload']
default_role = 'grid'
default_instance = 40
__all__ = [
'EnergyMeter',
'ModbusDevice',
]