ups fixes
This commit is contained in:
parent
5c8960d8d2
commit
d32cdb11f2
@ -10,9 +10,11 @@ import (
|
|||||||
var upsClient nut.Client
|
var upsClient nut.Client
|
||||||
var ups nut.UPS
|
var ups nut.UPS
|
||||||
var Vars map[string]nut.Variable
|
var Vars map[string]nut.Variable
|
||||||
|
var Connected bool
|
||||||
|
|
||||||
var defaults = map[string]interface{}{
|
var defaults = map[string]interface{}{
|
||||||
"Ups.Host": "localhost",
|
"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.Name": "ups",
|
||||||
"Ups.Username": "",
|
"Ups.Username": "",
|
||||||
"Ups.Password": "",
|
"Ups.Password": "",
|
||||||
@ -22,6 +24,7 @@ func init() {
|
|||||||
for k, v := range defaults {
|
for k, v := range defaults {
|
||||||
viper.SetDefault(k, v)
|
viper.SetDefault(k, v)
|
||||||
}
|
}
|
||||||
|
Connected = false
|
||||||
events.AddListener("config_changed", Reconfigure)
|
events.AddListener("config_changed", Reconfigure)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +33,7 @@ func UpsConnect() {
|
|||||||
var connectErr, authenticationError error
|
var connectErr, authenticationError error
|
||||||
var u nut.UPS
|
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 {
|
if connectErr != nil {
|
||||||
syslog.Err(connectErr.Error())
|
syslog.Err(connectErr.Error())
|
||||||
return
|
return
|
||||||
@ -58,6 +61,7 @@ func UpsConnect() {
|
|||||||
for _, u = range upsList {
|
for _, u = range upsList {
|
||||||
if u.Name == viper.GetString("Ups.Name") {
|
if u.Name == viper.GetString("Ups.Name") {
|
||||||
ups = u
|
ups = u
|
||||||
|
Connected = true
|
||||||
syslog.Notice("UPS connected")
|
syslog.Notice("UPS connected")
|
||||||
events.FireEvent("ups_connected")
|
events.FireEvent("ups_connected")
|
||||||
UpsReadVars()
|
UpsReadVars()
|
||||||
@ -77,5 +81,10 @@ func Reconfigure() {
|
|||||||
upsClient = nut.Client{}
|
upsClient = nut.Client{}
|
||||||
syslog.Notice("UPS disconnected")
|
syslog.Notice("UPS disconnected")
|
||||||
events.FireEvent("ups_disconnected")
|
events.FireEvent("ups_disconnected")
|
||||||
|
Connected = false
|
||||||
|
for key, _ := range Vars {
|
||||||
|
delete(Vars, key)
|
||||||
|
}
|
||||||
|
// Vars = make(map[string]nut.Variable)
|
||||||
go UpsConnect()
|
go UpsConnect()
|
||||||
}
|
}
|
||||||
|
@ -10,15 +10,17 @@ import (
|
|||||||
|
|
||||||
func upsPage(ctx *macaron.Context) {
|
func upsPage(ctx *macaron.Context) {
|
||||||
ctx.Data["host"] = viper.GetString("Ups.Host")
|
ctx.Data["host"] = viper.GetString("Ups.Host")
|
||||||
|
ctx.Data["port"] = viper.GetInt("Ups.Port")
|
||||||
ctx.Data["upsname"] = viper.GetString("Ups.Name")
|
ctx.Data["upsname"] = viper.GetString("Ups.Name")
|
||||||
ctx.Data["username"] = viper.GetString("Ups.Username")
|
ctx.Data["username"] = viper.GetString("Ups.Username")
|
||||||
ctx.Data["password"] = viper.GetString("Ups.Password")
|
ctx.Data["password"] = viper.GetString("Ups.Password")
|
||||||
|
|
||||||
// if ups == nil {
|
if ups.Connected {
|
||||||
// ctx.Data["serverstatus"] = "Disconnected"
|
ctx.Data["serverstatus"] = "Connected"
|
||||||
// ctx.Data["upsstatus"] = "unknown"
|
} else {
|
||||||
// } else {
|
ctx.Data["serverstatus"] = "Disconnected"
|
||||||
ctx.Data["serverstatus"] = "Connected"
|
}
|
||||||
|
|
||||||
ctx.Data["upsstatus"] = ups.Vars["ups.status"].Value
|
ctx.Data["upsstatus"] = ups.Vars["ups.status"].Value
|
||||||
ctx.Data["upsmfr"] = ups.Vars["device.mfr"].Value
|
ctx.Data["upsmfr"] = ups.Vars["device.mfr"].Value
|
||||||
ctx.Data["upsmodel"] = ups.Vars["device.model"].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["upsload"] = ups.Vars["ups.load"].Value
|
||||||
ctx.Data["upscharge"] = ups.Vars["battery.charge"].Value
|
ctx.Data["upscharge"] = ups.Vars["battery.charge"].Value
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
ctx.HTML(200, "ups")
|
ctx.HTML(200, "ups")
|
||||||
}
|
}
|
||||||
|
|
||||||
type UPSPostForm struct {
|
type UPSPostForm struct {
|
||||||
Host string `form:"host" binding:"Required"`
|
Host string `form:"host" binding:"Required"`
|
||||||
|
Port int `form:"port" binding:"Required"`
|
||||||
UpsName string `form:"upsname" binding:"Required"`
|
UpsName string `form:"upsname" binding:"Required"`
|
||||||
Username string `form:"username" binding:"Required"`
|
Username string `form:"username" binding:"Required"`
|
||||||
Password string `form:"password" binding:"Required"`
|
Password string `form:"password" binding:"Required"`
|
||||||
@ -40,12 +41,11 @@ type UPSPostForm struct {
|
|||||||
|
|
||||||
func upsPost(ctx *macaron.Context, f UPSPostForm) {
|
func upsPost(ctx *macaron.Context, f UPSPostForm) {
|
||||||
viper.Set("Ups.Host", strings.ToLower(strings.TrimSpace(f.Host)))
|
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.Name", strings.TrimSpace(f.UpsName))
|
||||||
viper.Set("Ups.Username", strings.TrimSpace(f.Username))
|
viper.Set("Ups.Username", strings.TrimSpace(f.Username))
|
||||||
viper.Set("Ups.Password", f.Password)
|
viper.Set("Ups.Password", f.Password)
|
||||||
|
|
||||||
viper.WriteConfig()
|
viper.WriteConfig()
|
||||||
ups.Reconfigure()
|
|
||||||
|
|
||||||
upsPage(ctx)
|
upsPage(ctx)
|
||||||
}
|
}
|
||||||
|
@ -88,6 +88,14 @@
|
|||||||
</div>
|
</div>
|
||||||
</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">
|
<div class="form-group">
|
||||||
<label for="upsname" class="col-sm-2 control-label">UPS Name</label>
|
<label for="upsname" class="col-sm-2 control-label">UPS Name</label>
|
||||||
<div class="col-sm-10">
|
<div class="col-sm-10">
|
||||||
|
Loading…
Reference in New Issue
Block a user