Store timestamp in database

This commit is contained in:
Siegfried Siegert 2020-09-20 17:53:23 +02:00
parent 5045802242
commit e02af3c353
2 changed files with 11 additions and 4 deletions

View File

@ -30,7 +30,7 @@ func NewDB(dataSourceName string) (*DB, error) {
projectName TEXT,
mac TEXT,
sn TEXT,
lastMsg TEXT);`
lastMsg TIMESTAMP DEFAULT CURRENT_TIMESTAMP);`
_, err = db.Exec(sqlStmt)
if err != nil {
return nil, err

View File

@ -2,6 +2,7 @@ package models
import (
"fmt"
"time"
)
type Device struct {
@ -33,13 +34,19 @@ func (db *DB) AllDevices() ([]*Device, error) {
if err = rows.Err(); err != nil {
return nil, err
}
return devices, nil
}
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);`
_, err := db.Exec(sqlStmt, device.TOPIC, device.CustomerID, device.DeviceID, device.ProjectName, device.MAC, device.SN, device.LastMsg)
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)
if err != nil {
return err
}