mqtt reconfigure

This commit is contained in:
Paolo Asperti 2021-01-19 10:06:31 +01:00
parent 264ecbf657
commit f9813a0c3f
3 changed files with 27 additions and 5 deletions

View File

@ -78,6 +78,9 @@ func Subscribe(topic string, handler func(MQTT.Client, MQTT.Message)) {
}
func Connected() bool {
if MQTTclient == nil {
return false
}
return MQTTclient.IsConnected()
}

View File

@ -8,6 +8,7 @@ import (
"git.openpdu.org/OpenPDU/openpdu/mqtt"
"git.openpdu.org/OpenPDU/openpdu/outlet"
"git.openpdu.org/OpenPDU/openpdu/syslog"
"git.openpdu.org/OpenPDU/openpdu/webui"
"github.com/spf13/viper"
)
@ -40,6 +41,11 @@ func CreateOutlets() {
}
}
func init() {
webui.SetMQTTReconfigFunction(MQTTreconfigure)
// mqtt.MQTTReconfig = MQTTreconfigure()
}
func MQTTSetup() {
mqtt.Setup()
for o := range outlet.Outlets {
@ -56,15 +62,16 @@ func MQTTreconfigure() {
mqtt.Disconnect()
}
MQTTSetup()
go MQTTRefreshLoop()
}
func MQTTRefreshLoop() {
for {
for o := range outlet.Outlets {
topic := outlet.Outlets[o].Channel.MQTTStateTopic()
value := outlet.Outlets[o].Channel.ToString()
mqtt.Publish(topic, value)
if mqtt.Connected() {
for o := range outlet.Outlets {
topic := outlet.Outlets[o].Channel.MQTTStateTopic()
value := outlet.Outlets[o].Channel.ToString()
mqtt.Publish(topic, value)
}
}
time.Sleep(30 * time.Second)
}

View File

@ -7,6 +7,14 @@ import (
"gopkg.in/macaron.v1"
)
type MQTTReconfigFunction func()
var mqttReconfigFunction MQTTReconfigFunction
func SetMQTTReconfigFunction(f MQTTReconfigFunction) {
mqttReconfigFunction = f
}
func mqttPage(ctx *macaron.Context) {
ctx.Data["schema"] = viper.GetString("Mqtt.Schema")
ctx.Data["host"] = viper.GetString("Mqtt.Host")
@ -52,5 +60,9 @@ func mqttPost(ctx *macaron.Context, f MQTTPostForm) {
viper.WriteConfig()
if mqttReconfigFunction != nil {
go mqttReconfigFunction()
}
mqttPage(ctx)
}