th7/db/adapter.go

60 lines
1.2 KiB
Go

package db
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
var no_logging bool
var adapter ports.DBPort
var err 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()
go startLoggingProcess(adapter, corePort, duration)
return adapter, nil
}
db := fmt.Sprint(cfg.DB["type"])
switch db {
case "sqlite3":
adapter, err = NewSQLite3Adapter(cfg)
default:
return &DummyAdapter{}, errors.New("unknown db type")
}
if err != nil {
return &DummyAdapter{}, err
}
go startLoggingProcess(adapter, corePort, duration)
return adapter, nil
}