forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
254 lines (211 loc) Β· 6.11 KB
/
config.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
package cmd
import (
"fmt"
"net/http"
"time"
"github.com/dustin/go-humanize"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger"
"github.com/evcc-io/evcc/meter"
"github.com/evcc-io/evcc/provider/mqtt"
"github.com/evcc-io/evcc/push"
"github.com/evcc-io/evcc/server"
autoauth "github.com/evcc-io/evcc/server/auth"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle"
"github.com/evcc-io/evcc/vehicle/wrapper"
"github.com/gorilla/handlers"
)
type config struct {
URI string
Log string
SponsorToken string
Metrics bool
Profile bool
Levels map[string]string
Interval time.Duration
Mqtt mqttConfig
Javascript map[string]interface{}
Influx server.InfluxConfig
EEBus map[string]interface{}
HEMS typedConfig
Messaging messagingConfig
Meters []qualifiedConfig
Chargers []qualifiedConfig
Vehicles []qualifiedConfig
Tariffs tariffConfig
Site map[string]interface{}
LoadPoints []map[string]interface{}
}
type mqttConfig struct {
mqtt.Config `mapstructure:",squash"`
Topic string
}
func (conf *mqttConfig) RootTopic() string {
if conf.Topic != "" {
return conf.Topic
}
return "evcc"
}
type qualifiedConfig struct {
Name, Type string
Other map[string]interface{} `mapstructure:",remain"`
}
type typedConfig struct {
Type string
Other map[string]interface{} `mapstructure:",remain"`
}
type messagingConfig struct {
Events map[string]push.EventTemplateConfig
Services []typedConfig
}
type tariffConfig struct {
Currency string
Grid typedConfig
FeedIn typedConfig
}
// ConfigProvider provides configuration items
type ConfigProvider struct {
meters map[string]api.Meter
chargers map[string]api.Charger
vehicles map[string]api.Vehicle
visited map[string]bool
auth *util.AuthCollection
}
func (cp *ConfigProvider) TrackVisitors() {
cp.visited = make(map[string]bool)
}
// Meter provides meters by name
func (cp *ConfigProvider) Meter(name string) api.Meter {
if meter, ok := cp.meters[name]; ok {
// track duplicate usage https://github.com/evcc-io/evcc/issues/1744
if cp.visited != nil {
if _, ok := cp.visited[name]; ok {
log.FATAL.Fatalf("duplicate meter usage: %s", name)
}
cp.visited[name] = true
}
return meter
}
log.FATAL.Fatalf("invalid meter: %s", name)
return nil
}
// Charger provides chargers by name
func (cp *ConfigProvider) Charger(name string) api.Charger {
if charger, ok := cp.chargers[name]; ok {
return charger
}
log.FATAL.Fatalf("invalid charger: %s", name)
return nil
}
// Vehicle provides vehicles by name
func (cp *ConfigProvider) Vehicle(name string) api.Vehicle {
if vehicle, ok := cp.vehicles[name]; ok {
return vehicle
}
log.FATAL.Fatalf("invalid vehicle: %s", name)
return nil
}
func (cp *ConfigProvider) configure(conf config) error {
err := cp.configureMeters(conf)
if err == nil {
err = cp.configureChargers(conf)
}
if err == nil {
err = cp.configureVehicles(conf)
}
return err
}
func (cp *ConfigProvider) configureMeters(conf config) error {
cp.meters = make(map[string]api.Meter)
for id, cc := range conf.Meters {
if cc.Name == "" {
return fmt.Errorf("cannot create %s meter: missing name", humanize.Ordinal(id+1))
}
m, err := meter.NewFromConfig(cc.Type, cc.Other)
if err != nil {
err = fmt.Errorf("cannot create meter '%s': %w", cc.Name, err)
return err
}
if _, exists := cp.meters[cc.Name]; exists {
return fmt.Errorf("duplicate meter name: %s already defined and must be unique", cc.Name)
}
cp.meters[cc.Name] = m
}
return nil
}
func (cp *ConfigProvider) configureChargers(conf config) error {
cp.chargers = make(map[string]api.Charger)
for id, cc := range conf.Chargers {
if cc.Name == "" {
return fmt.Errorf("cannot create %s charger: missing name", humanize.Ordinal(id+1))
}
c, err := charger.NewFromConfig(cc.Type, cc.Other)
if err != nil {
err = fmt.Errorf("cannot create charger '%s': %w", cc.Name, err)
return err
}
if _, exists := cp.chargers[cc.Name]; exists {
return fmt.Errorf("duplicate charger name: %s already defined and must be unique", cc.Name)
}
cp.chargers[cc.Name] = c
}
return nil
}
func (cp *ConfigProvider) configureVehicles(conf config) error {
cp.vehicles = make(map[string]api.Vehicle)
for id, cc := range conf.Vehicles {
if cc.Name == "" {
return fmt.Errorf("cannot create %s vehicle: missing name", humanize.Ordinal(id+1))
}
v, err := vehicle.NewFromConfig(cc.Type, cc.Other)
if err != nil {
// wrap any created errors to prevent fatals
v, _ = wrapper.New(v, err)
}
if _, exists := cp.vehicles[cc.Name]; exists {
return fmt.Errorf("duplicate vehicle name: %s already defined and must be unique", cc.Name)
}
cp.vehicles[cc.Name] = v
}
return nil
}
// webControl handles routing for devices. For now only api.ProviderLogin related routes
func (cp *ConfigProvider) webControl(httpd *server.HTTPd, paramC chan<- util.Param) {
router := httpd.Router()
auth := router.PathPrefix("/oauth").Subrouter()
auth.Use(handlers.CompressHandler)
auth.Use(handlers.CORS(
handlers.AllowedHeaders([]string{"Content-Type"}),
))
// wire the handler
autoauth.Setup(auth)
// initialize
cp.auth = util.NewAuthCollection(paramC)
// TODO make evccURI configurable, add warnings for any network/ localhost
evccURI := fmt.Sprintf("http://%s", httpd.Addr)
authURI := fmt.Sprintf("%s/oauth", evccURI)
var id int
for _, v := range cp.vehicles {
if provider, ok := v.(api.ProviderLogin); ok {
id += 1
basePath := fmt.Sprintf("vehicles/%d", id)
baseURI := fmt.Sprintf("%s/auth/%s", evccURI, basePath)
// register vehicle
ap := cp.auth.Register(baseURI, v.Title())
provider.SetCallbackParams(evccURI, authURI, ap.Handler())
auth.
Methods(http.MethodPost).
Path(fmt.Sprintf("/%s/login", basePath)).
HandlerFunc(provider.LoginHandler())
auth.
Methods(http.MethodPost).
Path(fmt.Sprintf("/%s/logout", basePath)).
HandlerFunc(provider.LogoutHandler())
}
}
if id > 0 {
log.INFO.Printf("ensure the oauth client redirect/callback is configured for: %s", authURI)
}
cp.auth.Publish()
}