From a53d1eabe588445137c6868c07c5fd56d86cbc4d Mon Sep 17 00:00:00 2001 From: Joel Schmid Date: Sat, 20 Mar 2021 22:32:17 +0100 Subject: [PATCH] optimize regex --- weathersource/mqtt-source.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/weathersource/mqtt-source.go b/weathersource/mqtt-source.go index 0c1f763..23fa130 100644 --- a/weathersource/mqtt-source.go +++ b/weathersource/mqtt-source.go @@ -13,11 +13,9 @@ import ( "github.com/google/uuid" ) -var uuidRegexPattern = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" -var mqttTopicRegexPattern = fmt.Sprintf("^sensor/%s/(temp|pressure|humidity|co2level)$", uuidRegexPattern) +var mqttTopicRegexPattern = "(^sensor/)([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})(/(temp|pressure|humidity|co2level)$)" var regexTopic *regexp.Regexp = regexp.MustCompile(mqttTopicRegexPattern) -var regexUuid *regexp.Regexp = regexp.MustCompile(uuidRegexPattern) type mqttWeatherSource struct { url string @@ -67,10 +65,11 @@ func (source *mqttWeatherSource) mqttMessageHandler() mqtt.MessageHandler { return } - sensorId, err := uuid.Parse(regexUuid.FindAllString(msg.Topic(), 1)[0]) + sensorId, err := uuid.Parse(regexTopic.FindStringSubmatch(msg.Topic())[2]) if err != nil { return } + fmt.Println(sensorId) lastWeatherData, found := source.getUnwrittenDatapoints(sensorId)