forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle.go
168 lines (147 loc) Β· 4.57 KB
/
vehicle.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
package vehicle
import (
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
)
//go:generate go run ../cmd/tools/decorate.go -f decorateVehicle -b api.Vehicle -t "api.SocLimiter,GetLimitSoc,func() (int64, error)" -t "api.ChargeState,Status,func() (api.ChargeStatus, error)" -t "api.VehicleRange,Range,func() (int64, error)" -t "api.VehicleOdometer,Odometer,func() (float64, error)" -t "api.VehicleClimater,Climater,func() (bool, error)" -t "api.CurrentController,MaxCurrent,func(int64) error" -t "api.CurrentGetter,GetMaxCurrent,func() (float64, error)" -t "api.VehicleFinishTimer,FinishTime,func() (time.Time, error)" -t "api.Resurrector,WakeUp,func() error" -t "api.ChargeController,ChargeEnable,func(bool) error"
// Vehicle is an api.Vehicle implementation with configurable getters and setters.
type Vehicle struct {
*embed
socG func() (float64, error)
}
func init() {
registry.Add(api.Custom, NewConfigurableFromConfig)
}
// NewConfigurableFromConfig creates a new Vehicle
func NewConfigurableFromConfig(other map[string]interface{}) (api.Vehicle, error) {
var cc struct {
embed `mapstructure:",squash"`
Soc provider.Config
LimitSoc *provider.Config
Status *provider.Config
Range *provider.Config
Odometer *provider.Config
Climater *provider.Config
MaxCurrent *provider.Config
GetMaxCurrent *provider.Config
FinishTime *provider.Config
Wakeup *provider.Config
ChargeEnable *provider.Config
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
socG, err := provider.NewFloatGetterFromConfig(cc.Soc)
if err != nil {
return nil, fmt.Errorf("soc: %w", err)
}
v := &Vehicle{
embed: &cc.embed,
socG: socG,
}
// decorate range
var limitSoc func() (int64, error)
if cc.LimitSoc != nil {
limitSoc, err = provider.NewIntGetterFromConfig(*cc.LimitSoc)
if err != nil {
return nil, fmt.Errorf("limitSoc: %w", err)
}
}
// decorate status
var status func() (api.ChargeStatus, error)
if cc.Status != nil {
get, err := provider.NewStringGetterFromConfig(*cc.Status)
if err != nil {
return nil, fmt.Errorf("status: %w", err)
}
status = func() (api.ChargeStatus, error) {
s, err := get()
if err != nil {
return api.StatusNone, err
}
return api.ChargeStatusString(s)
}
}
// decorate range
var rng func() (int64, error)
if cc.Range != nil {
rng, err = provider.NewIntGetterFromConfig(*cc.Range)
if err != nil {
return nil, fmt.Errorf("range: %w", err)
}
}
// decorate odometer
var odo func() (float64, error)
if cc.Odometer != nil {
odo, err = provider.NewFloatGetterFromConfig(*cc.Odometer)
if err != nil {
return nil, fmt.Errorf("odometer: %w", err)
}
}
// decorate climater
var climater func() (bool, error)
if cc.Climater != nil {
climater, err = provider.NewBoolGetterFromConfig(*cc.Climater)
if err != nil {
return nil, fmt.Errorf("climater: %w", err)
}
}
// decorate maxCurrent
var maxCurrent func(int64) error
if cc.MaxCurrent != nil {
maxCurrent, err = provider.NewIntSetterFromConfig("maxcurrent", *cc.MaxCurrent)
if err != nil {
return nil, fmt.Errorf("maxCurrent: %w", err)
}
}
// decorate getMaxCurrent
var getMaxCurrent func() (float64, error)
if cc.GetMaxCurrent != nil {
getMaxCurrent, err = provider.NewFloatGetterFromConfig(*cc.GetMaxCurrent)
if err != nil {
return nil, fmt.Errorf("getMaxCurrent: %w", err)
}
}
// decorate finishtime
var finishTime func() (time.Time, error)
if cc.FinishTime != nil {
stringG, err := provider.NewStringGetterFromConfig(*cc.FinishTime)
if err != nil {
return nil, fmt.Errorf("finishTime: %w", err)
}
finishTime = func() (time.Time, error) {
s, err := stringG()
if err != nil {
return time.Time{}, err
}
return time.Parse(time.RFC3339, s)
}
}
// decorate wakeup
var wakeup func() error
if cc.Wakeup != nil {
set, err := provider.NewBoolSetterFromConfig("wakeup", *cc.Wakeup)
if err != nil {
return nil, fmt.Errorf("wakeup: %w", err)
}
wakeup = func() error {
return set(true)
}
}
// decorate chargeEnable
var chargeEnable func(bool) error
if cc.ChargeEnable != nil {
chargeEnable, err = provider.NewBoolSetterFromConfig("chargeenable", *cc.ChargeEnable)
if err != nil {
return nil, fmt.Errorf("chargeEnable: %w", err)
}
}
return decorateVehicle(v, limitSoc, status, rng, odo, climater, maxCurrent, getMaxCurrent, finishTime, wakeup, chargeEnable), nil
}
// Soc implements the api.Vehicle interface
func (v *Vehicle) Soc() (float64, error) {
return v.socG()
}