forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmercedes.go
77 lines (62 loc) · 1.74 KB
/
mercedes.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
package vehicle
import (
"errors"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/mercedes"
)
// Mercedes is an api.Vehicle implementation for Mercedes cars
type Mercedes struct {
*embed
api.AuthProvider
*mercedes.Provider
}
func init() {
registry.Add("mercedes", NewMercedesFromConfig)
}
// NewMercedesFromConfig creates a new Mercedes vehicle
func NewMercedesFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
ClientID, ClientSecret string
VIN string
Sandbox bool
Cache time.Duration
}{
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.ClientID == "" && cc.ClientSecret == "" {
return nil, errors.New("missing credentials")
}
if cc.VIN == "" {
return nil, errors.New("missing vin")
}
var options []mercedes.IdentityOption
// TODO Load tokens from a persistence storage and use those during startup
// e.g. persistence.Load("key")
// if tokens != nil {
// options = append(options, mercedes.WithToken(&oauth2.Token{
// AccessToken: tokens.Access,
// RefreshToken: tokens.Refresh,
// Expiry: tokens.Expiry,
// }))
// }
log := util.NewLogger("mercedes")
// TODO session secret from config/persistence
identity, err := mercedes.NewIdentity(log, cc.ClientID, cc.ClientSecret, options...)
if err != nil {
return nil, err
}
api := mercedes.NewAPI(log, identity, cc.Sandbox)
v := &Mercedes{
embed: &cc.embed,
Provider: mercedes.NewProvider(api, strings.ToUpper(cc.VIN), cc.Cache),
AuthProvider: identity, // expose the OAuth2 login
}
return v, nil
}