This commit is contained in:
Paolo Asperti 2021-01-02 16:08:41 +01:00
parent 1868b1f1b6
commit 23e8c067e8
Signed by: paspo
GPG Key ID: 06D46905D19D5182
3 changed files with 84 additions and 66 deletions

View File

@ -3,16 +3,19 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"strings"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
type Channel struct { type Channel struct {
id string
num uint num uint
name string name string
value bool value bool
inverted bool // low level only inverted bool // low level only
startValue bool startValue bool
parent *Board
} }
type Board struct { type Board struct {
@ -23,6 +26,12 @@ type Board struct {
channels []Channel channels []Channel
} }
type Outlet struct {
num uint
board *Board
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
@ -43,13 +52,18 @@ func (c Channel) Status() bool {
} }
var boards []Board var boards []Board
var allChannels map[string]*Channel
var outlets []Outlet
func newDummyChannel(v *viper.Viper, num uint) Channel { func newDummyChannel(v *viper.Viper, channelID string) Channel {
v.SetDefault("name", fmt.Sprintf("channel %d", num)) v.SetDefault("name", "unknown")
v.SetDefault("startValue", false) v.SetDefault("startValue", false)
v.SetDefault("inverted", false) v.SetDefault("inverted", false)
// newUUID := UUID.New().String()
// v.SetDefault("id", newUUID)
return Channel{ return Channel{
num: num, id: channelID,
num: v.GetUint("num"),
name: v.GetString("name"), name: v.GetString("name"),
value: v.GetBool("startValue"), value: v.GetBool("startValue"),
inverted: v.GetBool("inverted"), inverted: v.GetBool("inverted"),
@ -58,6 +72,8 @@ func newDummyChannel(v *viper.Viper, num uint) Channel {
} }
func newDummyBoard(v *viper.Viper, id string) Board { func newDummyBoard(v *viper.Viper, id string) Board {
var b Board
v.SetDefault("name", "board "+id) v.SetDefault("name", "board "+id)
v.SetDefault("inverted", false) v.SetDefault("inverted", false)
v.SetDefault("type", "dummy") v.SetDefault("type", "dummy")
@ -70,28 +86,40 @@ func newDummyBoard(v *viper.Viper, id string) Board {
} }
} }
channels := make([]Channel, v.GetInt("channelCount")) b = Board{
if v.GetInt("channelCount") > 0 {
channelsConfig := v.Sub("channels")
if channelsConfig != nil {
for num := range channelsConfig.AllSettings() {
channelConfig := channelsConfig.Sub(num)
n := channelConfig.GetUint("num")
channels[n] = newDummyChannel(channelConfig, n)
}
}
}
return Board{
id: id, id: id,
name: v.GetString("name"), name: v.GetString("name"),
channelCount: v.GetUint("channelCount"), channelCount: v.GetUint("channelCount"),
inverted: v.GetBool("inverted"), inverted: v.GetBool("inverted"),
channels: channels,
} }
channels := make([]Channel, v.GetInt("channelCount"))
if v.GetInt("channelCount") > 0 {
channelsConfig := v.Sub("channels")
if channelsConfig != nil {
for channelid1 := range channelsConfig.AllSettings() {
channelid := strings.ToLower(channelid1)
channelConfig := channelsConfig.Sub(channelid)
c := newDummyChannel(channelConfig, channelid)
c.parent = &b
if c.num >= v.GetUint("channelCount") {
continue
}
channels[c.num] = c
allChannels[c.id] = &c
}
}
}
b.channels = channels
return b
} }
func parseBoardsConfig() { func parseBoardsConfig() {
// TODO: init boards array?
allChannels = make(map[string]*Channel)
boardsConfig := viper.Sub("boards") boardsConfig := viper.Sub("boards")
if boardsConfig == nil { if boardsConfig == nil {
logWarning("No board configured") logWarning("No board configured")
@ -111,11 +139,37 @@ func parseBoardsConfig() {
} }
outletsConfig := viper.Sub("outlets")
if outletsConfig == nil {
logWarning("No outlet configured")
return
}
for key := range outletsConfig.AllSettings() {
outletConfig := outletsConfig.Sub(key)
num := outletConfig.GetUint("num")
channelID := strings.ToLower(outletConfig.GetString("channelID"))
channel := allChannels[channelID]
// TODO: channel non esistente
o := Outlet{
num: num,
board: channel.parent,
channel: channel,
}
outlets = append(outlets, o)
}
// dumpa tutto // dumpa tutto
for b := range boards { for b := range boards {
boards[b].Dump() boards[b].Dump()
} }
// dumpa tutto
for o := range outlets {
outlets[o].Dump()
}
} }
func (c Channel) Dump() { func (c Channel) Dump() {
@ -132,3 +186,7 @@ func (b Board) Dump() {
b.channels[c].Dump() b.channels[c].Dump()
} }
} }
func (o Outlet) Dump() {
log.Printf("Outlet %v: channel name: %v\n", o.num, o.channel.name)
}

View File

@ -26,6 +26,7 @@ var MyBoard = I2CBoard{
var i2cbus i2c.Bus var i2cbus i2c.Bus
func (b I2CBoard) channelStatus(ch uint) (bool, error) { func (b I2CBoard) channelStatus(ch uint) (bool, error) {
return false, nil
if b.channels <= 0 { if b.channels <= 0 {
return false, errors.New("Board without channels") return false, errors.New("Board without channels")
} }

View File

@ -27,59 +27,18 @@ func statusPage(ctx *macaron.Context) {
} }
func jsonStatus(ctx *macaron.Context) { func jsonStatus(ctx *macaron.Context) {
MQTTpublish("openpdu/status", "asdss") // MQTTpublish("openpdu/status", "asdss")
p0, err := MyBoard.channelStatus(0) var data = [][]string{}
if err != nil { var o Outlet
log.Fatal(err) for i := range outlets {
} o = outlets[i]
d := []string{fmt.Sprintf("%d", o.num), o.channel.name, fmt.Sprintf("%v", o.channel.value)}
p1, err := MyBoard.channelStatus(1) data = append(data, d)
if err != nil {
log.Fatal(err)
}
p2, err := MyBoard.channelStatus(2)
if err != nil {
log.Fatal(err)
}
p3, err := MyBoard.channelStatus(3)
if err != nil {
log.Fatal(err)
}
p4, err := MyBoard.channelStatus(4)
if err != nil {
log.Fatal(err)
}
p5, err := MyBoard.channelStatus(5)
if err != nil {
log.Fatal(err)
}
p6, err := MyBoard.channelStatus(6)
if err != nil {
log.Fatal(err)
}
p7, err := MyBoard.channelStatus(7)
if err != nil {
log.Fatal(err)
} }
ctx.JSON(http.StatusOK, Dictionary{ ctx.JSON(http.StatusOK, Dictionary{
"data": [][]string{ "data": data,
{"0", "p0", fmt.Sprint(p0)},
{"1", "p1", fmt.Sprint(p1)},
{"2", "p2", fmt.Sprint(p2)},
{"3", "p3", fmt.Sprint(p3)},
{"4", "p4", fmt.Sprint(p4)},
{"5", "p5", fmt.Sprint(p5)},
{"6", "p6", fmt.Sprint(p6)},
{"7", "p7", fmt.Sprint(p7)},
},
}) })
} }