Expand database and device
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user