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, "user": config.MacDb.UserFile,
}).Info("SpaceDevices starting...") }).Info("SpaceDevices starting...")
//spaceDevices.EnableMqttDebugLogging() //mqtt.EnableMqttDebugLogging()
userDb := db.NewUserDb(config.MacDb) userDb := db.NewUserDb(config.MacDb)
masterDb := db.NewMasterDb(config.MacDb) masterDb := db.NewMasterDb(config.MacDb)

View File

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

View File

@@ -21,6 +21,9 @@ username = "user"
password = "pass" password = "pass"
sessionTopic = "/net/wlan-sessions" sessionTopic = "/net/wlan-sessions"
devicesTopic = "/net/devices" 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]] [[location]]
name = "Bar" name = "Bar"

2
do.sh
View File

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

View File

@@ -21,7 +21,7 @@ type MqttHandler struct {
newDataChan chan []byte newDataChan chan []byte
sessionTopic string sessionTopic string
devicesTopic string devicesTopic string
watchDog *watchDog
} }
//func init() { //func init() {
@@ -70,7 +70,6 @@ func NewMqttHandler(conf conf.MqttConf) *MqttHandler {
opts.SetMaxReconnectInterval(5 * time.Minute) opts.SetMaxReconnectInterval(5 * time.Minute)
opts.SetWill(conf.DevicesTopic, emptyPeopleAndDevices(), 0, true) opts.SetWill(conf.DevicesTopic, emptyPeopleAndDevices(), 0, true)
handler := MqttHandler{newDataChan: make(chan []byte), devicesTopic: conf.DevicesTopic, sessionTopic: conf.SessionTopic} handler := MqttHandler{newDataChan: make(chan []byte), devicesTopic: conf.DevicesTopic, sessionTopic: conf.SessionTopic}
opts.SetOnConnectHandler(handler.onConnect) opts.SetOnConnectHandler(handler.onConnect)
opts.SetConnectionLostHandler(handler.onConnectionLost) 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.") 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 return &handler
} }
@@ -111,6 +115,10 @@ func (h *MqttHandler) onConnect(client mqtt.Client) {
err := subscribe(client, h.sessionTopic, err := subscribe(client, h.sessionTopic,
func(client mqtt.Client, message mqtt.Message) { func(client mqtt.Client, message mqtt.Message) {
mqttLogger.Debug("new wifi sessions") mqttLogger.Debug("new wifi sessions")
if h.watchDog != nil {
h.watchDog.Ping()
}
/* /*
mock := []byte(`{ "38134": { mock := []byte(`{ "38134": {
"last-auth": 1509211121, "last-auth": 1509211121,
@@ -151,7 +159,7 @@ func (h *MqttHandler) onConnect(client mqtt.Client) {
}) })
if err != nil { if err != nil {
mqttLogger.WithError(err).Fatal("Could not subscribe") mqttLogger.WithField("topic", h.sessionTopic).WithError(err).Fatal("Could not subscribe.")
} }
} }