/
provider.go
204 lines (164 loc) Β· 5.09 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
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
package vw
import (
"cmp"
"fmt"
"os"
"slices"
"strconv"
"strings"
"text/tabwriter"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider"
"github.com/samber/lo"
)
// Provider implements the vehicle api
type Provider struct {
chargerG func() (ChargerResponse, error)
statusG func() (StatusResponse, error)
climateG func() (ClimaterResponse, error)
positionG func() (PositionResponse, error)
action func(action, value string) error
rr func() (RolesRights, error)
}
// NewProvider creates a vehicle api provider
func NewProvider(api *API, vin string, cache time.Duration) *Provider {
impl := &Provider{
chargerG: provider.Cached(func() (ChargerResponse, error) {
return api.Charger(vin)
}, cache),
statusG: provider.Cached(func() (StatusResponse, error) {
return api.Status(vin)
}, cache),
climateG: provider.Cached(func() (ClimaterResponse, error) {
return api.Climater(vin)
}, cache),
positionG: provider.Cached(func() (PositionResponse, error) {
return api.Position(vin)
}, cache),
action: func(action, value string) error {
return api.Action(vin, action, value)
},
rr: func() (RolesRights, error) {
return api.RolesRights(vin)
},
}
return impl
}
var _ api.Battery = (*Provider)(nil)
// Soc implements the api.Vehicle interface
func (v *Provider) Soc() (float64, error) {
res, err := v.chargerG()
if err == nil {
return float64(res.Charger.Status.BatteryStatusData.StateOfCharge.Content), nil
}
return 0, err
}
var _ api.ChargeState = (*Provider)(nil)
// Status implements the api.ChargeState interface
func (v *Provider) Status() (api.ChargeStatus, error) {
status := api.StatusA // disconnected
res, err := v.chargerG()
if err == nil {
if res.Charger.Status.PlugStatusData.PlugState.Content == "connected" {
status = api.StatusB
}
if res.Charger.Status.ChargingStatusData.ChargingState.Content == "charging" {
status = api.StatusC
}
}
return status, err
}
var _ api.VehicleFinishTimer = (*Provider)(nil)
// FinishTime implements the api.VehicleFinishTimer interface
func (v *Provider) FinishTime() (time.Time, error) {
res, err := v.chargerG()
if err == nil {
rct := res.Charger.Status.BatteryStatusData.RemainingChargingTime
// estimate not available
if rct.Content == 65535 {
return time.Time{}, api.ErrNotAvailable
}
return time.Now().Add(time.Duration(rct.Content) * time.Minute), nil
}
return time.Time{}, err
}
var _ api.VehicleRange = (*Provider)(nil)
// Range implements the api.VehicleRange interface
func (v *Provider) Range() (rng int64, err error) {
res, err := v.chargerG()
if err == nil {
crsd := res.Charger.Status.CruisingRangeStatusData
rng = int64(crsd.PrimaryEngineRange.Content)
if crsd.EngineTypeFirstEngine.Content != "typeIsElectric" {
rng = int64(crsd.SecondaryEngineRange.Content)
}
}
return rng, err
}
var _ api.VehicleOdometer = (*Provider)(nil)
// Odometer implements the api.VehicleOdometer interface
func (v *Provider) Odometer() (float64, error) {
res, err := v.statusG()
if err == nil {
err = api.ErrNotAvailable
if sd := res.ServiceByID(ServiceOdometer); sd != nil {
if fd := sd.FieldByID(ServiceOdometer); fd != nil {
if val, err := strconv.ParseFloat(fd.Value, 64); err == nil {
return val, nil
}
}
}
}
return 0, err
}
var _ api.VehicleClimater = (*Provider)(nil)
// Climater implements the api.VehicleClimater interface
func (v *Provider) Climater() (bool, error) {
res, err := v.climateG()
if err == nil {
state := strings.ToLower(res.Climater.Status.ClimatisationStatusData.ClimatisationState.Content)
active := state != "off" && state != "invalid" && state != "error"
return active, nil
}
return false, err
}
var _ api.VehiclePosition = (*Provider)(nil)
// Position implements the api.VehiclePosition interface
func (v *Provider) Position() (float64, float64, error) {
res, err := v.positionG()
if err == nil {
coord := res.FindCarResponse.Position.CarCoordinate
return float64(coord.Latitude) / 1e6, float64(coord.Longitude) / 1e6, nil
}
return 0, 0, err
}
var _ api.ChargeController = (*Provider)(nil)
// ChargeEnable implements the api.ChargeController interface
func (v *Provider) ChargeEnable(enable bool) error {
action := map[bool]string{true: ActionChargeStart, false: ActionChargeStop}
return v.action(ActionCharge, action[enable])
}
var _ api.Diagnosis = (*Provider)(nil)
// Diagnose implements the api.Diagnosis interface
func (v *Provider) Diagnose() {
rr, err := v.rr()
if err != nil {
return
}
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
slices.SortFunc(rr.OperationList.ServiceInfo, func(i, j ServiceInfo) int {
return cmp.Compare(i.ServiceId, j.ServiceId)
})
for _, si := range rr.OperationList.ServiceInfo {
if si.InvocationUrl.Content != "" {
fmt.Fprintf(tw, "%s:\t%s\n", si.ServiceId, si.InvocationUrl.Content)
}
}
// list remaining service
services := lo.FilterMap(rr.OperationList.ServiceInfo, func(si ServiceInfo, _ int) (string, bool) {
return si.ServiceId, si.InvocationUrl.Content == ""
})
fmt.Fprintf(tw, "without uri:\t%s\n", strings.Join(services, ","))
tw.Flush()
}