sqlite3 now logs cfg info such as offset, gain, type
This commit is contained in:
parent
01ab06fbf0
commit
b7da565c82
@ -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
|
||||
}
|
||||
|
||||
|
@ -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",
|
||||
}
|
||||
)
|
||||
|
@ -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")
|
||||
|
@ -6,7 +6,10 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"th7/data/config"
|
||||
"th7/data/core"
|
||||
"th7/data/thermocouple"
|
||||
"time"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@ -15,18 +18,19 @@ import (
|
||||
type SQLite3Adapter struct {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user