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 { func Connected() bool {
if MQTTclient == nil {
return false
}
return MQTTclient.IsConnected() return MQTTclient.IsConnected()
} }

View File

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

View File

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