adds a watch dog for the mqtt server

This commit is contained in:
Holger Cremer
2018-06-03 00:35:28 +02:00
parent ea72bc33da
commit e4aa9eac69
5 changed files with 20 additions and 8 deletions

View File

@@ -26,7 +26,7 @@ func main() {
"user": config.MacDb.UserFile,
}).Info("SpaceDevices starting...")
//spaceDevices.EnableMqttDebugLogging()
//mqtt.EnableMqttDebugLogging()
userDb := db.NewUserDb(config.MacDb)
masterDb := db.NewMasterDb(config.MacDb)

View File

@@ -51,7 +51,8 @@ type MqttConf struct {
Username string
Password string
// if empty, the system certificates are used
CertFile string
SessionTopic string
DevicesTopic string
CertFile string
SessionTopic string
DevicesTopic string
WatchDogTimeoutInMinutes int
}

View File

@@ -21,6 +21,9 @@ username = "user"
password = "pass"
sessionTopic = "/net/wlan-sessions"
devicesTopic = "/net/devices"
# after this amount of minutes without any data from the sessions toptic, the program will be killed
# a value < 1 will disable this check
watchDogTimeoutInMinutes = 5
[[location]]
name = "Bar"

2
do.sh
View File

@@ -1,7 +1,7 @@
#!/bin/bash
usage() {
echo "Usage $0 (build-linux|sync)"
echo "Usage $0 (build-linux|test-sync|sync)"
}
if [ "$1" == "" ]; then

View File

@@ -21,7 +21,7 @@ type MqttHandler struct {
newDataChan chan []byte
sessionTopic string
devicesTopic string
watchDog *watchDog
}
//func init() {
@@ -70,7 +70,6 @@ func NewMqttHandler(conf conf.MqttConf) *MqttHandler {
opts.SetMaxReconnectInterval(5 * time.Minute)
opts.SetWill(conf.DevicesTopic, emptyPeopleAndDevices(), 0, true)
handler := MqttHandler{newDataChan: make(chan []byte), devicesTopic: conf.DevicesTopic, sessionTopic: conf.SessionTopic}
opts.SetOnConnectHandler(handler.onConnect)
opts.SetConnectionLostHandler(handler.onConnectionLost)
@@ -80,6 +79,11 @@ func NewMqttHandler(conf conf.MqttConf) *MqttHandler {
mqttLogger.WithError(tok.Error()).Fatal("Could not connect to mqtt server.")
}
if conf.WatchDogTimeoutInMinutes > 0 {
mqttLogger.Println("Enable mqtt watch dog, timeout in minutes is", conf.WatchDogTimeoutInMinutes)
handler.watchDog = NewWatchDog(time.Duration(conf.WatchDogTimeoutInMinutes) * time.Minute)
}
return &handler
}
@@ -111,6 +115,10 @@ func (h *MqttHandler) onConnect(client mqtt.Client) {
err := subscribe(client, h.sessionTopic,
func(client mqtt.Client, message mqtt.Message) {
mqttLogger.Debug("new wifi sessions")
if h.watchDog != nil {
h.watchDog.Ping()
}
/*
mock := []byte(`{ "38134": {
"last-auth": 1509211121,
@@ -151,7 +159,7 @@ func (h *MqttHandler) onConnect(client mqtt.Client) {
})
if err != nil {
mqttLogger.WithError(err).Fatal("Could not subscribe")
mqttLogger.WithField("topic", h.sessionTopic).WithError(err).Fatal("Could not subscribe.")
}
}