optimize regex
This commit is contained in:
parent
ce22efcb96
commit
a53d1eabe5
1 changed files with 3 additions and 4 deletions
|
@ -13,11 +13,9 @@ import (
|
||||||
"github.com/google/uuid"
|
"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 = "(^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 mqttTopicRegexPattern = fmt.Sprintf("^sensor/%s/(temp|pressure|humidity|co2level)$", uuidRegexPattern)
|
|
||||||
|
|
||||||
var regexTopic *regexp.Regexp = regexp.MustCompile(mqttTopicRegexPattern)
|
var regexTopic *regexp.Regexp = regexp.MustCompile(mqttTopicRegexPattern)
|
||||||
var regexUuid *regexp.Regexp = regexp.MustCompile(uuidRegexPattern)
|
|
||||||
|
|
||||||
type mqttWeatherSource struct {
|
type mqttWeatherSource struct {
|
||||||
url string
|
url string
|
||||||
|
@ -67,10 +65,11 @@ func (source *mqttWeatherSource) mqttMessageHandler() mqtt.MessageHandler {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sensorId, err := uuid.Parse(regexUuid.FindAllString(msg.Topic(), 1)[0])
|
sensorId, err := uuid.Parse(regexTopic.FindStringSubmatch(msg.Topic())[2])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Println(sensorId)
|
||||||
|
|
||||||
lastWeatherData, found := source.getUnwrittenDatapoints(sensorId)
|
lastWeatherData, found := source.getUnwrittenDatapoints(sensorId)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue