ups fixes

This commit is contained in:
Paolo Asperti 2021-10-01 17:45:37 +02:00
parent 5c8960d8d2
commit d32cdb11f2
Signed by: paspo
GPG Key ID: 06D46905D19D5182
3 changed files with 27 additions and 10 deletions

View File

@ -10,9 +10,11 @@ import (
var upsClient nut.Client
var ups nut.UPS
var Vars map[string]nut.Variable
var Connected bool
var defaults = map[string]interface{}{
"Ups.Host": "localhost",
"Ups.Port": 3493, // not yet implemented in github.com/robbiet480/go.nut, see this PR https://github.com/robbiet480/go.nut/pull/5
"Ups.Name": "ups",
"Ups.Username": "",
"Ups.Password": "",
@ -22,6 +24,7 @@ func init() {
for k, v := range defaults {
viper.SetDefault(k, v)
}
Connected = false
events.AddListener("config_changed", Reconfigure)
}
@ -30,7 +33,7 @@ func UpsConnect() {
var connectErr, authenticationError error
var u nut.UPS
upsClient, connectErr = nut.Connect(viper.GetString("Ups.Host"))
upsClient, connectErr = nut.Connect(viper.GetString("Ups.Host"), viper.GetInt("Ups.Port"))
if connectErr != nil {
syslog.Err(connectErr.Error())
return
@ -58,6 +61,7 @@ func UpsConnect() {
for _, u = range upsList {
if u.Name == viper.GetString("Ups.Name") {
ups = u
Connected = true
syslog.Notice("UPS connected")
events.FireEvent("ups_connected")
UpsReadVars()
@ -77,5 +81,10 @@ func Reconfigure() {
upsClient = nut.Client{}
syslog.Notice("UPS disconnected")
events.FireEvent("ups_disconnected")
Connected = false
for key, _ := range Vars {
delete(Vars, key)
}
// Vars = make(map[string]nut.Variable)
go UpsConnect()
}

View File

@ -10,15 +10,17 @@ import (
func upsPage(ctx *macaron.Context) {
ctx.Data["host"] = viper.GetString("Ups.Host")
ctx.Data["port"] = viper.GetInt("Ups.Port")
ctx.Data["upsname"] = viper.GetString("Ups.Name")
ctx.Data["username"] = viper.GetString("Ups.Username")
ctx.Data["password"] = viper.GetString("Ups.Password")
// if ups == nil {
// ctx.Data["serverstatus"] = "Disconnected"
// ctx.Data["upsstatus"] = "unknown"
// } else {
if ups.Connected {
ctx.Data["serverstatus"] = "Connected"
} else {
ctx.Data["serverstatus"] = "Disconnected"
}
ctx.Data["upsstatus"] = ups.Vars["ups.status"].Value
ctx.Data["upsmfr"] = ups.Vars["device.mfr"].Value
ctx.Data["upsmodel"] = ups.Vars["device.model"].Value
@ -26,13 +28,12 @@ func upsPage(ctx *macaron.Context) {
ctx.Data["upsload"] = ups.Vars["ups.load"].Value
ctx.Data["upscharge"] = ups.Vars["battery.charge"].Value
// }
ctx.HTML(200, "ups")
}
type UPSPostForm struct {
Host string `form:"host" binding:"Required"`
Port int `form:"port" binding:"Required"`
UpsName string `form:"upsname" binding:"Required"`
Username string `form:"username" binding:"Required"`
Password string `form:"password" binding:"Required"`
@ -40,12 +41,11 @@ type UPSPostForm struct {
func upsPost(ctx *macaron.Context, f UPSPostForm) {
viper.Set("Ups.Host", strings.ToLower(strings.TrimSpace(f.Host)))
viper.Set("Ups.Port", f.Port)
viper.Set("Ups.Name", strings.TrimSpace(f.UpsName))
viper.Set("Ups.Username", strings.TrimSpace(f.Username))
viper.Set("Ups.Password", f.Password)
viper.WriteConfig()
ups.Reconfigure()
upsPage(ctx)
}

View File

@ -88,6 +88,14 @@
</div>
</div>
<div class="form-group">
<label for="port" class="col-sm-2 control-label">Port</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="port" name="port" placeholder="NUT port, default 3493"
value="{{ port }}">
</div>
</div>
<div class="form-group">
<label for="upsname" class="col-sm-2 control-label">UPS Name</label>
<div class="col-sm-10">