Update board.go, board_dummy.go, and 2 more files...
This commit is contained in:
parent
a7c24f5e94
commit
04d2fac964
@ -4,6 +4,7 @@ import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@ -21,6 +22,8 @@ type Channel interface {
|
||||
SetOnBoot(string)
|
||||
SetMQTTStateTopic(string)
|
||||
SetMQTTCommandTopic(string)
|
||||
MQTTCommandTopic() string
|
||||
MQTTHandler(MQTT.Client, MQTT.Message)
|
||||
}
|
||||
|
||||
type Board interface {
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@ -13,7 +14,7 @@ type DummyChannel struct {
|
||||
Num uint
|
||||
name string
|
||||
MQTTStateTopic string
|
||||
MQTTCommandTopic string
|
||||
mqttCommandTopic string
|
||||
Value bool
|
||||
onboot string
|
||||
parent *DummyBoard
|
||||
@ -46,7 +47,7 @@ func newDummyChannel(v *viper.Viper, channelID string) DummyChannel {
|
||||
Num: v.GetUint("num"),
|
||||
name: v.GetString("name"),
|
||||
MQTTStateTopic: v.GetString("mqtt.statetopic"),
|
||||
MQTTCommandTopic: v.GetString("mqtt.commandtopic"),
|
||||
mqttCommandTopic: v.GetString("mqtt.commandtopic"),
|
||||
Value: value,
|
||||
onboot: v.GetString("onboot"),
|
||||
}
|
||||
@ -96,19 +97,22 @@ func newDummyBoard(v *viper.Viper, id string) *DummyBoard {
|
||||
|
||||
func (c *DummyChannel) Toggle() (bool, error) {
|
||||
c.Value = !c.Value
|
||||
c.OnChange()
|
||||
c.SaveLastState()
|
||||
c.UpdateMQTT()
|
||||
return c.Value, nil
|
||||
}
|
||||
|
||||
func (c *DummyChannel) On() error {
|
||||
c.Value = true
|
||||
c.OnChange()
|
||||
c.SaveLastState()
|
||||
c.UpdateMQTT()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *DummyChannel) Off() error {
|
||||
c.Value = true
|
||||
c.OnChange()
|
||||
c.SaveLastState()
|
||||
c.UpdateMQTT()
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -123,13 +127,12 @@ func (c *DummyChannel) UpdateMQTT() {
|
||||
MQTTpublish(c.MQTTStateTopic, c.ToString())
|
||||
}
|
||||
|
||||
func (c *DummyChannel) OnChange() {
|
||||
func (c *DummyChannel) SaveLastState() {
|
||||
if c.onboot == "last" {
|
||||
s := fmt.Sprintf("boards.%s.channels.%s.lastvalue", c.parent.ID, c.ID)
|
||||
viper.Set(s, c.Value)
|
||||
viper.WriteConfig()
|
||||
}
|
||||
c.UpdateMQTT()
|
||||
}
|
||||
|
||||
func (c *DummyChannel) Status() bool {
|
||||
@ -141,7 +144,7 @@ func (c *DummyChannel) Parent() Board {
|
||||
}
|
||||
|
||||
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() {
|
||||
@ -180,7 +183,26 @@ func (c *DummyChannel) SetMQTTStateTopic(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)
|
||||
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
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ type MQTTChannel struct {
|
||||
Num uint
|
||||
name string
|
||||
MQTTStateTopic string
|
||||
MQTTCommandTopic string
|
||||
mqttCommandTopic string
|
||||
Value bool
|
||||
onboot string
|
||||
parent *MQTTBoard
|
||||
@ -63,7 +63,7 @@ func newMQTTChannel(v *viper.Viper, channelID string) MQTTChannel {
|
||||
Num: v.GetUint("num"),
|
||||
name: v.GetString("name"),
|
||||
MQTTStateTopic: v.GetString("mqtt.statetopic"),
|
||||
MQTTCommandTopic: v.GetString("mqtt.commandtopic"),
|
||||
mqttCommandTopic: v.GetString("mqtt.commandtopic"),
|
||||
Value: value,
|
||||
onboot: v.GetString("onboot"),
|
||||
MQTTRemoteStateTopic: v.GetString("mqttremote.statetopic"),
|
||||
@ -187,7 +187,7 @@ func (c *MQTTChannel) Parent() Board {
|
||||
}
|
||||
|
||||
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() {
|
||||
@ -242,11 +242,11 @@ func (b *MQTTBoard) Init() {
|
||||
if topic == "" {
|
||||
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()) {
|
||||
case c.MQTTRemotePayloadOn:
|
||||
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 {
|
||||
return b.Channels[num]
|
||||
}
|
||||
@ -288,7 +305,11 @@ func (c *MQTTChannel) SetMQTTStateTopic(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)
|
||||
viper.Set(s, str)
|
||||
}
|
||||
|
||||
func (c *MQTTChannel) MQTTCommandTopic() string {
|
||||
return c.mqttCommandTopic
|
||||
}
|
||||
|
@ -69,6 +69,14 @@ func mqttLoop() {
|
||||
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()
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user