initial ups support

This commit is contained in:
2020-12-27 22:29:16 +01:00
parent 2958e13b5e
commit 28dcadbeef
8 changed files with 410 additions and 147 deletions

View File

@@ -19,6 +19,7 @@ func init() {
func main() {
readConfig()
go mqttLoop()
go UpsConnect()
log.Printf("hostname: %v\n", viper.Get("hostname"))
startServer()
}

72
src/ups.go Normal file
View File

@@ -0,0 +1,72 @@
package main
import (
"fmt"
nut "github.com/robbiet480/go.nut"
"github.com/spf13/viper"
)
var upsClient nut.Client
var ups nut.UPS
var upsVars map[string]nut.Variable
func init() {
viper.SetDefault("UpsHost", "localhost")
viper.SetDefault("UpsName", "ups")
viper.SetDefault("UpsUsername", "")
viper.SetDefault("UpsPassword", "")
}
func UpsConnect() {
var username, password string
var connectErr, authenticationError error
var u nut.UPS
upsClient, connectErr = nut.Connect(viper.GetString("UpsHost"))
if connectErr != nil {
fmt.Print(connectErr)
return
}
if username = viper.GetString("UpsUsername"); username == "" {
return
}
if password = viper.GetString("UpsPassword"); password == "" {
return
}
_, authenticationError = upsClient.Authenticate(username, password)
if authenticationError != nil {
fmt.Print(authenticationError)
return
}
upsList, listErr := upsClient.GetUPSList()
if listErr != nil {
fmt.Print(listErr)
return
}
for _, u = range upsList {
if u.Name == viper.GetString("UpsName") {
ups = u
fmt.Print("UPS connected")
UpsReadVars()
}
}
}
func UpsReadVars() {
upsVars = map[string]nut.Variable{}
for _, v := range ups.Variables {
upsVars[v.Name] = v
}
}
func UpsReconfigure() {
ups = nut.UPS{}
upsClient = nut.Client{}
fmt.Print("UPS disconnected")
go UpsConnect()
}

47
src/ups_ui.go Normal file
View File

@@ -0,0 +1,47 @@
package main
import (
"strings"
"github.com/spf13/viper"
"gopkg.in/macaron.v1"
)
func upsPage(ctx *macaron.Context) {
ctx.Data["host"] = viper.GetString("UpsHost")
ctx.Data["upsname"] = viper.GetString("UpsName")
ctx.Data["username"] = viper.GetString("UpsUsername")
ctx.Data["password"] = viper.GetString("UpsPassword")
// 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
// }
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) {
viper.Set("UpsHost", strings.ToLower(strings.TrimSpace(f.Host)))
viper.Set("UpsName", strings.TrimSpace(f.UpsName))
viper.Set("UpsUsername", strings.TrimSpace(f.Username))
viper.Set("UpsPassword", f.Password)
viper.WriteConfig()
UpsReconfigure()
upsPage(ctx)
}

View File

@@ -107,6 +107,8 @@ func startServer() {
m.Get("/", statusPage)
m.Get("/mqtt", mqttPage)
m.Post("/mqtt", binding.Bind(MQTTPostForm{}), mqttPost)
m.Get("/ups", upsPage)
m.Post("/ups", binding.Bind(UPSPostForm{}), upsPost)
m.Get("/json/status", jsonStatus)
m.Post("/json/outlet/:id/toggle", jsonOutletToggle)