From 30c66efffd0a7f56832f4120b661901575594550 Mon Sep 17 00:00:00 2001 From: Siegfried Siegert Date: Sun, 22 Nov 2020 14:41:04 +0100 Subject: [PATCH] Handle new default topic scheme 'ATB/#' --- mqtt/mqtt.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mqtt/mqtt.go b/mqtt/mqtt.go index d215bb4..8fb232e 100644 --- a/mqtt/mqtt.go +++ b/mqtt/mqtt.go @@ -126,6 +126,11 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) { regexpDefault := "ATB\\/[0-9]+\\/[\\-]*[0-9]+\\/[\\-]*[0-9]+\\/[0-9]+\\/mo" topicSlice := strings.Split(msg.Topic(), "/") + + // remove a possible empty element (-> handle '/ATB/#' and 'ATB/#' + if (len(topicSlice[0]) == 0) { + topicSlice = topicSlice[1:] + } matchedWithType, err := regexp.MatchString(regexpWithType, msg.Topic()) if err != nil { @@ -140,12 +145,12 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) { var deviceID string if matchedWithType { fmt.Printf("Topic matched regexpWithType\n") - customerID = topicSlice[3] - deviceID = topicSlice[4] + customerID = topicSlice[2] + deviceID = topicSlice[3] } else if matchedDefault { fmt.Printf("Topic matched regexpDefault\n") - customerID = topicSlice[2] - deviceID = topicSlice[5] + customerID = topicSlice[1] + deviceID = topicSlice[4] } else { log.Printf("ERROR: no matching topic: %s", msg.Topic()) return