-
-
Notifications
You must be signed in to change notification settings - Fork 518
/
config.go
187 lines (163 loc) Β· 5.14 KB
/
config.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
package provider
import (
"strings"
"time"
"github.com/andig/evcc/util"
)
const (
execTimeout = 5 * time.Second
)
// Config is the general provider config
type Config struct {
Type string
Other map[string]interface{} `mapstructure:",remain"`
}
// mqttConfig is the specific mqtt getter/setter configuration
type mqttConfig struct {
Topic, Payload string // Payload only applies to setters
Scale float64
Timeout time.Duration
}
// scriptConfig is the specific script getter/setter configuration
type scriptConfig struct {
Cmd string
Timeout time.Duration
Cache time.Duration
}
// MQTT singleton
var MQTT *MqttClient
func mqttFromConfig(log *util.Logger, other map[string]interface{}) mqttConfig {
if MQTT == nil {
log.FATAL.Fatal("mqtt not configured")
}
var pc mqttConfig
util.DecodeOther(log, other, &pc)
if pc.Scale == 0 {
pc.Scale = 1
}
return pc
}
func scriptFromConfig(log *util.Logger, other map[string]interface{}) scriptConfig {
var pc scriptConfig
util.DecodeOther(log, other, &pc)
if pc.Timeout == 0 {
pc.Timeout = execTimeout
}
return pc
}
// NewFloatGetterFromConfig creates a FloatGetter from config
func NewFloatGetterFromConfig(log *util.Logger, config Config) (res FloatGetter) {
switch strings.ToLower(config.Type) {
case "http":
res = NewHTTPProviderFromConfig(log, config.Other).FloatGetter
case "mqtt":
pc := mqttFromConfig(log, config.Other)
res = MQTT.FloatGetter(pc.Topic, pc.Scale, pc.Timeout)
case "script":
pc := scriptFromConfig(log, config.Other)
res = NewScriptProvider(pc.Timeout).FloatGetter(pc.Cmd)
if pc.Cache > 0 {
res = NewCached(log, res, pc.Cache).FloatGetter()
}
case "modbus":
res = FloatGetter(NewModbusFromConfig(log, config.Type, config.Other).FloatGetter)
default:
log.FATAL.Fatalf("invalid provider type %s", config.Type)
}
return
}
// NewIntGetterFromConfig creates a IntGetter from config
func NewIntGetterFromConfig(log *util.Logger, config Config) (res IntGetter) {
switch strings.ToLower(config.Type) {
case "http":
res = NewHTTPProviderFromConfig(log, config.Other).IntGetter
case "mqtt":
pc := mqttFromConfig(log, config.Other)
res = MQTT.IntGetter(pc.Topic, int64(pc.Scale), pc.Timeout)
case "script":
pc := scriptFromConfig(log, config.Other)
res = NewScriptProvider(pc.Timeout).IntGetter(pc.Cmd)
if pc.Cache > 0 {
res = NewCached(log, res, pc.Cache).IntGetter()
}
case "modbus-rtu", "modbus-tcp", "modbus-rtuovertcp", "modbus-tcprtu", "modbus-rtutcp":
res = IntGetter(NewModbusFromConfig(log, config.Type, config.Other).IntGetter)
default:
log.FATAL.Fatalf("invalid provider type %s", config.Type)
}
return
}
// NewStringGetterFromConfig creates a StringGetter from config
func NewStringGetterFromConfig(log *util.Logger, config Config) (res StringGetter) {
switch strings.ToLower(config.Type) {
case "http":
res = NewHTTPProviderFromConfig(log, config.Other).StringGetter
case "mqtt":
pc := mqttFromConfig(log, config.Other)
res = MQTT.StringGetter(pc.Topic, pc.Timeout)
case "script":
pc := scriptFromConfig(log, config.Other)
res = NewScriptProvider(pc.Timeout).StringGetter(pc.Cmd)
if pc.Cache > 0 {
res = NewCached(log, res, pc.Cache).StringGetter()
}
case "combined", "openwb":
res = openWBStatusFromConfig(log, config.Other)
default:
log.FATAL.Fatalf("invalid provider type %s", config.Type)
}
return
}
// NewBoolGetterFromConfig creates a BoolGetter from config
func NewBoolGetterFromConfig(log *util.Logger, config Config) (res BoolGetter) {
switch strings.ToLower(config.Type) {
case "http":
res = NewHTTPProviderFromConfig(log, config.Other).BoolGetter
case "mqtt":
pc := mqttFromConfig(log, config.Other)
res = MQTT.BoolGetter(pc.Topic, pc.Timeout)
case "script":
pc := scriptFromConfig(log, config.Other)
res = NewScriptProvider(pc.Timeout).BoolGetter(pc.Cmd)
if pc.Cache > 0 {
res = NewCached(log, res, pc.Cache).BoolGetter()
}
default:
log.FATAL.Fatalf("invalid provider type %s", config.Type)
}
return
}
// NewIntSetterFromConfig creates a IntSetter from config
func NewIntSetterFromConfig(log *util.Logger, param string, config Config) (res IntSetter) {
switch strings.ToLower(config.Type) {
case "http":
res = NewHTTPProviderFromConfig(log, config.Other).IntSetter
case "mqtt":
pc := mqttFromConfig(log, config.Other)
res = MQTT.IntSetter(param, pc.Topic, pc.Payload)
case "script":
pc := scriptFromConfig(log, config.Other)
script := NewScriptProvider(pc.Timeout)
res = script.IntSetter(param, pc.Cmd)
default:
log.FATAL.Fatalf("invalid setter type %s", config.Type)
}
return
}
// NewBoolSetterFromConfig creates a BoolSetter from config
func NewBoolSetterFromConfig(log *util.Logger, param string, config Config) (res BoolSetter) {
switch strings.ToLower(config.Type) {
case "http":
res = NewHTTPProviderFromConfig(log, config.Other).BoolSetter
case "mqtt":
pc := mqttFromConfig(log, config.Other)
res = MQTT.BoolSetter(param, pc.Topic, pc.Payload)
case "script":
pc := scriptFromConfig(log, config.Other)
script := NewScriptProvider(pc.Timeout)
res = script.BoolSetter(param, pc.Cmd)
default:
log.FATAL.Fatalf("invalid setter type %s", config.Type)
}
return
}