sqlite3 now logs cfg info such as offset, gain, type

This commit is contained in:
William Clark 2023-11-23 16:55:57 +00:00
parent 01ab06fbf0
commit b7da565c82
4 changed files with 61 additions and 16 deletions

View File

@ -15,7 +15,7 @@ var (
) )
var ( var (
thermocoupleTypes = map[string]thermocouple.Type{ ThermocoupleTypes = map[string]thermocouple.Type{
"B": thermocouple.B, "B": thermocouple.B,
"E": thermocouple.E, "E": thermocouple.E,
"J": thermocouple.J, "J": thermocouple.J,
@ -30,7 +30,7 @@ var (
func getThermocoupleType(t string) (thermocouple.Type, error) { func getThermocoupleType(t string) (thermocouple.Type, error) {
t = strings.ToUpper(t) t = strings.ToUpper(t)
if val, ok := thermocoupleTypes[t]; ok { if val, ok := ThermocoupleTypes[t]; ok {
return val, nil return val, nil
} else { } else {
return thermocouple.None, ErrConfigBadThermocoupleType return thermocouple.None, ErrConfigBadThermocoupleType
@ -77,7 +77,7 @@ func Load() (config.Config, error) {
tc, err := getThermocoupleType(v.GetString(head + ".type")) tc, err := getThermocoupleType(v.GetString(head + ".type"))
if err != nil { 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 return cfg, err
} }

View File

@ -22,3 +22,17 @@ var (
ErrUV = errors.New("uv value out of range") ErrUV = errors.New("uv value out of range")
ErrC = errors.New("c 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",
}
)

View File

@ -4,10 +4,25 @@ import (
"errors" "errors"
"fmt" "fmt"
"th7/data/config" "th7/data/config"
"th7/data/thermocouple"
"th7/ports" "th7/ports"
"time" "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) { func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error) {
var duration time.Duration var duration time.Duration
@ -18,8 +33,9 @@ func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error
no_logging = cfg.Board.NoLogging no_logging = cfg.Board.NoLogging
duration = time.Duration(cfg.Board.Logfreq) * time.Second 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 { if _, ok := cfg.DB["type"]; !ok || no_logging {
adapter, _ = NewDummyAdapter() // it doesn't fail adapter, _ = NewDummyAdapter()
go startLoggingProcess(adapter, corePort, duration) go startLoggingProcess(adapter, corePort, duration)
return adapter, nil return adapter, nil
} }
@ -28,7 +44,7 @@ func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error
switch db { switch db {
case "sqlite3": case "sqlite3":
adapter, err = NewSQLite3Adapter(cfg.DB) adapter, err = NewSQLite3Adapter(cfg)
default: default:
return &DummyAdapter{}, errors.New("unknown db type") return &DummyAdapter{}, errors.New("unknown db type")

View File

@ -6,7 +6,10 @@ import (
"fmt" "fmt"
"log" "log"
"sync" "sync"
"th7/data/config"
"th7/data/core" "th7/data/core"
"th7/data/thermocouple"
"time" "time"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@ -15,18 +18,19 @@ import (
type SQLite3Adapter struct { type SQLite3Adapter struct {
mu sync.Mutex mu sync.Mutex
db *sql.DB db *sql.DB
cfg config.Config
} }
func NewSQLite3Adapter(dbInfo map[string]interface{}) (*SQLite3Adapter, error) { func NewSQLite3Adapter(config config.Config) (*SQLite3Adapter, error) {
var adapter SQLite3Adapter var adapter SQLite3Adapter
// if path is given ... // 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") 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) db, err := sql.Open("sqlite3", path)
if err != nil { if err != nil {
@ -34,10 +38,14 @@ func NewSQLite3Adapter(dbInfo map[string]interface{}) (*SQLite3Adapter, error) {
} }
adapter.db = db adapter.db = db
adapter.cfg = config
const create string = ` const create string = `
CREATE TABLE IF NOT EXISTS logs ( CREATE TABLE IF NOT EXISTS logs (
id INTEGER NOT NULL, id INTEGER NOT NULL,
type TEXT NOT NULL,
gain REAL NOT NULL,
offset REAL NOT NULL,
value REAL NOT NULL, value REAL NOT NULL,
timestamp TEXT NOT NULL timestamp TEXT NOT NULL
);` );`
@ -57,22 +65,29 @@ func (ad *SQLite3Adapter) Save(channels []core.Channel) error {
ad.mu.Lock() ad.mu.Lock()
defer ad.mu.Unlock() 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) statement, err := ad.db.Prepare(insertLogSQL)
if err != nil { if err != nil {
log.Fatalln(err.Error()) log.Fatalln(err.Error())
} }
timestamp := time.Now().Format(time.DateTime)
timestamp := time.Now().Format(time.RFC1123)
for c := range channels { 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 { if err != nil {
log.Fatalln(err.Error()) 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 return nil