-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathloadpoint_effective.go
More file actions
258 lines (216 loc) Β· 6.76 KB
/
loadpoint_effective.go
File metadata and controls
258 lines (216 loc) Β· 6.76 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
package core
import (
"slices"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/core/vehicle"
"github.com/evcc-io/evcc/util"
)
// PublishEffectiveValues publishes all effective values
func (lp *Loadpoint) PublishEffectiveValues() {
lp.publish(keys.EffectivePriority, lp.EffectivePriority())
lp.publish(keys.EffectivePlanId, lp.EffectivePlanId())
lp.publish(keys.EffectivePlanTime, lp.EffectivePlanTime())
lp.publish(keys.EffectivePlanSoc, lp.EffectivePlanSoc())
lp.publish(keys.EffectivePlanStrategy, lp.EffectivePlanStrategy())
lp.publish(keys.EffectiveMinCurrent, lp.effectiveMinCurrent())
lp.publish(keys.EffectiveMaxCurrent, lp.effectiveMaxCurrent())
lp.publish(keys.EffectiveLimitSoc, lp.EffectiveLimitSoc())
}
// EffectivePriority returns the effective priority
func (lp *Loadpoint) EffectivePriority() int {
if v := lp.GetVehicle(); v != nil {
if res, ok := v.OnIdentified().GetPriority(); ok {
return res
}
}
return lp.GetPriority()
}
type plan struct {
Id int
Start time.Time // last possible start time
End time.Time // user-selected finish time
Soc int
}
func (lp *Loadpoint) nextActivePlan(maxPower float64, plans []plan) *plan {
for i, p := range plans {
requiredDuration := lp.getPlanRequiredDuration(float64(p.Soc), maxPower)
plans[i].Start = p.End.Add(-requiredDuration)
}
// sort plans by start time
slices.SortStableFunc(plans, func(i, j plan) int {
return i.Start.Compare(j.Start)
})
for _, p := range plans {
if lp.vehicleSoc == 0 || lp.vehicleSoc < float64(p.Soc) {
return &p
}
}
return nil
}
// NextVehiclePlan returns the next vehicle plan time, soc and id
func (lp *Loadpoint) NextVehiclePlan() (time.Time, int, int) {
lp.RLock()
defer lp.RUnlock()
return lp.nextVehiclePlan()
}
// nextVehiclePlan returns the next vehicle plan time, soc, id
func (lp *Loadpoint) nextVehiclePlan() (time.Time, int, int) {
if v := lp.GetVehicle(); v != nil {
var plans []plan
// static plan
if planTime, soc := vehicle.Settings(lp.log, v).GetPlanSoc(); soc != 0 {
plans = append(plans, plan{Id: 1, Soc: soc, End: planTime})
}
// repeating plans
for index, rp := range vehicle.Settings(lp.log, v).GetRepeatingPlans() {
if !rp.Active || len(rp.Weekdays) == 0 {
continue
}
planTime, err := util.GetNextOccurrence(rp.Weekdays, rp.Time, rp.Tz)
if err != nil {
lp.log.DEBUG.Printf("invalid repeating plan: weekdays=%v, time=%s, tz=%s, error=%v", rp.Weekdays, rp.Time, rp.Tz, err)
continue
}
plans = append(plans, plan{Id: index + 2, Soc: rp.Soc, End: planTime})
}
// calculate earliest required plan start
if plan := lp.nextActivePlan(lp.effectiveMaxPower(), plans); plan != nil {
return plan.End, plan.Soc, plan.Id
}
}
return time.Time{}, 0, 0
}
// EffectivePlanSoc returns the soc target for the current plan
func (lp *Loadpoint) EffectivePlanSoc() int {
_, soc, _ := lp.NextVehiclePlan()
return soc
}
// EffectivePlanId returns the id for the current plan
func (lp *Loadpoint) EffectivePlanId() int {
if lp.socBasedPlanning() {
_, _, id := lp.NextVehiclePlan()
return id
}
if lp.planEnergy > 0 {
return 1
}
// no plan
return 0
}
// EffectivePlanTime returns the effective plan time
func (lp *Loadpoint) EffectivePlanTime() time.Time {
if lp.socBasedPlanning() {
ts, _, _ := lp.NextVehiclePlan()
return ts
}
ts, _ := lp.GetPlanEnergy()
return ts
}
// SocBasedPlanning returns true if soc based planning is enabled
func (lp *Loadpoint) SocBasedPlanning() bool {
return lp.socBasedPlanning()
}
// effectiveMinCurrent returns the effective min current
func (lp *Loadpoint) effectiveMinCurrent() float64 {
lpMin := lp.getMinCurrent()
var vehicleMin, chargerMin float64
if v := lp.GetVehicle(); v != nil {
if res, ok := v.OnIdentified().GetMinCurrent(); ok {
vehicleMin = res
}
}
if c, ok := lp.charger.(api.CurrentLimiter); ok {
if res, _, err := c.GetMinMaxCurrent(); err == nil {
chargerMin = res
}
}
switch {
case max(vehicleMin, chargerMin) == 0:
return lpMin
case chargerMin > 0:
return max(vehicleMin, chargerMin)
default:
return max(vehicleMin, lpMin)
}
}
// effectiveMaxCurrent returns the effective max current
func (lp *Loadpoint) effectiveMaxCurrent() float64 {
maxCurrent := lp.getMaxCurrent()
if v := lp.GetVehicle(); v != nil {
if res, ok := v.OnIdentified().GetMaxCurrent(); ok && res > 0 {
maxCurrent = min(maxCurrent, res)
}
}
if c, ok := lp.charger.(api.CurrentLimiter); ok {
if _, res, err := c.GetMinMaxCurrent(); err == nil && res > 0 {
maxCurrent = min(maxCurrent, res)
}
}
return maxCurrent
}
// EffectiveLimitSoc returns the effective session limit soc
func (lp *Loadpoint) EffectiveLimitSoc() int {
lp.RLock()
defer lp.RUnlock()
return lp.effectiveLimitSoc()
}
// effectiveLimitSoc returns the effective session limit soc
// TODO take vehicle api limits into account
func (lp *Loadpoint) effectiveLimitSoc() int {
if lp.limitSoc > 0 {
return lp.limitSoc
}
if v := lp.GetVehicle(); v != nil {
if soc := vehicle.Settings(lp.log, v).GetLimitSoc(); soc > 0 {
return soc
}
}
// MUST return 100 here as UI looks at effectiveLimitSoc and not limitSoc (VehicleSoc.vue)
return 100
}
// EffectiveStepPower returns the effective step power for the currently active phases
func (lp *Loadpoint) EffectiveStepPower() float64 {
return Voltage * float64(lp.ActivePhases())
}
// EffectiveMinPower returns the effective min power for the minimum active phases
func (lp *Loadpoint) EffectiveMinPower() float64 {
lp.RLock()
defer lp.RUnlock()
return Voltage * lp.effectiveMinCurrent() * float64(lp.minActivePhases())
}
// EffectiveMaxPower returns the effective max power taking vehicle capabilities,
// phase scaling and load management power limits into account
func (lp *Loadpoint) EffectiveMaxPower() float64 {
lp.RLock()
defer lp.RUnlock()
if circuitMaxPower := circuitMaxPower(lp.circuit); circuitMaxPower > 0 {
return min(lp.effectiveMaxPower(), circuitMaxPower)
}
return lp.effectiveMaxPower()
}
// effectiveMaxPower returns the effective max power taking vehicle capabilities and phase scaling into account
func (lp *Loadpoint) effectiveMaxPower() float64 {
res := Voltage * lp.effectiveMaxCurrent() * float64(lp.maxActivePhases())
if lp.vehicle != nil {
if maxPower, ok := lp.vehicle.OnIdentified().GetMaxPower(); ok {
return min(maxPower, res)
}
}
return res
}
// EffectivePlanStrategy returns the effective plan strategy
func (lp *Loadpoint) EffectivePlanStrategy() api.PlanStrategy {
lp.RLock()
defer lp.RUnlock()
return lp.getEffectivePlanStrategy()
}
func (lp *Loadpoint) getEffectivePlanStrategy() api.PlanStrategy {
if v := lp.GetVehicle(); v != nil {
if lp.socBasedPlanning() {
return vehicle.Settings(lp.log, v).GetPlanStrategy()
}
}
return lp.getPlanStrategy()
}