/
wrapper.go
100 lines (80 loc) · 2.16 KB
/
wrapper.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
package wrapper
import (
"fmt"
"strings"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
)
// Wrapper wraps an api.Vehicle to capture initialization errors
type Wrapper struct {
err error
title string
icon string
phases int
capacity float64
Features_ []api.Feature
}
// New creates a new Vehicle
func New(name string, other map[string]interface{}, err error) api.Vehicle {
var cc struct {
Title string
Icon string
Phases int
Capacity float64
Other map[string]interface{} `mapstructure:",remain"`
}
// try to decode vehicle-specific config and look for title attribute
_ = util.DecodeOther(other, &cc)
if cc.Title == "" {
//lint:ignore SA1019 as Title is safe on ascii
cc.Title = strings.Title(name)
}
v := &Wrapper{
err: fmt.Errorf("vehicle not available: %w", err),
title: fmt.Sprintf("%s (offline)", cc.Title),
icon: cc.Icon,
phases: cc.Phases,
capacity: cc.Capacity,
Features_: []api.Feature{api.Offline},
}
return v
}
var _ api.Vehicle = (*Wrapper)(nil)
// Title implements the api.Vehicle interface
func (v *Wrapper) Title() string {
return v.title
}
// SetTitle implements the api.TitleSetter interface
func (v *Wrapper) SetTitle(title string) {
v.title = fmt.Sprintf("%s (unavailable)", title)
}
// Icon implements the api.Vehicle interface
func (v *Wrapper) Icon() string {
return v.icon
}
// Capacity implements the api.Vehicle interface
func (v *Wrapper) Capacity() float64 {
return v.capacity
}
// Phases implements the api.Vehicle interface
func (v *Wrapper) Phases() int {
return v.phases
}
// Identifiers implements the api.Vehicle interface
func (v *Wrapper) Identifiers() []string {
return nil
}
// OnIdentified implements the api.Vehicle interface
func (v *Wrapper) OnIdentified() api.ActionConfig {
return api.ActionConfig{}
}
var _ api.FeatureDescriber = (*Wrapper)(nil)
// Features implements the api.FeatureDescriber interface
func (v *Wrapper) Features() []api.Feature {
return []api.Feature{api.Offline}
}
var _ api.Battery = (*Wrapper)(nil)
// Soc implements the api.Battery interface
func (v *Wrapper) Soc() (float64, error) {
return 0, v.err
}