-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
provider.go
82 lines (66 loc) · 2.04 KB
/
provider.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
package polestar
import (
"context"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
)
type Provider struct {
statusG func() (BatteryData, error)
odoG func() (OdometerData, error)
}
func NewProvider(log *util.Logger, api *API, vin string, timeout, cache time.Duration) *Provider {
v := &Provider{
statusG: provider.Cached(func() (BatteryData, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return api.Status(ctx, vin)
}, cache),
odoG: provider.Cached(func() (OdometerData, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return api.Odometer(ctx, vin)
}, cache),
}
return v
}
// Soc implements the api.Vehicle interface
func (v *Provider) Soc() (float64, error) {
res, err := v.statusG()
return res.BatteryChargeLevelPercentage, err
}
var _ api.ChargeState = (*Provider)(nil)
// Range implements the api.VehicleRange interface
func (v *Provider) Status() (api.ChargeStatus, error) {
status, err := v.statusG()
res := api.StatusA
if status.ChargerConnectionStatus == "CHARGER_CONNECTION_STATUS_CONNECTED" {
res = api.StatusB
}
if status.ChargingStatus == "CHARGING_STATUS_CHARGING" {
res = api.StatusB
}
return res, err
}
var _ api.VehicleRange = (*Provider)(nil)
// Range implements the api.VehicleRange interface
func (v *Provider) Range() (int64, error) {
res, err := v.statusG()
return int64(res.EstimatedDistanceToEmptyKm), err
}
var _ api.VehicleOdometer = (*Provider)(nil)
// Odometer implements the Provider.VehicleOdometer interface
func (v *Provider) Odometer() (float64, error) {
res, err := v.odoG()
return res.OdometerMeters / 1e3, err
}
var _ api.VehicleFinishTimer = (*Provider)(nil)
// FinishTime implements the api.VehicleFinishTimer interface
func (v *Provider) FinishTime() (time.Time, error) {
res, err := v.statusG()
if err != nil {
return time.Time{}, err
}
return time.Now().Add(time.Duration(res.EstimatedChargingTimeToFullMinutes) * time.Minute), nil
}