forked from shirou/mqttcli
/
mqtt.go
125 lines (104 loc) · 2.72 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
package main
import (
"crypto/rand"
"crypto/tls"
"fmt"
"time"
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
var MaxClientIdLen = 8
type MQTTClient struct {
Client *MQTT.MqttClient
Opts *MQTT.ClientOptions
}
// Connects connect to the MQTT broker with Options.
func (m *MQTTClient) Connect() (*MQTT.MqttClient, error) {
m.Client = MQTT.NewClient(m.Opts)
_, err := m.Client.Start()
if err != nil {
return nil, err
}
return m.Client, nil
}
func (m *MQTTClient) Publish(topic string, payload []byte, qos int, retain bool) error {
mqttmsg := MQTT.NewMessage(payload)
// FIXME: validate qos number
mqttmsg.SetQoS(MQTT.QoS(qos))
mqttmsg.SetRetainedFlag(retain)
receipt := m.Client.PublishMessage(topic, mqttmsg)
<-receipt
return nil
}
func onMessageReceived(client *MQTT.MqttClient, message MQTT.Message) {
log.Infof("topic:%s / msg:%s", message.Topic(), message.Payload())
fmt.Println(string(message.Payload()))
}
func (m *MQTTClient) Subscribe(topic string, qos int) error {
topicFilter, err := MQTT.NewTopicFilter(topic, byte(qos))
if err != nil {
return err
}
_, err = m.Client.StartSubscription(onMessageReceived, topicFilter)
if err != nil {
return err
}
for {
time.Sleep(1 * time.Second)
}
return nil
}
// getRandomClientId returns randomized ClientId.
func getRandomClientId() string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, MaxClientIdLen)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return "mqttcli-" + string(bytes)
}
// NewOption returns ClientOptions via parsing command line options.
func NewOption(c *cli.Context) *MQTT.ClientOptions {
opts := MQTT.NewClientOptions()
getSettingsFromFile(c.String("conf"), opts)
clientId := c.String("i")
if clientId == "" {
clientId = getRandomClientId()
}
opts.SetClientId(clientId)
cafile := c.String("cafile")
scheme := "tcp"
if cafile != "" {
scheme = "ssl"
}
insecure := true
if insecure {
tlsConfig := &tls.Config{InsecureSkipVerify: true}
opts.SetTlsConfig(tlsConfig)
}
user := c.String("u")
if user != "" {
opts.SetUsername(user)
}
password := c.String("P")
if password != "" {
opts.SetPassword(password)
}
host := c.String("host")
port := c.Int("p")
if host != "" {
brokerUri := fmt.Sprintf("%s://%s:%d", scheme, host, port)
log.Infof("Broker URI: %s", brokerUri)
opts.AddBroker(brokerUri)
}
keepalive := c.Int("keepalive")
if keepalive == 30 {
log.Infof("KeepAlive is default 30")
} else {
log.Infof("KeepAlive is set other than default 30: %d", keepalive)
opts.SetKeepAlive(uint(keepalive))
}
return opts
}