openpdu/src/mqtt/mqtt.go

103 lines
2.6 KiB
Go
Raw Normal View History

2021-01-09 18:38:57 +00:00
package mqtt
2020-12-23 09:11:11 +00:00
import (
2021-01-09 18:38:57 +00:00
"fmt"
"time"
2020-12-23 09:11:11 +00:00
2021-01-09 18:38:57 +00:00
"git.openpdu.org/OpenPDU/openpdu/syslog"
2020-12-23 09:11:11 +00:00
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/spf13/viper"
2020-12-23 09:11:11 +00:00
)
var MQTTclient MQTT.Client
2020-12-23 09:11:11 +00:00
func init() {
2021-01-04 11:28:20 +00:00
viper.SetDefault("Mqtt.CliendID", "OpenPDU") // max 23 chars
viper.SetDefault("Mqtt.Prefix", "openpdu") // max 23 chars
viper.SetDefault("Mqtt.Schema", "tcp")
viper.SetDefault("Mqtt.Host", "localhost")
viper.SetDefault("Mqtt.Port", "1883")
viper.SetDefault("Mqtt.Username", "")
viper.SetDefault("Mqtt.Password", "")
2021-01-19 13:27:24 +00:00
viper.SetDefault("Mqtt.HomeAssistant", false)
2020-12-23 09:11:11 +00:00
// MQTT.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
// MQTT.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
// MQTT.WARN = log.New(os.Stdout, "[WARN] ", 0)
// MQTT.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)
2020-12-23 09:11:11 +00:00
}
// https://girishjoshi.io/post/golang-paho-mqtt/
2021-01-09 18:38:57 +00:00
func Setup() {
2021-01-04 11:28:20 +00:00
uri := viper.GetString("Mqtt.Schema") + "://" + viper.GetString("Mqtt.Host") + ":" + viper.GetString("Mqtt.Port")
opts := MQTT.NewClientOptions().AddBroker(uri)
2021-01-04 11:28:20 +00:00
opts.SetClientID(viper.GetString("Mqtt.CliendID"))
2021-01-04 11:28:20 +00:00
if username := viper.GetString("Mqtt.Username"); username != "" {
opts.SetUsername(username)
}
2021-01-04 11:28:20 +00:00
if password := viper.GetString("Mqtt.Password"); password != "" {
opts.SetPassword(password)
}
opts.SetAutoReconnect(true)
opts.SetConnectRetryInterval(5 * time.Second)
opts.SetConnectTimeout(5 * time.Second)
opts.SetConnectionLostHandler(func(c MQTT.Client, err error) {
2021-01-09 18:38:57 +00:00
syslog.Err("mqtt connection lost error: " + err.Error())
})
opts.SetReconnectingHandler(func(c MQTT.Client, options *MQTT.ClientOptions) {
2021-01-09 18:38:57 +00:00
syslog.Notice("mqtt reconnecting")
})
opts.SetOnConnectHandler(func(c MQTT.Client) {
2021-01-09 18:38:57 +00:00
syslog.Notice("mqtt connected")
// MQTTclient.Publish("openpdu/status", 0, false, "connected")
})
MQTTclient = MQTT.NewClient(opts)
for {
token := MQTTclient.Connect()
token.Wait()
if MQTTclient.IsConnected() {
break
}
time.Sleep(5 * time.Second)
}
}
2021-01-09 18:38:57 +00:00
func Subscribe(topic string, handler func(MQTT.Client, MQTT.Message)) {
// MQTTHandler(MQTT.Client, MQTT.Message)
2021-01-19 13:27:24 +00:00
MQTTclient.Subscribe(viper.GetString("Mqtt.Prefix")+"/switch/"+topic, 1, handler)
2021-01-09 18:38:57 +00:00
syslog.Debug(fmt.Sprintf("MQTT subscribed to topic: %s", topic))
}
2021-01-09 18:38:57 +00:00
func Connected() bool {
2021-01-19 09:06:31 +00:00
if MQTTclient == nil {
return false
}
2021-01-09 18:38:57 +00:00
return MQTTclient.IsConnected()
}
2021-01-09 18:38:57 +00:00
func Disconnect() {
MQTTclient.Disconnect(250)
}
2021-01-09 18:38:57 +00:00
func Publish(topic string, value string) {
if MQTTclient.IsConnected() {
2021-01-19 13:27:24 +00:00
MQTTclient.Publish(viper.GetString("Mqtt.Prefix")+"/switch/"+topic, 0, false, value)
}
}
func PublishRoot(topic string, value string) {
if MQTTclient.IsConnected() {
MQTTclient.Publish(topic, 0, false, value)
}
}