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

This commit is contained in:
Paolo Asperti 2021-01-05 17:57:45 +01:00
parent a7c24f5e94
commit 04d2fac964
4 changed files with 69 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import (
"log" "log"
"strings" "strings"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -21,6 +22,8 @@ type Channel interface {
SetOnBoot(string) SetOnBoot(string)
SetMQTTStateTopic(string) SetMQTTStateTopic(string)
SetMQTTCommandTopic(string) SetMQTTCommandTopic(string)
MQTTCommandTopic() string
MQTTHandler(MQTT.Client, MQTT.Message)
} }
type Board interface { type Board interface {

View File

@ -5,6 +5,7 @@ import (
"log" "log"
"strings" "strings"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -13,7 +14,7 @@ type DummyChannel struct {
Num uint Num uint
name string name string
MQTTStateTopic string MQTTStateTopic string
MQTTCommandTopic string mqttCommandTopic string
Value bool Value bool
onboot string onboot string
parent *DummyBoard parent *DummyBoard
@ -46,7 +47,7 @@ func newDummyChannel(v *viper.Viper, channelID string) DummyChannel {
Num: v.GetUint("num"), Num: v.GetUint("num"),
name: v.GetString("name"), name: v.GetString("name"),
MQTTStateTopic: v.GetString("mqtt.statetopic"), MQTTStateTopic: v.GetString("mqtt.statetopic"),
MQTTCommandTopic: v.GetString("mqtt.commandtopic"), mqttCommandTopic: v.GetString("mqtt.commandtopic"),
Value: value, Value: value,
onboot: v.GetString("onboot"), onboot: v.GetString("onboot"),
} }
@ -96,19 +97,22 @@ func newDummyBoard(v *viper.Viper, id string) *DummyBoard {
func (c *DummyChannel) Toggle() (bool, error) { func (c *DummyChannel) Toggle() (bool, error) {
c.Value = !c.Value c.Value = !c.Value
c.OnChange() c.SaveLastState()
c.UpdateMQTT()
return c.Value, nil return c.Value, nil
} }
func (c *DummyChannel) On() error { func (c *DummyChannel) On() error {
c.Value = true c.Value = true
c.OnChange() c.SaveLastState()
c.UpdateMQTT()
return nil return nil
} }
func (c *DummyChannel) Off() error { func (c *DummyChannel) Off() error {
c.Value = true c.Value = true
c.OnChange() c.SaveLastState()
c.UpdateMQTT()
return nil return nil
} }
@ -123,13 +127,12 @@ func (c *DummyChannel) UpdateMQTT() {
MQTTpublish(c.MQTTStateTopic, c.ToString()) MQTTpublish(c.MQTTStateTopic, c.ToString())
} }
func (c *DummyChannel) OnChange() { func (c *DummyChannel) SaveLastState() {
if c.onboot == "last" { if c.onboot == "last" {
s := fmt.Sprintf("boards.%s.channels.%s.lastvalue", c.parent.ID, c.ID) s := fmt.Sprintf("boards.%s.channels.%s.lastvalue", c.parent.ID, c.ID)
viper.Set(s, c.Value) viper.Set(s, c.Value)
viper.WriteConfig() viper.WriteConfig()
} }
c.UpdateMQTT()
} }
func (c *DummyChannel) Status() bool { func (c *DummyChannel) Status() bool {
@ -141,7 +144,7 @@ func (c *DummyChannel) Parent() Board {
} }
func (c *DummyChannel) Dump() { func (c *DummyChannel) 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 *DummyBoard) Dump() { func (b *DummyBoard) Dump() {
@ -180,7 +183,26 @@ func (c *DummyChannel) SetMQTTStateTopic(str string) {
} }
func (c *DummyChannel) SetMQTTCommandTopic(str string) { func (c *DummyChannel) SetMQTTCommandTopic(str string) {
c.MQTTCommandTopic = str c.mqttCommandTopic = str
s := fmt.Sprintf("boards.%s.channels.%s.mqtt.commandtopic", c.parent.ID, c.ID) s := fmt.Sprintf("boards.%s.channels.%s.mqtt.commandtopic", c.parent.ID, c.ID)
viper.Set(s, str) viper.Set(s, str)
} }
func (c *DummyChannel) MQTTHandler(client MQTT.Client, msg MQTT.Message) {
switch string(msg.Payload()) {
case "on":
if !c.Value {
c.Value = true
c.SaveLastState()
}
case "off":
if c.Value {
c.Value = false
c.SaveLastState()
}
}
}
func (c *DummyChannel) MQTTCommandTopic() string {
return c.mqttCommandTopic
}

View File

@ -15,7 +15,7 @@ type MQTTChannel struct {
Num uint Num uint
name string name string
MQTTStateTopic string MQTTStateTopic string
MQTTCommandTopic string mqttCommandTopic string
Value bool Value bool
onboot string onboot string
parent *MQTTBoard parent *MQTTBoard
@ -63,7 +63,7 @@ func newMQTTChannel(v *viper.Viper, channelID string) MQTTChannel {
Num: v.GetUint("num"), Num: v.GetUint("num"),
name: v.GetString("name"), name: v.GetString("name"),
MQTTStateTopic: v.GetString("mqtt.statetopic"), MQTTStateTopic: v.GetString("mqtt.statetopic"),
MQTTCommandTopic: v.GetString("mqtt.commandtopic"), mqttCommandTopic: v.GetString("mqtt.commandtopic"),
Value: value, Value: value,
onboot: v.GetString("onboot"), onboot: v.GetString("onboot"),
MQTTRemoteStateTopic: v.GetString("mqttremote.statetopic"), MQTTRemoteStateTopic: v.GetString("mqttremote.statetopic"),
@ -187,7 +187,7 @@ func (c *MQTTChannel) Parent() Board {
} }
func (c *MQTTChannel) Dump() { func (c *MQTTChannel) 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 *MQTTBoard) Dump() { func (b *MQTTBoard) Dump() {
@ -242,11 +242,11 @@ func (b *MQTTBoard) Init() {
if topic == "" { if topic == "" {
continue continue
} }
b.MQTTClient.Subscribe(topic, 1, b.Channels[i].MQTTSubHandler) b.MQTTClient.Subscribe(topic, 1, b.Channels[i].MQTTRemoteHandler)
} }
} }
func (c *MQTTChannel) MQTTSubHandler(client MQTT.Client, msg MQTT.Message) { func (c *MQTTChannel) MQTTRemoteHandler(client MQTT.Client, msg MQTT.Message) {
switch string(msg.Payload()) { switch string(msg.Payload()) {
case c.MQTTRemotePayloadOn: case c.MQTTRemotePayloadOn:
if !c.Value { if !c.Value {
@ -263,6 +263,23 @@ func (c *MQTTChannel) MQTTSubHandler(client MQTT.Client, msg MQTT.Message) {
} }
} }
func (c *MQTTChannel) MQTTHandler(client MQTT.Client, msg MQTT.Message) {
switch string(msg.Payload()) {
case "on":
if !c.Value {
c.Value = true
c.SaveLastState()
c.UpdateRemoteMQTT()
}
case "off":
if c.Value {
c.Value = false
c.SaveLastState()
c.UpdateRemoteMQTT()
}
}
}
func (b *MQTTBoard) Channel(num uint64) Channel { func (b *MQTTBoard) Channel(num uint64) Channel {
return b.Channels[num] return b.Channels[num]
} }
@ -288,7 +305,11 @@ func (c *MQTTChannel) SetMQTTStateTopic(str string) {
} }
func (c *MQTTChannel) SetMQTTCommandTopic(str string) { func (c *MQTTChannel) SetMQTTCommandTopic(str string) {
c.MQTTCommandTopic = str c.mqttCommandTopic = str
s := fmt.Sprintf("boards.%s.channels.%s.mqtt.commandtopic", c.parent.ID, c.ID) s := fmt.Sprintf("boards.%s.channels.%s.mqtt.commandtopic", c.parent.ID, c.ID)
viper.Set(s, str) viper.Set(s, str)
} }
func (c *MQTTChannel) MQTTCommandTopic() string {
return c.mqttCommandTopic
}

View File

@ -69,6 +69,14 @@ func mqttLoop() {
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
} }
for o := range outlets {
topic := outlets[o].Channel.MQTTCommandTopic()
if topic == "" {
continue
}
MQTTclient.Subscribe(viper.GetString("Mqtt.Prefix")+"/"+topic, 1, outlets[o].Channel.MQTTHandler)
}
go MQTTRefreshLoop() go MQTTRefreshLoop()
} }