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
OnBoot() string
SetOnBoot(string)
SetMQTTTopic(string)
SetMQTTStateTopic(string)
SetMQTTCommandTopic(string)
}
type Board interface {

View File

@ -9,13 +9,14 @@ import (
)
type DummyChannel struct {
ID string
Num uint
name string
MQTTTopic string
Value bool
onboot string
parent *DummyBoard
ID string
Num uint
name string
MQTTStateTopic string
MQTTCommandTopic string
Value bool
onboot string
parent *DummyBoard
}
type DummyBoard struct {
@ -29,7 +30,7 @@ func newDummyChannel(v *viper.Viper, channelID string) DummyChannel {
v.SetDefault("name", "unknown")
v.SetDefault("lastValue", false)
v.SetDefault("onboot", "off")
v.SetDefault("mqtttopic", v.GetString("name"))
v.SetDefault("mqtt.statetopic", v.GetString("name"))
value := false
switch v.GetString("onboot") {
@ -41,12 +42,13 @@ func newDummyChannel(v *viper.Viper, channelID string) DummyChannel {
// newUUID := UUID.New().String()
// v.SetDefault("id", newUUID)
return DummyChannel{
ID: channelID,
Num: v.GetUint("num"),
name: v.GetString("name"),
MQTTTopic: v.GetString("mqtttopic"),
Value: value,
onboot: v.GetString("onboot"),
ID: channelID,
Num: v.GetUint("num"),
name: v.GetString("name"),
MQTTStateTopic: v.GetString("mqtt.statetopic"),
MQTTCommandTopic: v.GetString("mqtt.commandtopic"),
Value: value,
onboot: v.GetString("onboot"),
}
}
@ -118,7 +120,7 @@ func (c *DummyChannel) ToString() string {
}
func (c *DummyChannel) UpdateMQTT() {
MQTTpublish(c.MQTTTopic, c.ToString())
MQTTpublish(c.MQTTStateTopic, c.ToString())
}
func (c *DummyChannel) OnChange() {
@ -171,8 +173,14 @@ func (c *DummyChannel) SetOnBoot(str string) {
viper.Set(s, str)
}
func (c *DummyChannel) SetMQTTTopic(str string) {
c.MQTTTopic = str
s := fmt.Sprintf("boards.%s.channels.%s.mqtttopic", c.parent.ID, c.ID)
func (c *DummyChannel) SetMQTTStateTopic(str string) {
c.MQTTStateTopic = str
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)
}

View File

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

View File

@ -18,9 +18,10 @@ func outletsPage(ctx *macaron.Context) {
}
type OutletPostForm struct {
Description string `form:"description" binding:"Required"`
MQTTTopic string `form:"mqtttopic"`
OnBoot string `form:"onboot" binding:"Required"`
Description string `form:"description" binding:"Required"`
MQTTStateTopic string `form:"mqttstatetopic"`
MQTTCommandTopic string `form:"mqttcommandtopic"`
OnBoot string `form:"onboot" binding:"Required"`
}
func outletEditPage(ctx *macaron.Context) {
@ -74,12 +75,17 @@ func outletEditPost(ctx *macaron.Context, f OutletPostForm) {
s2 := fmt.Sprintf("outlets.%s.description", outlets[num].ID)
viper.Set(s2, outlets[num].Description)
mqtt := strings.TrimSpace(f.MQTTTopic)
if mqtt == "" {
mqtt = outlets[num].Channel.Name()
mqttstate := strings.TrimSpace(f.MQTTStateTopic)
if mqttstate == "" {
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()

View File

@ -62,10 +62,18 @@
</div>
<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">
<input type="text" class="form-control" id="mqtttopic" name="mqtttopic" placeholder="MQTT Topic without prefix (defaults to board port name)"
value="{{ outlet.Channel.MQTTTopic }}">
<input type="text" class="form-control" id="mqttstatetopic" name="mqttstatetopic" placeholder="MQTT State Topic without prefix (defaults to board port name)"
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>