forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volvo.go
167 lines (133 loc) Β· 4.1 KB
/
volvo.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package vehicle
import (
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"github.com/evcc-io/evcc/vehicle/volvo"
)
// Volvo is an api.Vehicle implementation for Volvo. cars
type Volvo struct {
*embed
*request.Helper
vin string
statusG func() (volvo.Status, error)
}
func init() {
registry.Add("volvo", NewVolvoFromConfig)
}
// NewVolvoFromConfig creates a new vehicle
func NewVolvoFromConfig(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
}
basicAuth := transport.BasicAuthHeader(cc.User, cc.Password)
log := util.NewLogger("volvo").Redact(cc.User, cc.Password, cc.VIN, basicAuth)
v := &Volvo{
embed: &cc.embed,
Helper: request.NewHelper(log),
vin: cc.VIN,
}
v.Client.Transport = &transport.Decorator{
Base: v.Client.Transport,
Decorator: transport.DecorateHeaders(map[string]string{
"Authorization": basicAuth,
"Content-Type": "application/json",
"X-Device-Id": "Device",
"X-OS-Type": "Android",
"X-Originator-Type": "App",
"X-OS-Version": "22",
}),
}
v.statusG = provider.Cached(v.StatusRequest, cc.Cache)
var err error
v.vin, err = ensureVehicle(cc.VIN, v.Vehicles)
return v, err
}
// vehicles implements returns the list of user vehicles
func (v *Volvo) Vehicles() ([]string, error) {
var vehicles []string
uri := fmt.Sprintf("%s/customeraccounts", volvo.ApiURI)
var res volvo.AccountResponse
err := v.GetJSON(uri, &res)
if err == nil {
for _, rel := range res.VehicleRelations {
var vehicle volvo.VehicleRelation
if err := v.GetJSON(rel, &vehicle); err != nil {
return vehicles, err
}
vehicles = append(vehicles, vehicle.VehicleID)
}
} else if res.ErrorLabel != "" {
err = fmt.Errorf("%w: %s: %s", err, res.ErrorLabel, res.ErrorDescription)
}
return vehicles, err
}
func (v *Volvo) StatusRequest() (volvo.Status, error) {
var res volvo.Status
uri := fmt.Sprintf("%s/vehicles/%s/status", volvo.ApiURI, v.vin)
err := v.GetJSON(uri, &res)
if err != nil && res.ErrorLabel != "" {
err = fmt.Errorf("%w: %s: %s", err, res.ErrorLabel, res.ErrorDescription)
}
return res, err
}
// Soc implements the api.Vehicle interface
func (v *Volvo) Soc() (float64, error) {
res, err := v.statusG()
return float64(res.HvBattery.HvBatteryLevel), err
}
var _ api.ChargeState = (*Volvo)(nil)
// Status implements the api.ChargeState interface
func (v *Volvo) Status() (api.ChargeStatus, error) {
res, err := v.statusG()
if err == nil {
switch res.HvBattery.HvBatteryChargeStatusDerived {
case "CableNotPluggedInCar":
return api.StatusA, nil
case "CablePluggedInCar", "CablePluggedInCar_FullyCharged", "CablePluggedInCar_ChargingPaused":
return api.StatusB, nil
case "Charging", "CablePluggedInCar_Charging":
return api.StatusC, nil
}
}
return api.StatusNone, err
}
var _ api.VehicleRange = (*Volvo)(nil)
// VehicleRange implements the api.VehicleRange interface
func (v *Volvo) Range() (int64, error) {
res, err := v.statusG()
return int64(res.HvBattery.DistanceToHVBatteryEmpty), err
}
var _ api.VehicleOdometer = (*Volvo)(nil)
// VehicleOdometer implements the api.VehicleOdometer interface
func (v *Volvo) Odometer() (float64, error) {
res, err := v.statusG()
return res.Odometer / 1e3, err
}
var _ api.VehicleFinishTimer = (*Volvo)(nil)
// FinishTime implements the VehicleFinishTimer interface
func (v *Volvo) FinishTime() (time.Time, error) {
res, err := v.statusG()
if err == nil {
timestamp := res.HvBattery.TimeToHVBatteryFullyChargedTimestamp.Add(time.Duration(res.HvBattery.DistanceToHVBatteryEmpty) * time.Minute)
if timestamp.Before(time.Now()) {
return time.Time{}, api.ErrNotAvailable
}
return timestamp, err
}
return time.Time{}, err
}