forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tronity.go
241 lines (194 loc) · 6.15 KB
/
tronity.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package vehicle
// LICENSE
// Copyright (c) 2019-2022 andig
// This module is NOT covered by the MIT license. All rights reserved.
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import (
"context"
"errors"
"fmt"
"net/http"
"slices"
"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/oauth"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/sponsor"
"github.com/evcc-io/evcc/vehicle/tronity"
"golang.org/x/oauth2"
)
// Tronity is an api.Vehicle implementation for the Tronity api
type Tronity struct {
*embed
*request.Helper
log *util.Logger
oc *oauth2.Config
vid string
bulkG func() (tronity.Bulk, error)
}
func init() {
registry.Add("tronity", NewTronityFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateTronity -b *Tronity -r api.Vehicle -t "api.ChargeState,Status,func() (api.ChargeStatus, error)" -t "api.VehicleOdometer,Odometer,func() (float64, error)" -t "api.ChargeController,ChargeEnable,func(bool) error"
// NewTronityFromConfig creates a new vehicle
func NewTronityFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
Credentials ClientCredentials
Tokens Tokens
VIN string
Cache time.Duration
}{
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if err := cc.Credentials.Error(); err != nil {
return nil, err
}
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
// authenticated http client with logging injected to the tronity client
log := util.NewLogger("tronity").Redact(cc.Credentials.ID, cc.Credentials.Secret)
oc, err := tronity.OAuth2Config(cc.Credentials.ID, cc.Credentials.Secret)
if err != nil {
return nil, err
}
v := &Tronity{
log: log,
embed: &cc.embed,
Helper: request.NewHelper(log),
oc: oc,
}
var ts oauth2.TokenSource
token, err := cc.Tokens.Token()
// https://app.platform.tronity.io/docs#tag/Authentication
if err != nil {
// use app flow if we don't have tokens
ts = oauth.RefreshTokenSource(nil, v)
} else {
// use provided tokens generated by code flow
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewClient(log))
ts = oc.TokenSource(ctx, token)
}
// replace client transport with authenticated transport
v.Client.Transport = &oauth2.Transport{
Source: ts,
Base: v.Client.Transport,
}
vehicle, err := ensureVehicleEx(
cc.VIN, v.vehicles,
func(v tronity.Vehicle) (string, error) {
return v.VIN, nil
},
)
if err != nil {
return nil, err
}
v.vid = vehicle.ID
v.bulkG = provider.Cached(v.bulk, cc.Cache)
var status func() (api.ChargeStatus, error)
if slices.Contains(vehicle.Scopes, tronity.ReadCharge) {
status = v.status
}
var odometer func() (float64, error)
if slices.Contains(vehicle.Scopes, tronity.ReadOdometer) {
odometer = v.odometer
}
var chargeEnable func(bool) error
if slices.Contains(vehicle.Scopes, tronity.WriteChargeStartStop) {
chargeEnable = v.chargeEnable
}
return decorateTronity(v, status, odometer, chargeEnable), nil
}
// RefreshToken performs token refresh by logging in with app context
func (v *Tronity) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
data := struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
GrantType string `json:"grant_type"`
}{
ClientID: v.oc.ClientID,
ClientSecret: v.oc.ClientSecret,
GrantType: "app",
}
req, _ := request.New(http.MethodPost, v.oc.Endpoint.TokenURL, request.MarshalJSON(data), request.JSONEncoding)
var token oauth2.Token
err := request.NewHelper(v.log).DoJSON(req, &token)
return util.TokenWithExpiry(&token), err
}
// vehicles implements the vehicles api
func (v *Tronity) vehicles() ([]tronity.Vehicle, error) {
uri := fmt.Sprintf("%s/tronity/vehicles", tronity.URI)
var res tronity.Vehicles
err := v.GetJSON(uri, &res)
return res.Data, err
}
// bulk implements the bulk api
func (v *Tronity) bulk() (tronity.Bulk, error) {
uri := fmt.Sprintf("%s/tronity/vehicles/%s/last_record", tronity.URI, v.vid)
var res tronity.Bulk
err := v.GetJSON(uri, &res)
return res, err
}
// Soc implements the api.Vehicle interface
func (v *Tronity) Soc() (float64, error) {
res, err := v.bulkG()
return res.Level, err
}
// status implements the api.ChargeState interface
func (v *Tronity) status() (api.ChargeStatus, error) {
status := api.StatusA // disconnected
res, err := v.bulkG()
if err != nil {
return status, err
}
switch {
case res.Charging == "Charging":
status = api.StatusC
case res.Plugged:
status = api.StatusB
}
return status, nil
}
var _ api.VehicleRange = (*Tronity)(nil)
// Range implements the api.VehicleRange interface
func (v *Tronity) Range() (int64, error) {
res, err := v.bulkG()
return int64(res.Range), err
}
// odometer implements the api.VehicleOdometer interface
func (v *Tronity) odometer() (float64, error) {
res, err := v.bulkG()
return res.Odometer, err
}
func (v *Tronity) post(uri string) error {
resp, err := v.Post(uri, "", nil)
if err == nil {
err = request.ResponseError(resp)
}
// ignore HTTP 405
var se request.StatusError
if errors.As(err, &se) && se.StatusCode() == http.StatusMethodNotAllowed {
err = nil
}
return err
}
// chargeEnable implements the api.ChargeController interface
func (v *Tronity) chargeEnable(enable bool) error {
action := map[bool]string{true: "start_charging", false: "stop_charging"}
uri := fmt.Sprintf("%s/tronity/vehicles/%s/%s", tronity.URI, v.vid, action[enable])
return v.post(uri)
}