diff --git a/config.toml b/config.toml index a4bd0aa..85b4799 100644 --- a/config.toml +++ b/config.toml @@ -2,7 +2,7 @@ [TH7] port = 9090 # web port debug = true # enable to use simulated PCB for development -nolog = true +nolog = false # all DB settings are individual to each DB [DB] diff --git a/db/sqlite3.go b/db/sqlite3.go index 995c086..6a12c6c 100644 --- a/db/sqlite3.go +++ b/db/sqlite3.go @@ -4,8 +4,10 @@ import ( "database/sql" "errors" "fmt" + "log" "sync" "th7/data/core" + "time" _ "github.com/mattn/go-sqlite3" ) @@ -36,8 +38,6 @@ func NewSQLite3Adapter(dbInfo map[string]interface{}) (*SQLite3Adapter, error) { const create string = ` CREATE TABLE IF NOT EXISTS logs ( id INTEGER NOT NULL, - type TEXT NOT NULL, - unit TEXT NOT NULL, value REAL NOT NULL, timestamp TEXT NOT NULL );` @@ -53,10 +53,27 @@ func (ad *SQLite3Adapter) Close() { ad.db.Close() } -func (ad *SQLite3Adapter) Save([]core.Channel) error { +func (ad *SQLite3Adapter) Save(channels []core.Channel) error { ad.mu.Lock() defer ad.mu.Unlock() - fmt.Println("TODO save data...") + insertLogSQL := `INSERT INTO logs (id, value, timestamp) VALUES (?, ?, ?)` + statement, err := ad.db.Prepare(insertLogSQL) + if err != nil { + log.Fatalln(err.Error()) + } + + + timestamp := time.Now().Format(time.RFC1123) + + for c := range channels { + + _, err = statement.Exec(channels[c].Id, channels[c].Value, timestamp) + if err != nil { + log.Fatalln(err.Error()) + } + fmt.Printf("[%s] INSERTED id=%d value=%f\n", timestamp, channels[c].Id, channels[c].Value) + } + return nil }