From 28dcadbeef58b030dac6597601f3ce68fb47dc0b Mon Sep 17 00:00:00 2001 From: Paolo Asperti Date: Sun, 27 Dec 2020 22:29:16 +0100 Subject: [PATCH] initial ups support --- .editorconfig | 13 ++ src/main.go | 1 + src/ups.go | 72 +++++++++++ src/ups_ui.go | 47 +++++++ src/webui.go | 2 + templates/common/sidebar-menu.html | 103 +++++++-------- templates/mqtt.html | 195 ++++++++++++++--------------- templates/ups.html | 124 ++++++++++++++++++ 8 files changed, 410 insertions(+), 147 deletions(-) create mode 100644 .editorconfig create mode 100644 src/ups.go create mode 100644 src/ups_ui.go create mode 100644 templates/ups.html diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3a9278b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*] +charset = utf-8 + +[*.html] +indent_style = space +indent_size = 2 \ No newline at end of file diff --git a/src/main.go b/src/main.go index 0a103d7..c19bc18 100644 --- a/src/main.go +++ b/src/main.go @@ -19,6 +19,7 @@ func init() { func main() { readConfig() go mqttLoop() + go UpsConnect() log.Printf("hostname: %v\n", viper.Get("hostname")) startServer() } diff --git a/src/ups.go b/src/ups.go new file mode 100644 index 0000000..7fb4c00 --- /dev/null +++ b/src/ups.go @@ -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() +} diff --git a/src/ups_ui.go b/src/ups_ui.go new file mode 100644 index 0000000..53065cc --- /dev/null +++ b/src/ups_ui.go @@ -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) +} diff --git a/src/webui.go b/src/webui.go index 0e31e0a..ac120d9 100644 --- a/src/webui.go +++ b/src/webui.go @@ -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) diff --git a/templates/common/sidebar-menu.html b/templates/common/sidebar-menu.html index 3518600..686347d 100644 --- a/templates/common/sidebar-menu.html +++ b/templates/common/sidebar-menu.html @@ -1,61 +1,66 @@ - - \ No newline at end of file diff --git a/templates/mqtt.html b/templates/mqtt.html index 6f1c16a..42a8c33 100644 --- a/templates/mqtt.html +++ b/templates/mqtt.html @@ -1,42 +1,41 @@ + {% include "common/common-head.html" %} + -
+
- {% include "common/page-header.html" %} - {% with pageselected="mqtt" %} - {% include "common/sidebar-menu.html" %} - {% endwith %} + {% include "common/page-header.html" %} {% with pageselected="mqtt" %} {% include "common/sidebar-menu.html" %} {% endwith %} - -
- - - - -
+ +
-
-
+ + +
-
-
-

MQTT configuration

-
- - -
-
- -
- -
- {% for i in schemas %} {% if i == schema %} @@ -45,79 +44,79 @@ {% endif %} {% endfor %} -
+
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + +
+ + + + + +
+ +
+
+ -
- -
- -
-
- -
- -
- -
-
- -
- -
- -
-
- -
- -
- -
-
- -
- -
- -
-
- -
- -
- -
-
- - -
- - - - - -
- -
- - - - - - + + - + {% include "common/footer.html" %} - - - - - {% include "common/common-js.html" %} - - - - \ No newline at end of file + + + + + {% include "common/common-js.html" %} + + + + \ No newline at end of file diff --git a/templates/ups.html b/templates/ups.html new file mode 100644 index 0000000..421c181 --- /dev/null +++ b/templates/ups.html @@ -0,0 +1,124 @@ + + + + + {% include "common/common-head.html" %} + + + +
+ + {% include "common/page-header.html" %} {% with pageselected="ups" %} {% include "common/sidebar-menu.html" %} {% endwith %} + + +
+ + +
+ +
+
+ +
+
+

UPS configuration

+
+ + +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + +
+ + + + +
+
+ +
+ +
+ + +
+ +
+ + + {% include "common/footer.html" %} + +
+ + + {% include "common/common-js.html" %} + + + +