forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsilence.go
86 lines (68 loc) · 1.82 KB
/
silence.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
package vehicle
import (
"errors"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/silence"
)
// Silence is an api.Vehicle implementation for Silence S01 vehicles
type Silence struct {
*embed
apiG func() (silence.Vehicle, error)
}
func init() {
registry.Add("silence", NewSilenceFromConfig)
}
// NewFordFromConfig creates a new vehicle
func NewSilenceFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password string
VIN string
Cache time.Duration
}{
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.User == "" || cc.Password == "" {
return nil, errors.New("missing user or password")
}
log := util.NewLogger("s01").Redact(cc.User, cc.Password)
v := &Silence{
embed: &cc.embed,
}
identity, err := silence.NewIdentity(log, cc.User, cc.Password)
if err != nil {
return nil, err
}
api := silence.NewAPI(log, identity)
vin, err := ensureVehicle(strings.ToLower(cc.VIN), api.Vehicles)
if err == nil {
v.apiG = provider.Cached(func() (silence.Vehicle, error) {
return api.Status(vin)
}, cc.Cache)
}
return v, err
}
// Soc implements the api.Vehicle interface
func (v *Silence) Soc() (float64, error) {
res, err := v.apiG()
return float64(res.BatterySoc), err
}
var _ api.VehicleRange = (*Silence)(nil)
// Range implements the api.VehicleRange interface
func (v *Silence) Range() (int64, error) {
res, err := v.apiG()
return int64(res.Range), err
}
var _ api.VehicleOdometer = (*Silence)(nil)
// Odometer implements the api.VehicleOdometer interface
func (v *Silence) Odometer() (float64, error) {
res, err := v.apiG()
return float64(res.Odometer), err
}