-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontroller.go
More file actions
510 lines (472 loc) · 17.2 KB
/
Copy pathcontroller.go
File metadata and controls
510 lines (472 loc) · 17.2 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
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
package sensonet
import (
"fmt"
"time"
)
type Controller struct {
conn *Connection
logger Logger
homesCache Cacheable[[]Home]
systemsCache Cacheable[AllSystems]
systemDevicesCache Cacheable[AllSystemDevices]
systemMpcDataCache Cacheable[AllSystemMpcData]
currentQuickmode string
quickmodeStarted time.Time
quickmodeStopped time.Time
quickModeExpiresAt string
}
const CACHE_DURATION_HOMES = 1800
const CACHE_DURATION_SYSTEMS = 90
const CACHE_DURATION_DEVICES = 1800
const CACHE_DURATION_MPCDATA = 90
// NewController creates a new Sensonet controller.
func NewController(conn *Connection, opts ...CtrlOption) (*Controller, error) {
ctrl := &Controller{
conn: conn,
quickmodeStarted: time.Now(),
quickmodeStopped: time.Now().Add(-2 * time.Minute), // time stamp is set in the past so that first call of refreshCurrentQuickMode() changes currentQuickmode if necessary
quickModeExpiresAt: "",
}
for _, opt := range opts {
opt(ctrl)
}
ctrl.homesCache = ResettableCached(func() ([]Home, error) {
//var res Homes
res, err := ctrl.conn.GetHomes()
return res, err
}, CACHE_DURATION_HOMES*time.Second)
ctrl.systemsCache = ResettableCached(func() (AllSystems, error) {
var res AllSystems
homes, err := ctrl.homesCache.Get()
for i, home := range homes {
var systemAndStatus SystemAndStatus
systemAndStatus.SystemId = home.SystemID
systemAndStatus.SystemStatus, err = ctrl.conn.GetSystem(home.SystemID)
if err != nil {
return res, err
}
if len(res.SystemsAndStatus) <= i {
res.SystemsAndStatus = append(res.SystemsAndStatus, systemAndStatus)
} else {
res.SystemsAndStatus[i] = systemAndStatus
}
// For the beginning, currentQuickMode is only calculated from the system status of Homes[0].SystemId
if i == 0 {
ctrl.refreshCurrentQuickMode(&systemAndStatus.SystemStatus)
}
}
return res, err
}, CACHE_DURATION_SYSTEMS*time.Second)
ctrl.systemDevicesCache = ResettableCached(func() (AllSystemDevices, error) {
var res AllSystemDevices
homes, err := ctrl.homesCache.Get()
for i, home := range homes {
var systemDevicesAndSystemId SystemDevicesAndSystemId
systemDevicesAndSystemId.SystemId = home.SystemID
systemDevicesAndSystemId.SystemDevices, err = ctrl.conn.GetSystemDevices(home.SystemID)
if err != nil {
return res, err
}
if len(res.SystemDevicesAndSystemId) <= i {
res.SystemDevicesAndSystemId = append(res.SystemDevicesAndSystemId, systemDevicesAndSystemId)
} else {
res.SystemDevicesAndSystemId[i] = systemDevicesAndSystemId
}
}
return res, err
}, CACHE_DURATION_DEVICES*time.Second)
ctrl.systemMpcDataCache = ResettableCached(func() (AllSystemMpcData, error) {
var res AllSystemMpcData
homes, err := ctrl.homesCache.Get()
for i, home := range homes {
var systemMpcData SystemMpcData
systemMpcData.SystemId = home.SystemID
systemMpcData.MpcData.Devices, err = ctrl.conn.GetMpcData(home.SystemID)
if err != nil {
return res, err
}
if len(res.SystemMpcData) <= i {
res.SystemMpcData = append(res.SystemMpcData, systemMpcData)
} else {
res.SystemMpcData[i] = systemMpcData
}
}
return res, err
}, CACHE_DURATION_MPCDATA*time.Second)
return ctrl, nil
}
func (c *Controller) debug(fmt string, arg ...any) {
if c.logger != nil {
c.logger.Printf(fmt, arg...)
}
}
// Returns all "homes" that belong to the current user under the myVaillant portal
func (c *Controller) GetHomes() ([]Home, error) {
homes, err := c.homesCache.Get()
if err != nil {
return nil, err
}
if len(homes) < 1 {
return nil, fmt.Errorf("error: no homes")
}
return homes, nil
}
// Returns the system report (state, properties and configuration) for a specific systemId
func (c *Controller) GetSystem(systemId string) (SystemStatus, error) {
var systemStatus SystemStatus
systems, err := c.systemsCache.Get()
if err != nil {
return systemStatus, err
}
for _, sys := range systems.SystemsAndStatus {
if sys.SystemId == systemId {
return sys.SystemStatus, nil
}
}
return systemStatus, fmt.Errorf("no data found for system %s", systemId)
}
// Returns the device data for given criteria
func (c *Controller) GetDeviceData(systemId string, whichDevices int) ([]DeviceAndInfo, error) {
var devices []DeviceAndInfo
systemDevices, err := c.GetSystemDevices(systemId)
if err != nil {
return devices, err
}
var deviceAndInfo DeviceAndInfo
if systemDevices.PrimaryHeatGenerator.DeviceUUID != "" && (whichDevices == DEVICES_PRIMARY_HEATER || whichDevices == DEVICES_ALL) {
deviceAndInfo.Device = systemDevices.PrimaryHeatGenerator
deviceAndInfo.Info = "primary_heat_generator"
devices = append(devices, deviceAndInfo)
}
if whichDevices == DEVICES_SECONDARY_HEATER || whichDevices == DEVICES_ALL {
for _, secHeatGen := range systemDevices.SecondaryHeatGenerators {
deviceAndInfo.Device = secHeatGen
deviceAndInfo.Info = "secondary_heat_generator"
devices = append(devices, deviceAndInfo)
}
}
if systemDevices.ElectricBackupHeater.DeviceUUID != "" && (whichDevices == DEVICES_BACKUP_HEATER || whichDevices == DEVICES_ALL) {
deviceAndInfo.Device = systemDevices.ElectricBackupHeater
deviceAndInfo.Info = "electric_backup_heater"
devices = append(devices, deviceAndInfo)
}
return devices, nil
}
// Returns the energy data for systemId, deviceUuid and other given criteria
func (c *Controller) GetEnergyData(systemId, deviceUuid, operationMode, energyType, resolution string, startDate, endDate time.Time) (EnergyData, error) {
return c.conn.GetEnergyData(systemId, deviceUuid, operationMode, energyType, resolution, startDate, endDate)
}
// Returns the mpc data for systemId
func (c *Controller) GetMpcData(systemId string) ([]MpcDevice, error) {
var mpcData MpcData
allSystemMpcData, err := c.systemMpcDataCache.Get()
if err != nil {
return mpcData.Devices, err
}
for _, systemMpcData := range allSystemMpcData.SystemMpcData {
if systemMpcData.SystemId == systemId {
return systemMpcData.MpcData.Devices, err
}
}
return mpcData.Devices, fmt.Errorf("no mpc data found for system %s", systemId)
}
// Returns the system devices for a specific systemId
func (c *Controller) GetSystemDevices(systemId string) (SystemDevices, error) {
var systemDevices SystemDevices
allSystemDevices, err := c.systemDevicesCache.Get()
if err != nil {
return systemDevices, err
}
for _, sys := range allSystemDevices.SystemDevicesAndSystemId {
if sys.SystemId == systemId {
return sys.SystemDevices, nil
}
}
return systemDevices, fmt.Errorf("no data found for system %s", systemId)
}
// Returns the current power consumption for systemId
func (c *Controller) GetSystemCurrentPower(systemId string) (float64, error) {
mpcDevices, err := c.GetMpcData(systemId)
if err != nil || len(mpcDevices) < 1 {
return -1.0, err
}
totalPower := 0.0
for _, dev := range mpcDevices {
totalPower = totalPower + dev.CurrentPower
}
return totalPower, nil
}
// Returns the current power consumption and product name for deviceUuid. If "All" is given as deviceUuid, then the function return the power consumption and product name for all devices of systemId
func (c *Controller) GetDeviceCurrentPower(systemId, deviceUuid string) (DevicePowerMap, error) {
devicePowerMap := make(DevicePowerMap)
if deviceUuid == "All" {
devicePowerMap["All"] = DevicePower{CurrentPower: -1.0, ProductName: "All Devices"}
}
mpcDevices, err := c.GetMpcData(systemId)
if err != nil || len(mpcDevices) < 1 {
return devicePowerMap, err
}
devices, err := c.GetDeviceData(systemId, DEVICES_ALL)
if err != nil {
return devicePowerMap, err
}
totalPower := 0.0
for _, dev := range mpcDevices {
totalPower = totalPower + dev.CurrentPower
if dev.DeviceID == deviceUuid || deviceUuid == "All" {
for _, dev2 := range devices {
if dev.DeviceID == dev2.Device.DeviceUUID {
devicePowerMap[dev.DeviceID] = DevicePower{CurrentPower: dev.CurrentPower, ProductName: dev2.Device.ProductName}
}
}
}
}
devicePowerMap["All"] = DevicePower{CurrentPower: totalPower, ProductName: "All Devices"}
return devicePowerMap, nil
}
func (c *Controller) GetCurrentQuickMode() string {
return c.currentQuickmode
}
func (c *Controller) GetQuickModeExpiresAt() string {
return c.quickModeExpiresAt
}
func (c *Controller) refreshCurrentQuickMode(state *SystemStatus) {
newQuickMode := ""
for _, dhw := range state.State.Dhw {
if dhw.CurrentSpecialFunction == SPECIAL_FUNCTION_HOTWATER_BOOST {
newQuickMode = QUICKMODE_HOTWATER
break
}
}
for _, domesticHotWater := range state.State.DomesticHotWater {
if domesticHotWater.CurrentSpecialFunction == SPECIAL_FUNCTION_HOTWATER_BOOST {
newQuickMode = QUICKMODE_HOTWATER
break
}
}
for _, zone := range state.State.Zones {
if zone.CurrentSpecialFunction == SPECIAL_FUNCTION_QUICK_VETO {
newQuickMode = QUICKMODE_HEATING
break
}
}
if newQuickMode != c.currentQuickmode {
if newQuickMode == "" && time.Now().After(c.quickmodeStarted.Add(2*CACHE_DURATION_SYSTEMS*time.Second)) {
if c.currentQuickmode == QUICKMODE_NOTHING && time.Now().Before(c.quickmodeStarted.Add(10*time.Minute)) {
c.debug("Idle mode active for less then 10 minutes. Keeping the idle mode")
} else {
c.debug(fmt.Sprintf("Old quickmode: \"%s\" New quickmode: \"%s\"", c.currentQuickmode, newQuickMode))
c.currentQuickmode = newQuickMode
c.quickmodeStopped = time.Now()
}
}
if newQuickMode != "" && time.Now().After(c.quickmodeStopped.Add(2*CACHE_DURATION_SYSTEMS*time.Second)) {
c.debug(fmt.Sprintf("Old quickmode: \"%s\" New quickmode: \"%s\"", c.currentQuickmode, newQuickMode))
c.currentQuickmode = newQuickMode
c.quickmodeStarted = time.Now()
}
}
}
func (c *Controller) StartZoneQuickVeto(systemId string, zone int, setpoint float32, duration float32) error {
err := c.conn.StartZoneQuickVeto(systemId, zone, setpoint, duration)
if err == nil && c.currentQuickmode != QUICKMODE_HOTWATER {
c.currentQuickmode = QUICKMODE_HEATING
c.quickmodeStarted = time.Now()
}
return err
}
func (c *Controller) StopZoneQuickVeto(systemId string, zone int) error {
err := c.conn.StopZoneQuickVeto(systemId, zone)
if err == nil && c.currentQuickmode != QUICKMODE_HOTWATER {
c.currentQuickmode = ""
c.quickmodeStopped = time.Now()
c.systemsCache.Reset()
}
return err
}
func (c *Controller) StartHotWaterBoost(systemId string, hotwaterIndex int) error {
err := c.conn.StartHotWaterBoost(systemId, hotwaterIndex)
if err == nil {
c.currentQuickmode = QUICKMODE_HOTWATER
c.quickmodeStarted = time.Now()
}
return err
}
func (c *Controller) StopHotWaterBoost(systemId string, hotwaterIndex int) error {
err := c.conn.StopHotWaterBoost(systemId, hotwaterIndex)
if err == nil && c.currentQuickmode != QUICKMODE_HEATING {
c.currentQuickmode = ""
c.quickmodeStopped = time.Now()
c.systemsCache.Reset()
}
return err
}
func (c *Controller) StartStrategybased(systemId string, strategy int, heatingPar *HeatingParStruct, hotwaterPar *HotwaterParStruct) (string, error) {
c.systemsCache.Reset()
state, err := c.GetSystem(systemId)
if err != nil {
return "", err
}
c.refreshCurrentQuickMode(&state)
// Extracting correct State.Dhw element
dhwData := GetDhwData(state, hotwaterPar.Index)
// Extracting correct State.Dhw element
domesticHotWaterData := GetDomesticHotWaterData(state, hotwaterPar.Index)
// Extracting correct State.Zone element
zoneData := GetZoneData(state, heatingPar.ZoneIndex)
if c.currentQuickmode != "" {
c.debug(fmt.Sprint("System is already in quick mode:", c.currentQuickmode))
c.debug("Is there any need to change that?")
if dhwData != nil {
c.debug(fmt.Sprint("Special Function of Dhw: ", dhwData.State.CurrentSpecialFunction))
}
if domesticHotWaterData != nil {
c.debug(fmt.Sprint("Operationg Mode of DomesticHotWater: ", domesticHotWaterData.State.CurrentSpecialFunction))
}
c.debug(fmt.Sprint("Special Function of Heating Zone: ", zoneData.State.CurrentSpecialFunction))
return QUICKMODE_ERROR_ALREADYON, err
}
whichQuickMode := c.WhichQuickMode(dhwData, domesticHotWaterData, zoneData, strategy, heatingPar, hotwaterPar)
c.debug(fmt.Sprint("whichQuickMode=", whichQuickMode))
switch whichQuickMode {
case 1:
err = c.StartHotWaterBoost(systemId, hotwaterPar.Index)
if err == nil {
c.currentQuickmode = QUICKMODE_HOTWATER
c.quickmodeStarted = time.Now()
c.debug("Starting hotwater boost")
c.quickModeExpiresAt = ""
}
case 2:
err = c.StartZoneQuickVeto(systemId, heatingPar.ZoneIndex, heatingPar.VetoSetpoint, heatingPar.VetoDuration)
if err == nil {
c.currentQuickmode = QUICKMODE_HEATING
c.quickmodeStarted = time.Now()
c.debug("Starting zone quick veto")
if heatingPar.VetoDuration < 0.0 {
c.quickModeExpiresAt = (time.Now().Add(time.Duration(int64(ZONEVETODURATION_DEFAULT*60) * int64(time.Minute)))).Format("15:04")
} else {
c.quickModeExpiresAt = (time.Now().Add(time.Duration(int64(heatingPar.VetoDuration*60) * int64(time.Minute)))).Format("15:04")
}
}
default:
if c.currentQuickmode == QUICKMODE_HOTWATER {
// if hotwater boost active, then stop it
err = c.StopHotWaterBoost(systemId, hotwaterPar.Index)
if err == nil {
c.debug("Stopping hotwater boost")
}
}
if c.currentQuickmode == QUICKMODE_HEATING {
// if zone quick veto active, then stop it
err = c.StopZoneQuickVeto(systemId, heatingPar.ZoneIndex)
if err == nil {
c.debug("Stopping zone quick veto")
}
}
c.currentQuickmode = QUICKMODE_NOTHING
c.quickmodeStarted = time.Now()
c.quickModeExpiresAt = (time.Now().Add(time.Duration(10 * time.Minute))).Format("15:04")
c.debug("Enable called but no quick mode possible. Starting idle mode")
}
c.systemsCache.Reset()
return c.currentQuickmode, err
}
func (c *Controller) StopStrategybased(systemId string, heatingPar *HeatingParStruct, hotwaterPar *HotwaterParStruct) (string, error) {
c.systemsCache.Reset()
state, err := c.GetSystem(systemId)
if err != nil {
return "", err
}
c.refreshCurrentQuickMode(&state)
// Extracting correct State.Dhw element
dhwData := GetDhwData(state, hotwaterPar.Index)
// Extracting correct State.Dhw element
domesticHotWaterData := GetDomesticHotWaterData(state, hotwaterPar.Index)
// Extracting correct State.Zone element
zoneData := GetZoneData(state, heatingPar.ZoneIndex)
if dhwData != nil {
c.debug(fmt.Sprint("Operationg Mode of Dhw: ", dhwData.State.CurrentSpecialFunction))
}
if domesticHotWaterData != nil {
c.debug(fmt.Sprint("Operationg Mode of DomesticHotWater: ", domesticHotWaterData.State.CurrentSpecialFunction))
}
c.debug(fmt.Sprint("Operationg Mode of Heating: ", zoneData.State.CurrentSpecialFunction))
switch c.currentQuickmode {
case QUICKMODE_HOTWATER:
err = c.StopHotWaterBoost(systemId, hotwaterPar.Index)
if err == nil {
c.debug(fmt.Sprint("Stopping quick mode", c.currentQuickmode))
}
case QUICKMODE_HEATING:
err = c.StopZoneQuickVeto(systemId, heatingPar.ZoneIndex)
if err == nil {
c.debug("Stopping zone quick veto")
}
case QUICKMODE_NOTHING:
c.debug("Stopping idle quick mode")
default:
c.debug("Nothing to do, no quick mode active")
}
c.currentQuickmode = ""
c.quickModeExpiresAt = ""
c.quickmodeStopped = time.Now()
c.systemsCache.Reset()
return c.currentQuickmode, err
}
// This function checks the operation mode of heating and hotwater and the hotwater live temperature
// and returns, which quick mode should be started, when evcc sends an "Enable"
func (c *Controller) WhichQuickMode(dhwData *DhwData, domesticHotWaterData *DomesticHotWaterData, zoneData *ZoneData, strategy int, heatingPar *HeatingParStruct, hotwater *HotwaterParStruct) int {
//c.debug(fmt.Sprint("Strategy = ", strategy))
// For strategy=STRATEGY_HOTWATER, a hotwater boost is possible when hotwater storage temperature is less than the temperature setpoint.
// For other strategies, a hotwater boost is possible when hotwater storage temperature is less than the temperature setpoint minus 5°C
addOn := -5.0
if strategy == STRATEGY_HOTWATER {
addOn = 0.0
}
hotWaterBoostPossible := false
if dhwData != nil {
if dhwData.State.CurrentDhwTemperature < dhwData.Configuration.TappingSetpoint+addOn &&
dhwData.Configuration.OperationModeDhw == OPERATIONMODE_TIME_CONTROLLED {
hotWaterBoostPossible = true
}
}
if domesticHotWaterData != nil {
if domesticHotWaterData.State.CurrentDomesticHotWaterTemperature < domesticHotWaterData.Configuration.TappingSetpoint+addOn &&
domesticHotWaterData.Configuration.OperationModeDomesticHotWater == OPERATIONMODE_TIME_CONTROLLED {
hotWaterBoostPossible = true
}
}
heatingQuickVetoPossible := false
if zoneData != nil {
if zoneData.Configuration.Heating.OperationModeHeating == OPERATIONMODE_TIME_CONTROLLED {
heatingQuickVetoPossible = true
}
}
whichQuickMode := 0
switch strategy {
case STRATEGY_HOTWATER:
if hotWaterBoostPossible {
whichQuickMode = 1
} else {
c.debug("Strategy = hotwater, but hotwater boost not possible")
}
case STRATEGY_HEATING:
if heatingQuickVetoPossible {
whichQuickMode = 2
} else {
c.debug("Strategy = heating, but zone quick veto not possible")
}
case STRATEGY_HOTWATER_THEN_HEATING:
if hotWaterBoostPossible {
whichQuickMode = 1
} else {
if heatingQuickVetoPossible {
whichQuickMode = 2
} else {
c.debug("Strategy = hotwater_then_heating, but both not possible")
}
}
}
return whichQuickMode
}