53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package mqtt
|
|
|
|
import (
|
|
mqttLib "github.com/eclipse/paho.mqtt.golang"
|
|
)
|
|
|
|
type connection struct {
|
|
conf *Config
|
|
client mqttLib.Client
|
|
}
|
|
|
|
func newConnection(conf *Config) *connection {
|
|
conn := &connection{
|
|
conf: conf,
|
|
}
|
|
|
|
opts := mqttLib.NewClientOptions()
|
|
|
|
broker := conn.conf.Host + ":" + conn.conf.Port
|
|
|
|
opts.AddBroker(broker)
|
|
opts.SetClientID(conn.conf.ClientID)
|
|
opts.SetUsername(conn.conf.Username)
|
|
opts.SetPassword(conn.conf.Password)
|
|
opts.SetCleanSession(conn.conf.CleanSession)
|
|
opts.SetAutoReconnect(conn.conf.AutoReconnect)
|
|
opts.SetKeepAlive(conn.conf.KeepAlive)
|
|
opts.SetMessageChannelDepth(conn.conf.MsgChanDept)
|
|
if conn.conf.AutoReconnect {
|
|
opts.SetResumeSubs(true)
|
|
}
|
|
|
|
conn.client = mqttLib.NewClient(opts)
|
|
|
|
return conn
|
|
}
|
|
|
|
func (conn *connection) connect() error {
|
|
if token := conn.client.Connect(); token.Wait() && token.Error() != nil {
|
|
return token.Error()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (conn *connection) publish(topic string, qos byte, payload []byte, retain bool) mqttLib.Token {
|
|
return conn.client.Publish(topic, qos, retain, payload)
|
|
}
|
|
|
|
func (conn *connection) subscribe(topic string, qos byte, callback func(mqttClient mqttLib.Client, message mqttLib.Message)) mqttLib.Token {
|
|
return conn.client.Subscribe(topic, qos, callback)
|
|
}
|