Skip to content

Commit

Permalink
add last will and comment disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetancollaud committed Jan 21, 2025
1 parent bb884da commit 1f35c1a
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/mqtt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ func NewClient(options *ClientOptions) Client {
}
mqttOptions := mqtt.NewClientOptions().
AddBroker(options.MqttUrl).
SetClientID("digitalstrom-mqtt-" + uuid.New().String()).
SetClientID("digitalstrom-mqtt-"+uuid.New().String()).
SetOrderMatters(false).
SetUsername(options.Username).
SetPassword(options.Password).
SetWill(serverStatus, Offline, QOS, true).
SetAutoReconnect(true).
SetReconnectingHandler(func(client mqtt.Client, opts *mqtt.ClientOptions) {
log.Info().Str("url", options.MqttUrl).Msg("Reconnecting to MQTT server.")
Expand Down Expand Up @@ -119,10 +120,10 @@ func (c *client) Connect() error {
}

func (c *client) Disconnect() error {
log.Info().Msg("Publishing Offline status to MQTT server.")
if err := c.publishServerStatus(Offline); err != nil {
return err
}
//log.Info().Msg("Publishing Offline status to MQTT server.")
//if err := c.publishServerStatus(Offline); err != nil {
// return err
//}
c.mqttClient.Disconnect(uint(c.options.DisconnectTimeout.Milliseconds()))
log.Info().Msg("Disconnected from MQTT server.")
return nil
Expand Down

0 comments on commit 1f35c1a

Please sign in to comment.