-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
site_api.go
332 lines (265 loc) Β· 7.69 KB
/
site_api.go
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
package core
import (
"errors"
"strings"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/server/db/settings"
"github.com/evcc-io/evcc/util/config"
"github.com/samber/lo"
)
var _ site.API = (*Site)(nil)
var ErrBatteryNotConfigured = errors.New("battery not configured")
const (
GridTariff = "grid"
FeedinTariff = "feedin"
PlannerTariff = "planner"
)
// isConfigurable checks if the meter is configurable
func isConfigurable(ref string) bool {
dev, _ := config.Meters().ByName(ref)
_, ok := dev.(config.ConfigurableDevice[api.Meter])
return ok
}
// filterConfigurable filters configurable meters
func filterConfigurable(ref []string) []string {
var res []string
for _, r := range ref {
if isConfigurable(r) {
res = append(res, r)
}
}
return res
}
// GetTitle returns the title
func (site *Site) GetTitle() string {
site.RLock()
defer site.RUnlock()
return site.Title
}
// SetTitle sets the title
func (site *Site) SetTitle(title string) {
site.Lock()
defer site.Unlock()
site.Title = title
site.publish(keys.SiteTitle, title)
settings.SetString(keys.Title, title)
}
// GetGridMeterRef returns the GridMeterRef
func (site *Site) GetGridMeterRef() string {
site.RLock()
defer site.RUnlock()
return site.Meters.GridMeterRef
}
// SetGridMeterRef sets the GridMeterRef
func (site *Site) SetGridMeterRef(ref string) {
site.Lock()
defer site.Unlock()
site.Meters.GridMeterRef = ref
settings.SetString(keys.GridMeter, ref)
}
// GetPVMeterRefs returns the PvMeterRef
func (site *Site) GetPVMeterRefs() []string {
site.RLock()
defer site.RUnlock()
return site.Meters.PVMetersRef
}
// SetPVMeterRefs sets the PvMeterRef
func (site *Site) SetPVMeterRefs(ref []string) {
site.Lock()
defer site.Unlock()
site.Meters.PVMetersRef = ref
settings.SetString(keys.PvMeters, strings.Join(filterConfigurable(ref), ","))
}
// GetBatteryMeterRefs returns the BatteryMeterRef
func (site *Site) GetBatteryMeterRefs() []string {
site.RLock()
defer site.RUnlock()
return site.Meters.BatteryMetersRef
}
// SetBatteryMeterRefs sets the BatteryMeterRef
func (site *Site) SetBatteryMeterRefs(ref []string) {
site.Lock()
defer site.Unlock()
site.Meters.BatteryMetersRef = ref
settings.SetString(keys.BatteryMeters, strings.Join(filterConfigurable(ref), ","))
}
// GetAuxMeterRefs returns the AuxMeterRef
func (site *Site) GetAuxMeterRefs() []string {
site.RLock()
defer site.RUnlock()
return site.Meters.AuxMetersRef
}
// SetAuxMeterRefs sets the AuxMeterRef
func (site *Site) SetAuxMeterRefs(ref []string) {
site.Lock()
defer site.Unlock()
site.Meters.AuxMetersRef = ref
settings.SetString(keys.AuxMeters, strings.Join(filterConfigurable(ref), ","))
}
// Loadpoints returns the loadpoints as api interfaces
func (site *Site) Loadpoints() []loadpoint.API {
return lo.Map(site.loadpoints, func(lp *Loadpoint, _ int) loadpoint.API { return lp })
}
// loadpointsAsCircuitDevices returns the loadpoints as circuit devices
func (site *Site) loadpointsAsCircuitDevices() []api.CircuitLoad {
return lo.Map(site.loadpoints, func(lp *Loadpoint, _ int) api.CircuitLoad { return lp })
}
// Vehicles returns the site vehicles
func (site *Site) Vehicles() site.Vehicles {
return &vehicles{log: site.log}
}
// GetCircuit returns the circuit
func (site *Site) GetCircuit() api.Circuit {
if site.circuit == nil {
// return untyped nil
return nil
}
return site.circuit
}
// GetPrioritySoc returns the PrioritySoc
func (site *Site) GetPrioritySoc() float64 {
site.RLock()
defer site.RUnlock()
return site.prioritySoc
}
// SetPrioritySoc sets the PrioritySoc
func (site *Site) SetPrioritySoc(soc float64) error {
site.Lock()
defer site.Unlock()
if len(site.batteryMeters) == 0 {
return ErrBatteryNotConfigured
}
if site.bufferSoc != 0 && soc > site.bufferSoc {
return errors.New("priority soc must be smaller or equal than buffer soc")
}
site.log.DEBUG.Println("set priority soc:", soc)
if site.prioritySoc != soc {
site.prioritySoc = soc
settings.SetFloat(keys.PrioritySoc, site.prioritySoc)
site.publish(keys.PrioritySoc, site.prioritySoc)
}
return nil
}
// GetBufferSoc returns the BufferSoc
func (site *Site) GetBufferSoc() float64 {
site.RLock()
defer site.RUnlock()
return site.bufferSoc
}
// SetBufferSoc sets the BufferSoc
func (site *Site) SetBufferSoc(soc float64) error {
site.Lock()
defer site.Unlock()
if len(site.batteryMeters) == 0 {
return ErrBatteryNotConfigured
}
if soc != 0 && soc < site.prioritySoc {
return errors.New("buffer soc must not be smaller than priority soc")
}
if site.bufferStartSoc != 0 && soc > site.bufferStartSoc {
return errors.New("buffer soc must be smaller or equal than buffer start soc")
}
site.log.DEBUG.Println("set buffer soc:", soc)
if site.bufferSoc != soc {
site.bufferSoc = soc
settings.SetFloat(keys.BufferSoc, site.bufferSoc)
site.publish(keys.BufferSoc, site.bufferSoc)
}
return nil
}
// GetBufferStartSoc returns the BufferStartSoc
func (site *Site) GetBufferStartSoc() float64 {
site.RLock()
defer site.RUnlock()
return site.bufferStartSoc
}
// SetBufferStartSoc sets the BufferStartSoc
func (site *Site) SetBufferStartSoc(soc float64) error {
site.Lock()
defer site.Unlock()
if len(site.batteryMeters) == 0 {
return ErrBatteryNotConfigured
}
if soc != 0 && soc < site.bufferSoc {
return errors.New("buffer start soc must be larger than buffer soc")
}
site.log.DEBUG.Println("set buffer start soc:", soc)
if site.bufferStartSoc != soc {
site.bufferStartSoc = soc
settings.SetFloat(keys.BufferStartSoc, site.bufferStartSoc)
site.publish(keys.BufferStartSoc, site.bufferStartSoc)
}
return nil
}
// GetResidualPower returns the ResidualPower
func (site *Site) GetResidualPower() float64 {
site.RLock()
defer site.RUnlock()
return site.ResidualPower
}
// SetResidualPower sets the ResidualPower
func (site *Site) SetResidualPower(power float64) error {
site.Lock()
defer site.Unlock()
site.log.DEBUG.Println("set residual power:", power)
if site.ResidualPower != power {
site.ResidualPower = power
site.publish(keys.ResidualPower, site.ResidualPower)
}
return nil
}
// GetTariff returns the respective tariff if configured or nil
func (site *Site) GetTariff(tariff string) api.Tariff {
site.RLock()
defer site.RUnlock()
switch tariff {
case GridTariff:
return site.tariffs.Grid
case FeedinTariff:
return site.tariffs.FeedIn
case PlannerTariff:
switch {
case site.tariffs.Planner != nil:
// prio 0: manually set planner tariff
return site.tariffs.Planner
case site.tariffs.Grid != nil && site.tariffs.Grid.Type() == api.TariffTypePriceForecast:
// prio 1: grid tariff with forecast
return site.tariffs.Grid
case site.tariffs.Co2 != nil:
// prio 2: co2 tariff
return site.tariffs.Co2
default:
// prio 3: static grid tariff
return site.tariffs.Grid
}
default:
return nil
}
}
// GetBatteryControl returns the battery control mode
func (site *Site) GetBatteryDischargeControl() bool {
site.RLock()
defer site.RUnlock()
return site.batteryDischargeControl
}
// SetBatteryControl sets the battery control mode
func (site *Site) SetBatteryDischargeControl(val bool) error {
site.log.DEBUG.Println("set battery discharge control:", val)
if site.GetBatteryDischargeControl() != val {
// reset to normal when disabling
if mode := site.GetBatteryMode(); !val && batteryModeModified(mode) {
if err := site.applyBatteryMode(api.BatteryNormal); err != nil {
return err
}
}
site.Lock()
defer site.Unlock()
site.batteryDischargeControl = val
settings.SetBool(keys.BatteryDischargeControl, val)
site.publish(keys.BatteryDischargeControl, val)
}
return nil
}