openpdu/src/ups_ui.go

51 lines
1.4 KiB
Go
Raw Normal View History

2020-12-27 21:29:16 +00:00
package main
import (
"strings"
"github.com/spf13/viper"
"gopkg.in/macaron.v1"
)
func upsPage(ctx *macaron.Context) {
2021-01-04 11:28:20 +00:00
ctx.Data["host"] = viper.GetString("Ups.Host")
ctx.Data["upsname"] = viper.GetString("Ups.Name")
ctx.Data["username"] = viper.GetString("Ups.Username")
ctx.Data["password"] = viper.GetString("Ups.Password")
2020-12-27 21:29:16 +00:00
// if ups == nil {
// ctx.Data["serverstatus"] = "Disconnected"
// ctx.Data["upsstatus"] = "unknown"
// } else {
ctx.Data["serverstatus"] = "Connected"
ctx.Data["upsstatus"] = upsVars["ups.status"].Value
ctx.Data["upsmfr"] = upsVars["device.mfr"].Value
ctx.Data["upsmodel"] = upsVars["device.model"].Value
ctx.Data["upsserial"] = upsVars["device.serial"].Value
2020-12-28 21:58:08 +00:00
ctx.Data["upsload"] = upsVars["ups.load"].Value
ctx.Data["upscharge"] = upsVars["battery.charge"].Value
2020-12-27 21:29:16 +00:00
// }
ctx.HTML(200, "ups")
}
type UPSPostForm struct {
Host string `form:"host" binding:"Required"`
UpsName string `form:"upsname" binding:"Required"`
Username string `form:"username" binding:"Required"`
Password string `form:"password" binding:"Required"`
}
func upsPost(ctx *macaron.Context, f UPSPostForm) {
2021-01-04 11:28:20 +00:00
viper.Set("Ups.Host", strings.ToLower(strings.TrimSpace(f.Host)))
viper.Set("Ups.Name", strings.TrimSpace(f.UpsName))
viper.Set("Ups.Username", strings.TrimSpace(f.Username))
viper.Set("Ups.Password", f.Password)
2020-12-27 21:29:16 +00:00
viper.WriteConfig()
UpsReconfigure()
upsPage(ctx)
}