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) { opts.SetOnConnectHandler(func(c PahoMQTT.Client) {
syslog.Notice("mqtt connected") syslog.Notice("mqtt connected")
// MQTTclient.Publish("openpdu/status", 0, false, "connected")
}) })
b.MQTTClient = PahoMQTT.NewClient(opts) b.MQTTClient = PahoMQTT.NewClient(opts)

View File

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

View File

@ -66,6 +66,9 @@ func MQTTSetup() {
"command_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTCommandTopic(), "command_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTCommandTopic(),
"payload_off": "off", "payload_off": "off",
"payload_on": "on", "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), "unique_id": viper.GetString("Mqtt.Prefix") + "_" + fmt.Sprint(o),
"device": map[string]interface{}{ "device": map[string]interface{}{
"identifiers": []string{viper.GetString("Mqtt.Prefix")}, "identifiers": []string{viper.GetString("Mqtt.Prefix")},
@ -91,10 +94,9 @@ func MQTTSetup() {
if viper.GetBool("Mqtt.HomeAssistant") { if viper.GetBool("Mqtt.HomeAssistant") {
cfg := map[string]interface{}{ cfg := map[string]interface{}{
"name": viper.GetString("system.hostname"), "name": viper.GetString("system.hostname"),
// "state_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTStateTopic(), "avty_t": viper.GetString("Mqtt.Prefix") + "/" + viper.GetString("Mqtt.LWTTopic"),
// "command_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTCommandTopic(), "pl_avail": viper.GetString("Mqtt.LWTMessageOnline"),
"payload_off": "off", "pl_not_avail": viper.GetString("Mqtt.LWTMessageOffline"),
"payload_on": "on",
"unique_id": viper.GetString("Mqtt.Prefix"), "unique_id": viper.GetString("Mqtt.Prefix"),
"device": map[string]interface{}{ "device": map[string]interface{}{
"identifiers": []string{viper.GetString("Mqtt.Prefix")}, "identifiers": []string{viper.GetString("Mqtt.Prefix")},

View File

@ -24,6 +24,9 @@ func mqttPage(ctx *macaron.Context) {
ctx.Data["prefix"] = viper.GetString("Mqtt.Prefix") ctx.Data["prefix"] = viper.GetString("Mqtt.Prefix")
ctx.Data["username"] = viper.GetString("Mqtt.Username") ctx.Data["username"] = viper.GetString("Mqtt.Username")
ctx.Data["password"] = viper.GetString("Mqtt.Password") 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") ctx.Data["homeassistant"] = viper.GetString("Mqtt.homeassistant")
if mqtt.Connected() { if mqtt.Connected() {
@ -44,6 +47,9 @@ type MQTTPostForm struct {
ClientID string `form:"clientid" binding:"Required"` ClientID string `form:"clientid" binding:"Required"`
Username string `form:"username"` Username string `form:"username"`
Password string `form:"password"` Password string `form:"password"`
LWTTopic string `form:"lwttopic"`
LWTMessageOnline string `form:"lwtonline"`
LWTMessageOffline string `form:"lwtoffline"`
HomeAssistant bool `form:"homeassistant"` HomeAssistant bool `form:"homeassistant"`
} }
@ -66,6 +72,9 @@ func mqttPost(ctx *macaron.Context, f MQTTPostForm) {
viper.Set("Mqtt.Prefix", strings.TrimSpace(f.Prefix)) viper.Set("Mqtt.Prefix", strings.TrimSpace(f.Prefix))
viper.Set("Mqtt.Username", strings.TrimSpace(f.Username)) viper.Set("Mqtt.Username", strings.TrimSpace(f.Username))
viper.Set("Mqtt.Password", f.Password) 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.Set("Mqtt.HomeAssistant", f.HomeAssistant)
viper.WriteConfig() viper.WriteConfig()

View File

@ -99,6 +99,30 @@
</div> </div>
</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"> <div class="form-group">
<label for="homeassistant" class="col-sm-2 control-label">Home Assistant Integration</label> <label for="homeassistant" class="col-sm-2 control-label">Home Assistant Integration</label>
<div class="col-sm-10"> <div class="col-sm-10">