-
Notifications
You must be signed in to change notification settings - Fork 1
/
Eastron.py
337 lines (271 loc) · 11.5 KB
/
Eastron.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
from copy import copy
import logging
import time
import device
import probe
from register import *
import time
from settingsdevice import SettingsDevice
log = logging.getLogger()
class Reg_f32b(Reg_num):
coding = ('>f', '>2H')
count = 2
rtype = float
nr_phases = [ 0, 1, 3, 3 ]
phase_configs = [
'undefined',
'1p2w',
'3p3w',
'3p4w',
]
_kwh = lambda v: (str(round(v, 3)) + ' kWh')
_kwh2 = lambda p, v: (str(round(v, 3)) + ' kWh')
_a = lambda v: (str(round(v, 1)) + 'A')
_w = lambda v: (str(round(v, 1)) + 'W')
_v = lambda v: (str(round(v, 1)) + 'V')
_hz = lambda v: (str(round(v, 1)) + 'Hz')
MAXREFRESHRATE = 10
class ModbusDeviceEastron():
def init_device_settings(self, dbus):
super().init_device_settings(dbus)
self.interval_item = self.settings.addSetting(
self.settings_path + '/RefreshRate', 1, 1, MAXREFRESHRATE,
callback=self.refresh_setting_changed)
self.age_limit_fast = 1 / self.interval_item.get_value()
def device_init_late(self):
super().device_init_late()
self.dbus.add_path('/RefreshRate', self.interval_item.get_value(),
writeable=True,
onchangecallback=self.interval_changed)
self.dbus.add_path('/RefreshTime', int(1000 / self.interval_item.get_value()),
writeable=False)
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_input_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]):
d[reg.name] = copy(reg) if reg.isvalid() else None
reg.time = now
self.update_energy()
return latency
def get_ident(self):
return 'ea_%s' % self.info['/Serial']
def dbus_write_register(self, reg, path, val):
super().dbus_write_register(reg, path, val)
self.sched_reinit()
def interval_changed(self, path, val):
if not 1 <= val <= MAXREFRESHRATE:
return False
self.interval_item.set_value(val)
if self.age_limit_fast != 1/val:
self.sched_reinit()
return True
def refresh_setting_changed(self, service, path, value):
if self.age_limit_fast != 1 / value['Value']:
self.sched_reinit()
return
class Eastron_1phase(ModbusDeviceEastron, device.CustomName, device.EnergyMeter):
phase = 0
def phase_regs(self, n):
return [
Reg_f32b(0x0000, '/Ac/L%d/Voltage' % n, 1, _v),
Reg_f32b(0x0006, '/Ac/L%d/Current' % n, 1, _a),
Reg_f32b(0x000c, '/Ac/L%d/Power' % n, 1, _w),
Reg_f32b(0x0048, '/Ac/L%d/Energy/Forward' % n, 1, _kwh),
Reg_f32b(0x004a, '/Ac/L%d/Energy/Reverse' % n, 1, _kwh),
]
def device_init(self):
self.info_regs = [
Reg_u32b(0xfc00, '/Serial'),
Reg_u16 (0xfc02, '/HardwareVersion'),
Reg_u16 (0xfc03, '/FirmwareVersion'),
]
self.read_info()
regs = [
Reg_f32b(0x000c, '/Ac/Power', 1, _w),
Reg_f32b(0x0006, '/Ac/Current', 1, _a),
Reg_f32b(0x0046, '/Ac/Frequency', 1, _hz),
Reg_f32b(0x0048, '/Ac/Energy/Forward', 1, _kwh),
Reg_f32b(0x004a, '/Ac/Energy/Reverse', 1, _kwh),
]
regs += self.phase_regs(self.phase+1)
self.data_regs = regs
self.nr_phases = self.phase+1
def device_init_late(self):
super().device_init_late()
self.dbus.add_path('/Phase', self.phase_item.get_value(),
writeable=True,
onchangecallback=self.phase_changed)
if self.phase != self.phase_item.get_value():
self.phase = self.phase_item.get_value()
self.reinit()
def init_device_settings(self, dbus):
super().init_device_settings(dbus)
self.phase_item = self.settings.addSetting(
self.settings_path + '/Phase', 0, 0, 2,
callback=self.phase_setting_changed)
def phase_setting_changed(self, service, path, value):
if self.phase != value['Value']:
self.phase = value['Value']
self.sched_reinit()
return
def phase_changed(self, path, val):
if not 0 <= val <= 2:
return False
self.phase_item.set_value(val)
if self.phase != val:
self.phase = val
self.sched_reinit()
return True
def update_energy(self):
pass
class Eastron_3phase(ModbusDeviceEastron, device.CustomName, device.EnergyMeter):
deviceEnergyForward = None
deviceEnergyReverse = None
def phase_regs(self, n):
s = 2 * (n - 1)
return [
Reg_f32b(0x0000 + s, '/Ac/L%d/Voltage' % n, 1, _v),
Reg_f32b(0x0006 + s, '/Ac/L%d/Current' % n, 1, _a),
Reg_f32b(0x000c + s, '/Ac/L%d/Power' % n, 1, _w),
Reg_f32b(0x015a + s, '/Ac/L%d/Energy/Forward' % n, 1, _kwh),
Reg_f32b(0x0160 + s, '/Ac/L%d/Energy/Reverse' % n, 1, _kwh),
]
def device_init(self):
self.info_regs = [
Reg_u32b(0xfc00, '/Serial'),
Reg_u16 (0xfc02, '/HardwareVersion'),
Reg_u16 (0xfc03, '/FirmwareVersion'),
Reg_f32b(0x000a, '/PhaseConfig', text=phase_configs, write=(0, 3)),
]
self.read_info()
phases = nr_phases[int(self.info['/PhaseConfig'])]
regs = [
Reg_f32b(0x0034, '/Ac/Power', 1, _w, onchange=self.power_balance),
Reg_f32b(0x0030, '/Ac/Current', 1, _a),
Reg_f32b(0x0046, '/Ac/Frequency', 1, _hz),
Reg_f32b(0x0048, '/Ac/Energy/ForwardSum', 1, _kwh, onchange=self.deviceEnergyForward_changed),
Reg_f32b(0x004a, '/Ac/Energy/ReverseSum', 1, _kwh, onchange=self.deviceEnergyReverse_changed),
]
for n in range(1, phases + 1):
regs += self.phase_regs(n)
self.data_regs = regs
self.nr_phases = phases
self.last_time = time.time()
self.energy_time = time.time() - 4
self.balancing_time = time.time()
self.last_power = 0
def device_init_late(self):
super().device_init_late()
self.dbus.add_path('/Ac/Energy/ForwardBalancing', self.forwardBalancing_item.get_value(), writeable=True, gettextcallback=_kwh2)
self.dbus.add_path('/Ac/Energy/ReverseBalancing', self.reverseBalancing_item.get_value(), writeable=True, gettextcallback=_kwh2)
self.dbus.add_path('/Ac/Energy/Forward', None, writeable=True, gettextcallback=_kwh2)
self.dbus.add_path('/Ac/Energy/Reverse', None, writeable=True, gettextcallback=_kwh2)
self.dbus.add_path('/EnergyCounter', self.energyCounter_item.get_value(),writeable=True, onchangecallback=self.energyCounter_changed)
self.last_time = time.time()
def init_device_settings(self, dbus):
super().init_device_settings(dbus)
self.energyCounter_item = self.settings.addSetting(self.settings_path + '/EnergyCounter', 0, 0, 2)
self.forwardBalancing_item = self.settings.addSetting(self.settings_path + '/ForwardBalancing', 0.0, 0, 0)
self.reverseBalancing_item = self.settings.addSetting(self.settings_path + '/ReverseBalancing', 0.0, 0, 0)
def power_balance(self, reg):
deltaT = time.time() - self.last_time
if (self.last_power > 0):
self.dbus['/Ac/Energy/ForwardBalancing'] = float(self.dbus['/Ac/Energy/ForwardBalancing']) + (self.last_power * deltaT)/3600000
else:
self.dbus['/Ac/Energy/ReverseBalancing'] = float(self.dbus['/Ac/Energy/ReverseBalancing']) + (abs(self.last_power) * deltaT)/3600000
self.last_time = time.time()
self.last_power = float(copy(reg)) if reg.isvalid() else 0
def deviceEnergyForward_changed(self, reg):
self.deviceEnergyForward = float(copy(reg)) if reg.isvalid() else 0
def deviceEnergyReverse_changed(self, reg):
self.deviceEnergyReverse = float(copy(reg)) if reg.isvalid() else 0
def update_energy(self):
if time.time() - self.energy_time >= 5:
if self.dbus['/EnergyCounter'] == 1:
self.dbus['/Ac/Energy/Forward'] = self.dbus['/Ac/Energy/ForwardBalancing']
self.dbus['/Ac/Energy/Reverse'] = self.dbus['/Ac/Energy/ReverseBalancing']
elif self.dbus['/EnergyCounter'] == 2:
if self.deviceEnergyForward is not None and self.deviceEnergyReverse is not None:
forward_result = round(self.deviceEnergyForward - self.deviceEnergyReverse, 5)
self.dbus['/Ac/Energy/Forward'] = forward_result
self.dbus['/Ac/Energy/Reverse'] = -forward_result
else:
self.dbus['/Ac/Energy/Forward'] = self.deviceEnergyForward
self.dbus['/Ac/Energy/Reverse'] = self.deviceEnergyReverse
self.energy_time = time.time()
if time.time() - self.balancing_time >= 300:
self.forwardBalancing_item.set_value(self.dbus['/Ac/Energy/ForwardBalancing'])
self.reverseBalancing_item.set_value(self.dbus['/Ac/Energy/ReverseBalancing'])
self.balancing_time = time.time()
def energyCounter_changed(self, path, val):
if not 0 <= val <= 2:
return False
self.energyCounter_item.set_value(val)
return True
class Eastron_SDM72DM(Eastron_3phase):
productid = 0xb023 # id assigned by Victron Support
productname = 'Eastron SDM72D-M'
min_timeout = 0.5
class Eastron_SDM72DM2(Eastron_3phase):
productid = 0xb023 # id assigned by Victron Support
productname = 'Eastron SDM72D-M-2'
min_timeout = 0.5
class Eastron_SDM120M(Eastron_1phase):
productid = 0xb023 # id assigned by Victron Support
productname = 'Eastron SDM120-M'
min_timeout = 0.5
class Eastron_SDM230M(Eastron_1phase):
productid = 0xb023 # id assigned by Victron Support
productname = 'Eastron SDM230-Modbus'
min_timeout = 0.5
class Eastron_SDM630M(Eastron_3phase):
productid = 0xb023 # id assigned by Victron Support
productname = 'Eastron SDM630-Modbus'
min_timeout = 0.5
class Eastron_SDM630MCT(Eastron_3phase):
productid = 0xb023 # id assigned by Victron Support
productname = 'Eastron SDM630-MCT'
min_timeout = 0.5
models = {
132: {
'model': 'SDM72DM',
'handler': Eastron_SDM72DM,
},
137: {
'model': 'SDM72DM2',
'handler': Eastron_SDM72DM2,
},
32: {
'model': 'SDM120M',
'handler': Eastron_SDM120M,
},
43: {
'model': 'SDM230Modbus',
'handler': Eastron_SDM230M,
},
112: {
'model': 'SDM630Modbus',
'handler': Eastron_SDM630M,
},
121: {
'model': 'SDM630MCT',
'handler': Eastron_SDM630MCT,
},
}
probe.add_handler(probe.ModelRegister(Reg_u16(0xfc02), models,
methods=['rtu','tcp'],
rates=[9600, 19200, 38400],
units=[1,2]))