From d072fb317b8f58ac2ca923f9b65e539abf12ba4c Mon Sep 17 00:00:00 2001 From: Holger Cremer Date: Thu, 4 Jan 2018 23:10:19 +0100 Subject: [PATCH] moves the topics into config --- conf/config.go | 4 +++- config.example.toml | 2 ++ mqtt/mqtt.go | 25 +++++++++++-------------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/conf/config.go b/conf/config.go index 31825c2..39398f2 100644 --- a/conf/config.go +++ b/conf/config.go @@ -51,5 +51,7 @@ type MqttConf struct { Username string Password string // if empty, the system certificates are used - CertFile string + CertFile string + SessionTopic string + DevicesTopic string } diff --git a/config.example.toml b/config.example.toml index ab40e9e..bf0f846 100644 --- a/config.example.toml +++ b/config.example.toml @@ -19,6 +19,8 @@ url = "tls://server:8883" certFile = "server.cert.pem" username = "user" password = "pass" +sessionTopic = "/net/wlan-sessions" +devicesTopic = "/net/devices" [[location]] name = "Bar" diff --git a/mqtt/mqtt.go b/mqtt/mqtt.go index 1fafbfd..b8b0f83 100644 --- a/mqtt/mqtt.go +++ b/mqtt/mqtt.go @@ -13,17 +13,14 @@ import ( ) const CLIENT_ID = "spaceDevices2" -const TOPIC_SESSIONS = "/net/wlan-sessions" - -// ATTENTION: test topic! -//const TOPIC_DEVICES = "/test/net_devices" -const TOPIC_DEVICES = "/net/devices" var mqttLogger = log.WithField("where", "mqtt") type MqttHandler struct { - client mqtt.Client - newDataChan chan []byte + client mqtt.Client + newDataChan chan []byte + sessionTopic string + devicesTopic string } //func init() { @@ -70,9 +67,9 @@ func NewMqttHandler(conf conf.MqttConf) *MqttHandler { opts.SetAutoReconnect(true) opts.SetKeepAlive(10 * time.Second) opts.SetMaxReconnectInterval(5 * time.Minute) - opts.SetWill(TOPIC_DEVICES, emptyPeopleAndDevices(), 0, true) + opts.SetWill(conf.DevicesTopic, emptyPeopleAndDevices(), 0, true) - handler := MqttHandler{newDataChan: make(chan []byte)} + handler := MqttHandler{newDataChan: make(chan []byte), devicesTopic: conf.DevicesTopic, sessionTopic: conf.SessionTopic} opts.SetOnConnectHandler(handler.onConnect) opts.SetConnectionLostHandler(handler.onConnectionLost) @@ -98,10 +95,10 @@ func (h *MqttHandler) SendPeopleAndDevices(data PeopleAndDevices) { mqttLogger.Infof("Sending PeopleAndDevices: %d, %d, %d, %d", data.PeopleCount, data.DeviceCount, data.UnknownDevicesCount, len(data.People)) - token := h.client.Publish(TOPIC_DEVICES, 0, true, string(bytes)) + token := h.client.Publish(h.devicesTopic, 0, true, string(bytes)) ok := token.WaitTimeout(time.Duration(time.Second * 10)) if !ok { - mqttLogger.Warn("Error sending devices to:", TOPIC_DEVICES) + mqttLogger.Warn("Error sending devices to:", h.devicesTopic) return } } @@ -109,7 +106,7 @@ func (h *MqttHandler) SendPeopleAndDevices(data PeopleAndDevices) { func (h *MqttHandler) onConnect(client mqtt.Client) { mqttLogger.Info("connected") - err := subscribe(client, TOPIC_SESSIONS, + err := subscribe(client, h.sessionTopic, func(client mqtt.Client, message mqtt.Message) { mqttLogger.Debug("new wifi sessions") /* @@ -143,7 +140,7 @@ func (h *MqttHandler) onConnect(client mqtt.Client) { }}`) */ select { - //case h.newDataChan <- mock: + //case h.newDataChan <- mock: case h.newDataChan <- message.Payload(): break default: @@ -191,7 +188,7 @@ func defaultCertPool(certFile string) *x509.CertPool { } func emptyPeopleAndDevices() string { - pad := PeopleAndDevices{People:[]Person{}} + pad := PeopleAndDevices{People: []Person{}} bytes, err := json.Marshal(pad) if err != nil { mqttLogger.WithError(err).Panic()