From b7da565c82b06f315a20e5d201c950dd8fcbfb8a Mon Sep 17 00:00:00 2001 From: William Clark Date: Thu, 23 Nov 2023 16:55:57 +0000 Subject: [PATCH] sqlite3 now logs cfg info such as offset, gain, type --- config/config.go | 6 +++--- data/thermocouple/data.go | 14 ++++++++++++++ db/adapter.go | 20 ++++++++++++++++++-- db/sqlite3.go | 37 ++++++++++++++++++++++++++----------- 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/config/config.go b/config/config.go index 27a4c92..097e657 100644 --- a/config/config.go +++ b/config/config.go @@ -15,7 +15,7 @@ var ( ) var ( - thermocoupleTypes = map[string]thermocouple.Type{ + ThermocoupleTypes = map[string]thermocouple.Type{ "B": thermocouple.B, "E": thermocouple.E, "J": thermocouple.J, @@ -30,7 +30,7 @@ var ( func getThermocoupleType(t string) (thermocouple.Type, error) { t = strings.ToUpper(t) - if val, ok := thermocoupleTypes[t]; ok { + if val, ok := ThermocoupleTypes[t]; ok { return val, nil } else { return thermocouple.None, ErrConfigBadThermocoupleType @@ -77,7 +77,7 @@ func Load() (config.Config, error) { tc, err := getThermocoupleType(v.GetString(head + ".type")) if err != nil { - fmt.Printf("%s.type=%s\n", head, v.GetString(head+ ".type")) + fmt.Printf("%s.type=%s\n", head, v.GetString(head+".type")) return cfg, err } diff --git a/data/thermocouple/data.go b/data/thermocouple/data.go index d84bb2e..c8147cc 100644 --- a/data/thermocouple/data.go +++ b/data/thermocouple/data.go @@ -22,3 +22,17 @@ var ( ErrUV = errors.New("uv value out of range") ErrC = errors.New("c value out of range") ) + +var ( + ThermoStringLookup = map[Type]string{ + B: "B", + E: "E", + J: "J", + K: "K", + N: "N", + R: "R", + S: "S", + T: "T", + None: "U", + } +) diff --git a/db/adapter.go b/db/adapter.go index 8817f04..35fd2fa 100644 --- a/db/adapter.go +++ b/db/adapter.go @@ -4,10 +4,25 @@ import ( "errors" "fmt" "th7/data/config" + "th7/data/thermocouple" "th7/ports" "time" ) +var ( + thermocoupleTypes = map[string]thermocouple.Type{ + "B": thermocouple.B, + "E": thermocouple.E, + "J": thermocouple.J, + "K": thermocouple.K, + "N": thermocouple.N, + "R": thermocouple.R, + "S": thermocouple.S, + "T": thermocouple.T, + "U": thermocouple.None, // for uV measurements + } +) + func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error) { var duration time.Duration @@ -18,8 +33,9 @@ func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error no_logging = cfg.Board.NoLogging duration = time.Duration(cfg.Board.Logfreq) * time.Second + // if no DB is specified, or nolog=true then use a dummy db adapter if _, ok := cfg.DB["type"]; !ok || no_logging { - adapter, _ = NewDummyAdapter() // it doesn't fail + adapter, _ = NewDummyAdapter() go startLoggingProcess(adapter, corePort, duration) return adapter, nil } @@ -28,7 +44,7 @@ func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error switch db { case "sqlite3": - adapter, err = NewSQLite3Adapter(cfg.DB) + adapter, err = NewSQLite3Adapter(cfg) default: return &DummyAdapter{}, errors.New("unknown db type") diff --git a/db/sqlite3.go b/db/sqlite3.go index 6a12c6c..2860d4a 100644 --- a/db/sqlite3.go +++ b/db/sqlite3.go @@ -6,27 +6,31 @@ import ( "fmt" "log" "sync" + + "th7/data/config" "th7/data/core" + "th7/data/thermocouple" "time" _ "github.com/mattn/go-sqlite3" ) type SQLite3Adapter struct { - mu sync.Mutex - db *sql.DB + mu sync.Mutex + db *sql.DB + cfg config.Config } -func NewSQLite3Adapter(dbInfo map[string]interface{}) (*SQLite3Adapter, error) { +func NewSQLite3Adapter(config config.Config) (*SQLite3Adapter, error) { var adapter SQLite3Adapter // if path is given ... - if _, ok := dbInfo["path"]; !ok { + if _, ok := config.DB["path"]; !ok { return &adapter, errors.New("sqlite3 requires a path config variable") } - path := fmt.Sprint(dbInfo["path"]) + path := fmt.Sprint(config.DB["path"]) db, err := sql.Open("sqlite3", path) if err != nil { @@ -34,10 +38,14 @@ func NewSQLite3Adapter(dbInfo map[string]interface{}) (*SQLite3Adapter, error) { } adapter.db = db + adapter.cfg = config const create string = ` CREATE TABLE IF NOT EXISTS logs ( id INTEGER NOT NULL, + type TEXT NOT NULL, + gain REAL NOT NULL, + offset REAL NOT NULL, value REAL NOT NULL, timestamp TEXT NOT NULL );` @@ -57,22 +65,29 @@ func (ad *SQLite3Adapter) Save(channels []core.Channel) error { ad.mu.Lock() defer ad.mu.Unlock() - insertLogSQL := `INSERT INTO logs (id, value, timestamp) VALUES (?, ?, ?)` + insertLogSQL := `INSERT INTO logs (id, type, gain, offset, value, timestamp) VALUES (?, ?, ?, ?, ?, ?)` statement, err := ad.db.Prepare(insertLogSQL) if err != nil { log.Fatalln(err.Error()) } - - timestamp := time.Now().Format(time.RFC1123) + timestamp := time.Now().Format(time.DateTime) for c := range channels { - - _, err = statement.Exec(channels[c].Id, channels[c].Value, timestamp) + + id := channels[c].Id + value := channels[c].Value + gain := ad.cfg.Channels[c].Gain + offset := ad.cfg.Channels[c].Offset + thermo := thermocouple.ThermoStringLookup[ad.cfg.Channels[c].Thermo] + + _, err = statement.Exec(id, thermo, gain, offset, 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) + + fmt.Printf("[%s] Logged: id=%d type=%s, gain=%.2f, offset=%.2f, value=%.2f\n", timestamp, id, thermo, gain, offset, value) + } return nil