forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmg.go
54 lines (42 loc) · 1.08 KB
/
mg.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
package vehicle
import (
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/saic"
)
// MG is an api.Vehicle implementation for probably all SAIC cars
type MG struct {
*embed
*saic.Provider // provides the api implementations
}
func init() {
registry.Add("mg", NewMGFromConfig)
}
// NewBMWFromConfig creates a new vehicle
func NewMGFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, VIN string
Cache time.Duration
}{
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.User == "" || cc.Password == "" {
return nil, api.ErrMissingCredentials
}
log := util.NewLogger("mg").Redact(cc.User, cc.Password, cc.VIN)
identity := saic.NewIdentity(log, cc.User, cc.Password)
if err := identity.Login(); err != nil {
return nil, err
}
api := saic.NewAPI(log, identity)
v := &MG{
embed: &cc.embed,
Provider: saic.NewProvider(api, cc.VIN, cc.Cache),
}
return v, nil
}