This commit is contained in:
Paolo Asperti 2021-12-09 11:17:14 +01:00
parent e20dab5b6a
commit fd75fefc94
Signed by: paspo
GPG Key ID: 06D46905D19D5182
5 changed files with 55 additions and 16 deletions

View File

@ -235,7 +235,6 @@ func (b *MQTTBoard) Initialize() {
})
opts.SetOnConnectHandler(func(c PahoMQTT.Client) {
syslog.Notice("mqtt connected")
// MQTTclient.Publish("openpdu/status", 0, false, "connected")
})
b.MQTTClient = PahoMQTT.NewClient(opts)

View File

@ -19,6 +19,9 @@ func init() {
viper.SetDefault("Mqtt.Port", "1883")
viper.SetDefault("Mqtt.Username", "")
viper.SetDefault("Mqtt.Password", "")
viper.SetDefault("Mqtt.LWTTopic", "LWT")
viper.SetDefault("Mqtt.LWTMessageOnline", "Online")
viper.SetDefault("Mqtt.LWTMessageOffline", "Offline")
viper.SetDefault("Mqtt.HomeAssistant", false)
// MQTT.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
@ -35,6 +38,7 @@ func Setup() {
opts := MQTT.NewClientOptions().AddBroker(uri)
opts.SetClientID(viper.GetString("Mqtt.CliendID"))
opts.SetWill(viper.GetString("Mqtt.Prefix")+"/"+viper.GetString("Mqtt.LWTTopic"), viper.GetString("Mqtt.LWTMessageOffline"), 0, false)
if username := viper.GetString("Mqtt.Username"); username != "" {
opts.SetUsername(username)
@ -47,6 +51,7 @@ func Setup() {
opts.SetAutoReconnect(true)
opts.SetConnectRetryInterval(5 * time.Second)
opts.SetConnectTimeout(5 * time.Second)
opts.SetCleanSession(true)
opts.SetConnectionLostHandler(func(c MQTT.Client, err error) {
syslog.Err("mqtt connection lost error: " + err.Error())
@ -56,7 +61,7 @@ func Setup() {
})
opts.SetOnConnectHandler(func(c MQTT.Client) {
syslog.Notice("mqtt connected")
// MQTTclient.Publish("openpdu/status", 0, false, "connected")
PublishRoot(viper.GetString("Mqtt.Prefix")+"/"+viper.GetString("Mqtt.LWTTopic"), viper.GetString("Mqtt.LWTMessageOnline"))
})
MQTTclient = MQTT.NewClient(opts)

View File

@ -66,6 +66,9 @@ func MQTTSetup() {
"command_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTCommandTopic(),
"payload_off": "off",
"payload_on": "on",
"avty_t": viper.GetString("Mqtt.Prefix") + "/" + viper.GetString("Mqtt.LWTTopic"),
"pl_avail": viper.GetString("Mqtt.LWTMessageOnline"),
"pl_not_avail": viper.GetString("Mqtt.LWTMessageOffline"),
"unique_id": viper.GetString("Mqtt.Prefix") + "_" + fmt.Sprint(o),
"device": map[string]interface{}{
"identifiers": []string{viper.GetString("Mqtt.Prefix")},
@ -90,12 +93,11 @@ func MQTTSetup() {
v := ""
if viper.GetBool("Mqtt.HomeAssistant") {
cfg := map[string]interface{}{
"name": viper.GetString("system.hostname"),
// "state_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTStateTopic(),
// "command_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTCommandTopic(),
"payload_off": "off",
"payload_on": "on",
"unique_id": viper.GetString("Mqtt.Prefix"),
"name": viper.GetString("system.hostname"),
"avty_t": viper.GetString("Mqtt.Prefix") + "/" + viper.GetString("Mqtt.LWTTopic"),
"pl_avail": viper.GetString("Mqtt.LWTMessageOnline"),
"pl_not_avail": viper.GetString("Mqtt.LWTMessageOffline"),
"unique_id": viper.GetString("Mqtt.Prefix"),
"device": map[string]interface{}{
"identifiers": []string{viper.GetString("Mqtt.Prefix")},
"name": viper.GetString("system.hostname"),

View File

@ -24,6 +24,9 @@ func mqttPage(ctx *macaron.Context) {
ctx.Data["prefix"] = viper.GetString("Mqtt.Prefix")
ctx.Data["username"] = viper.GetString("Mqtt.Username")
ctx.Data["password"] = viper.GetString("Mqtt.Password")
ctx.Data["lwttopic"] = viper.GetString("Mqtt.LWTTopic")
ctx.Data["lwtonline"] = viper.GetString("Mqtt.LWTMessageOnline")
ctx.Data["lwtoffline"] = viper.GetString("Mqtt.LWTMessageOffline")
ctx.Data["homeassistant"] = viper.GetString("Mqtt.homeassistant")
if mqtt.Connected() {
@ -37,14 +40,17 @@ func mqttPage(ctx *macaron.Context) {
}
type MQTTPostForm struct {
Schema string `form:"schema" binding:"Required"`
Host string `form:"host" binding:"Required"`
Port string `form:"port" binding:"Required"`
Prefix string `form:"prefix" binding:"Required"`
ClientID string `form:"clientid" binding:"Required"`
Username string `form:"username"`
Password string `form:"password"`
HomeAssistant bool `form:"homeassistant"`
Schema string `form:"schema" binding:"Required"`
Host string `form:"host" binding:"Required"`
Port string `form:"port" binding:"Required"`
Prefix string `form:"prefix" binding:"Required"`
ClientID string `form:"clientid" binding:"Required"`
Username string `form:"username"`
Password string `form:"password"`
LWTTopic string `form:"lwttopic"`
LWTMessageOnline string `form:"lwtonline"`
LWTMessageOffline string `form:"lwtoffline"`
HomeAssistant bool `form:"homeassistant"`
}
func mqttPost(ctx *macaron.Context, f MQTTPostForm) {
@ -66,6 +72,9 @@ func mqttPost(ctx *macaron.Context, f MQTTPostForm) {
viper.Set("Mqtt.Prefix", strings.TrimSpace(f.Prefix))
viper.Set("Mqtt.Username", strings.TrimSpace(f.Username))
viper.Set("Mqtt.Password", f.Password)
viper.Set("Mqtt.LWTTopic", strings.TrimSpace(f.LWTTopic))
viper.Set("Mqtt.LWTMessageOnline", strings.TrimSpace(f.LWTMessageOnline))
viper.Set("Mqtt.LWTMessageOffline", strings.TrimSpace(f.LWTMessageOffline))
viper.Set("Mqtt.HomeAssistant", f.HomeAssistant)
viper.WriteConfig()

View File

@ -99,6 +99,30 @@
</div>
</div>
<div class="form-group">
<label for="lwttopic" class="col-sm-2 control-label">LWT topic</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lwttopic" name="lwttopic"
placeholder="LWT" value="{{ lwttopic }}">
</div>
</div>
<div class="form-group">
<label for="lwtonline" class="col-sm-2 control-label">LWT Online Message</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lwtonline" name="lwtonline"
placeholder="Online" value="{{ lwtonline }}">
</div>
</div>
<div class="form-group">
<label for="lwtoffline" class="col-sm-2 control-label">LWT Offline Message</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lwtoffline" name="lwtoffline"
placeholder="Offline" value="{{ lwtoffline }}">
</div>
</div>
<div class="form-group">
<label for="homeassistant" class="col-sm-2 control-label">Home Assistant Integration</label>
<div class="col-sm-10">