Update board_mqtt.go
This commit is contained in:
parent
5e2a7c46bc
commit
355bb6532b
@ -12,14 +12,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MQTTChannel struct {
|
type MQTTChannel struct {
|
||||||
ID string
|
ID string
|
||||||
Num uint
|
Num uint
|
||||||
name string
|
name string
|
||||||
MQTTTopic string
|
MQTTTopic string
|
||||||
Value bool
|
Value bool
|
||||||
onboot string
|
onboot string
|
||||||
parent *MQTTBoard
|
parent *MQTTBoard
|
||||||
MQTTRemoteTopic string
|
MQTTRemoteStateTopic string
|
||||||
|
MQTTRemoteCommandTopic string
|
||||||
|
MQTTRemotePayloadOn string
|
||||||
|
MQTTRemotePayloadOff string
|
||||||
}
|
}
|
||||||
|
|
||||||
type MQTTBoard struct {
|
type MQTTBoard struct {
|
||||||
@ -40,7 +43,10 @@ func newMQTTChannel(v *viper.Viper, channelID string) MQTTChannel {
|
|||||||
v.SetDefault("lastValue", false)
|
v.SetDefault("lastValue", false)
|
||||||
v.SetDefault("onboot", "off")
|
v.SetDefault("onboot", "off")
|
||||||
v.SetDefault("mqtttopic", v.GetString("name"))
|
v.SetDefault("mqtttopic", v.GetString("name"))
|
||||||
v.SetDefault("mqttremote.topic", v.GetString("name"))
|
v.SetDefault("mqttremote.statetopic", v.GetString("name"))
|
||||||
|
v.SetDefault("mqttremote.commandtopic", v.GetString("name"))
|
||||||
|
v.SetDefault("mqttremote.payloadon", "on")
|
||||||
|
v.SetDefault("mqttremote.payloadoff", "off")
|
||||||
|
|
||||||
value := false
|
value := false
|
||||||
switch v.GetString("onboot") {
|
switch v.GetString("onboot") {
|
||||||
@ -52,13 +58,16 @@ func newMQTTChannel(v *viper.Viper, channelID string) MQTTChannel {
|
|||||||
// newUUID := UUID.New().String()
|
// newUUID := UUID.New().String()
|
||||||
// v.SetDefault("id", newUUID)
|
// v.SetDefault("id", newUUID)
|
||||||
return MQTTChannel{
|
return MQTTChannel{
|
||||||
ID: channelID,
|
ID: channelID,
|
||||||
Num: v.GetUint("num"),
|
Num: v.GetUint("num"),
|
||||||
name: v.GetString("name"),
|
name: v.GetString("name"),
|
||||||
MQTTTopic: v.GetString("mqtttopic"),
|
MQTTTopic: v.GetString("mqtttopic"),
|
||||||
Value: value,
|
Value: value,
|
||||||
onboot: v.GetString("onboot"),
|
onboot: v.GetString("onboot"),
|
||||||
MQTTRemoteTopic: v.GetString("mqttremote.topic"),
|
MQTTRemoteStateTopic: v.GetString("mqttremote.statetopic"),
|
||||||
|
MQTTRemoteCommandTopic: v.GetString("mqttremote.commandtopic"),
|
||||||
|
MQTTRemotePayloadOn: v.GetString("mqttremote.payloadon"),
|
||||||
|
MQTTRemotePayloadOff: v.GetString("mqttremote.payloadoff"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +168,11 @@ func (c *MQTTChannel) SaveLastState() {
|
|||||||
|
|
||||||
func (c *MQTTChannel) UpdateRemoteMQTT() {
|
func (c *MQTTChannel) UpdateRemoteMQTT() {
|
||||||
if c.parent.MQTTClient.IsConnected() {
|
if c.parent.MQTTClient.IsConnected() {
|
||||||
c.parent.MQTTClient.Publish(c.MQTTRemoteTopic, 0, false, c.ToString())
|
v := c.MQTTRemotePayloadOff
|
||||||
|
if c.Value {
|
||||||
|
v = c.MQTTRemotePayloadOn
|
||||||
|
}
|
||||||
|
c.parent.MQTTClient.Publish(c.MQTTRemoteCommandTopic, 0, false, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,7 +236,7 @@ func (b *MQTTBoard) Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := range b.Channels {
|
for i := range b.Channels {
|
||||||
topic := b.Channels[i].MQTTRemoteTopic
|
topic := b.Channels[i].MQTTRemoteStateTopic
|
||||||
if topic == "" {
|
if topic == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -233,13 +246,13 @@ func (b *MQTTBoard) Init() {
|
|||||||
|
|
||||||
func (c *MQTTChannel) MQTTSubHandler(client mqtt.Client, msg mqtt.Message) {
|
func (c *MQTTChannel) MQTTSubHandler(client mqtt.Client, msg mqtt.Message) {
|
||||||
switch string(msg.Payload()) {
|
switch string(msg.Payload()) {
|
||||||
case "on":
|
case c.MQTTRemotePayloadOn:
|
||||||
if !c.Value {
|
if !c.Value {
|
||||||
c.Value = true
|
c.Value = true
|
||||||
c.SaveLastState()
|
c.SaveLastState()
|
||||||
c.UpdateMQTT()
|
c.UpdateMQTT()
|
||||||
}
|
}
|
||||||
case "off":
|
case c.MQTTRemotePayloadOff:
|
||||||
if c.Value {
|
if c.Value {
|
||||||
c.Value = false
|
c.Value = false
|
||||||
c.SaveLastState()
|
c.SaveLastState()
|
||||||
|
Loading…
Reference in New Issue
Block a user