Expand database and device

This commit is contained in:
Siegfried Siegert 2020-09-13 18:37:22 +02:00
parent 3f0fa81f18
commit f21e4ff1d6
4 changed files with 39 additions and 15 deletions

10
env/env.go vendored
View File

@ -26,6 +26,14 @@ func (env *Env) DevicesIndex(w http.ResponseWriter, r *http.Request) {
return
}
for _, device := range devices {
fmt.Fprintf(w, "%s, %s, %s, £%.2f\n", device.ID, device.MAC, device.SN, device.LastMsg)
fmt.Fprintf(w, "%s, %s, %s, %s, %s, %s, %s\n",
device.TOPIC,
device.CustomerID,
device.DeviceID,
device.ProjectName,
device.MAC,
device.SN,
device.LastMsg,
)
}
}

View File

@ -24,7 +24,13 @@ func NewDB(dataSourceName string) (*DB, error) {
return nil, err
}
sqlStmt := "CREATE TABLE IF NOT EXISTS devices (id TEXT not null primary key, mac TEXT, sn TEXT, lastMsg TEXT);"
sqlStmt := `CREATE TABLE IF NOT EXISTS devices (topic TEXT not null primary key,
customerID TEXT,
deviceID TEXT,
projectName TEXT,
mac TEXT,
sn TEXT,
lastMsg TEXT);`
_, err = db.Exec(sqlStmt)
if err != nil {
return nil, err

View File

@ -5,10 +5,13 @@ import (
)
type Device struct {
ID string
MAC string
SN string
LastMsg string
TOPIC string
CustomerID string
DeviceID string
ProjectName string
MAC string
SN string
LastMsg string
}
func (db *DB) AllDevices() ([]*Device, error) {
@ -21,7 +24,7 @@ func (db *DB) AllDevices() ([]*Device, error) {
devices := make([]*Device, 0)
for rows.Next() {
device := new(Device)
err := rows.Scan(&device.ID, &device.MAC, &device.SN, &device.LastMsg)
err := rows.Scan(&device.TOPIC, device.CustomerID, device.DeviceID, device.ProjectName, &device.MAC, &device.SN, &device.LastMsg)
if err != nil {
return nil, err
}
@ -34,15 +37,15 @@ func (db *DB) AllDevices() ([]*Device, error) {
}
func (db *DB) InsertDevice(device *Device) error {
sqlStmt := `INSERT OR REPLACE INTO devices (id, mac, sn, lastMsg)
VALUES($1, $2, $3, $4);`
_, err := db.Exec(sqlStmt, device.ID, device.MAC, device.SN, device.LastMsg)
sqlStmt := `INSERT OR REPLACE INTO devices (topic, customerID, deviceID, projectName, mac, sn, lastMsg)
VALUES($1, $2, $3, $4, $5, $6, $7);`
_, err := db.Exec(sqlStmt, device.TOPIC, device.CustomerID, device.DeviceID, device.ProjectName, device.MAC, device.SN, device.LastMsg)
if err != nil {
return err
}
// DEBUG
fmt.Printf("Sucessfully inserted device with ID %s to database\n", device.ID)
fmt.Printf("Sucessfully inserted device with ID %s to database\n", device.TOPIC)
return err
}

View File

@ -138,13 +138,16 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
log.Printf("ERROR: matchedDefault: %s", err.Error())
}
var customerID string
var deviceID string
if matchedWithType {
fmt.Printf("Topic matched regexpWithType\n")
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4]
customerID = topicSlice[3]
deviceID = topicSlice[4]
} else if matchedDefault {
fmt.Printf("Topic matched regexpDefault\n")
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4] + "_" + topicSlice[5]
customerID = topicSlice[2]
deviceID = topicSlice[5]
} else {
log.Printf("ERROR: no matching topic: %s", msg.Topic())
return
@ -153,7 +156,11 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
fmt.Printf("Generated deviceID = %s\n", deviceID)
var device models.Device
device.ID = deviceID
device.TOPIC = msg.Topic()
device.CustomerID = customerID
device.DeviceID = deviceID
device.ProjectName = "projectName t.b.d."
device.MAC = "t.b.d."
device.SN = "t.b.d."
device.LastMsg = "t.b.d"
@ -174,7 +181,7 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
fmt.Printf("Device MAC = %s\n", device.MAC)
fmt.Printf("Device SN = %s\n", device.SN)
// TODO: store this device in database
// store this device in database
err = ev.DB.InsertDevice(&device)
if err != nil {
fmt.Println(err.Error())