forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_config_helper.go
190 lines (147 loc) Β· 4.14 KB
/
http_config_helper.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
package server
import (
"errors"
"sync"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util/config"
"github.com/evcc-io/evcc/util/templates"
)
const (
// typeTemplate is the updatable configuration type
typeTemplate = "template"
// masked indicates a masked config parameter value
masked = "***"
)
var (
dirty bool
mu sync.Mutex
)
// ConfigDirty returns the dirty flag
func ConfigDirty() bool {
mu.Lock()
defer mu.Unlock()
return dirty
}
// setConfigDirty sets the dirty flag indicating that a restart is required
func setConfigDirty() {
mu.Lock()
defer mu.Unlock()
dirty = true
}
func templateForConfig(class templates.Class, conf map[string]any) (templates.Template, error) {
typ, ok := conf[typeTemplate].(string)
if !ok {
return templates.Template{}, errors.New("config template not found")
}
return templates.ByName(class, typ)
}
func sanitizeMasked(class templates.Class, conf map[string]any) (map[string]any, error) {
tmpl, err := templateForConfig(class, conf)
if err != nil {
return nil, err
}
res := make(map[string]any, len(conf))
for k, v := range conf {
if i, p := tmpl.ParamByName(k); i >= 0 && p.IsMasked() {
v = masked
}
res[k] = v
}
return res, nil
}
func mergeMasked(class templates.Class, conf, old map[string]any) (map[string]any, error) {
tmpl, err := templateForConfig(class, conf)
if err != nil {
return nil, err
}
res := make(map[string]any, len(conf))
for k, v := range conf {
if i, p := tmpl.ParamByName(k); i >= 0 && p.IsMasked() && v == masked {
v = old[k]
}
res[k] = v
}
return res, nil
}
func deviceInstanceFromMergedConfig[T any](id int, class templates.Class, conf map[string]any, newFromConf func(string, map[string]any) (T, error), h config.Handler[T]) (config.Device[T], T, map[string]any, error) {
var zero T
dev, err := h.ByName(config.NameForID(id))
if err != nil {
return nil, zero, nil, err
}
merged, err := mergeMasked(class, conf, dev.Config().Other)
if err != nil {
return nil, zero, nil, err
}
instance, err := newFromConf(typeTemplate, merged)
return dev, instance, merged, err
}
type testResult = struct {
Value any `json:"value"`
Error string `json:"error"`
}
// testInstance tests the given instance similar to dump
// TODO refactor together with dump
func testInstance(instance any) map[string]testResult {
res := make(map[string]testResult)
makeResult := func(val any, err error) testResult {
res := testResult{Value: val}
if err != nil {
res.Error = err.Error()
}
return res
}
if dev, ok := instance.(api.Meter); ok {
val, err := dev.CurrentPower()
res["power"] = makeResult(val, err)
}
if dev, ok := instance.(api.MeterEnergy); ok {
val, err := dev.TotalEnergy()
res["energy"] = makeResult(val, err)
}
if dev, ok := instance.(api.Battery); ok {
val, err := dev.Soc()
res["soc"] = makeResult(val, err)
}
if dev, ok := instance.(api.VehicleOdometer); ok {
val, err := dev.Odometer()
res["odometer"] = makeResult(val, err)
}
if dev, ok := instance.(api.BatteryCapacity); ok {
val := dev.Capacity()
res["capacity"] = makeResult(val, nil)
}
if dev, ok := instance.(api.PhaseCurrents); ok {
i1, i2, i3, err := dev.Currents()
res["phaseCurrents"] = makeResult([]float64{i1, i2, i3}, err)
}
if dev, ok := instance.(api.PhaseVoltages); ok {
u1, u2, u3, err := dev.Voltages()
res["phaseVoltages"] = makeResult([]float64{u1, u2, u3}, err)
}
if dev, ok := instance.(api.PhasePowers); ok {
p1, p2, p3, err := dev.Powers()
res["phasePowers"] = makeResult([]float64{p1, p2, p3}, err)
}
if dev, ok := instance.(api.ChargeState); ok {
val, err := dev.Status()
res["chargeStatus"] = makeResult(val, err)
}
if dev, ok := instance.(api.Charger); ok {
val, err := dev.Enabled()
res["enabled"] = makeResult(val, err)
}
if dev, ok := instance.(api.ChargeRater); ok {
val, err := dev.ChargedEnergy()
res["chargedEnergy"] = makeResult(val, err)
}
if dev, ok := instance.(api.VehicleRange); ok {
val, err := dev.Range()
res["range"] = makeResult(val, err)
}
if dev, ok := instance.(api.SocLimiter); ok {
val, err := dev.TargetSoc()
res["socLimit"] = makeResult(val, err)
}
return res
}