forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyundai.go
86 lines (71 loc) · 2.26 KB
/
hyundai.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
package vehicle
import (
"fmt"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/bluelink"
)
// Hyundai is an api.Vehicle implementation
type Hyundai struct {
*embed
*bluelink.Provider
}
func init() {
registry.Add("hyundai", NewHyundaiFromConfig)
}
// NewHyundaiFromConfig creates a new Vehicle
func NewHyundaiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password string
VIN string
Expiry time.Duration
Cache time.Duration
}{
Expiry: expiry,
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
settings := bluelink.Config{
URI: "https://prd.eu-ccapi.hyundai.com:8080",
BasicToken: "NmQ0NzdjMzgtM2NhNC00Y2YzLTk1NTctMmExOTI5YTk0NjU0OktVeTQ5WHhQekxwTHVvSzB4aEJDNzdXNlZYaG10UVI5aVFobUlGampvWTRJcHhzVg==",
CCSPServiceID: "6d477c38-3ca4-4cf3-9557-2a1929a94654",
CCSPApplicationID: bluelink.HyundaiAppID,
AuthClientID: "64621b96-0f0d-11ec-82a8-0242ac130003",
BrandAuthUrl: "https://eu-account.hyundai.com/auth/realms/euhyundaiidm/protocol/openid-connect/auth?client_id=%s&scope=openid%%20profile%%20email%%20phone&response_type=code&hkid_session_reset=true&redirect_uri=%s/api/v1/user/integration/redirect/login&ui_locales=%s&state=%s:%s",
}
log := util.NewLogger("hyundai").Redact(cc.User, cc.Password, cc.VIN)
identity := bluelink.NewIdentity(log, settings)
if err := identity.Login(cc.User, cc.Password); err != nil {
return nil, err
}
api := bluelink.NewAPI(log, settings.URI, identity, cc.Cache)
vehicles, err := api.Vehicles()
if err != nil {
return nil, err
}
var vehicle bluelink.Vehicle
if cc.VIN == "" && len(vehicles) == 1 {
vehicle = vehicles[0]
log.DEBUG.Printf("found vehicle: %v", vehicle.VIN)
} else {
for _, v := range vehicles {
if v.VIN == strings.ToUpper(cc.VIN) {
vehicle = v
log.DEBUG.Printf("found vehicle: %v", vehicle.VIN)
}
}
}
if len(vehicle.VIN) == 0 {
return nil, fmt.Errorf("cannot find vehicle: %v", vehicles)
}
v := &Hyundai{
embed: &cc.embed,
Provider: bluelink.NewProvider(api, vehicle.VehicleID, cc.Expiry, cc.Cache),
}
return v, nil
}