- fix re-connection after connection loss to broker
- add subscribe call after reconnect - additional config settings for timeout values - add more info/warn outputs - change default log level from "warn" to "info"
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package MqttService
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/url"
|
||||
"rpiMqttControl/internal/PinControlService"
|
||||
"time"
|
||||
)
|
||||
@@ -66,10 +68,7 @@ func (m *MqttService) _subscribe() {
|
||||
}
|
||||
|
||||
func (m *MqttService) Start() {
|
||||
token := m.mqttClient.Connect()
|
||||
go m._asyncWait(token, func() {
|
||||
m._subscribe()
|
||||
})
|
||||
m.mqttClient.Connect()
|
||||
}
|
||||
|
||||
func NewMqttService(config MqttServiceConfig, pinService *PinControlService.PinControlService) MqttService {
|
||||
@@ -82,10 +81,23 @@ func NewMqttService(config MqttServiceConfig, pinService *PinControlService.PinC
|
||||
}
|
||||
|
||||
clientOptions.SetConnectRetry(true)
|
||||
clientOptions.SetConnectRetryInterval(10 * time.Second)
|
||||
clientOptions.SetConnectRetryInterval(time.Duration(config.ConnectRetryIntervalMs) * time.Millisecond)
|
||||
clientOptions.SetMaxReconnectInterval(time.Duration(config.MaxReconnectIntervalMs) * time.Millisecond)
|
||||
clientOptions.SetAutoReconnect(config.AutoReconnect)
|
||||
clientOptions.SetConnectTimeout(time.Duration(config.ConnectTimeoutMs) * time.Millisecond)
|
||||
clientOptions.SetConnectionLostHandler(func(client mqtt.Client, err error) {
|
||||
log.Warn("connection lost to broker")
|
||||
})
|
||||
|
||||
clientOptions.SetConnectionAttemptHandler(func(broker *url.URL, tlsCfg *tls.Config) *tls.Config {
|
||||
log.Infof("connection attempt to %s", broker.String())
|
||||
return tlsCfg
|
||||
})
|
||||
var mqttService *MqttService
|
||||
clientOptions.SetOnConnectHandler(func(client mqtt.Client) {
|
||||
log.Info("successfully connected to broker")
|
||||
mqttService._subscribe()
|
||||
})
|
||||
mqttClient := mqtt.NewClient(clientOptions)
|
||||
|
||||
m := MqttService{
|
||||
@@ -94,6 +106,7 @@ func NewMqttService(config MqttServiceConfig, pinService *PinControlService.PinC
|
||||
pinService: pinService,
|
||||
config: config,
|
||||
}
|
||||
mqttService = &m
|
||||
|
||||
pinService.OnCycleCallback = m.pinCyclicCallback
|
||||
pinService.OnChangeCallback = m.pinEventCallback
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
package MqttService
|
||||
|
||||
type MqttServiceConfig struct {
|
||||
BrokerAddress string `yaml:"broker-address"`
|
||||
TopicPrefix string `yaml:"topic-prefix"`
|
||||
AutoReconnect bool `yaml:"auto-reconnect"`
|
||||
ConnectTimeoutMs int `yaml:"connect-timeout-ms"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
|
||||
BrokerAddress string `yaml:"broker-address"`
|
||||
TopicPrefix string `yaml:"topic-prefix"`
|
||||
AutoReconnect bool `yaml:"auto-reconnect"`
|
||||
ConnectTimeoutMs int `yaml:"connect-timeout-ms"`
|
||||
ConnectRetryIntervalMs int `yaml:"connect-retry-interval-ms"`
|
||||
MaxReconnectIntervalMs int `yaml:"max-reconnect-interval-ms"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
}
|
||||
|
||||
func NewMqttServiceConfig() MqttServiceConfig {
|
||||
return MqttServiceConfig{
|
||||
BrokerAddress: "tcp://localhost:1883",
|
||||
TopicPrefix: "/rpicontrol",
|
||||
AutoReconnect: true,
|
||||
ConnectTimeoutMs: 10000,
|
||||
|
||||
BrokerAddress: "tcp://localhost:1883",
|
||||
TopicPrefix: "/rpicontrol",
|
||||
AutoReconnect: true,
|
||||
ConnectTimeoutMs: 10000,
|
||||
ConnectRetryIntervalMs: 10000,
|
||||
MaxReconnectIntervalMs: 10000,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user