Update board.go, main.go, and webui.go
This commit is contained in:
parent
1b542a3109
commit
1868b1f1b6
134
src/board.go
Normal file
134
src/board.go
Normal file
@ -0,0 +1,134 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Channel struct {
|
||||
num uint
|
||||
name string
|
||||
value bool
|
||||
inverted bool // low level only
|
||||
startValue bool
|
||||
}
|
||||
|
||||
type Board struct {
|
||||
id string
|
||||
name string
|
||||
channelCount uint
|
||||
inverted bool // low level only
|
||||
channels []Channel
|
||||
}
|
||||
|
||||
func (c Channel) Toggle() (bool, error) {
|
||||
c.value = !c.value
|
||||
return c.value, nil
|
||||
}
|
||||
|
||||
func (c Channel) On() error {
|
||||
c.value = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Channel) Off() error {
|
||||
c.value = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Channel) Status() bool {
|
||||
return c.value
|
||||
}
|
||||
|
||||
var boards []Board
|
||||
|
||||
func newDummyChannel(v *viper.Viper, num uint) Channel {
|
||||
v.SetDefault("name", fmt.Sprintf("channel %d", num))
|
||||
v.SetDefault("startValue", false)
|
||||
v.SetDefault("inverted", false)
|
||||
return Channel{
|
||||
num: num,
|
||||
name: v.GetString("name"),
|
||||
value: v.GetBool("startValue"),
|
||||
inverted: v.GetBool("inverted"),
|
||||
startValue: v.GetBool("startValue"),
|
||||
}
|
||||
}
|
||||
|
||||
func newDummyBoard(v *viper.Viper, id string) Board {
|
||||
v.SetDefault("name", "board "+id)
|
||||
v.SetDefault("inverted", false)
|
||||
v.SetDefault("type", "dummy")
|
||||
v.SetDefault("channelCount", 0)
|
||||
v.SetDefault("channels", "")
|
||||
|
||||
if v.GetInt("channelCount") > 0 {
|
||||
for i := 0; i < v.GetInt("channelCount"); i++ {
|
||||
v.SetDefault("channels."+fmt.Sprint(i)+".num", i)
|
||||
}
|
||||
}
|
||||
|
||||
channels := make([]Channel, v.GetInt("channelCount"))
|
||||
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,
|
||||
name: v.GetString("name"),
|
||||
channelCount: v.GetUint("channelCount"),
|
||||
inverted: v.GetBool("inverted"),
|
||||
channels: channels,
|
||||
}
|
||||
}
|
||||
|
||||
func parseBoardsConfig() {
|
||||
boardsConfig := viper.Sub("boards")
|
||||
if boardsConfig == nil {
|
||||
logWarning("No board configured")
|
||||
return
|
||||
}
|
||||
|
||||
for key := range boardsConfig.AllSettings() {
|
||||
boardConfig := boardsConfig.Sub(key)
|
||||
|
||||
boardType := boardConfig.GetString("type")
|
||||
switch boardType {
|
||||
case "gpio":
|
||||
default:
|
||||
boards = append(boards, newDummyBoard(boardConfig, key))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// dumpa tutto
|
||||
for b := range boards {
|
||||
boards[b].Dump()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c Channel) Dump() {
|
||||
v := "off"
|
||||
if c.startValue {
|
||||
v = "on"
|
||||
}
|
||||
log.Printf(" Channel %d (start: %s): %s \n", c.num, v, c.name)
|
||||
}
|
||||
|
||||
func (b Board) Dump() {
|
||||
log.Printf("Board '%s' (id: %s): %d channels\n", b.name, b.id, b.channelCount)
|
||||
for c := range b.channels {
|
||||
b.channels[c].Dump()
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ func init() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
parseBoardsConfig()
|
||||
go mqttLoop()
|
||||
go UpsConnect()
|
||||
logInfo("hostname: " + viper.GetString("hostname"))
|
||||
|
@ -88,12 +88,12 @@ func jsonOutletToggle(ctx *macaron.Context) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
MQTTpublish("openpdu/toggolo1", string(id))
|
||||
MQTTpublish("openpdu/toggolo1", fmt.Sprint(id))
|
||||
err = MyBoard.channelToggle(uint(id))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
MQTTpublish("openpdu/toggolo2", string(id))
|
||||
MQTTpublish("openpdu/toggolo2", fmt.Sprint(id))
|
||||
ctx.JSON(http.StatusOK, Dictionary{
|
||||
"data": "ok",
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user