From 199fceb41eff5f971230cc7bec0e988ff3a570fb Mon Sep 17 00:00:00 2001 From: a-bali Date: Mon, 4 Sep 2023 15:29:09 +0200 Subject: [PATCH] Added option to specify custom names for MQTT topics (closes: #13). --- .github/workflows/release-binaries.yaml | 1 + config.yml | 2 ++ main.go | 17 +++++++++++++++-- templates/index.html | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release-binaries.yaml b/.github/workflows/release-binaries.yaml index fc5edad..6c7b1a0 100644 --- a/.github/workflows/release-binaries.yaml +++ b/.github/workflows/release-binaries.yaml @@ -32,5 +32,6 @@ jobs: github_token: ${{ secrets.GITHUB_TOKEN }} goos: ${{ matrix.goos }} goarch: ${{ matrix.goarch }} + goarm: ${{ matrix.goarm }} build_command: make extra_files: LICENSE README.md config.yml diff --git a/config.yml b/config.yml index 522ad1a..6b455da 100644 --- a/config.yml +++ b/config.yml @@ -42,6 +42,8 @@ monitor: - topic: "/sensors/aqara_inside/#" # Custom timeout threshold (fixed value in seconds) to be used for the topic (default: standardtimeout as defined above) timeout: 3600 + # Custom name to show instead of MQTT topic (note: does not work with wildcard topic names, only exact matches) + name: "Inside temperature" - topic: "/sensors/aqara_outside/#" timeout: 3600 - topic: "/sensors/#" diff --git a/main.go b/main.go index b2c13c9..3427bbe 100644 --- a/main.go +++ b/main.go @@ -60,6 +60,7 @@ type Config struct { StandardTimeout float64 Targets []struct { Topic string + Name string Timeout int } } @@ -110,6 +111,7 @@ type TimedEntry struct { // MQTTTopic stores status information on a MQTT topic. type MQTTMonitorData struct { + Name string FirstSeen time.Time LastSeen time.Time LastError time.Time @@ -565,6 +567,16 @@ func connectMqttAlert() { } } +// finds a custom name in the configuration for a given topic +func findTopicName(topic string) string { + for _, t := range getConfig().Monitor.MQTT.Targets { + if t.Topic == topic && t.Name != "" { + return t.Name + } + } + return topic +} + // Receives an MQTT message and updates status accordingly. func onMessageReceived(client mqtt.Client, message mqtt.Message) { debug("MQTT: " + message.Topic() + ": " + string(message.Payload())) @@ -576,6 +588,7 @@ func onMessageReceived(client mqtt.Client, message mqtt.Message) { if !ok { monitorData.MQTT[message.Topic()] = &MQTTMonitorData{} e = monitorData.MQTT[message.Topic()] + e.Name = findTopicName(message.Topic()) } if e.Deleted { @@ -682,7 +695,7 @@ func evaluateMQTT() { if elapsed > timeout { if v.Status != STATUS_ERROR { - alert("MQTT", topic, STATUS_ERROR, v.LastSeen, fmt.Sprintf("timeout %.2fs", timeout)) + alert("MQTT", v.Name, STATUS_ERROR, v.LastSeen, fmt.Sprintf("timeout %.2fs", timeout)) v.LastError = time.Now() v.Alerts++ } @@ -691,7 +704,7 @@ func evaluateMQTT() { v.Status = STATUS_WARN } else { if v.Status == STATUS_ERROR { - alert("MQTT", topic, STATUS_OK, v.LastError, "") + alert("MQTT", v.Name, STATUS_OK, v.LastError, "") } v.Status = STATUS_OK } diff --git a/templates/index.html b/templates/index.html index b026f23..505a32f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -171,7 +171,7 @@

MQTT targets

- {{ $topic }} + {{ $value.Name }} (x)