forked from victronenergy/dbus-modbus-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.py
583 lines (447 loc) · 16.1 KB
/
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
from copy import copy
import dbus
from functools import partial
import logging
import os
import time
import traceback
from settingsdevice import SettingsDevice
from vedbus import VeDbusService, VeDbusItemImport, ServiceContext
import __main__
from register import Reg
from utils import *
log = logging.getLogger()
def pack_regs(method, 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 method == 'tcp':
overhead += 2 * (20 + 7) # TCP + MBAP
elif method == 'udp':
overhead += 2 * (8 + 7) # UDP + MBAP
elif 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 > 125 or 2 * (r.base - end) > overhead:
regs.append(rg)
rg = []
rg.append(r)
if rg:
regs.append(rg)
return regs
class BaseDevice:
min_timeout = 0.1
refresh_time = None
age_limit = 4
age_limit_fast = 1
fast_regs = ('/Ac/L1/Power', '/Ac/L2/Power', '/Ac/L3/Power', '/Ac/Power')
allowed_roles = None
def __init__(self):
self.role = None
self.info = {}
self.dbus = None
self.settings = None
self._settings = None
self.dbus_settings = {}
self.info_regs = []
self.data_regs = []
def destroy(self):
if self.dbus:
self._dbus.__del__()
self._dbus = None
self.dbus = None
if self.settings:
self.settings._settings = None
self.settings = None
def read_register(self, reg):
rr = self.modbus.read_holding_registers(reg.base, reg.count,
unit=self.unit)
if rr.isError():
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 rr.isError():
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]) or not reg.time:
if reg.name:
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
self.settings_path = '/Settings/Devices/' + self.get_ident()
def_inst = '%s:%s' % (self.default_role, self.default_instance)
self._settings = {
'instance': [self.settings_path + '/ClassAndVrmInstance', def_inst, 0, 0],
}
self.settings = SettingsDevice(dbus, self._settings, self.setting_changed)
role, self.devinst = self.get_role_instance()
if self.role:
self.settings['instance'] = '%s:%s' % (self.role, self.devinst)
else:
self.role = role
def setting_changed(self, name, old, new):
if self.dbus and name in self.dbus_settings:
self.dbus[self.dbus_settings[name]] = new
if name == 'instance':
role, inst = self.get_role_instance()
if role != self.role:
self.role = role
self.sched_reinit()
return True
if self.dbus:
self.dbus['/DeviceInstance'] = inst
return True
return False
def add_settings(self, settings):
for s in settings.values():
if not s[0].startswith('/Settings/'):
s[0] = self.settings_path + s[0]
self._settings.update(settings)
self.settings.addSettings(settings)
def update_setting(self, setting, path, val):
s = self._settings[setting]
if type(s[1]) == type(s[2]): # valid range limits
if not s[2] <= val <= s[3]:
return False
self.settings[setting] = val
return True
def add_dbus_setting(self, setting, path):
self.dbus_settings[setting] = path
cb = partial(self.update_setting, setting)
self.dbus.add_path(path, self.settings[setting], writeable=True,
onchangecallback=cb)
def get_role_instance(self):
val = self.settings['instance'].split(':')
return val[0], int(val[1])
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 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):
if r.name in self.dbus:
del self.dbus[r.name]
v = copy(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 set_max_age(self, reg):
if reg.name in self.fast_regs:
reg.max_age = self.age_limit_fast
else:
reg.max_age = self.age_limit
def init_dbus(self):
ident = self.get_ident()
svcname = 'com.victronenergy.%s.%s' % (self.role, ident)
self._dbus = VeDbusService(svcname, private_bus())
self.dbus = ServiceContext(self._dbus)
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)
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.refresh_time is not None:
self.dbus.add_path('/RefreshTime', self.refresh_time)
for p in self.info:
self.dbus_add_register(self.info[p])
def init_data_regs(self):
self.data_regs = pack_regs(self.modbus.method, self.data_regs)
for r in self.data_regs:
for rr in r:
if rr.max_age is None:
self.set_max_age(rr)
if rr.name:
self.dbus_add_register(rr)
def update_data_regs(self):
latency = []
for r in self.data_regs:
t = self.read_data_regs(r, self.dbus)
if t:
latency.append(t)
return latency
def post_update(self):
self.dbus.flush()
def device_init(self):
pass
def device_init_late(self):
pass
class ModbusDevice(BaseDevice):
def __init__(self, spec, modbus, model):
super().__init__()
self.spec = spec
self.modbus = modbus.get()
self.unit = spec.unit
self.model = model
self.subdevices = []
self.latency = modbus.timeout
self.need_reinit = False
def destroy(self):
for s in self.subdevices:
s.destroy()
super().destroy()
self.info.clear()
self.modbus.put()
def __eq__(self, other):
return str(self) == str(other)
def __hash__(self):
return hash(self.spec)
def __str__(self):
return str(self.spec)
def connection(self):
if self.modbus.method == 'tcp':
return 'Modbus %s %s' % (self.modbus.method.upper(),
self.modbus.socket.getpeername()[0])
elif self.modbus.method == 'udp':
return 'Modbus %s %s' % (self.modbus.method.upper(),
self.modbus.host)
elif self.modbus.method in ['rtu', 'ascii']:
return 'Modbus %s %s:%d' % (self.modbus.method.upper(),
os.path.basename(self.modbus.port),
self.unit)
return 'Modbus'
def init_device_settings(self, dbus):
if self.settings:
return
self.settings_path = '/Settings/Devices/' + self.get_ident()
settings_root = VeDbusItemImport(dbus, 'com.victronenergy.settings',
self.settings_path)
def_enable = settings_root.exists
super().init_device_settings(dbus)
self.settings.addSettings({
'enabled': [self.settings_path + '/Enabled', def_enable, 0, 1],
})
if self.enabled:
self.settings['enabled'] = 1
else:
self.enabled = self.settings['enabled']
def setting_changed(self, name, old, new):
if super().setting_changed(name, old, new):
return True
if name == 'enabled':
if new != old:
self.set_enabled(bool(new))
return True
return False
def reinit(self):
self.modbus.get()
self.destroy()
self.init(self.settings_dbus, self.enabled)
self.need_reinit = False
def sched_reinit(self):
self.need_reinit = True
def init(self, dbus, enable=True):
self.enabled = enable
self.modbus.timeout = self.timeout
self.device_init()
self.read_info()
self.init_device_settings(dbus)
self.need_reinit = False
if not self.enabled:
self.modbus.put()
return
self.init_dbus()
self.init_data_regs()
self.latfilt = LatencyFilter(self.latency)
self.device_init_late()
self.need_reinit = False
self.dbus.flush()
for s in self.subdevices:
s.init()
def update(self):
if self.need_reinit:
self.reinit()
if not self.enabled:
return
self.modbus.timeout = self.timeout
self.device_update()
self.post_update()
def device_update(self):
latency = self.update_data_regs()
for s in self.subdevices:
s.device_update()
s.post_update()
if latency:
self.latency = self.latfilt.filter(latency)
self.timeout = max(self.min_timeout, self.latency * 4)
def set_enabled(self, enabled):
if enabled == self.enabled:
return
self.enabled = enabled
self.settings['enabled'] = enabled
if enabled:
self.modbus.get()
self.sched_reinit()
class SubDevice(BaseDevice):
inherit_info = (
'/Serial',
'/FirmwareVersion',
'/HardwareVersion',
)
def __init__(self, parent, subid):
super().__init__()
self.parent = parent
self.subid = subid
self.modbus = parent.modbus
self.unit = parent.unit
self.model = parent.model
self.productid = parent.productid
self.productname = parent.productname
def connection(self):
return self.parent.connection()
def get_ident(self):
return self.parent.get_ident() + '_%s' % self.subid
def init(self):
self.device_init()
self.read_info()
for i in self.inherit_info:
if i in self.parent.info:
self.info.setdefault(i, self.parent.info[i])
self.init_device_settings(self.parent.settings_dbus)
self.init_dbus()
self.init_data_regs()
self.device_init_late()
self.dbus.flush()
def sched_reinit(self):
self.parent.sched_reinit()
def device_update(self):
self.update_data_regs()
class LatencyFilter:
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 CustomName:
def device_init_late(self):
super().device_init_late()
self.add_settings({'customname': ['/CustomName', '', 0, 0]})
self.add_dbus_setting('customname', '/CustomName')
class EnergyMeter(ModbusDevice):
role_names = ['grid', 'pvinverter', 'genset', 'acload']
allowed_roles = role_names
default_role = 'grid'
default_instance = 40
nr_phases = None
position = None
def device_init_late(self):
super().device_init_late()
if self.nr_phases is not None:
self.dbus.add_path('/NrOfPhases', self.nr_phases)
if self.role == 'pvinverter' and self.position is None:
self.add_settings({'position': ['/Position', 0, 0, 2]})
self.add_dbus_setting('position', '/Position')
class Tank:
default_role = 'tank'
default_instance = 20
def device_init_late(self):
super().device_init_late()
rvmin = self.raw_value_min
rvmax = self.raw_value_max
self.add_settings({
'capacity': ['/Capacity', 0.2, 0, 1000],
'fluidtype': ['/FluidType', 0, 0, 11],
'rawvalempty': ['/RawValueEmpty', rvmin, rvmin, rvmax],
'rawvalfull': ['/RawValueFull', rvmax, rvmin, rvmax],
})
self.add_dbus_setting('capacity', '/Capacity')
self.add_dbus_setting('fluidtype', '/FluidType')
self.add_dbus_setting('rawvalempty', '/RawValueEmpty')
self.add_dbus_setting('rawvalfull', '/RawValueFull')
self.dbus.add_path('/RawUnit', self.raw_unit)
self.dbus.add_path('/Level', None)
self.dbus.add_path('/Remaining', None)
def device_update(self):
super().device_update()
rvempty = self.settings['rawvalempty']
rvfull = self.settings['rawvalfull']
rvlo = min(rvempty, rvfull)
rvhi = max(rvempty, rvfull)
rval = float(self.dbus['/RawValue'])
rval = min(max(rval, rvlo), rvhi)
level = (rval - rvempty) / (rvfull - rvempty)
remain = level * self.settings['capacity']
self.dbus['/Level'] = 100 * level
self.dbus['/Remaining'] = remain
__all__ = [
'CustomName',
'EnergyMeter',
'ModbusDevice',
'SubDevice',
]