Expand database and device
This commit is contained in:
parent
3f0fa81f18
commit
f21e4ff1d6
10
env/env.go
vendored
10
env/env.go
vendored
@ -26,6 +26,14 @@ func (env *Env) DevicesIndex(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, device := range devices {
|
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,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,13 @@ func NewDB(dataSourceName string) (*DB, error) {
|
|||||||
return nil, err
|
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)
|
_, err = db.Exec(sqlStmt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -5,7 +5,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
ID string
|
TOPIC string
|
||||||
|
CustomerID string
|
||||||
|
DeviceID string
|
||||||
|
ProjectName string
|
||||||
MAC string
|
MAC string
|
||||||
SN string
|
SN string
|
||||||
LastMsg string
|
LastMsg string
|
||||||
@ -21,7 +24,7 @@ func (db *DB) AllDevices() ([]*Device, error) {
|
|||||||
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.TOPIC, device.CustomerID, device.DeviceID, device.ProjectName, &device.MAC, &device.SN, &device.LastMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -34,15 +37,15 @@ func (db *DB) AllDevices() ([]*Device, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) InsertDevice(device *Device) error {
|
func (db *DB) InsertDevice(device *Device) error {
|
||||||
sqlStmt := `INSERT OR REPLACE INTO devices (id, mac, sn, lastMsg)
|
sqlStmt := `INSERT OR REPLACE INTO devices (topic, customerID, deviceID, projectName, mac, sn, lastMsg)
|
||||||
VALUES($1, $2, $3, $4);`
|
VALUES($1, $2, $3, $4, $5, $6, $7);`
|
||||||
_, err := db.Exec(sqlStmt, device.ID, device.MAC, device.SN, device.LastMsg)
|
_, err := db.Exec(sqlStmt, device.TOPIC, device.CustomerID, device.DeviceID, device.ProjectName, device.MAC, device.SN, device.LastMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEBUG
|
// 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
|
return err
|
||||||
}
|
}
|
||||||
|
15
mqtt/mqtt.go
15
mqtt/mqtt.go
@ -138,13 +138,16 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
|||||||
log.Printf("ERROR: matchedDefault: %s", err.Error())
|
log.Printf("ERROR: matchedDefault: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var customerID string
|
||||||
var deviceID string
|
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]
|
customerID = topicSlice[3]
|
||||||
|
deviceID = 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]
|
customerID = topicSlice[2]
|
||||||
|
deviceID = topicSlice[5]
|
||||||
} else {
|
} else {
|
||||||
log.Printf("ERROR: no matching topic: %s", msg.Topic())
|
log.Printf("ERROR: no matching topic: %s", msg.Topic())
|
||||||
return
|
return
|
||||||
@ -153,7 +156,11 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
|||||||
fmt.Printf("Generated deviceID = %s\n", deviceID)
|
fmt.Printf("Generated deviceID = %s\n", deviceID)
|
||||||
|
|
||||||
var device models.Device
|
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.MAC = "t.b.d."
|
||||||
device.SN = "t.b.d."
|
device.SN = "t.b.d."
|
||||||
device.LastMsg = "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 MAC = %s\n", device.MAC)
|
||||||
fmt.Printf("Device SN = %s\n", device.SN)
|
fmt.Printf("Device SN = %s\n", device.SN)
|
||||||
|
|
||||||
// TODO: store this device in database
|
// store this device in database
|
||||||
err = ev.DB.InsertDevice(&device)
|
err = ev.DB.InsertDevice(&device)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
|
Loading…
Reference in New Issue
Block a user