Skip to content

Commit

Permalink
Use hierarchical topic names for lines, strings and tariffs
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Mar 17, 2020
1 parent d2a309b commit dadf94b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

0 comments on commit dadf94b

Please sign in to comment.