diff --git a/cmd/spaceDevices.go b/cmd/spaceDevices.go index 48a71ee..2b48d2c 100644 --- a/cmd/spaceDevices.go +++ b/cmd/spaceDevices.go @@ -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) diff --git a/conf/config.go b/conf/config.go index 39398f2..72b2ef7 100644 --- a/conf/config.go +++ b/conf/config.go @@ -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 } diff --git a/config.example.toml b/config.example.toml index bf0f846..05d1d80 100644 --- a/config.example.toml +++ b/config.example.toml @@ -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" diff --git a/do.sh b/do.sh index d7949d0..46cc4bb 100755 --- a/do.sh +++ b/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 diff --git a/mqtt/mqtt.go b/mqtt/mqtt.go index 327db1f..171332e 100644 --- a/mqtt/mqtt.go +++ b/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.") } }