forked from rondoval/aquarea2mqtt
/
mqtt.go
103 lines (87 loc) · 2.87 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
package main
import (
"context"
"fmt"
"log"
"strings"
"sync"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
type aquareaMQTT struct {
mqttClient mqtt.Client
commandChannel chan aquareaCommand
}
func mqttHandler(ctx context.Context, wg *sync.WaitGroup, config configType, dataChannel chan map[string]string, commandChannel chan aquareaCommand, statusChannel chan bool) {
defer wg.Done()
log.Println("Starting MQTT handler")
mqttKeepalive, err := time.ParseDuration(config.MqttKeepalive)
if err != nil {
log.Fatal(err)
}
var mqttInstance aquareaMQTT
mqttInstance.commandChannel = commandChannel
mqttInstance.makeMQTTConn(config.MqttServer, config.MqttPort, config.MqttLogin, config.MqttPass, config.MqttClientID, mqttKeepalive)
defer mqttInstance.mqttClient.Disconnect(2000)
defer mqttInstance.setStatus(false)
for {
select {
case dataToPublish := <-dataChannel:
mqttInstance.publish(dataToPublish)
case online := <-statusChannel:
mqttInstance.setStatus(online)
case <-ctx.Done():
return
}
}
}
func (am *aquareaMQTT) makeMQTTConn(mqttServer string, mqttPort int, mqttLogin, mqttPass, mqttClientID string, mqttKeepalive time.Duration) {
log.Println("Connecting to MQTT broker")
//set MQTT options
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("%s://%s:%v", "tcp", mqttServer, mqttPort))
opts.SetPassword(mqttPass)
opts.SetUsername(mqttLogin)
opts.SetClientID(mqttClientID)
opts.SetKeepAlive(mqttKeepalive)
opts.SetCleanSession(true) // don't want to receive entire backlog of setting changes
opts.SetAutoReconnect(true) // default, but I want it explicit
opts.SetOnConnectHandler(func(c mqtt.Client) {
c.Subscribe("aquarea/+/settings/+/set", 2, am.handleSubscription)
})
opts.SetWill("aquarea/status", "offline", byte(0), true)
// connect to broker
am.mqttClient = mqtt.NewClient(opts)
token := am.mqttClient.Connect()
if token.Wait() && token.Error() != nil {
log.Fatalf("Fail to connect broker, %v", token.Error())
}
log.Println("MQTT connected")
am.setStatus(false) // offline till Service Cloud is connected
}
func (am *aquareaMQTT) setStatus(online bool) {
var status string
if online {
status = "online"
} else {
status = "offline"
}
am.mqttClient.Publish("aquarea/status", byte(0), true, status)
}
func (am *aquareaMQTT) handleSubscription(mclient mqtt.Client, msg mqtt.Message) {
topicPieces := strings.Split(msg.Topic(), "/")
if len(topicPieces) > 3 {
deviceID := topicPieces[1]
setting := topicPieces[3]
log.Printf("Received: Device ID %s setting: %s", deviceID, setting)
am.commandChannel <- aquareaCommand{deviceID, setting, string(msg.Payload())}
}
}
func (am *aquareaMQTT) publish(data map[string]string) {
for key, value := range data {
token := am.mqttClient.Publish(key, byte(0), true, value)
if token.Wait() && token.Error() != nil {
fmt.Printf("Fail to publish, %v", token.Error())
}
}
}