forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.go
188 lines (152 loc) Β· 4.38 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
package provider
import (
"time"
"github.com/evcc-io/evcc/provider/mqtt"
"github.com/evcc-io/evcc/provider/pipeline"
"github.com/evcc-io/evcc/util"
)
// Mqtt provider
type Mqtt struct {
log *util.Logger
client *mqtt.Client
topic string
retained bool
payload string
scale float64
timeout time.Duration
pipeline *pipeline.Pipeline
}
func init() {
registry.Add("mqtt", NewMqttFromConfig)
}
// NewMqttFromConfig creates Mqtt provider
func NewMqttFromConfig(other map[string]interface{}) (IntProvider, error) {
cc := struct {
mqtt.Config `mapstructure:",squash"`
Topic, Payload string // Payload only applies to setters
Retained bool
Scale float64
Timeout time.Duration
pipeline.Settings `mapstructure:",squash"`
}{
Scale: 1,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
log := util.NewLogger("mqtt")
client, err := mqtt.RegisteredClientOrDefault(log, cc.Config)
if err != nil {
return nil, err
}
m := NewMqtt(log, client, cc.Topic, cc.Timeout).WithScale(cc.Scale).WithPayload(cc.Payload)
if cc.Retained {
m = m.WithRetained()
}
pipe, err := pipeline.New(cc.Settings)
if err == nil {
m = m.WithPipeline(pipe)
}
return m, err
}
// NewMqtt creates mqtt provider for given topic
func NewMqtt(log *util.Logger, client *mqtt.Client, topic string, timeout time.Duration) *Mqtt {
m := &Mqtt{
log: log,
client: client,
topic: topic,
scale: 1,
timeout: timeout,
}
return m
}
// WithPayload adds payload for setters
func (m *Mqtt) WithPayload(payload string) *Mqtt {
m.payload = payload
return m
}
// WithRetained adds retained flag for setters
func (m *Mqtt) WithRetained() *Mqtt {
m.retained = true
return m
}
// WithScale sets scaler for getters
func (m *Mqtt) WithScale(scale float64) *Mqtt {
m.scale = scale
return m
}
// WithPipeline adds a processing pipeline
func (p *Mqtt) WithPipeline(pipeline *pipeline.Pipeline) *Mqtt {
p.pipeline = pipeline
return p
}
var _ FloatProvider = (*Mqtt)(nil)
// newReceiver creates a msgHandler and subscribes it to the topic.
// receiver will ensure actual data guarded by `timeout` and return error
// if initial value is not received within `timeout` or max. 10s if timeout is not given.
func (m *Mqtt) newReceiver() *msgHandler {
h := &msgHandler{
topic: m.topic,
scale: m.scale,
wait: util.NewWaiter(m.timeout, func() { m.log.DEBUG.Printf("%s wait for initial value", m.topic) }),
pipeline: m.pipeline,
}
m.client.Listen(m.topic, h.receive)
return h
}
// FloatGetter creates handler for float64 from MQTT topic that returns cached value
func (m *Mqtt) FloatGetter() func() (float64, error) {
h := m.newReceiver()
return h.floatGetter
}
var _ IntProvider = (*Mqtt)(nil)
// IntGetter creates handler for int64 from MQTT topic that returns cached value
func (m *Mqtt) IntGetter() func() (int64, error) {
h := m.newReceiver()
return h.intGetter
}
var _ StringProvider = (*Mqtt)(nil)
// StringGetter creates handler for string from MQTT topic that returns cached value
func (m *Mqtt) StringGetter() func() (string, error) {
h := m.newReceiver()
return h.stringGetter
}
var _ BoolProvider = (*Mqtt)(nil)
// BoolGetter creates handler for string from MQTT topic that returns cached value
func (m *Mqtt) BoolGetter() func() (bool, error) {
h := m.newReceiver()
return h.boolGetter
}
var _ SetIntProvider = (*Mqtt)(nil)
// IntSetter publishes topic with parameter replaced by int value
func (m *Mqtt) IntSetter(param string) func(int64) error {
return func(v int64) error {
payload, err := setFormattedValue(m.payload, param, v)
if err != nil {
return err
}
return m.client.Publish(m.topic, m.retained, payload)
}
}
var _ SetBoolProvider = (*Mqtt)(nil)
// BoolSetter invokes script with parameter replaced by bool value
func (m *Mqtt) BoolSetter(param string) func(bool) error {
return func(v bool) error {
payload, err := setFormattedValue(m.payload, param, v)
if err != nil {
return err
}
return m.client.Publish(m.topic, m.retained, payload)
}
}
var _ SetStringProvider = (*Mqtt)(nil)
// StringSetter invokes script with parameter replaced by string value
func (m *Mqtt) StringSetter(param string) func(string) error {
return func(v string) error {
payload, err := setFormattedValue(m.payload, param, v)
if err != nil {
return err
}
return m.client.Publish(m.topic, m.retained, payload)
}
}