-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.go
192 lines (164 loc) · 4.55 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
package main
import (
"crypto/tls"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"path"
"strconv"
"strings"
)
func GetMessageHandlerSetColor(bleLight *BleLight) (handler func(client mqtt.Client, message mqtt.Message)) {
return func(client mqtt.Client, message mqtt.Message) {
colorValue := string(message.Payload()[:])
color, err := numberStringToUInt8Slice(colorValue)
if err != nil {
log.Errorf("unable to parse color, '%s': %v", colorValue, err)
return
}
if len(color) != 3 {
log.Errorf("invalid color length: %d", len(color))
return
}
r := color[0]
g := color[1]
b := color[2]
// Simulate simple power control to be nice to Google Assistant
if r == g && g == b && b == r && r == 0 {
if err := (*bleLight).SetPower(false); err != nil {
log.Error("unable to turn off light: ", err)
}
return
} else {
if err := (*bleLight).SetPower(true); err != nil {
log.Error("unable to turn on light: ", err)
// ignore error, light might be already on so we can as well try to set the other values
}
}
// Simulate simple white control
if r == g && g == b && b == r {
if err := (*bleLight).SetWarmWhite(r); err != nil {
log.Error("unable to set white intensity: ", err)
}
return
}
if err := (*bleLight).SetRGB(r, g, b); err != nil {
log.Error("unable to set RGB color: ", err)
return
}
}
}
func GetMessageHandlerSetMode(bleLight *BleLight) (handler func(client mqtt.Client, message mqtt.Message)) {
return func(client mqtt.Client, message mqtt.Message) {
str := string(message.Payload()[:])
splitStr := strings.Split(str, ",")
if len(splitStr) != 2 {
log.Errorf("invalid number of separators in mode string '%s': %d", str, len(splitStr))
return
}
mode := splitStr[0]
speed, err := strconv.ParseUint(splitStr[1], 10, 8)
if err != nil {
log.Errorf("unable to parse mode speed '%s': %v", str, err)
return
}
if err := (*bleLight).SetMode(mode, uint8(speed)); err != nil {
log.Error("unable to set mode: ", err)
return
}
}
}
func GetMessageHandlerSetPower(bleLight *BleLight) (handler func(client mqtt.Client, message mqtt.Message)) {
return func(client mqtt.Client, message mqtt.Message) {
str := string(message.Payload()[:])
if str != "off" && str != "on" {
log.Error("invalid power control string: ", str)
}
if err := (*bleLight).SetPower(str == "on"); err != nil {
log.Error("unable to set light power: ", err)
}
}
}
func StatusChanPublisher(
mountpoint string,
client *mqtt.Client,
statusChan <-chan LightStatus,
stopRope StopRope,
) {
if err := stopRope.Hold(); err != nil {
return
}
defer stopRope.Release()
var lastUpdate *map[string]string = nil
modeTopic := path.Join(mountpoint, "status/mode")
rgbTopic := path.Join(mountpoint, "status/color")
powerTopic := path.Join(mountpoint, "status/power")
Loop:
for {
select {
case status, ok := <-statusChan:
if !ok {
break Loop
}
update := make(map[string]string)
var mode string
if status.Mode == "control" {
if status.WarmWhite {
mode = "white"
} else {
mode = "rgb"
}
} else {
mode = fmt.Sprintf("%s,%d", status.Mode, status.Speed)
}
update[modeTopic] = mode
update[rgbTopic] = getColorString(status.R, status.G, status.B)
if status.Power {
update[powerTopic] = "on"
} else {
update[powerTopic] = "off"
}
// Publish only changed values
for topic, payload := range update {
if lastUpdate != nil {
if oldPayload := (*lastUpdate)[topic]; oldPayload == payload {
continue
}
}
(*client).Publish(topic, 1, true, payload)
}
lastUpdate = &update
break
case <-stopRope.WaitCut():
break Loop
}
}
}
func ConnectClient(config *MQTTConfig) (client mqtt.Client, err error) {
onlineTopic := path.Join(*(config.MountPoint), "online")
clientOptions := mqtt.NewClientOptions()
for _, broker := range config.Servers {
clientOptions.AddBroker(broker)
}
clientOptions.SetAutoReconnect(true)
clientOptions.SetWill(onlineTopic, "false", 1, true)
if config.ClientID != nil {
clientOptions.SetClientID(*config.ClientID)
}
if config.Username != nil {
clientOptions.SetUsername(*config.Username)
}
if config.Password != nil {
clientOptions.SetPassword(*config.Password)
}
if config.TLS != nil {
clientOptions.SetTLSConfig(&tls.Config{
InsecureSkipVerify: config.TLS.InsecureSkipVerify,
})
}
client = mqtt.NewClient(clientOptions)
if token := client.Connect(); token.Wait() && token.Error() != nil {
err = token.Error()
}
client.Publish(onlineTopic, 1, true, "true")
return
}