-
-
Notifications
You must be signed in to change notification settings - Fork 526
/
psa.go
91 lines (75 loc) · 1.95 KB
/
psa.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
package vehicle
import (
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/psa"
)
// https://github.com/TA2k/ioBroker.psa
func init() {
registry.Add("citroen", func(other map[string]any) (api.Vehicle, error) {
return newPSA("citroen", "clientsB2CCitroen", other)
})
registry.Add("ds", func(other map[string]any) (api.Vehicle, error) {
return newPSA("ds", "clientsB2CDS", other)
})
registry.Add("opel", func(other map[string]any) (api.Vehicle, error) {
return newPSA("opel", "clientsB2COpel", other)
})
registry.Add("peugeot", func(other map[string]any) (api.Vehicle, error) {
return newPSA("peugeot", "clientsB2CPeugeot", other)
})
}
// PSA is an api.Vehicle implementation for PSA cars
type PSA struct {
*embed
*psa.Provider // provides the api implementations
}
// newPSA creates a new vehicle
func newPSA(brand, realm string, other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
VIN string
User string
Password string `mapstructure:"password"`
Country string
Tokens Tokens
Cache time.Duration
}{
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.User == "" {
return nil, api.ErrMissingCredentials
}
token, err := cc.Tokens.Token()
if err != nil {
return nil, err
}
v := &PSA{
embed: &cc.embed,
}
log := util.NewLogger(brand)
log.Redact(cc.User, cc.Tokens.Access, cc.Tokens.Refresh)
oc := psa.Oauth2Config(brand, strings.ToLower(cc.Country))
identity, err := psa.NewIdentity(log, brand, cc.User, oc, token)
if err != nil {
return nil, err
}
// TODO still needed?
api := psa.NewAPI(log, identity, realm, oc.ClientID)
vehicle, err := ensureVehicleEx(
cc.VIN, api.Vehicles,
func(v psa.Vehicle) string {
return v.VIN
},
)
if err != nil {
return nil, err
}
v.Provider = psa.NewProvider(api, vehicle.ID, cc.Cache)
return v, err
}