From 1edac26c50260a0d86965c53d67712d513811f60 Mon Sep 17 00:00:00 2001 From: Paolo Asperti Date: Tue, 5 Jan 2021 11:27:49 +0100 Subject: [PATCH] Update board.go, mqtt.go, and 4 more files... --- src/board.go | 52 ++++++++++++++++++++++++--------- src/mqtt.go | 14 +++++++++ src/outlets_ui.go | 9 ++++++ src/status_ui.go | 60 ++++++++++++++++++++++++++++++++++++++ src/webui.go | 54 ---------------------------------- templates/outlet_edit.html | 8 +++++ 6 files changed, 130 insertions(+), 67 deletions(-) create mode 100644 src/status_ui.go diff --git a/src/board.go b/src/board.go index b5f3ad5..4001a1c 100644 --- a/src/board.go +++ b/src/board.go @@ -9,13 +9,14 @@ import ( ) type Channel struct { - ID string - Num uint - Name string - Value bool - Inverted bool // low level only - OnBoot string - Parent *Board + ID string + Num uint + Name string + MQTTTopic string + Value bool + Inverted bool // low level only + OnBoot string + Parent *Board } type Board struct { @@ -36,19 +37,42 @@ type Outlet struct { func (c *Channel) Toggle() (bool, error) { c.Value = !c.Value + c.OnChange() return c.Value, nil } func (c *Channel) On() error { c.Value = true + c.OnChange() return nil } func (c *Channel) Off() error { c.Value = true + c.OnChange() return nil } +func (c *Channel) ToString() string { + if !c.Value { + return "off" + } + return "on" +} + +func (c *Channel) UpdateMQTT() { + MQTTpublish(c.MQTTTopic, c.ToString()) +} + +func (c *Channel) OnChange() { + if c.OnBoot == "last" { + s := fmt.Sprintf("boards.%s.channels.%s.lastvalue", c.Parent.ID, c.ID) + viper.Set(s, c.Value) + viper.WriteConfig() + } + c.UpdateMQTT() +} + func (c *Channel) Status() bool { return c.Value } @@ -62,6 +86,7 @@ func newDummyChannel(v *viper.Viper, channelID string) Channel { v.SetDefault("lastValue", false) v.SetDefault("inverted", false) v.SetDefault("onboot", "off") + v.SetDefault("mqtttopic", v.GetString("name")) value := false switch v.GetString("onboot") { @@ -73,12 +98,13 @@ func newDummyChannel(v *viper.Viper, channelID string) Channel { // newUUID := UUID.New().String() // v.SetDefault("id", newUUID) return Channel{ - ID: channelID, - Num: v.GetUint("num"), - Name: v.GetString("name"), - Value: value, - Inverted: v.GetBool("inverted"), - OnBoot: v.GetString("onboot"), + ID: channelID, + Num: v.GetUint("num"), + Name: v.GetString("name"), + MQTTTopic: v.GetString("mqtttopic"), + Value: value, + Inverted: v.GetBool("inverted"), + OnBoot: v.GetString("onboot"), } } diff --git a/src/mqtt.go b/src/mqtt.go index 155b752..6def3c6 100644 --- a/src/mqtt.go +++ b/src/mqtt.go @@ -69,6 +69,7 @@ func mqttLoop() { time.Sleep(5 * time.Second) } + go MQTTRefreshLoop() } func MQTTreconfigure() { @@ -83,3 +84,16 @@ func MQTTpublish(topic string, value string) { MQTTclient.Publish(viper.GetString("Mqtt.Prefix")+"/"+topic, 0, false, value) } } + +func MQTTRefreshLoop() { + for { + MQTTRefresh() + time.Sleep(30 * time.Second) + } +} + +func MQTTRefresh() { + for o := range outlets { + outlets[o].Channel.UpdateMQTT() + } +} diff --git a/src/outlets_ui.go b/src/outlets_ui.go index 5c6c225..5bc2065 100644 --- a/src/outlets_ui.go +++ b/src/outlets_ui.go @@ -19,6 +19,7 @@ func outletsPage(ctx *macaron.Context) { type OutletPostForm struct { Description string `form:"description" binding:"Required"` + MQTTTopic string `form:"mqtttopic"` OnBoot string `form:"onboot" binding:"Required"` } @@ -76,6 +77,14 @@ func outletEditPost(ctx *macaron.Context, f OutletPostForm) { s2 := fmt.Sprintf("outlets.%s.description", outlets[num].ID) viper.Set(s2, outlets[num].Description) + mqtt := strings.TrimSpace(f.MQTTTopic) + if mqtt == "" { + mqtt = outlets[num].Channel.Name + } + outlets[num].Channel.MQTTTopic = mqtt + s3 := fmt.Sprintf("boards.%s.channels.%s.mqtttopic", outlets[num].Board.ID, outlets[num].Channel.ID) + viper.Set(s3, mqtt) + viper.WriteConfig() ctx.Redirect("/outlets") diff --git a/src/status_ui.go b/src/status_ui.go new file mode 100644 index 0000000..888c490 --- /dev/null +++ b/src/status_ui.go @@ -0,0 +1,60 @@ +package main + +import ( + "fmt" + "net/http" + "strconv" + + "gopkg.in/macaron.v1" +) + +func statusPage(ctx *macaron.Context) { + ctx.HTML(200, "status") // 200 is the response code. +} + +func jsonStatus(ctx *macaron.Context) { + // MQTTpublish("openpdu/status", "asdss") + + var data = [][]string{} + var o *Outlet + for i := range outlets { + o = outlets[i] + d := []string{fmt.Sprintf("%d", o.Num), o.Channel.Name, fmt.Sprintf("%v", o.Channel.Value)} + data = append(data, d) + } + + ctx.JSON(http.StatusOK, Dictionary{ + "data": data, + }) +} + +func jsonOutletToggle(ctx *macaron.Context) { + var err error + var num uint64 + var outlet *Outlet + + num, err = strconv.ParseUint(ctx.Params(":id"), 10, 64) + if err != nil { + ctx.JSON(http.StatusOK, Dictionary{ + "data": "error1", + }) + } + + if num >= uint64(len(outlets)) || num < 0 { + ctx.JSON(http.StatusOK, Dictionary{ + "data": "error2", + }) + } + + outlet = outlets[num] + _, err = outlet.Channel.Toggle() + if err != nil { + ctx.JSON(http.StatusOK, Dictionary{ + "data": "error3", + }) + } + + ctx.JSON(http.StatusOK, Dictionary{ + "data": "ok", + }) +} diff --git a/src/webui.go b/src/webui.go index 27223f0..133eb82 100644 --- a/src/webui.go +++ b/src/webui.go @@ -1,9 +1,7 @@ package main import ( - "fmt" "net/http" - "strconv" "github.com/go-macaron/binding" "github.com/go-macaron/pongo2" @@ -11,58 +9,6 @@ import ( "gopkg.in/macaron.v1" ) -func statusPage(ctx *macaron.Context) { - ctx.HTML(200, "status") // 200 is the response code. -} - -func jsonStatus(ctx *macaron.Context) { - // MQTTpublish("openpdu/status", "asdss") - - var data = [][]string{} - var o *Outlet - for i := range outlets { - o = outlets[i] - d := []string{fmt.Sprintf("%d", o.Num), o.Channel.Name, fmt.Sprintf("%v", o.Channel.Value)} - data = append(data, d) - } - - ctx.JSON(http.StatusOK, Dictionary{ - "data": data, - }) -} - -func jsonOutletToggle(ctx *macaron.Context) { - var err error - var num uint64 - var outlet *Outlet - - num, err = strconv.ParseUint(ctx.Params(":id"), 10, 64) - if err != nil { - ctx.JSON(http.StatusOK, Dictionary{ - "data": "error1", - }) - } - - if num >= uint64(len(outlets)) || num < 0 { - ctx.JSON(http.StatusOK, Dictionary{ - "data": "error2", - }) - } - - outlet = outlets[num] - _, err = outlet.Channel.Toggle() - if err != nil { - ctx.JSON(http.StatusOK, Dictionary{ - "data": "error3", - }) - } - // MQTTpublish("openpdu/toggolo1", fmt.Sprint(id)) - - ctx.JSON(http.StatusOK, Dictionary{ - "data": "ok", - }) -} - func init() { viper.SetDefault("system.listeningport", 4000) } diff --git a/templates/outlet_edit.html b/templates/outlet_edit.html index 43373b7..46c9eef 100644 --- a/templates/outlet_edit.html +++ b/templates/outlet_edit.html @@ -61,6 +61,14 @@ +
+ +
+ +
+
+