Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hierarchical topic names for lines, strings and tariffs #100

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion server/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ package server
import (
"fmt"
"log"
"regexp"
"strings"
"time"

MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/volkszaehler/mbmd/meters"
)

const (
publishTimeout = 2000 * time.Millisecond
)

var (
topicRE = regexp.MustCompile(`(\w+)([LTS]\d)`)
)

// MqttClient is a MQTT publisher
type MqttClient struct {
Client MQTT.Client
Expand Down Expand Up @@ -105,13 +111,27 @@ func NewMqttRunner(options *MQTT.ClientOptions, qos byte, topic string, verbose
}
}

// topicFromMeasurement converts measurements of type MeasureLx/MeasureSx/MeasureTx to hierarchical Measure/Lx topics
func topicFromMeasurement(measurement meters.Measurement) string {
name := measurement.String()
match := topicRE.FindStringSubmatch(name)
if len(match) != 3 {
return name
}

topic := fmt.Sprintf("%s/%s", match[1], match[2])

return topic
}

// Run MqttClient publisher
func (m *MqttRunner) Run(in <-chan QuerySnip) {
// notify connection and override will
m.MqttClient.Publish(fmt.Sprintf("%s/status", m.topic), true, "connected")

for snip := range in {
topic := fmt.Sprintf("%s/%s/%s", m.topic, mqttDeviceTopic(snip.Device), snip.Measurement)
subtopic := topicFromMeasurement(snip.Measurement)
topic := fmt.Sprintf("%s/%s/%s", m.topic, mqttDeviceTopic(snip.Device), subtopic)
message := fmt.Sprintf("%.3f", snip.Value)
go m.Publish(topic, false, message)
}
Expand Down
2 changes: 1 addition & 1 deletion server/queryengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const (
// deviceIDregex is the regex pattern that identifies valid device ids
deviceIDregex = "\\w*(\\d+)\\.(\\d+)"
deviceIDregex = `\w*(\d+)\.(\d+)`
)

// DeviceInfo returns device descriptor by device id
Expand Down