forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.go
303 lines (263 loc) · 7.74 KB
/
mqtt.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package server
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/core/vehicle"
"github.com/evcc-io/evcc/provider/mqtt"
"github.com/evcc-io/evcc/util"
)
// MQTT is the MQTT server. It uses the MQTT client for publishing.
type MQTT struct {
log *util.Logger
Handler *mqtt.Client
root string
publisher func(topic string, retained bool, payload string)
}
// NewMQTT creates MQTT server
func NewMQTT(root string, site site.API) (*MQTT, error) {
m := &MQTT{
log: util.NewLogger("mqtt"),
Handler: mqtt.Instance,
root: root,
}
m.publisher = m.publishString
err := m.Handler.Cleanup(m.root, true)
if err == nil {
err = m.Listen(site)
}
if err != nil {
err = fmt.Errorf("mqtt: %w", err)
}
return m, err
}
func (m *MQTT) encode(v interface{}) string {
// nil should erase the value
if v == nil {
return ""
}
switch val := v.(type) {
case string:
return val
case float64:
return fmt.Sprintf("%.5g", val)
case time.Time:
if val.IsZero() {
return ""
}
return strconv.FormatInt(val.Unix(), 10)
case time.Duration:
// must be before stringer to convert to seconds instead of string
return strconv.Itoa(int(val.Seconds()))
case fmt.Stringer:
return val.String()
default:
return fmt.Sprintf("%v", val)
}
}
func (m *MQTT) publishComplex(topic string, retained bool, payload interface{}) {
if _, ok := payload.(fmt.Stringer); ok || payload == nil {
m.publishSingleValue(topic, retained, payload)
return
}
switch typ := reflect.TypeOf(payload); typ.Kind() {
case reflect.Slice:
// publish count
val := reflect.ValueOf(payload)
m.publishSingleValue(topic, retained, val.Len())
// loop slice
for i := 0; i < val.Len(); i++ {
m.publishComplex(fmt.Sprintf("%s/%d", topic, i+1), retained, val.Index(i).Interface())
}
case reflect.Map:
// loop map
for iter := reflect.ValueOf(payload).MapRange(); iter.Next(); {
k := iter.Key().String()
m.publishComplex(fmt.Sprintf("%s/%s", topic, k), retained, iter.Value().Interface())
}
case reflect.Struct:
val := reflect.ValueOf(payload)
typ := val.Type()
// loop struct
for i := 0; i < typ.NumField(); i++ {
if f := typ.Field(i); f.IsExported() {
n := f.Name
m.publishComplex(fmt.Sprintf("%s/%s", topic, strings.ToLower(n[:1])+n[1:]), retained, val.Field(i).Interface())
}
}
case reflect.Pointer:
if !reflect.ValueOf(payload).IsNil() {
m.publishComplex(topic, retained, reflect.Indirect(reflect.ValueOf(payload)).Interface())
return
}
payload = nil
fallthrough
default:
m.publishSingleValue(topic, retained, payload)
}
}
func (m *MQTT) publishString(topic string, retained bool, payload string) {
token := m.Handler.Client.Publish(topic, m.Handler.Qos, retained, m.encode(payload))
go m.Handler.WaitForToken("send", topic, token)
}
func (m *MQTT) publishSingleValue(topic string, retained bool, payload interface{}) {
m.publisher(topic, retained, m.encode(payload))
}
func (m *MQTT) publish(topic string, retained bool, payload interface{}) {
// publish phase values
if slice, ok := payload.([]float64); ok && len(slice) == 3 {
var total float64
for i, v := range slice {
total += v
m.publishSingleValue(fmt.Sprintf("%s/l%d", topic, i+1), retained, v)
}
// publish sum value
m.publishSingleValue(topic, retained, total)
return
}
m.publishComplex(topic, retained, payload)
}
func (m *MQTT) Listen(site site.API) error {
if err := m.listenSiteSetters(m.root+"/site", site); err != nil {
return err
}
// loadpoint setters
for id, lp := range site.Loadpoints() {
topic := fmt.Sprintf("%s/loadpoints/%d", m.root, id+1)
if err := m.listenLoadpointSetters(topic, site, lp); err != nil {
return err
}
}
// vehicle setters
for _, vehicle := range site.Vehicles().Settings() {
topic := fmt.Sprintf("%s/vehicles/%s", m.root, vehicle.Name())
if err := m.listenVehicleSetters(topic, vehicle); err != nil {
return err
}
}
return nil
}
func (m *MQTT) listenSiteSetters(topic string, site site.API) error {
for _, s := range []setter{
{"/bufferSoc", floatSetter(site.SetBufferSoc)},
{"/bufferStartSoc", floatSetter(site.SetBufferStartSoc)},
{"/batteryDischargeControl", boolSetter(site.SetBatteryDischargeControl)},
{"/prioritySoc", floatSetter(site.SetPrioritySoc)},
{"/residualPower", floatSetter(site.SetResidualPower)},
{"/smartCostLimit", floatSetter(func(limit float64) error {
for _, lp := range site.Loadpoints() {
lp.SetSmartCostLimit(limit)
}
return nil
})},
} {
if err := m.Handler.ListenSetter(topic+s.topic, s.fun); err != nil {
return err
}
}
return nil
}
func (m *MQTT) listenLoadpointSetters(topic string, site site.API, lp loadpoint.API) error {
for _, s := range []setter{
{"/mode", setterFunc(api.ChargeModeString, pass(lp.SetMode))},
{"/phases", intSetter(lp.SetPhases)},
{"/limitSoc", intSetter(pass(lp.SetLimitSoc))},
{"/minCurrent", floatSetter(lp.SetMinCurrent)},
{"/maxCurrent", floatSetter(lp.SetMaxCurrent)},
{"/limitEnergy", floatSetter(pass(lp.SetLimitEnergy))},
{"/enableThreshold", floatSetter(pass(lp.SetEnableThreshold))},
{"/disableThreshold", floatSetter(pass(lp.SetDisableThreshold))},
{"/smartCostLimit", floatSetter(pass(lp.SetSmartCostLimit))},
{"/planEnergy", func(payload string) error {
var plan struct {
Time time.Time `json:"time"`
Value float64 `json:"value"`
}
err := json.Unmarshal([]byte(payload), &plan)
if err == nil {
err = lp.SetPlanEnergy(plan.Time, plan.Value)
}
return err
}},
{"/vehicle", func(payload string) error {
// https://github.com/evcc-io/evcc/issues/11184 empty payload is swallowed by listener
if payload == "-" {
lp.SetVehicle(nil)
return nil
}
vehicle, err := site.Vehicles().ByName(payload)
if err == nil {
lp.SetVehicle(vehicle.Instance())
}
return err
}},
} {
if err := m.Handler.ListenSetter(topic+s.topic, s.fun); err != nil {
return err
}
}
return nil
}
func (m *MQTT) listenVehicleSetters(topic string, v vehicle.API) error {
for _, s := range []setter{
{topic + "/limitSoc", intSetter(pass(v.SetLimitSoc))},
{topic + "/minSoc", intSetter(pass(v.SetMinSoc))},
{topic + "/planSoc", func(payload string) error {
var plan struct {
Time time.Time `json:"time"`
Value int `json:"value"`
}
err := json.Unmarshal([]byte(payload), &plan)
if err == nil {
err = v.SetPlanSoc(plan.Time, plan.Value)
}
return err
}},
} {
if err := m.Handler.ListenSetter(s.topic, s.fun); err != nil {
return err
}
}
return nil
}
// Run starts the MQTT publisher for the MQTT API
func (m *MQTT) Run(site site.API, in <-chan util.Param) {
// number of loadpoints
topic := fmt.Sprintf("%s/loadpoints", m.root)
m.publish(topic, true, len(site.Loadpoints()))
// number of vehicles
topic = fmt.Sprintf("%s/vehicles", m.root)
m.publish(topic, true, len(site.Vehicles().Settings()))
for i := 0; i < 10; i++ {
m.publish(fmt.Sprintf("%s/site/pv/%d", m.root, i), true, nil)
m.publish(fmt.Sprintf("%s/site/battery/%d", m.root, i), true, nil)
m.publish(fmt.Sprintf("%s/site/vehicles/%d", m.root, i), true, nil)
}
// alive indicator
var updated time.Time
// publish
for p := range in {
switch {
case p.Loadpoint != nil:
id := *p.Loadpoint + 1
topic = fmt.Sprintf("%s/loadpoints/%d/%s", m.root, id, p.Key)
case p.Key == "vehicles":
topic = fmt.Sprintf("%s/vehicles", m.root)
default:
topic = fmt.Sprintf("%s/site/%s", m.root, p.Key)
}
// alive indicator
if time.Since(updated) > time.Second {
updated = time.Now()
m.publish(fmt.Sprintf("%s/updated", m.root), true, updated.Unix())
}
// value
m.publish(topic, true, p.Val)
}
}