2020-08-30 07:16:50 +02:00
|
|
|
package models
|
|
|
|
|
2020-09-02 09:02:30 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-20 17:53:23 +02:00
|
|
|
"time"
|
2020-09-02 09:02:30 +02:00
|
|
|
)
|
|
|
|
|
2020-08-30 07:16:50 +02:00
|
|
|
type Device struct {
|
2020-09-13 18:37:22 +02:00
|
|
|
TOPIC string
|
|
|
|
CustomerID string
|
|
|
|
DeviceID string
|
|
|
|
ProjectName string
|
|
|
|
MAC string
|
|
|
|
SN string
|
|
|
|
LastMsg string
|
2020-08-30 07:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) AllDevices() ([]*Device, error) {
|
2020-08-30 21:42:01 +02:00
|
|
|
rows, err := db.Query("SELECT * FROM devices")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
devices := make([]*Device, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
device := new(Device)
|
2020-09-20 17:44:47 +02:00
|
|
|
err := rows.Scan(&device.TOPIC, &device.CustomerID, &device.DeviceID, &device.ProjectName, &device.MAC, &device.SN, &device.LastMsg)
|
2020-08-30 21:42:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-20 17:53:48 +02:00
|
|
|
device.ProjectName, err = db.GetProjectName(device.CustomerID)
|
2020-08-30 21:42:01 +02:00
|
|
|
devices = append(devices, device)
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-20 17:53:23 +02:00
|
|
|
|
2020-08-30 21:42:01 +02:00
|
|
|
return devices, nil
|
2020-08-30 07:16:50 +02:00
|
|
|
}
|
|
|
|
|
2020-08-30 21:42:01 +02:00
|
|
|
func (db *DB) InsertDevice(device *Device) error {
|
2020-09-20 17:53:23 +02:00
|
|
|
|
|
|
|
timeStamp := time.Now().Format(time.RFC3339)
|
|
|
|
// DEBUG
|
|
|
|
fmt.Printf("Timestamp: %s\n", timeStamp)
|
|
|
|
|
|
|
|
sqlStmt := `INSERT OR REPLACE INTO devices (topic, customerID, deviceID, projectName, mac, sn)
|
|
|
|
VALUES($1, $2, $3, $4, $5, $6);`
|
|
|
|
_, err := db.Exec(sqlStmt, device.TOPIC, device.CustomerID, device.DeviceID, device.ProjectName, device.MAC, device.SN)
|
2020-08-30 21:42:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-30 07:16:50 +02:00
|
|
|
|
2020-09-02 09:02:30 +02:00
|
|
|
// DEBUG
|
2020-09-13 18:37:22 +02:00
|
|
|
fmt.Printf("Sucessfully inserted device with ID %s to database\n", device.TOPIC)
|
2020-09-02 09:02:30 +02:00
|
|
|
|
2020-08-30 21:42:01 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-09-20 17:53:48 +02:00
|
|
|
|
|
|
|
func (db *DB) GetProjectName(projectNumber string) (string, error) {
|
|
|
|
|
|
|
|
projectNameRows, err := db.Query("SELECT projectName FROM projects WHERE projectNumber = $1", projectNumber)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer projectNameRows.Close()
|
|
|
|
|
|
|
|
projectName := ""
|
|
|
|
for projectNameRows.Next() {
|
|
|
|
err := projectNameRows.Scan(&projectName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = projectNameRows.Err(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return projectName, err
|
|
|
|
|
|
|
|
}
|