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:
parent
231c17d7c3
commit
985b4e8fde
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
type Datastore interface {
|
type Datastore interface {
|
||||||
AllDevices() ([]*Device, error)
|
AllDevices() ([]*Device, error)
|
||||||
|
InsertDevice(*Device) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
|
|
|
@ -1,36 +1,41 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
ID string
|
ID string
|
||||||
MAC string
|
MAC string
|
||||||
SN string
|
SN string
|
||||||
LastMsg string
|
LastMsg string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func (db *DB) AllDevices() ([]*Device, error) {
|
func (db *DB) AllDevices() ([]*Device, error) {
|
||||||
rows, err := db.Query("SELECT * FROM devices")
|
rows, err := db.Query("SELECT * FROM devices")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
devices := make([]*Device, 0)
|
devices := make([]*Device, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
device := new(Device)
|
device := new(Device)
|
||||||
err := rows.Scan(&device.ID, &device.MAC, &device.SN, &device.LastMsg)
|
err := rows.Scan(&device.ID, &device.MAC, &device.SN, &device.LastMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
devices = append(devices, device)
|
devices = append(devices, device)
|
||||||
}
|
}
|
||||||
if err = rows.Err(); err != nil {
|
if err = rows.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return devices, nil
|
return devices, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) InsertDevice(device *Device) error {
|
||||||
|
sqlStmt := `INSERT OR REPLACE INTO table (id, mac, sn, lastMsg)
|
||||||
|
VALUES($1, $2, $3, $4);`
|
||||||
|
_, err := db.Exec(sqlStmt, device.ID, device.MAC, device.SN, device.LastMsg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
20
mqtt/mqtt.go
20
mqtt/mqtt.go
|
@ -7,10 +7,12 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
//"os"
|
//"os"
|
||||||
"mqttListener/Config"
|
"mqttListener/Config"
|
||||||
|
"mqttListener/models"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -124,9 +126,11 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
||||||
fmt.Printf("MSG: \n%s\n", msg.Payload())
|
fmt.Printf("MSG: \n%s\n", msg.Payload())
|
||||||
//fmt.Printf("* [%s] %s\n", msg.Topic(), string(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"
|
regexpDefault := "\\/ATB\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/mo"
|
||||||
|
|
||||||
|
topicSlice := strings.Split(msg.Topic(), "/")
|
||||||
|
|
||||||
matchedWithType, err := regexp.MatchString(regexpWithType, msg.Topic())
|
matchedWithType, err := regexp.MatchString(regexpWithType, msg.Topic())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERROR: matchedWithType: %s", err.Error())
|
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())
|
log.Printf("ERROR: matchedDefault: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var deviceID string
|
||||||
if matchedWithType {
|
if matchedWithType {
|
||||||
fmt.Printf("Topic matched regexpWithType\n")
|
fmt.Printf("Topic matched regexpWithType\n")
|
||||||
|
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4]
|
||||||
} else if matchedDefault {
|
} else if matchedDefault {
|
||||||
fmt.Printf("Topic matched regexpDefault\n")
|
fmt.Printf("Topic matched regexpDefault\n")
|
||||||
|
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4] + "_" + topicSlice[5]
|
||||||
} else {
|
} else {
|
||||||
log.Printf("ERROR: no matching topic: %s", msg.Topic())
|
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user