forked from evcc-io/evcc
/
provider_emobility.go
181 lines (153 loc) · 4.84 KB
/
provider_emobility.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package porsche
import (
"fmt"
"net/http"
"time"
"github.com/mark-sch/evcc/api"
"github.com/mark-sch/evcc/provider"
"github.com/mark-sch/evcc/util"
"github.com/mark-sch/evcc/util/request"
"golang.org/x/oauth2"
)
type EmobilityResponse struct {
BatteryChargeStatus struct {
ChargeRate struct {
Unit string
Value float64
ValueInKmPerHour int64
}
ChargingInDCMode bool
ChargingMode string
ChargingPower float64
ChargingReason string
ChargingState string
ChargingTargetDateTime string
ExternalPowerSupplyState string
PlugState string
RemainingChargeTimeUntil100PercentInMinutes int64
StateOfChargeInPercentage int64
RemainingERange struct {
OriginalUnit string
OriginalValue int64
Unit string
Value int64
ValueInKilometers int64
}
}
ChargingStatus string
DirectCharge struct {
Disabled bool
IsActive bool
}
DirectClimatisation struct {
ClimatisationState string
RemainingClimatisationTime int64
}
}
// EMobilityProvider is an api.Vehicle implementation for Porsche Taycan cars
type EMobilityProvider struct {
log *util.Logger
*request.Helper
token oauth2.Token
identity *Identity
statusG func() (interface{}, error)
}
// NewEMobilityProvider creates a new vehicle
func NewEMobilityProvider(log *util.Logger, identity *Identity, token oauth2.Token, vin string, cache time.Duration) *EMobilityProvider {
impl := &EMobilityProvider{
log: log,
token: token,
Helper: request.NewHelper(log),
identity: identity,
}
impl.statusG = provider.NewCached(func() (interface{}, error) {
return impl.status(vin)
}, cache).InterfaceGetter()
return impl
}
func (v *EMobilityProvider) request(uri string) (*http.Request, error) {
if v.token.AccessToken == "" || time.Since(v.token.Expiry) > 0 {
accessTokens, err := v.identity.Login()
if err != nil {
return nil, err
}
v.token = accessTokens.EmobilityToken
}
req, err := request.New(http.MethodGet, uri, nil, map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", v.token.AccessToken),
})
return req, err
}
// Status implements the vehicle status repsonse
func (v *EMobilityProvider) status(vin string) (interface{}, error) {
uri := fmt.Sprintf("https://api.porsche.com/service-vehicle/de/de_DE/e-mobility/J1/%s?timezone=Europe/Berlin", vin)
req, err := v.request(uri)
if err != nil {
return 0, err
}
req.Header.Set("apikey", EmobilityClientID)
var pr EmobilityResponse
err = v.DoJSON(req, &pr)
return pr, err
}
var _ api.Battery = (*Provider)(nil)
// SoC implements the api.Vehicle interface
func (v *EMobilityProvider) SoC() (float64, error) {
res, err := v.statusG()
if res, ok := res.(EmobilityResponse); err == nil && ok {
return float64(res.BatteryChargeStatus.StateOfChargeInPercentage), nil
}
return 0, err
}
var _ api.VehicleRange = (*Provider)(nil)
// Range implements the api.VehicleRange interface
func (v *EMobilityProvider) Range() (int64, error) {
res, err := v.statusG()
if res, ok := res.(EmobilityResponse); err == nil && ok {
return int64(res.BatteryChargeStatus.RemainingERange.ValueInKilometers), nil
}
return 0, err
}
var _ api.VehicleFinishTimer = (*EMobilityProvider)(nil)
// FinishTime implements the api.VehicleFinishTimer interface
func (v *EMobilityProvider) FinishTime() (time.Time, error) {
res, err := v.statusG()
if res, ok := res.(*EmobilityResponse); err == nil && ok {
t := time.Now()
return t.Add(time.Duration(res.BatteryChargeStatus.RemainingChargeTimeUntil100PercentInMinutes) * time.Minute), err
}
return time.Time{}, err
}
var _ api.ChargeState = (*EMobilityProvider)(nil)
// Status implements the api.ChargeState interface
func (v *EMobilityProvider) Status() (api.ChargeStatus, error) {
res, err := v.statusG()
if res, ok := res.(EmobilityResponse); err == nil && ok {
switch res.BatteryChargeStatus.PlugState {
case "DISCONNECTED":
return api.StatusA, nil
case "CONNECTED":
switch res.BatteryChargeStatus.ChargingState {
case "OFF", "COMPLETED":
return api.StatusB, nil
case "ON":
return api.StatusC, nil
}
}
}
return api.StatusNone, err
}
var _ api.VehicleClimater = (*EMobilityProvider)(nil)
// Climater implements the api.VehicleClimater interface
func (v *EMobilityProvider) Climater() (active bool, outsideTemp float64, targetTemp float64, err error) {
res, err := v.statusG()
if res, ok := res.(EmobilityResponse); err == nil && ok {
switch res.DirectClimatisation.ClimatisationState {
case "OFF":
return false, 0, 0, nil
case "ON":
return true, 0, 0, nil
}
}
return active, outsideTemp, targetTemp, err
}