mirror of
https://github.com/sascha-hemi/spaceDevices.git
synced 2026-03-21 00:04:23 +01:00
adds a watch dog for the mqtt server
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
2
do.sh
@@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
usage() {
|
||||
echo "Usage $0 (build-linux|sync)"
|
||||
echo "Usage $0 (build-linux|test-sync|sync)"
|
||||
}
|
||||
|
||||
if [ "$1" == "" ]; then
|
||||
|
||||
14
mqtt/mqtt.go
14
mqtt/mqtt.go
@@ -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.")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user