From 9d253644bd148974ad2961670194d1de6b10835c Mon Sep 17 00:00:00 2001 From: paspo Date: Mon, 28 Oct 2019 00:20:21 +0100 Subject: [PATCH] bella mqtt quasi va --- source/board.go | 5 - source/config.go | 8 +- source/web.go | 5 + source/webpages.go | 54 ++++++- templates/boards.html | 4 +- templates/common/common-head.html | 12 +- templates/common/common-js.html | 10 +- templates/common/page-header.html | 2 +- templates/common/sidebar-menu.html | 39 ++++- templates/settings/mqtt.html | 0 templates/settings_mqtt.html | 242 +++++++++++++++++++++++++++++ templates/status.html | 4 +- 12 files changed, 355 insertions(+), 30 deletions(-) delete mode 100644 templates/settings/mqtt.html create mode 100644 templates/settings_mqtt.html diff --git a/source/board.go b/source/board.go index dd0fbf6..4964234 100644 --- a/source/board.go +++ b/source/board.go @@ -1,7 +1,5 @@ package main -import "log" - // BoardType Status const ( BoardTypeDummy uint = 0 @@ -61,9 +59,7 @@ func (b *Board) PowerToggle(num uint) error { b.dummyValue = make(map[uint]bool) } v, _ := b.PowerStatus(num) - log.Printf("toggle prestatus %v:%v", num, b.dummyValue[num]) b.dummyValue[num] = !v - log.Printf("toggle poststatus %v:%v", num, b.dummyValue[num]) return nil } return nil @@ -81,7 +77,6 @@ func (b *Board) PowerStatus(num uint) (bool, error) { b.dummyValue[num] = false val = false } - log.Printf("status %v:%v", num, val) return val, nil } return false, nil diff --git a/source/config.go b/source/config.go index ef834ef..19a6fbb 100644 --- a/source/config.go +++ b/source/config.go @@ -13,6 +13,9 @@ type Configuration struct { MQTT MQTTConfig `json:"mqtt"` } +// TheConfig def +var TheConfig Configuration + func loadConfig(filename string) (Configuration, error) { bytes, err := ioutil.ReadFile(filename) if err != nil { @@ -37,5 +40,6 @@ func saveConfig(c Configuration, filename string) error { return ioutil.WriteFile(filename, bytes, 0644) } -// TheConfig def -var TheConfig Configuration +func writeConfig() error { + return saveConfig(TheConfig, "t1.json") +} diff --git a/source/web.go b/source/web.go index d449778..a8ab121 100644 --- a/source/web.go +++ b/source/web.go @@ -4,6 +4,7 @@ import ( "log" "net/http" + "github.com/go-macaron/binding" "github.com/go-macaron/pongo2" "gopkg.in/macaron.v1" ) @@ -20,6 +21,10 @@ func startServer() { m.Post("/json/outlet/:outlet/off", jsonOutletPowerOFF) m.Post("/json/outlet/:outlet/toggle", jsonOutletPowerToggle) + m.Get("/settings/mqtt", webGETSettingsMQTT) + m.Post("/json/settings/mqtt", binding.Bind(SettingsMQTTForm{}), webPOSTSettingsMQTT) + // m.Post("/settings/mqtt", webPOSTSettingsMQTT) + m.Get("/boards", func(ctx *macaron.Context) { ctx.HTML(200, "boards") // 200 is the response code. }) diff --git a/source/webpages.go b/source/webpages.go index 649764f..968149c 100644 --- a/source/webpages.go +++ b/source/webpages.go @@ -1,6 +1,10 @@ package main -import "gopkg.in/macaron.v1" +import ( + "net/http" + + "gopkg.in/macaron.v1" +) func statusPage(ctx *macaron.Context) { var pluglist []Dictionary @@ -12,3 +16,51 @@ func statusPage(ctx *macaron.Context) { ctx.Data["pluglist"] = pluglist ctx.HTML(200, "status") // 200 is the response code. } + +//SettingsMQTTForm definition +type SettingsMQTTForm struct { + BrokerIP string `form:"brokerip" binding:"Required"` + BrokerPort string `form:"brokerport" binding:"Required"` + ClientID string `form:"clientid" binding:"Required"` + Username string `form:"username"` + Password string `form:"password"` + Topic string `form:"topic" binding:"Required"` + CleanSession bool `form:"cleansession"` + HomeAssistant bool `form:"homeassistant"` +} + +func webGETSettingsMQTT(ctx *macaron.Context) { + ctx.Data["r_brokerip"] = TheConfig.MQTT.BrokerIP + ctx.Data["r_brokerport"] = TheConfig.MQTT.BrokerPort + ctx.Data["r_clientid"] = TheConfig.MQTT.ClientID + ctx.Data["r_username"] = TheConfig.MQTT.Username + ctx.Data["r_password"] = TheConfig.MQTT.Password + ctx.Data["r_cleansession"] = TheConfig.MQTT.CleanSession + ctx.Data["r_topic"] = TheConfig.MQTT.Topic + ctx.Data["r_homeassistant"] = TheConfig.MQTT.HomeAssistant + ctx.HTML(200, "settings_mqtt") // 200 is the response code. +} + +func webPOSTSettingsMQTT(ctx *macaron.Context, f SettingsMQTTForm) { + + TheConfig.MQTT.BrokerIP = f.BrokerIP + TheConfig.MQTT.BrokerPort = f.BrokerPort + TheConfig.MQTT.ClientID = f.ClientID + TheConfig.MQTT.Username = f.Username + TheConfig.MQTT.Password = f.Password + TheConfig.MQTT.Topic = f.Topic + // TODO: cleansession + // TODO: homeassistant + + err := writeConfig() + if err != nil { + ctx.JSON(http.StatusOK, Dictionary{ + "result": "error", + "error": err.Error(), + }) + } + + ctx.JSON(http.StatusOK, Dictionary{ + "result": "ok", + }) +} diff --git a/templates/boards.html b/templates/boards.html index 8660c60..713a236 100644 --- a/templates/boards.html +++ b/templates/boards.html @@ -82,8 +82,8 @@ - - + + + - + - + - + - + diff --git a/templates/common/page-header.html b/templates/common/page-header.html index 8d2d9f6..f911f6a 100644 --- a/templates/common/page-header.html +++ b/templates/common/page-header.html @@ -1,6 +1,6 @@
- + + {% if pageselected == "settings" || pageselected == "settings/mqtt" %} +
  • + {% endif %} + Settings -
      -
    • LAN
    • -
    • MQTT
    • -
    • syslog
    • -
    • backup
    • -
    • restore
    • + + {% if pageselected == "settings" || pageselected == "settings/mqtt" %} +
        + {% else %} +
      diff --git a/templates/settings/mqtt.html b/templates/settings/mqtt.html deleted file mode 100644 index e69de29..0000000 diff --git a/templates/settings_mqtt.html b/templates/settings_mqtt.html new file mode 100644 index 0000000..5943585 --- /dev/null +++ b/templates/settings_mqtt.html @@ -0,0 +1,242 @@ + + + + {% include "common/common-head.html" %} + + +
      + + {% include "common/page-header.html" %} + {% with pageselected="settings/mqtt" %} + {% include "common/sidebar-menu.html" %} + {% endwith %} + + +
      + +
      +

      + MQTT configuration +

      +
      + + +
      + + + + + + + + + + + + +
      + +
      + +
      + +
      + +
      +
      + +
      + +
      +
      + +
      + +
      + +
      +
      + +
      + +
      + +
      +
      + +
      + +
      + +
      +
      + +
      + +
      + +
      +
      + +
      + +
      + +
      +
      + + +
      +
      +
      + +
      +
      +
      + +
      +
      +
      + +
      +
      +
      + + +
      + + + + + + +
      +
      + + +
      + +
      + + + + +
      + +
      + + + {% include "common/footer.html" %} + +
      + + + {% include "common/common-js.html" %} + + + + + + + + + \ No newline at end of file diff --git a/templates/status.html b/templates/status.html index f48e12f..8d73c8e 100644 --- a/templates/status.html +++ b/templates/status.html @@ -65,8 +65,8 @@ {% include "common/common-js.html" %} - - + +