Prepare for storing device data in database
- create device id from mqtt topic - implement method for storing device in database Still todo: - extract other data (e.g. MAC, SN) from monitoring object - find a way in mqtt to use global env
This commit is contained in:
20
mqtt/mqtt.go
20
mqtt/mqtt.go
@@ -7,10 +7,12 @@ import (
|
||||
"log"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||
//"os"
|
||||
"mqttListener/Config"
|
||||
"mqttListener/models"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
@@ -124,9 +126,11 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
||||
fmt.Printf("MSG: \n%s\n", msg.Payload())
|
||||
//fmt.Printf("* [%s] %s\n", msg.Topic(), string(msg.Payload()))
|
||||
|
||||
regexpWithType := "\\/ATB\\/[A-Z]+\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/mo"
|
||||
regexpWithType := "\\/ATB\\/[A-Z]+\\/[0-9]+\\/[0-9]+\\/mo"
|
||||
regexpDefault := "\\/ATB\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/mo"
|
||||
|
||||
topicSlice := strings.Split(msg.Topic(), "/")
|
||||
|
||||
matchedWithType, err := regexp.MatchString(regexpWithType, msg.Topic())
|
||||
if err != nil {
|
||||
log.Printf("ERROR: matchedWithType: %s", err.Error())
|
||||
@@ -136,11 +140,25 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
||||
log.Printf("ERROR: matchedDefault: %s", err.Error())
|
||||
}
|
||||
|
||||
var deviceID string
|
||||
if matchedWithType {
|
||||
fmt.Printf("Topic matched regexpWithType\n")
|
||||
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4]
|
||||
} else if matchedDefault {
|
||||
fmt.Printf("Topic matched regexpDefault\n")
|
||||
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4] + "_" + topicSlice[5]
|
||||
} else {
|
||||
log.Printf("ERROR: no matching topic: %s", msg.Topic())
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Generated deviceID = %s\n", deviceID)
|
||||
|
||||
var device models.Device
|
||||
device.ID = deviceID
|
||||
device.MAC = "t.b.d."
|
||||
device.SN = "t.b.d."
|
||||
device.LastMsg = "t.b.d"
|
||||
|
||||
// TODO: store this device in database
|
||||
}
|
||||
|
Reference in New Issue
Block a user