config refactor

This commit is contained in:
Paolo Asperti 2021-01-04 12:28:20 +01:00
parent 23e8c067e8
commit 0687cb2258
5 changed files with 99 additions and 41 deletions

58
openpdu_example.yaml Normal file
View File

@ -0,0 +1,58 @@
display:
h: 32
rotated: false
swaptopbottom: false
type: ssd1306
w: 128
hostname: openpdu-rack1
mqtt:
cliendid: openpdu-main
host: 192.168.1.230
password: ""
port: "1883"
prefix: openpdu-main
schema: tcp
username: ""
ups:
host: 192.168.1.132
name: ups
password: slave
username: monuser
boards:
2306c7db-9d2e-4f55-8944-6a5a8b75a4a3:
type: dummy
inverted: false
channelCount: 4
channels:
4976c508-1f63-4894-a859-cc5bd555b3c1:
num: 0
name: autonomo
startValue: false
c0a4d271-2b75-41c0-8b05-86c5ca0b3e94:
num: 1
name: fenomeno
startValue: false
4d0b384f-6bcb-4eca-a803-ebea90c2a434:
num: 2
name: ecomeno
startValue: false
acb5e9d5-959c-4427-a850-17c262b96c16:
num: 3
name: ripopolo
startValue: false
outlets:
0fca047e-0e7f-4cee-83a8-2086ee306faf:
num: 0
channelID: 4976c508-1f63-4894-a859-cc5bd555b3c1
6b36abdc-dc8d-424a-997d-ffb59642c749:
num: 1
channelID: 4d0b384f-6bcb-4eca-a803-ebea90c2a434
e14f53ba-8b53-4327-a1c7-10cf148d6c42:
num: 2
channelID: c0a4d271-2b75-41c0-8b05-86c5ca0b3e94
1e66a9b5-54fe-4e0e-b34f-4d96b58f2111:
num: 3
channelID: acb5e9d5-959c-4427-a850-17c262b96c16

View File

@ -10,13 +10,13 @@ import (
var MQTTclient MQTT.Client var MQTTclient MQTT.Client
func init() { func init() {
viper.SetDefault("MqttCliendID", "OpenPDU") // max 23 chars viper.SetDefault("Mqtt.CliendID", "OpenPDU") // max 23 chars
viper.SetDefault("MqttPrefix", "openpdu") // max 23 chars viper.SetDefault("Mqtt.Prefix", "openpdu") // max 23 chars
viper.SetDefault("MqttSchema", "tcp") viper.SetDefault("Mqtt.Schema", "tcp")
viper.SetDefault("MqttHost", "localhost") viper.SetDefault("Mqtt.Host", "localhost")
viper.SetDefault("MqttPort", "1883") viper.SetDefault("Mqtt.Port", "1883")
viper.SetDefault("MqttUsername", "") viper.SetDefault("Mqtt.Username", "")
viper.SetDefault("MqttPassword", "") viper.SetDefault("Mqtt.Password", "")
// MQTT.ERROR = log.New(os.Stdout, "[ERROR] ", 0) // MQTT.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
// MQTT.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0) // MQTT.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
@ -30,16 +30,16 @@ func init() {
// https://girishjoshi.io/post/golang-paho-mqtt/ // https://girishjoshi.io/post/golang-paho-mqtt/
func mqttLoop() { func mqttLoop() {
uri := viper.GetString("MqttSchema") + "://" + viper.GetString("MqttHost") + ":" + viper.GetString("MqttPort") uri := viper.GetString("Mqtt.Schema") + "://" + viper.GetString("Mqtt.Host") + ":" + viper.GetString("Mqtt.Port")
opts := MQTT.NewClientOptions().AddBroker(uri) opts := MQTT.NewClientOptions().AddBroker(uri)
opts.SetClientID(viper.GetString("MqttCliendID")) opts.SetClientID(viper.GetString("Mqtt.CliendID"))
if username := viper.GetString("MqttUsername"); username != "" { if username := viper.GetString("Mqtt.Username"); username != "" {
opts.SetUsername(username) opts.SetUsername(username)
} }
if password := viper.GetString("MqttPassword"); password != "" { if password := viper.GetString("Mqtt.Password"); password != "" {
opts.SetPassword(password) opts.SetPassword(password)
} }
@ -80,6 +80,6 @@ func MQTTreconfigure() {
func MQTTpublish(topic string, value string) { func MQTTpublish(topic string, value string) {
if MQTTclient.IsConnected() { if MQTTclient.IsConnected() {
MQTTclient.Publish(viper.GetString("MqttPrefix")+"/"+topic, 0, false, value) MQTTclient.Publish(viper.GetString("Mqtt.Prefix")+"/"+topic, 0, false, value)
} }
} }

View File

@ -8,13 +8,13 @@ import (
) )
func mqttPage(ctx *macaron.Context) { func mqttPage(ctx *macaron.Context) {
ctx.Data["schema"] = viper.GetString("MqttSchema") ctx.Data["schema"] = viper.GetString("Mqtt.Schema")
ctx.Data["host"] = viper.GetString("MqttHost") ctx.Data["host"] = viper.GetString("Mqtt.Host")
ctx.Data["port"] = viper.GetString("MqttPort") ctx.Data["port"] = viper.GetString("Mqtt.Port")
ctx.Data["clientid"] = viper.GetString("MqttCliendID") ctx.Data["clientid"] = viper.GetString("Mqtt.CliendID")
ctx.Data["prefix"] = viper.GetString("MqttPrefix") ctx.Data["prefix"] = viper.GetString("Mqtt.Prefix")
ctx.Data["username"] = viper.GetString("MqttUsername") ctx.Data["username"] = viper.GetString("Mqtt.Username")
ctx.Data["password"] = viper.GetString("MqttPassword") ctx.Data["password"] = viper.GetString("Mqtt.Password")
ctx.Data["schemas"] = []string{"tcp", "ssl", "ws"} ctx.Data["schemas"] = []string{"tcp", "ssl", "ws"}
ctx.HTML(200, "mqtt") ctx.HTML(200, "mqtt")
@ -43,12 +43,12 @@ func mqttPost(ctx *macaron.Context, f MQTTPostForm) {
return return
} }
viper.Set("MqttHost", strings.ToLower(strings.TrimSpace(f.Host))) viper.Set("Mqtt.Host", strings.ToLower(strings.TrimSpace(f.Host)))
viper.Set("MqttPort", strings.ToLower(strings.TrimSpace(f.Port))) viper.Set("Mqtt.Port", strings.ToLower(strings.TrimSpace(f.Port)))
viper.Set("MqttCliendID", strings.TrimSpace(f.ClientID)) viper.Set("Mqtt.CliendID", strings.TrimSpace(f.ClientID))
viper.Set("MqttPrefix", strings.TrimSpace(f.Prefix)) viper.Set("Mqtt.Prefix", strings.TrimSpace(f.Prefix))
viper.Set("MqttUsername", strings.TrimSpace(f.Username)) viper.Set("Mqtt.Username", strings.TrimSpace(f.Username))
viper.Set("MqttPassword", f.Password) viper.Set("Mqtt.Password", f.Password)
viper.WriteConfig() viper.WriteConfig()

View File

@ -10,10 +10,10 @@ var ups nut.UPS
var upsVars map[string]nut.Variable var upsVars map[string]nut.Variable
func init() { func init() {
viper.SetDefault("UpsHost", "localhost") viper.SetDefault("Ups.Host", "localhost")
viper.SetDefault("UpsName", "ups") viper.SetDefault("Ups.Name", "ups")
viper.SetDefault("UpsUsername", "") viper.SetDefault("Ups.Username", "")
viper.SetDefault("UpsPassword", "") viper.SetDefault("Ups.Password", "")
} }
func UpsConnect() { func UpsConnect() {
@ -21,16 +21,16 @@ func UpsConnect() {
var connectErr, authenticationError error var connectErr, authenticationError error
var u nut.UPS var u nut.UPS
upsClient, connectErr = nut.Connect(viper.GetString("UpsHost")) upsClient, connectErr = nut.Connect(viper.GetString("Ups.Host"))
if connectErr != nil { if connectErr != nil {
logErr(connectErr.Error()) logErr(connectErr.Error())
return return
} }
if username = viper.GetString("UpsUsername"); username == "" { if username = viper.GetString("Ups.Username"); username == "" {
return return
} }
if password = viper.GetString("UpsPassword"); password == "" { if password = viper.GetString("Ups.Password"); password == "" {
return return
} }
@ -47,7 +47,7 @@ func UpsConnect() {
} }
for _, u = range upsList { for _, u = range upsList {
if u.Name == viper.GetString("UpsName") { if u.Name == viper.GetString("Ups.Name") {
ups = u ups = u
logNotice("UPS connected") logNotice("UPS connected")
UpsReadVars() UpsReadVars()

View File

@ -8,10 +8,10 @@ import (
) )
func upsPage(ctx *macaron.Context) { func upsPage(ctx *macaron.Context) {
ctx.Data["host"] = viper.GetString("UpsHost") ctx.Data["host"] = viper.GetString("Ups.Host")
ctx.Data["upsname"] = viper.GetString("UpsName") ctx.Data["upsname"] = viper.GetString("Ups.Name")
ctx.Data["username"] = viper.GetString("UpsUsername") ctx.Data["username"] = viper.GetString("Ups.Username")
ctx.Data["password"] = viper.GetString("UpsPassword") ctx.Data["password"] = viper.GetString("Ups.Password")
// if ups == nil { // if ups == nil {
// ctx.Data["serverstatus"] = "Disconnected" // ctx.Data["serverstatus"] = "Disconnected"
@ -38,10 +38,10 @@ type UPSPostForm struct {
} }
func upsPost(ctx *macaron.Context, f UPSPostForm) { func upsPost(ctx *macaron.Context, f UPSPostForm) {
viper.Set("UpsHost", strings.ToLower(strings.TrimSpace(f.Host))) viper.Set("Ups.Host", strings.ToLower(strings.TrimSpace(f.Host)))
viper.Set("UpsName", strings.TrimSpace(f.UpsName)) viper.Set("Ups.Name", strings.TrimSpace(f.UpsName))
viper.Set("UpsUsername", strings.TrimSpace(f.Username)) viper.Set("Ups.Username", strings.TrimSpace(f.Username))
viper.Set("UpsPassword", f.Password) viper.Set("Ups.Password", f.Password)
viper.WriteConfig() viper.WriteConfig()
UpsReconfigure() UpsReconfigure()