more config options

This commit is contained in:
William Clark 2023-11-25 10:37:02 +00:00
parent 6cc6238fdb
commit 33e7ca16b9
11 changed files with 78 additions and 37 deletions

View File

@ -39,6 +39,8 @@ The TH7 software uses sane default values where possible.
port = 9090 # port used by the web view
LED = true # enable the blinking lights on the Raspberry Pi Hat
debug = false # if true, the software will use a simulated TH7
nolog = false # debug variable, will simulate a DB
noweb = false # set to true to disable web view and the REST API
# Configure channel 1
[Channel_1]
@ -47,6 +49,7 @@ gain = 106.8 # gain is used during signal conversion
offset = -5.6 # offset is added to the final converted value (in °C)
filter.samples = 50 # samples for the filter function
filter.type = 0 # integer representing the filter function type
log = false # do not log this channel to DB (if applicable)
# Filter functions:
# 0: simple alpha-beta filter (default)

View File

@ -1,20 +1,14 @@
# TH7 demo configuration file.
[TH7]
port = 9090 # web port
debug = true # enable to use simulated PCB for development
nolog = false
port = 8080
debug = true
nolog = true
noweb = true
# all DB settings are individual to each DB
[DB]
type = "sqlite3"
path = "test.db"
freq = 60
# Configure channel 1
# type = 'U' => UV, else use other letters for resp type
# filter.samples => sample buffer size
# filter.type => filtering function to use [0-3; 0 default]
[Channel_1]
type = 'U'
filter.samples = 100
@ -26,14 +20,15 @@ filter.samples = 20
[Channel_3]
type = 'T'
gain = 112.28821
gain = 112.5
offset = 12.4
[Channel_4]
type = 'K'
gain = 2.8129883e2
gain = 2.8e2
filter.type = 3
[Channel_6]
type = 'J'
offset = -23.1
log = false

View File

@ -59,6 +59,7 @@ func Load() (config.Config, error) {
cfg.Board.Logfreq = v.GetInt("DB.freq")
cfg.Board.Debug = v.GetBool("TH7.debug")
cfg.Board.NoLogging = v.GetBool("TH7.nolog")
cfg.Board.NoWeb = v.GetBool("TH7.noweb")
cfg.Channels = make([]config.Channel, 0)
@ -82,6 +83,7 @@ func Load() (config.Config, error) {
}
c.Thermo = tc
c.Log = v.GetBool(head + ".log")
c.Filter.SampleSize = v.GetInt(head + ".filter.samples")
c.Filter.Type = v.GetInt(head + ".filter.type")

View File

@ -12,6 +12,7 @@ func SetDefaultConfig(v *viper.Viper) {
v.SetDefault("TH7.LED", true)
v.SetDefault("TH7.debug", false)
v.SetDefault("TH7.nolog", false)
v.SetDefault("TH7.noweb", false)
v.SetDefault("DB.freq", 600)
// set defaults for channels
@ -20,7 +21,7 @@ func SetDefaultConfig(v *viper.Viper) {
v.SetDefault(head+".type", "NOTSET")
v.SetDefault(head+".gain", 106.8)
v.SetDefault(head+".offset", 0.0)
v.SetDefault(head+".unit", "U")
v.SetDefault(head+".log", true)
// filter settings
v.SetDefault(head+".filter.samples", 50)
v.SetDefault(head+".filter.type", 0)

View File

@ -14,6 +14,7 @@ type Channel struct {
Thermo thermocouple.Type `json:"thermocouple"`
Gain float64 `json:"gain"`
Offset float64 `json:"offset"`
Log bool `json:"log"`
Filter Filter `json:"filter"`
}
@ -24,6 +25,7 @@ type TH7 struct {
Logfreq int `json:"logfreq"`
Debug bool `json:"debug"`
NoLogging bool `json:"nologging"`
NoWeb bool `json:"noweb"`
}
type Config struct {

View File

@ -20,7 +20,7 @@ func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error
// if no DB is specified, or nolog=true then use a dummy db adapter
if _, ok := cfg.DB["type"]; !ok || no_logging {
adapter, _ = NewDummyAdapter()
adapter, _ = NewDummyAdapter(cfg)
go startLoggingProcess(adapter, corePort, duration)
return adapter, nil
}
@ -35,11 +35,11 @@ func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error
adapter, err = NewInfluxDBAdapter(cfg)
default:
return &DummyAdapter{}, errors.New("unknown db type")
return &DummyAdapter{cfg: cfg}, errors.New("unknown db type")
}
if err != nil {
return &DummyAdapter{}, err
return &DummyAdapter{cfg: cfg}, err
}
go startLoggingProcess(adapter, corePort, duration)

View File

@ -1,26 +1,32 @@
package db
import (
"fmt"
"log"
"th7/data/config"
"th7/data/core"
)
type DummyAdapter struct {
cfg config.Config
}
func NewDummyAdapter() (*DummyAdapter, error) {
return &DummyAdapter{}, nil
func NewDummyAdapter(cfg config.Config) (*DummyAdapter, error) {
var adapter DummyAdapter
adapter.cfg = cfg
return &adapter, nil
}
func (d *DummyAdapter) Close() {
fmt.Println("Closing DB ...")
log.Println("Closing DB ...")
}
func (d *DummyAdapter) Save(channels []core.Channel) error {
fmt.Println("=======================================")
for _, val := range channels {
fmt.Printf("Saved: id=%d, value=%g\n", val.Id, val.Value)
log.Println("===============DEBUG LOG===============")
for idx := range channels {
if d.cfg.Channels[idx].Log {
log.Printf("Saved: id=%d, value=%g\n", channels[idx].Id, channels[idx].Value)
}
fmt.Println("=======================================")
}
log.Println("=======================================")
return nil
}

View File

@ -101,9 +101,15 @@ func (ad *InfluxDBAdapter) Save(channels []core.Channel) error {
var sb strings.Builder
var err error
logged := 0
// influx does timestamping when data arrives to server
for c := range channels {
if !ad.cfg.Channels[c].Log {
continue
}
id := channels[c].Id
value := channels[c].Value
gain := ad.cfg.Channels[c].Gain
@ -112,6 +118,14 @@ func (ad *InfluxDBAdapter) Save(channels []core.Channel) error {
sb.WriteString(fmt.Sprintf("TH7,channel=%d,thermocouple=%s gain=%g,offset=%g,value=%g\n",
id, thermo, gain, offset, value))
logged++
}
// need at least 1 iteration to bother sending data to influx server
if logged == 0 {
fmt.Printf("[%s] InfluxDB: nothing to do.\n", time.Now().Format(time.DateTime))
return nil
}
saveContext, cancel := context.WithTimeout(context.Background(), INFLUXDB_MAX_SAVE_DUR)
@ -122,7 +136,7 @@ func (ad *InfluxDBAdapter) Save(channels []core.Channel) error {
return err
}
fmt.Printf("[%s] InfluxDB: logged %d channels OK\n", time.Now().Format(time.DateTime), len(channels))
fmt.Printf("[%s] InfluxDB: logged %d channels OK\n", time.Now().Format(time.DateTime), logged)
return nil
}

View File

@ -1,7 +1,7 @@
package db
import (
"fmt"
"log"
"th7/ports"
"time"
)
@ -17,7 +17,7 @@ func startLoggingProcess(db ports.DBPort, core ports.CorePort, dur time.Duration
err := db.Save(val)
if err != nil {
fmt.Printf("Error encountered while saving values: %v\n", err)
log.Printf("[DB] Error encountered when attempting to save logs: %v\n", err)
}
time.Sleep(dur)

View File

@ -4,7 +4,7 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"log"
"sync"
"th7/data/config"
@ -76,7 +76,7 @@ func (ad *SQLite3Adapter) Save(channels []core.Channel) error {
insertLogSQL := `INSERT INTO logs (id, type, gain, offset, value, timestamp) VALUES (?, ?, ?, ?, ?, ?)`
statement, err := ad.db.Prepare(insertLogSQL)
if err != nil {
fmt.Println(err)
log.Println(err)
}
timestamp := time.Now().Format(time.DateTime)
@ -84,8 +84,13 @@ func (ad *SQLite3Adapter) Save(channels []core.Channel) error {
saveContext, cancel := context.WithTimeout(context.Background(), SQLITE3_MAX_SAVE_DUR)
defer cancel()
logged := 0
for c := range channels {
if !ad.cfg.Channels[c].Log {
continue
}
id := channels[c].Id
value := channels[c].Value
gain := ad.cfg.Channels[c].Gain
@ -96,9 +101,16 @@ func (ad *SQLite3Adapter) Save(channels []core.Channel) error {
if err != nil {
return err
}
logged++
}
fmt.Printf("[%s] SQLite3: logged %d channels.\n", timestamp, len(channels))
if logged == 0 {
log.Printf("[%s] SQLite3: nothing to do.\n", timestamp)
return nil
}
log.Printf("[%s] SQLite3: logged %d channels.\n", timestamp, logged)
return nil
}

View File

@ -54,15 +54,21 @@ func main() {
}
defer dbPort.Close()
// if noweb is false, then start web service
if !cfg.Board.NoWeb {
webPort = web.NewGinAdapter(corePort, cfg)
go webPort.Run()
}
color.Set(color.FgHiRed)
fmt.Printf("Started on: %s\n", time.Now().Format(time.DateTime))
color.Unset()
if !cfg.Board.NoWeb {
color.Set(color.FgHiGreen)
fmt.Printf("TH7 web view is live on http://localhost:%d/\n", cfg.Board.Port)
color.Unset()
}
sig := <-kill
log.Printf("Caught signal %v", sig)