/
mqttclient.go
69 lines (58 loc) · 1.52 KB
/
mqttclient.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
package publisher
import (
"encoding/json"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
logger "github.com/sirupsen/logrus"
)
type MqttMsg struct {
Topic string
Qos byte
Retained bool
Payload interface{}
}
type MqttConfig struct {
Host string
Port int
User string
Password string
ClientId string
}
type mqttClient struct {
cfg MqttConfig
client mqtt.Client
}
func newMqttClient(mqttconf MqttConfig) mqttClient {
newclient := mqttClient{
cfg: mqttconf,
}
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("tcp://%s:%d", mqttconf.Host, mqttconf.Port))
opts.SetClientID(mqttconf.ClientId)
opts.SetUsername(mqttconf.User)
opts.SetPassword(mqttconf.Password)
// opts.SetDefaultPublishHandler(messagePubHandler)
opts.OnConnect = connectHandler
opts.OnConnectionLost = connectLostHandler
newclient.client = mqtt.NewClient(opts)
return newclient
}
func (mc *mqttClient) Connect() {
if token := mc.client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
}
func (mc *mqttClient) Publish(msg *MqttMsg) {
jsonpayload, err := json.Marshal(msg.Payload)
if err != nil {
logger.Error("could not publish message: err:", err)
}
token := mc.client.Publish(msg.Topic, msg.Qos, msg.Retained, jsonpayload)
token.Wait()
}
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
logger.Info("mqtt client connected")
}
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
logger.Error("connect lost:", err.Error())
}