-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmqttpub.go
89 lines (76 loc) · 2.65 KB
/
mqttpub.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
package main
/* This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"bytes"
log "github.com/sirupsen/logrus"
"net"
"os"
"time"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
//define a function for the default message handler
var f MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
log.Debug("TOPIC/MSG", msg.Topic(), "/", msg.Payload())
//msg.Ack()
}
func getMacAddr() (addr string) {
interfaces, err := net.Interfaces()
if err == nil {
for _, i := range interfaces {
if i.Flags&net.FlagUp != 0 && bytes.Compare(i.HardwareAddr, nil) != 0 {
// Don't use random as we have a real address
addr = i.HardwareAddr.String()
break
}
}
}
return
}
func connectMQTT(host string, username string, password string) MQTT.Client {
opts := MQTT.NewClientOptions().AddBroker(host)
//when testing two clients may be running thus we grab a MAC address to create a semi-static machine specific clientID
clientId := "wolfmqttbridge-" + getMacAddr()
log.Info("Connecting as ", clientId)
opts.SetClientID(clientId)
opts.SetDefaultPublishHandler(f)
opts.SetAutoReconnect(true)
opts.SetPassword(password)
opts.SetUsername(username)
opts.SetKeepAlive(120 * time.Second)
opts.SetPingTimeout(20 * time.Second)
opts.SetConnectionLostHandler(onLost)
opts.SetOrderMatters(false)
opts.SetOnConnectHandler(onConnect)
opts.SetMaxReconnectInterval(10 * time.Second)
//create and start a client using the above ClientOptions
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
log.Fatal("failed to connect to MQTT Broker, bailing out ", token.Error())
os.Exit(-1)
}
return c
}
func onConnect(client MQTT.Client) {
log.Info("MQTT client connected.")
}
func onLost(client MQTT.Client, err error) {
log.Warn("MQTT connection lost: ", err)
}
func pub(cl MQTT.Client, topic string, payload string) error {
log.Debug("MQTT: ", topic, " <- ", payload)
if token := cl.Publish(topic, 1, false, payload); token.Wait() && token.Error() != nil {
log.Error("failed to publish message to ", topic, " error: ", token.Error())
return token.Error()
}
return nil
}