Skip to content

Commit

Permalink
Mqtt: update online status on reconnect (evcc-io#9394)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Aug 16, 2023
1 parent f41819f commit a8086a9
Showing 2 changed files with 11 additions and 8 deletions.
13 changes: 10 additions & 3 deletions cmd/setup.go
Original file line number Diff line number Diff line change
@@ -161,14 +161,21 @@ func configureInflux(conf server.InfluxConfig, site site.API, in <-chan util.Par
func configureMQTT(conf mqttConfig) error {
log := util.NewLogger("mqtt")

var err error
if mqtt.Instance, err = mqtt.RegisteredClient(log, conf.Broker, conf.User, conf.Password, conf.ClientID, 1, conf.Insecure, func(options *paho.ClientOptions) {
instance, err := mqtt.RegisteredClient(log, conf.Broker, conf.User, conf.Password, conf.ClientID, 1, conf.Insecure, func(options *paho.ClientOptions) {
topic := fmt.Sprintf("%s/status", strings.Trim(conf.Topic, "/"))
options.SetWill(topic, "offline", 1, true)
}); err != nil {

oc := options.OnConnect
options.SetOnConnectHandler(func(client paho.Client) {
oc(client) // original handler
_ = mqtt.Instance.Publish(topic, true, "online") // alive
})
})
if err != nil {
return fmt.Errorf("failed configuring mqtt: %w", err)
}

mqtt.Instance = instance
return nil
}

6 changes: 1 addition & 5 deletions server/mqtt.go
Original file line number Diff line number Diff line change
@@ -209,10 +209,6 @@ func (m *MQTT) listenSetters(topic string, site site.API, lp loadpoint.API) {

// Run starts the MQTT publisher for the MQTT API
func (m *MQTT) Run(site site.API, in <-chan util.Param) {
// alive
topic := fmt.Sprintf("%s/status", m.root)
m.publish(topic, true, "online")

// site setters
m.Handler.ListenSetter(m.root+"/site/prioritySoc", func(payload string) error {
val, err := parseFloat(payload)
@@ -255,7 +251,7 @@ func (m *MQTT) Run(site site.API, in <-chan util.Param) {
})

// number of loadpoints
topic = fmt.Sprintf("%s/loadpoints", m.root)
topic := fmt.Sprintf("%s/loadpoints", m.root)
m.publish(topic, true, len(site.Loadpoints()))

// loadpoint setters

0 comments on commit a8086a9

Please sign in to comment.