Update board.go, board_dummy.go, and 4 more files...

This commit is contained in:
Paolo Asperti 2021-01-10 15:43:23 +01:00
parent 1a5819d46f
commit be8d733232
6 changed files with 29 additions and 18 deletions

View File

@ -11,11 +11,11 @@ var boardCreatorFunctions = make(map[string]BoardCreatorFunction)
var AllChannels = make(map[string]Channel) var AllChannels = make(map[string]Channel)
type BoardCreatorFunction func(*viper.Viper, string) Board type BoardCreatorFunction func(*viper.Viper, string) *Board
type onChannelUpdateFunction func(oldValue bool, c Channel) type onChannelUpdateFunction func(oldValue bool, c Channel)
type Board interface { type Board interface {
Init() Initialize()
Dump() Dump()
Channel(uint64) Channel Channel(uint64) Channel
} }
@ -53,5 +53,6 @@ func CreateBoard(cfg *viper.Viper, key string) (Board, error) {
if !ok { if !ok {
return nil, errors.New("Unknown board type") return nil, errors.New("Unknown board type")
} }
return createNewBoard(cfg, key), nil
return *createNewBoard(cfg, key), nil
} }

View File

@ -54,8 +54,8 @@ func newDummyChannel(v *viper.Viper, channelID string) DummyChannel {
} }
} }
func newDummyBoard(v *viper.Viper, id string) Board { func newDummyBoard(v *viper.Viper, id string) *Board {
var b DummyBoard var b *DummyBoard
v.SetDefault("name", "board "+id) v.SetDefault("name", "board "+id)
v.SetDefault("type", "dummy") v.SetDefault("type", "dummy")
@ -68,7 +68,7 @@ func newDummyBoard(v *viper.Viper, id string) Board {
} }
} }
b = DummyBoard{ b = &DummyBoard{
ID: id, ID: id,
Name: v.GetString("name"), Name: v.GetString("name"),
ChannelCount: v.GetUint("channelCount"), ChannelCount: v.GetUint("channelCount"),
@ -82,7 +82,7 @@ func newDummyBoard(v *viper.Viper, id string) Board {
channelid := strings.ToLower(channelid1) channelid := strings.ToLower(channelid1)
channelConfig := channelsConfig.Sub(channelid) channelConfig := channelsConfig.Sub(channelid)
c := newDummyChannel(channelConfig, channelid) c := newDummyChannel(channelConfig, channelid)
c.parent = &b c.parent = b
if c.Num >= v.GetUint("channelCount") { if c.Num >= v.GetUint("channelCount") {
continue continue
} }
@ -92,8 +92,8 @@ func newDummyBoard(v *viper.Viper, id string) Board {
} }
} }
b.channels = channels b.channels = channels
var b1 Board = b
return b return &b1
} }
func (c *DummyChannel) Toggle() (bool, error) { func (c *DummyChannel) Toggle() (bool, error) {
@ -163,11 +163,11 @@ func (b DummyBoard) Dump() {
} }
} }
func (b DummyBoard) Init() { func (b DummyBoard) Initialize() {
return return
} }
func (b DummyBoard) Channel(num uint64) Channel { func (b *DummyBoard) Channel(num uint64) Channel {
return b.channels[num] return b.channels[num]
} }

View File

@ -75,7 +75,7 @@ func newMQTTChannel(v *viper.Viper, channelID string) MQTTChannel {
} }
} }
func newMQTTBoard(v *viper.Viper, id string) Board { func newMQTTBoard(v *viper.Viper, id string) *Board {
var b MQTTBoard var b MQTTBoard
v.SetDefault("name", "board "+id) v.SetDefault("name", "board "+id)
@ -124,7 +124,8 @@ func newMQTTBoard(v *viper.Viper, id string) Board {
} }
b.channels = channels b.channels = channels
return b var b1 Board = &b
return &b1
} }
func init() { func init() {
@ -208,7 +209,7 @@ func (b MQTTBoard) Dump() {
} }
} }
func (b MQTTBoard) Init() { func (b *MQTTBoard) Initialize() {
uri := b.MQTTRemoteSchema + "://" + b.MQTTRemoteHost + ":" + b.MQTTRemotePort uri := b.MQTTRemoteSchema + "://" + b.MQTTRemoteHost + ":" + b.MQTTRemotePort
opts := PahoMQTT.NewClientOptions().AddBroker(uri) opts := PahoMQTT.NewClientOptions().AddBroker(uri)
@ -300,7 +301,7 @@ func (c *MQTTChannel) MQTTHandler(client PahoMQTT.Client, msg PahoMQTT.Message)
} }
} }
func (b MQTTBoard) Channel(num uint64) Channel { func (b *MQTTBoard) Channel(num uint64) Channel {
return b.channels[num] return b.channels[num]
} }

View File

@ -9,7 +9,7 @@ import (
var boards []*board.Board var boards []*board.Board
func InitBoards() { func CreateBoards() {
// AllChannels = make(map[string]Channel) // AllChannels = make(map[string]Channel)
@ -52,3 +52,11 @@ func InitBoards() {
// } // }
} }
func InitBoards() {
var b board.Board
for i := range boards {
b = *boards[i]
b.Initialize()
}
}

View File

@ -12,8 +12,9 @@ func init() {
} }
func main() { func main() {
CreateBoards()
CreateOutlets()
InitBoards() InitBoards()
InitOutlets()
MQTTSetup() MQTTSetup()
go MQTTRefreshLoop() go MQTTRefreshLoop()
go ups.UpsConnect() go ups.UpsConnect()

View File

@ -11,7 +11,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func InitOutlets() { func CreateOutlets() {
outletsConfig := viper.Sub("outlets") outletsConfig := viper.Sub("outlets")
if outletsConfig == nil { if outletsConfig == nil {