forked from kwindrem/GuiMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtTransferSwitch.py
executable file
·423 lines (368 loc) · 15.2 KB
/
ExtTransferSwitch.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
#!/usr/bin/env python
# This program integrates an external transfer switch ahead of the single AC input
# of a MultiPlus or Quattro inverter/charger.
#
# A new type of digital input is defined to provide select grid or generator input profiles
#
# When the external transfer switch changes between grid and generator the data for that input must be switched between
# grid and generator settings
#
# These two sets of settings are stored in dbus Settings.
# When the transfer switch digital input changes, this program switches
# the Multiplus settings between these two stored values
# When the user changes the settings, the grid or generator-specific Settings are updated
#
# In order to function, one of the digital inputs must be set to External AC Transfer Switch
# This input should be connected to a contact closure on the external transfer switch to indicate
# which of it's sources is switched to its output
#
# For Quattro, the /Settings/TransferSwitch/TransferSwitchOnAc2 tells this program where the transfer switch is connected:
# 0 if connected to AC 1 In
# 1 if connected to AC 2 In
import platform
import argparse
import logging
import sys
import subprocess
import os
import time
import dbus
from gi.repository import GLib
# add the path to our own packages for import
sys.path.insert(1, "/data/SetupHelper/velib_python")
from vedbus import VeDbusService
from ve_utils import wrap_dbus_value
from settingsdevice import SettingsDevice
dbusSettingsPath = "com.victronenergy.settings"
dbusSystemPath = "com.victronenergy.system"
class Monitor:
def getVeBusObjects (self):
vebusService = ""
# invalidate all local parameters if transfer switch is not active
if not self.transferSwitchActive:
# release generator override if it's still active
try:
if self.remoteGeneratorSelectedItem != None:
self.remoteGeneratorSelectedItem.SetValue (wrap_dbus_value (0))
except:
logging.error ("could not release /Ac/Control/RemoteGeneratorSelected")
pass
self.remoteGeneratorSelectedItem = None
self.remoteGeneratorSelectedLocalValue = -1
self.dbusOk = False
self.numberOfAcInputs = 0
self.stopWhenAcAvailableObj = None
self.stopWhenAcAvailableFpObj = None
self.acInputTypeObj = None
self.veBusService = ""
self.transferSwitchLocation = 0
return
try:
obj = self.theBus.get_object (dbusSystemPath, '/VebusService')
vebusService = obj.GetText ()
except:
if self.dbusOk:
logging.info ("Multi/Quattro disappeared - /VebusService invalid")
self.veBusService = ""
self.dbusOk = False
self.numberOfAcInputs = 0
self.acInputTypeObj = None
if vebusService == "---":
if self.veBusService != "":
logging.info ("Multi/Quattro disappeared")
self.veBusService = ""
self.dbusOk = False
self.numberOfAcInputs = 0
elif self.veBusService == "" or vebusService != self.veBusService:
self.veBusService = vebusService
try:
self.numberOfAcInputs = self.theBus.get_object (vebusService, "/Ac/NumberOfAcInputs").GetValue ()
except:
self.numberOfAcInputs = 0
try:
self.remoteGeneratorSelectedItem = self.theBus.get_object (vebusService,
"/Ac/Control/RemoteGeneratorSelected")
except:
self.remoteGeneratorSelectedItem = None
self.remoteGeneratorSelectedLocalValue = -1
if self.numberOfAcInputs == 0:
self.dbusOk = False
elif self.numberOfAcInputs == 2:
logging.info ("discovered Quattro at " + vebusService)
elif self.numberOfAcInputs == 1:
logging.info ("discovered Multi at " + vebusService)
try:
self.currentLimitObj = self.theBus.get_object (vebusService, "/Ac/ActiveIn/CurrentLimit")
self.currentLimitIsAdjustableObj = self.theBus.get_object (vebusService, "/Ac/ActiveIn/CurrentLimitIsAdjustable")
except:
logging.error ("current limit dbus setup failed - changes can't be made")
self.dbusOk = False
# check to see where the transfer switch is connected
if self.numberOfAcInputs == 0:
transferSwitchLocation = 0
elif self.numberOfAcInputs == 1:
transferSwitchLocation = 1
elif self.DbusSettings['transferSwitchOnAc2'] == 1:
transferSwitchLocation = 2
else:
transferSwitchLocation = 1
# if changed, trigger refresh of object pointers
if transferSwitchLocation != self.transferSwitchLocation:
if transferSwitchLocation != 0:
logging.info ("Transfer switch is on AC %d in" % transferSwitchLocation)
self.transferSwitchLocation = transferSwitchLocation
self.stopWhenAcAvailableObj = None
self.stopWhenAcAvailableFpObj = None
try:
if self.transferSwitchLocation == 2:
self.acInputTypeObj = self.theBus.get_object (dbusSettingsPath, "/Settings/SystemSetup/AcInput2")
else:
self.acInputTypeObj = self.theBus.get_object (dbusSettingsPath, "/Settings/SystemSetup/AcInput1")
self.dbusOk = True
except:
self.dbusOk = False
logging.error ("AC input dbus setup failed - changes can't be made")
# set up objects for stop when AC available
# there's one for "Generator" and one for "FischerPanda"
# ignore errors if these aren't present
try:
if self.transferSwitchLocation == 2:
self.stopWhenAcAvailableObj = self.theBus.get_object (dbusSettingsPath, "/Settings/Generator0/StopWhenAc2Available")
else:
self.stopWhenAcAvailableObj = self.theBus.get_object (dbusSettingsPath, "/Settings/Generator0/StopWhenAc1Available")
except:
self.stopWhenAcAvailableObj = None
# first try new settings
try:
if self.transferSwitchLocation == 2:
self.stopWhenAcAvailableFpObj = self.theBus.get_object (dbusSettingsPath, "/Settings/Generator1/StopWhenAc2Available")
else:
self.stopWhenAcAvailableFpObj = self.theBus.get_object (dbusSettingsPath, "/Settings/Generator1/StopWhenAc1Available")
# next try old settings
except:
try:
if self.transferSwitchLocation == 2:
self.stopWhenAcAvailableFpObj = self.theBus.get_object (dbusSettingsPath, "/Settings/FischerPanda0/StopWhenAc2Available")
else:
self.stopWhenAcAvailableFpObj = self.theBus.get_object (dbusSettingsPath, "/Settings/FischerPanda0/StopWhenAc1Available")
except:
self.stopWhenAcAvailableFpObj = None
def updateTransferSwitchState (self):
inputInvalid = False
try:
if self.transferSwitchActive:
state = self.transferSwitchStateObj.GetValue ()
if state == 12: # 12 is the on generator value
self.onGenerator = True
elif state == 13: # 13 is the on grid value
self.onGenerator = False
# other value indicates the selected digital input is assigned to a different function
else:
inputInvalid = True
# digital input not active
# search for a new one only every 10 seconds to avoid unnecessary processing
elif self.tsInputSearchDelay >= 10:
newInputService = ""
for service in self.theBus.list_names():
# found a digital input service, now check the for valid state value
if service.startswith ("com.victronenergy.digitalinput"):
self.transferSwitchStateObj = self.theBus.get_object (service, '/State')
state = self.transferSwitchStateObj.GetValue()
# found it!
if state == 12 or state == 13:
newInputService = service
break
# found new service - set up to use it's values
if newInputService != "":
logging.info ("discovered transfer switch digital input service at %s", newInputService)
self.transferSwitchActive = True
elif self.transferSwitchActive:
logging.info ("Transfer switch digital input service NOT found")
self.transferSwitchActive = False
# any exception indicates the selected digital input is no longer active
except:
inputInvalid = True
if inputInvalid:
if self.transferSwitchActive:
logging.info ("Transfer switch digital input no longer valid")
self.transferSwitchActive = False
if self.transferSwitchActive:
self.tsInputSearchDelay = 0
else:
self.onGenerator = False
# if serch delay timer is active, increment it now
if self.tsInputSearchDelay < 10:
self.tsInputSearchDelay += 1
else:
self.tsInputSearchDelay = 0
def transferToGrid (self):
if self.dbusOk:
logging.info ("switching to grid settings")
# save current values for restore when switching back to generator
try:
self.DbusSettings['generatorCurrentLimit'] = self.currentLimitObj.GetValue ()
except:
logging.error ("dbus error generator AC input current limit not saved switching to grid")
try:
self.acInputTypeObj.SetValue (self.DbusSettings['gridInputType'])
except:
logging.error ("dbus error AC input type not changed to grid")
try:
if self.currentLimitIsAdjustableObj.GetValue () == 1:
self.currentLimitObj.SetValue (wrap_dbus_value (self.DbusSettings['gridCurrentLimit']))
else:
logging.warning ("Input current limit not adjustable - not changed")
except:
logging.error ("dbus error AC input current limit not changed switching to grid")
try:
if self.stopWhenAcAvailableObj != None:
self.stopWhenAcAvailableObj.SetValue (self.DbusSettings['stopWhenAcAvaiable'])
if self.stopWhenAcAvailableFpObj != None:
self.stopWhenAcAvailableFpObj.SetValue (self.DbusSettings['stopWhenAcAvaiableFp'])
except:
logging.error ("stopWhenAcAvailable update not changed when switching to grid")
def transferToGenerator (self):
if self.dbusOk:
logging.info ("switching to generator settings")
# save current values for restore when switching back to grid
try:
inputType = self.acInputTypeObj.GetValue ()
# grid input type can only be either 1 (grid) or 3 (shore)
# patch this up to prevent issues later
if inputType == 2:
logging.warning ("grid input can not be generator - setting to grid")
inputType = 1
self.DbusSettings['gridInputType'] = inputType
except:
logging.error ("dbus error AC input type not saved when switching to generator")
try:
self.DbusSettings['gridCurrentLimit'] = self.currentLimitObj.GetValue ()
except:
logging.error ("dbus error AC input current limit not saved when switching to generator")
try:
if self.stopWhenAcAvailableObj != None:
self.DbusSettings['stopWhenAcAvaiable'] = self.stopWhenAcAvailableObj.GetValue ()
else:
self.DbusSettings['stopWhenAcAvaiable'] = 0
if self.stopWhenAcAvailableFpObj != None:
self.DbusSettings['stopWhenAcAvaiableFp'] = self.stopWhenAcAvailableFpObj.GetValue ()
else:
self.DbusSettings['stopWhenAcAvaiableFp'] = 0
except:
logging.error ("dbus error stop when AC available settings not saved when switching to generator")
try:
self.acInputTypeObj.SetValue (2)
except:
logging.error ("dbus error AC input type not changed when switching to generator")
try:
if self.currentLimitIsAdjustableObj.GetValue () == 1:
self.currentLimitObj.SetValue (wrap_dbus_value (self.DbusSettings['generatorCurrentLimit']))
else:
logging.warning ("Input current limit not adjustable - not changed")
except:
logging.error ("dbus error AC input current limit not changed when switching to generator")
try:
if self.stopWhenAcAvailableObj != None:
self.stopWhenAcAvailableObj.SetValue (0)
if self.stopWhenAcAvailableFpObj != None:
self.stopWhenAcAvailableFpObj.SetValue (0)
except:
logging.error ("stopWhenAcAvailable update not changed switching to generator")
def background (self):
##startTime = time.time()
self.updateTransferSwitchState ()
self.getVeBusObjects ()
# skip processing if any dbus paramters were not initialized properly
if self.dbusOk and self.transferSwitchActive:
# process transfer switch state change
if self.lastOnGenerator != None and self.onGenerator != self.lastOnGenerator:
if self.onGenerator:
self.transferToGenerator ()
else:
self.transferToGrid ()
self.lastOnGenerator = self.onGenerator
elif self.onGenerator:
self.transferToGrid ()
# update main VE.Bus RemoteGeneratorSelected which is used to enable grid charging
# if renewable energy is turned on
if not self.dbusOk or not self.onGenerator:
newRemoteGeneratorSelectedLocalValue = 0
else:
newRemoteGeneratorSelectedLocalValue = 1
if self.remoteGeneratorSelectedItem == None:
self.remoteGeneratorSelectedLocalValue = -1
elif newRemoteGeneratorSelectedLocalValue != self.remoteGeneratorSelectedLocalValue:
try:
self.remoteGeneratorSelectedItem.SetValue (wrap_dbus_value (newRemoteGeneratorSelectedLocalValue))
except:
logging.error ("could not set /Ac/Control/RemoteGeneratorSelected")
pass
self.remoteGeneratorSelectedLocalValue = newRemoteGeneratorSelectedLocalValue
##stopTime = time.time()
##print ("#### background time %0.3f" % (stopTime - startTime))
return True
def __init__(self):
self.theBus = dbus.SystemBus()
self.onGenerator = False
self.veBusService = ""
self.lastVeBusService = ""
self.acInputTypeObj = None
self.numberOfAcInputs = 0
self.currentLimitObj = None
self.currentLimitIsAdjustableObj = None
self.stopWhenAcAvailableObj = None
self.stopWhenAcAvailableFpObj = None
self.remoteGeneratorSelectedItem = None
self.remoteGeneratorSelectedLocalValue = -1
self.transferSwitchStateObj = None
self.extTransferDigInputName = "External AC Input transfer switch" # must match name set in dbus_digitalInputs.py !!!!!
self.lastOnGenerator = None
self.transferSwitchActive = False
self.dbusOk = False
self.transferSwitchLocation = 0
self.tsInputSearchDelay = 99 # allow serch to occur immediately
# create / attach local settings
settingsList = {
'gridCurrentLimit': [ '/Settings/TransferSwitch/GridCurrentLimit', 0.0, 0.0, 0.0 ],
'generatorCurrentLimit': [ '/Settings/TransferSwitch/GeneratorCurrentLimit', 0.0, 0.0, 0.0 ],
'gridInputType': [ '/Settings/TransferSwitch/GridType', 0, 0, 0 ],
'stopWhenAcAvaiable': [ '/Settings/TransferSwitch/StopWhenAcAvailable', 0, 0, 0 ],
'stopWhenAcAvaiableFp': [ '/Settings/TransferSwitch/StopWhenAcAvailableFp', 0, 0, 0 ],
'transferSwitchOnAc2': [ '/Settings/TransferSwitch/TransferSwitchOnAc2', 0, 0, 0 ],
}
self.DbusSettings = SettingsDevice(bus=self.theBus, supportedSettings=settingsList,
timeout = 10, eventCallback=None )
# grid input type should be either 1 (grid) or 3 (shore)
# patch this up to prevent issues later
if self.DbusSettings['gridInputType'] == 2:
logging.warning ("grid input type was generator - resetting to grid")
self.DbusSettings['gridInputType'] = 1
GLib.timeout_add (1000, self.background)
return None
def main():
from dbus.mainloop.glib import DBusGMainLoop
# set logging level to include info level entries
logging.basicConfig(level=logging.INFO)
# Have a mainloop, so we can send/receive asynchronous calls to and from dbus
DBusGMainLoop(set_as_default=True)
installedVersion = "(no version installed)"
versionFile = "/etc/venus/installedVersion-GuiMods"
if os.path.exists (versionFile):
try:
proc = subprocess.Popen (["cat", versionFile], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except:
pass
else:
proc.wait()
# convert from binary to string
stdout, stderr = proc.communicate ()
stdout = stdout.decode ().strip ()
stderr = stderr.decode ().strip ()
returnCode = proc.returncode
if proc.returncode == 0:
installedVersion = stdout
logging.info (">>>>>>>>>>>>>>>> ExtTransferSwitch starting " + installedVersion + " <<<<<<<<<<<<<<<<")
Monitor ()
mainloop = GLib.MainLoop()
mainloop.run()
main()