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

This commit is contained in:
Paolo Asperti 2021-01-05 17:34:23 +01:00
parent 355bb6532b
commit a7c24f5e94
5 changed files with 69 additions and 38 deletions

View File

@ -19,7 +19,8 @@ type Channel interface {
Name() string Name() string
OnBoot() string OnBoot() string
SetOnBoot(string) SetOnBoot(string)
SetMQTTTopic(string) SetMQTTStateTopic(string)
SetMQTTCommandTopic(string)
} }
type Board interface { type Board interface {

View File

@ -12,7 +12,8 @@ type DummyChannel struct {
ID string ID string
Num uint Num uint
name string name string
MQTTTopic string MQTTStateTopic string
MQTTCommandTopic string
Value bool Value bool
onboot string onboot string
parent *DummyBoard parent *DummyBoard
@ -29,7 +30,7 @@ func newDummyChannel(v *viper.Viper, channelID string) DummyChannel {
v.SetDefault("name", "unknown") v.SetDefault("name", "unknown")
v.SetDefault("lastValue", false) v.SetDefault("lastValue", false)
v.SetDefault("onboot", "off") v.SetDefault("onboot", "off")
v.SetDefault("mqtttopic", v.GetString("name")) v.SetDefault("mqtt.statetopic", v.GetString("name"))
value := false value := false
switch v.GetString("onboot") { switch v.GetString("onboot") {
@ -44,7 +45,8 @@ func newDummyChannel(v *viper.Viper, channelID string) DummyChannel {
ID: channelID, ID: channelID,
Num: v.GetUint("num"), Num: v.GetUint("num"),
name: v.GetString("name"), name: v.GetString("name"),
MQTTTopic: v.GetString("mqtttopic"), MQTTStateTopic: v.GetString("mqtt.statetopic"),
MQTTCommandTopic: v.GetString("mqtt.commandtopic"),
Value: value, Value: value,
onboot: v.GetString("onboot"), onboot: v.GetString("onboot"),
} }
@ -118,7 +120,7 @@ func (c *DummyChannel) ToString() string {
} }
func (c *DummyChannel) UpdateMQTT() { func (c *DummyChannel) UpdateMQTT() {
MQTTpublish(c.MQTTTopic, c.ToString()) MQTTpublish(c.MQTTStateTopic, c.ToString())
} }
func (c *DummyChannel) OnChange() { func (c *DummyChannel) OnChange() {
@ -171,8 +173,14 @@ func (c *DummyChannel) SetOnBoot(str string) {
viper.Set(s, str) viper.Set(s, str)
} }
func (c *DummyChannel) SetMQTTTopic(str string) { func (c *DummyChannel) SetMQTTStateTopic(str string) {
c.MQTTTopic = str c.MQTTStateTopic = str
s := fmt.Sprintf("boards.%s.channels.%s.mqtttopic", c.parent.ID, c.ID) s := fmt.Sprintf("boards.%s.channels.%s.mqtt.statetopic", c.parent.ID, c.ID)
viper.Set(s, str)
}
func (c *DummyChannel) SetMQTTCommandTopic(str string) {
c.MQTTCommandTopic = str
s := fmt.Sprintf("boards.%s.channels.%s.mqtt.commandtopic", c.parent.ID, c.ID)
viper.Set(s, str) viper.Set(s, str)
} }

View File

@ -7,7 +7,6 @@ import (
"time" "time"
MQTT "github.com/eclipse/paho.mqtt.golang" MQTT "github.com/eclipse/paho.mqtt.golang"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -15,7 +14,8 @@ type MQTTChannel struct {
ID string ID string
Num uint Num uint
name string name string
MQTTTopic string MQTTStateTopic string
MQTTCommandTopic string
Value bool Value bool
onboot string onboot string
parent *MQTTBoard parent *MQTTBoard
@ -42,7 +42,8 @@ func newMQTTChannel(v *viper.Viper, channelID string) MQTTChannel {
v.SetDefault("name", "unknown") v.SetDefault("name", "unknown")
v.SetDefault("lastValue", false) v.SetDefault("lastValue", false)
v.SetDefault("onboot", "off") v.SetDefault("onboot", "off")
v.SetDefault("mqtttopic", v.GetString("name")) v.SetDefault("mqtt.statetopic", v.GetString("name"))
v.SetDefault("mqtt.commandtopic", v.GetString("name"))
v.SetDefault("mqttremote.statetopic", v.GetString("name")) v.SetDefault("mqttremote.statetopic", v.GetString("name"))
v.SetDefault("mqttremote.commandtopic", v.GetString("name")) v.SetDefault("mqttremote.commandtopic", v.GetString("name"))
v.SetDefault("mqttremote.payloadon", "on") v.SetDefault("mqttremote.payloadon", "on")
@ -61,7 +62,8 @@ func newMQTTChannel(v *viper.Viper, channelID string) 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"), MQTTStateTopic: v.GetString("mqtt.statetopic"),
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"),
@ -155,7 +157,7 @@ func (c *MQTTChannel) ToString() string {
} }
func (c *MQTTChannel) UpdateMQTT() { func (c *MQTTChannel) UpdateMQTT() {
MQTTpublish(c.MQTTTopic, c.ToString()) MQTTpublish(c.MQTTStateTopic, c.ToString())
} }
func (c *MQTTChannel) SaveLastState() { func (c *MQTTChannel) SaveLastState() {
@ -244,7 +246,7 @@ 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 c.MQTTRemotePayloadOn: case c.MQTTRemotePayloadOn:
if !c.Value { if !c.Value {
@ -279,8 +281,14 @@ func (c *MQTTChannel) SetOnBoot(str string) {
viper.Set(s, str) viper.Set(s, str)
} }
func (c *MQTTChannel) SetMQTTTopic(str string) { func (c *MQTTChannel) SetMQTTStateTopic(str string) {
c.MQTTTopic = str c.MQTTStateTopic = str
s := fmt.Sprintf("boards.%s.channels.%s.mqtttopic", c.parent.ID, c.ID) s := fmt.Sprintf("boards.%s.channels.%s.mqtt.statetopic", c.parent.ID, c.ID)
viper.Set(s, str)
}
func (c *MQTTChannel) SetMQTTCommandTopic(str string) {
c.MQTTCommandTopic = str
s := fmt.Sprintf("boards.%s.channels.%s.mqtt.commandtopic", c.parent.ID, c.ID)
viper.Set(s, str) viper.Set(s, str)
} }

View File

@ -19,7 +19,8 @@ func outletsPage(ctx *macaron.Context) {
type OutletPostForm struct { type OutletPostForm struct {
Description string `form:"description" binding:"Required"` Description string `form:"description" binding:"Required"`
MQTTTopic string `form:"mqtttopic"` MQTTStateTopic string `form:"mqttstatetopic"`
MQTTCommandTopic string `form:"mqttcommandtopic"`
OnBoot string `form:"onboot" binding:"Required"` OnBoot string `form:"onboot" binding:"Required"`
} }
@ -74,12 +75,17 @@ func outletEditPost(ctx *macaron.Context, f OutletPostForm) {
s2 := fmt.Sprintf("outlets.%s.description", outlets[num].ID) s2 := fmt.Sprintf("outlets.%s.description", outlets[num].ID)
viper.Set(s2, outlets[num].Description) viper.Set(s2, outlets[num].Description)
mqtt := strings.TrimSpace(f.MQTTTopic) mqttstate := strings.TrimSpace(f.MQTTStateTopic)
if mqtt == "" { if mqttstate == "" {
mqtt = outlets[num].Channel.Name() mqttstate = outlets[num].Channel.Name()
} }
outlets[num].Channel.SetMQTTStateTopic(mqttstate)
outlets[num].Channel.SetMQTTTopic(mqtt) mqttcommand := strings.TrimSpace(f.MQTTCommandTopic)
if mqttcommand == "" {
mqttcommand = outlets[num].Channel.Name()
}
outlets[num].Channel.SetMQTTCommandTopic(mqttcommand)
viper.WriteConfig() viper.WriteConfig()

View File

@ -62,10 +62,18 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="mqtttopic" class="col-sm-2 control-label">MQTT Topic</label> <label for="mqttstatetopic" class="col-sm-2 control-label">MQTT State Topic</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" id="mqtttopic" name="mqtttopic" placeholder="MQTT Topic without prefix (defaults to board port name)" <input type="text" class="form-control" id="mqttstatetopic" name="mqttstatetopic" placeholder="MQTT State Topic without prefix (defaults to board port name)"
value="{{ outlet.Channel.MQTTTopic }}"> value="{{ outlet.Channel.MQTTStateTopic }}">
</div>
</div>
<div class="form-group">
<label for="mqttcommandtopic" class="col-sm-2 control-label">MQTT Command Topic</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="mqttcommandtopic" name="mqttcommandtopic" placeholder="MQTT Command Topic without prefix (defaults to board port name)"
value="{{ outlet.Channel.MQTTCommandTopic }}">
</div> </div>
</div> </div>