forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
provider.go
85 lines (66 loc) · 1.83 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
83
84
85
package zero
import (
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider"
)
// Provider implements the vehicle api
type Provider struct {
status provider.Cacheable[State]
}
// NewProvider creates a vehicle api provider
func NewProvider(api *API, unitId string, cache time.Duration) *Provider {
impl := &Provider{
status: provider.ResettableCached(func() (State, error) {
return api.Status(unitId)
}, cache),
}
return impl
}
var _ api.Battery = (*Provider)(nil)
// Soc implements the api.Vehicle interface
func (v *Provider) Soc() (float64, error) {
res, err := v.status.Get()
return float64(res.Soc), err
}
var _ api.ChargeState = (*Provider)(nil)
// Status implements the api.ChargeState interface
func (v *Provider) Status() (api.ChargeStatus, error) {
res, err := v.status.Get()
if err != nil {
return api.StatusNone, err
}
status := api.StatusA
if res.Pluggedin != 0 {
status = api.StatusB
}
if res.Charging != 0 {
status = api.StatusC
}
return status, nil
}
var _ api.VehicleFinishTimer = (*Provider)(nil)
// FinishTime implements the api.VehicleFinishTimer interface
func (v *Provider) FinishTime() (time.Time, error) {
res, err := v.status.Get()
if err != nil {
return time.Time{}, err
}
t, err := time.Parse("20060102150405", res.DatetimeActual)
if err != nil {
t = time.Now()
}
return t.Add(time.Duration(res.Chargingtimeleft) * time.Minute), nil
}
var _ api.VehicleOdometer = (*Provider)(nil)
// Odometer implements the api.VehicleOdometer interface
func (v *Provider) Odometer() (float64, error) {
res, err := v.status.Get()
return res.Mileage, err
}
var _ api.VehiclePosition = (*Provider)(nil)
// Position implements the api.VehiclePosition interface
func (v *Provider) Position() (float64, float64, error) {
res, err := v.status.Get()
return res.Latitude, res.Longitude, err
}