-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqttclient.go
40 lines (32 loc) · 850 Bytes
/
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
package mattress
import (
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
"log"
)
type MqttClient struct {
client *MQTT.Client
}
var Mqtt *MqttClient
func NewMqttClient() *MqttClient {
clientoptions := MQTT.NewClientOptions().SetClientID("publisher").AddBroker("tcp://127.0.0.1:1883")
return &MqttClient{
client: MQTT.NewClient(clientoptions),
}
}
func (m *MqttClient) Connection() {
if token := m.client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
}
func (m *MqttClient) Publish(topic string, message string) {
if token := m.client.Publish(topic, 1, false, message); token.Wait() && token.Error() != nil {
log.Println(token.Error())
log.Println("Failed to send message")
}
}
func SetMqttClient(m *MqttClient) {
Mqtt = m
}
func GetMqttClient() *MqttClient {
return Mqtt
}