72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"git.openpdu.org/OpenPDU/openpdu/board"
|
|
"git.openpdu.org/OpenPDU/openpdu/mqtt"
|
|
"git.openpdu.org/OpenPDU/openpdu/outlet"
|
|
"git.openpdu.org/OpenPDU/openpdu/syslog"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func CreateOutlets() {
|
|
|
|
outletsConfig := viper.Sub("outlets")
|
|
if outletsConfig == nil {
|
|
syslog.Warning("No outlet configured")
|
|
return
|
|
}
|
|
|
|
outlet.Outlets = make([]*outlet.Outlet, len(board.AllChannels))
|
|
|
|
for key := range outletsConfig.AllSettings() {
|
|
outletConfig := outletsConfig.Sub(key)
|
|
num := outletConfig.GetUint("num")
|
|
outletConfig.SetDefault("description", "no description")
|
|
description := outletConfig.GetString("description")
|
|
channelID := strings.ToLower(outletConfig.GetString("channelID"))
|
|
channel := board.AllChannels[channelID]
|
|
// TODO: channel non esistente
|
|
o := outlet.Outlet{
|
|
ID: key,
|
|
Num: num,
|
|
Description: description,
|
|
Channel: channel,
|
|
}
|
|
|
|
outlet.Outlets[num] = &o
|
|
}
|
|
}
|
|
|
|
func MQTTSetup() {
|
|
mqtt.Setup()
|
|
for o := range outlet.Outlets {
|
|
topic := outlet.Outlets[o].Channel.MQTTCommandTopic()
|
|
if topic == "" {
|
|
continue
|
|
}
|
|
mqtt.Subscribe(topic, outlet.Outlets[o].Channel.MQTTHandler)
|
|
}
|
|
}
|
|
|
|
func MQTTreconfigure() {
|
|
if mqtt.Connected() {
|
|
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)
|
|
}
|
|
time.Sleep(30 * time.Second)
|
|
}
|
|
}
|