Store timestamp in database
This commit is contained in:
parent
5045802242
commit
e02af3c353
@ -30,7 +30,7 @@ func NewDB(dataSourceName string) (*DB, error) {
|
|||||||
projectName TEXT,
|
projectName TEXT,
|
||||||
mac TEXT,
|
mac TEXT,
|
||||||
sn TEXT,
|
sn TEXT,
|
||||||
lastMsg TEXT);`
|
lastMsg TIMESTAMP DEFAULT CURRENT_TIMESTAMP);`
|
||||||
_, err = db.Exec(sqlStmt)
|
_, err = db.Exec(sqlStmt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
@ -33,13 +34,19 @@ func (db *DB) AllDevices() ([]*Device, error) {
|
|||||||
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 {
|
func (db *DB) InsertDevice(device *Device) error {
|
||||||
sqlStmt := `INSERT OR REPLACE INTO devices (topic, customerID, deviceID, projectName, mac, sn, lastMsg)
|
|
||||||
VALUES($1, $2, $3, $4, $5, $6, $7);`
|
timeStamp := time.Now().Format(time.RFC3339)
|
||||||
_, err := db.Exec(sqlStmt, device.TOPIC, device.CustomerID, device.DeviceID, device.ProjectName, device.MAC, device.SN, device.LastMsg)
|
// 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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user