Update board.go, board_ui.go, and 2 more files...

This commit is contained in:
Paolo Asperti 2021-01-05 09:11:38 +01:00
parent ef2b809707
commit 335d34a21a
4 changed files with 40 additions and 35 deletions

View File

@ -23,7 +23,7 @@ type Board struct {
Name string Name string
ChannelCount uint ChannelCount uint
Inverted bool // low level only Inverted bool // low level only
Channels []Channel Channels []*Channel
} }
type Outlet struct { type Outlet struct {
@ -34,28 +34,28 @@ type Outlet struct {
Channel *Channel Channel *Channel
} }
func (c Channel) Toggle() (bool, error) { func (c *Channel) Toggle() (bool, error) {
c.Value = !c.Value c.Value = !c.Value
return c.Value, nil return c.Value, nil
} }
func (c Channel) On() error { func (c *Channel) On() error {
c.Value = true c.Value = true
return nil return nil
} }
func (c Channel) Off() error { func (c *Channel) Off() error {
c.Value = true c.Value = true
return nil return nil
} }
func (c Channel) Status() bool { func (c *Channel) Status() bool {
return c.Value return c.Value
} }
var boards []Board var boards []*Board
var allChannels map[string]*Channel var allChannels map[string]*Channel
var outlets []Outlet var outlets []*Outlet
func newDummyChannel(v *viper.Viper, channelID string) Channel { func newDummyChannel(v *viper.Viper, channelID string) Channel {
v.SetDefault("name", "unknown") v.SetDefault("name", "unknown")
@ -104,7 +104,7 @@ func newDummyBoard(v *viper.Viper, id string) Board {
Inverted: v.GetBool("inverted"), Inverted: v.GetBool("inverted"),
} }
channels := make([]Channel, v.GetInt("channelCount")) channels := make([]*Channel, v.GetInt("channelCount"))
if v.GetInt("channelCount") > 0 { if v.GetInt("channelCount") > 0 {
channelsConfig := v.Sub("channels") channelsConfig := v.Sub("channels")
if channelsConfig != nil { if channelsConfig != nil {
@ -116,7 +116,7 @@ func newDummyBoard(v *viper.Viper, id string) Board {
if c.Num >= v.GetUint("channelCount") { if c.Num >= v.GetUint("channelCount") {
continue continue
} }
channels[c.Num] = c channels[c.Num] = &c
allChannels[c.ID] = &c allChannels[c.ID] = &c
} }
} }
@ -144,13 +144,14 @@ func parseBoardsConfig() {
switch boardType { switch boardType {
case "gpio": case "gpio":
default: default:
boards = append(boards, newDummyBoard(boardConfig, key)) b := newDummyBoard(boardConfig, key)
boards = append(boards, &b)
} }
} }
outlets = make([]Outlet, len(allChannels)) outlets = make([]*Outlet, len(allChannels))
outletsConfig := viper.Sub("outlets") outletsConfig := viper.Sub("outlets")
if outletsConfig == nil { if outletsConfig == nil {
@ -174,7 +175,7 @@ func parseBoardsConfig() {
Channel: channel, Channel: channel,
} }
outlets[num] = o outlets[num] = &o
} }
// dumpa tutto // dumpa tutto
@ -189,17 +190,17 @@ func parseBoardsConfig() {
} }
func (c Channel) Dump() { func (c *Channel) Dump() {
log.Printf(" Channel %d (on boot: %s): %s \n", c.Num, c.OnBoot, c.Name) log.Printf(" Channel %d (on boot: %s): %s \n", c.Num, c.OnBoot, c.Name)
} }
func (b Board) Dump() { func (b *Board) Dump() {
log.Printf("Board '%s' (id: %s): %d channels\n", b.Name, b.ID, b.ChannelCount) log.Printf("Board '%s' (id: %s): %d channels\n", b.Name, b.ID, b.ChannelCount)
for c := range b.Channels { for c := range b.Channels {
b.Channels[c].Dump() b.Channels[c].Dump()
} }
} }
func (o Outlet) Dump() { func (o *Outlet) Dump() {
log.Printf("Outlet %v: channel name: %v\n", o.Num, o.Channel.Name) log.Printf("Outlet %v: channel name: %v\n", o.Num, o.Channel.Name)
} }

View File

@ -1 +0,0 @@
package main

View File

@ -25,7 +25,7 @@ type OutletPostForm struct {
func outletEditPage(ctx *macaron.Context) { func outletEditPage(ctx *macaron.Context) {
var err error var err error
var num uint64 var num uint64
var o Outlet var o *Outlet
num, err = strconv.ParseUint(ctx.Params(":num"), 10, 64) num, err = strconv.ParseUint(ctx.Params(":num"), 10, 64)
if err != nil { if err != nil {
ctx.JSON(http.StatusOK, Dictionary{ ctx.JSON(http.StatusOK, Dictionary{

View File

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"strconv" "strconv"
@ -13,16 +12,6 @@ import (
) )
func statusPage(ctx *macaron.Context) { func statusPage(ctx *macaron.Context) {
ctx.Data["pluglist"] = []Dictionary{
{"id": 1, "description": "p1"},
{"id": 2, "description": "p2"},
{"id": 3, "description": "p3"},
{"id": 4, "description": "p4"},
{"id": 5, "description": "p5"},
{"id": 6, "description": "p6"},
{"id": 7, "description": "p7"},
{"id": 8, "description": "p8"},
}
ctx.HTML(200, "status") // 200 is the response code. ctx.HTML(200, "status") // 200 is the response code.
} }
@ -30,7 +19,7 @@ func jsonStatus(ctx *macaron.Context) {
// MQTTpublish("openpdu/status", "asdss") // MQTTpublish("openpdu/status", "asdss")
var data = [][]string{} var data = [][]string{}
var o Outlet var o *Outlet
for i := range outlets { for i := range outlets {
o = outlets[i] o = outlets[i]
d := []string{fmt.Sprintf("%d", o.Num), o.Channel.Name, fmt.Sprintf("%v", o.Channel.Value)} d := []string{fmt.Sprintf("%d", o.Num), o.Channel.Name, fmt.Sprintf("%v", o.Channel.Value)}
@ -43,16 +32,32 @@ func jsonStatus(ctx *macaron.Context) {
} }
func jsonOutletToggle(ctx *macaron.Context) { func jsonOutletToggle(ctx *macaron.Context) {
id, err := strconv.ParseUint(ctx.Params(":id"), 10, 64) var err error
var num uint64
var outlet *Outlet
num, err = strconv.ParseUint(ctx.Params(":id"), 10, 64)
if err != nil { if err != nil {
log.Fatal(err) ctx.JSON(http.StatusOK, Dictionary{
"data": "error1",
})
} }
MQTTpublish("openpdu/toggolo1", fmt.Sprint(id))
err = MyBoard.channelToggle(uint(id)) if num >= uint64(len(outlets)) || num < 0 {
ctx.JSON(http.StatusOK, Dictionary{
"data": "error2",
})
}
outlet = outlets[num]
_, err = outlet.Channel.Toggle()
if err != nil { if err != nil {
log.Fatal(err) ctx.JSON(http.StatusOK, Dictionary{
"data": "error3",
})
} }
MQTTpublish("openpdu/toggolo2", fmt.Sprint(id)) // MQTTpublish("openpdu/toggolo1", fmt.Sprint(id))
ctx.JSON(http.StatusOK, Dictionary{ ctx.JSON(http.StatusOK, Dictionary{
"data": "ok", "data": "ok",
}) })