From 8270fdff90ff20cfd2034cd2f5f20c214a50f659 Mon Sep 17 00:00:00 2001 From: Holger Cremer Date: Fri, 29 Nov 2019 23:29:25 +0100 Subject: [PATCH] don't send a zero update when using listUnknown --- cmd/spaceDevices/spaceDevices.go | 2 +- cmd/unkownDevices/listUnkown.go | 2 +- internal/mqtt/mqtt.go | 20 ++++++++++++-------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cmd/spaceDevices/spaceDevices.go b/cmd/spaceDevices/spaceDevices.go index 125d9d9..47c432b 100644 --- a/cmd/spaceDevices/spaceDevices.go +++ b/cmd/spaceDevices/spaceDevices.go @@ -31,7 +31,7 @@ func main() { userDb := db.NewUserDb(config.MacDb) masterDb := db.NewMasterDb(config.MacDb) - mqttHandler := mqtt.NewMqttHandler(config.Mqtt) + mqttHandler := mqtt.NewMqttHandler(config.Mqtt, false) data := mqtt.NewDeviceData(config.Locations, mqttHandler, masterDb, userDb) data.ListenAndUpdatePeopleData() diff --git a/cmd/unkownDevices/listUnkown.go b/cmd/unkownDevices/listUnkown.go index c690de3..4d58b6b 100644 --- a/cmd/unkownDevices/listUnkown.go +++ b/cmd/unkownDevices/listUnkown.go @@ -27,7 +27,7 @@ func main() { userDb := db.NewUserDb(config.MacDb) masterDb := db.NewMasterDb(config.MacDb) - mqttHandler := mqtt.NewMqttHandler(config.Mqtt) + mqttHandler := mqtt.NewMqttHandler(config.Mqtt, true) data := mqtt.NewDeviceData(config.Locations, mqttHandler, masterDb, userDb) unknownSession := data.GetOneEntry() diff --git a/internal/mqtt/mqtt.go b/internal/mqtt/mqtt.go index a267517..e359842 100644 --- a/internal/mqtt/mqtt.go +++ b/internal/mqtt/mqtt.go @@ -9,8 +9,8 @@ import ( "github.com/eclipse/paho.mqtt.golang" "github.com/ktt-ol/spaceDevices/internal/conf" - log "github.com/sirupsen/logrus" "github.com/ktt-ol/spaceDevices/pkg/structs" + log "github.com/sirupsen/logrus" ) const CLIENT_ID = "spaceDevicesGo" @@ -47,7 +47,7 @@ func EnableMqttDebugLogging() { mqtt.DEBUG.SetOutput(stdLogWriter) } -func NewMqttHandler(conf conf.MqttConf) *MqttHandler { +func NewMqttHandler(conf conf.MqttConf, clientOnly bool) *MqttHandler { opts := mqtt.NewClientOptions() opts.AddBroker(conf.Url) @@ -66,21 +66,25 @@ func NewMqttHandler(conf conf.MqttConf) *MqttHandler { opts.SetTLSConfig(tlsConf) opts.SetClientID(CLIENT_ID + GenerateRandomString(4)) - opts.SetAutoReconnect(true) - opts.SetKeepAlive(10 * time.Second) - opts.SetMaxReconnectInterval(5 * time.Minute) - opts.SetWill(conf.DevicesTopic, emptyPeopleAndDevices(), 0, true) + if !clientOnly { + opts.SetAutoReconnect(true) + opts.SetKeepAlive(10 * time.Second) + 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) + if !clientOnly { + opts.SetConnectionLostHandler(handler.onConnectionLost) + } handler.client = mqtt.NewClient(opts) if tok := handler.client.Connect(); tok.WaitTimeout(5*time.Second) && tok.Error() != nil { mqttLogger.WithError(tok.Error()).Fatal("Could not connect to mqtt server.") } - if conf.WatchDogTimeoutInMinutes > 0 { + if !clientOnly && conf.WatchDogTimeoutInMinutes > 0 { mqttLogger.Println("Enable mqtt watch dog, timeout in minutes is", conf.WatchDogTimeoutInMinutes) handler.watchDog = NewWatchDog(time.Duration(conf.WatchDogTimeoutInMinutes) * time.Minute) }