forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
53 lines (42 loc) · 1.04 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
package vehicle
import (
"context"
"fmt"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
reg "github.com/evcc-io/evcc/util/registry"
)
const (
expiry = 5 * time.Minute // maximum response age before refresh
interval = 15 * time.Minute // refresh interval when charging
)
var registry = reg.New[api.Vehicle]("vehicle")
// Types returns the list of types
func Types() []string {
return registry.Types()
}
// NewFromConfig creates vehicle from configuration
func NewFromConfig(ctx context.Context, typ string, other map[string]interface{}) (api.Vehicle, error) {
var cc struct {
Cloud bool
Other map[string]interface{} `mapstructure:",remain"`
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.Cloud {
cc.Other["brand"] = typ
typ = "cloud"
}
factory, err := registry.Get(strings.ToLower(typ))
if err != nil {
return nil, err
}
v, err := factory(ctx, cc.Other)
if err != nil {
err = fmt.Errorf("cannot create vehicle type '%s': %w", typ, err)
}
return v, err
}