mqtt reconfigure
This commit is contained in:
parent
264ecbf657
commit
f9813a0c3f
@ -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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,16 +62,17 @@ func MQTTreconfigure() {
|
|||||||
mqtt.Disconnect()
|
mqtt.Disconnect()
|
||||||
}
|
}
|
||||||
MQTTSetup()
|
MQTTSetup()
|
||||||
go MQTTRefreshLoop()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func MQTTRefreshLoop() {
|
func MQTTRefreshLoop() {
|
||||||
for {
|
for {
|
||||||
|
if mqtt.Connected() {
|
||||||
for o := range outlet.Outlets {
|
for o := range outlet.Outlets {
|
||||||
topic := outlet.Outlets[o].Channel.MQTTStateTopic()
|
topic := outlet.Outlets[o].Channel.MQTTStateTopic()
|
||||||
value := outlet.Outlets[o].Channel.ToString()
|
value := outlet.Outlets[o].Channel.ToString()
|
||||||
mqtt.Publish(topic, value)
|
mqtt.Publish(topic, value)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
time.Sleep(30 * time.Second)
|
time.Sleep(30 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user