-
Notifications
You must be signed in to change notification settings - Fork 1
/
publish.go
89 lines (78 loc) · 2.31 KB
/
publish.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
import (
"bytes"
MQTT "github.com/eclipse/paho.mqtt.golang"
log "github.com/sirupsen/logrus"
"net"
"os"
"time"
)
//define a function for the default message handler
var f MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
log.WithFields(log.Fields{
"topic": msg.Topic(),
"payload": msg.Payload(),
}).Debug("MQTT recieved")
//msg.Ack()
}
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 := "xm122level-" + getMacAddr()
log.Info("Connecting as ", clientId)
opts.SetClientID(clientId)
opts.SetDefaultPublishHandler(f)
opts.SetAutoReconnect(true)
opts.SetPassword(password)
log.Debug("MQTT username: ", username)
opts.SetUsername(username)
opts.SetKeepAlive(60 * time.Second)
opts.SetPingTimeout(20 * time.Second)
opts.SetConnectionLostHandler(onLost)
opts.SetOrderMatters(false)
opts.SetOnConnectHandler(onConnect)
opts.SetMaxReconnectInterval(30 * time.Second)
opts.SetMessageChannelDepth(500)
//create and start a client using the above ClientOptions
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
log.Panic("failed to connect to MQTT Broker, bailing out: ", token.Error())
os.Exit(-1)
}
return c
}
/*
type OpenSenseMapJSON struct {
Sensor string `json:"sensor"`
Value string `json:"value"`
} */
func pub(cl MQTT.Client, topic string, payload string) error {
log.WithFields(log.Fields{
"topic": topic,
"payload": payload,
}).Debug("MQTT")
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
}
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 onConnect(client MQTT.Client) {
log.Info("MQTT client connected.")
}
func onLost(client MQTT.Client, err error) {
log.Warn("MQTT connection lost: ", err)
}