more config options
This commit is contained in:
parent
6cc6238fdb
commit
33e7ca16b9
@ -39,6 +39,8 @@ The TH7 software uses sane default values where possible.
|
|||||||
port = 9090 # port used by the web view
|
port = 9090 # port used by the web view
|
||||||
LED = true # enable the blinking lights on the Raspberry Pi Hat
|
LED = true # enable the blinking lights on the Raspberry Pi Hat
|
||||||
debug = false # if true, the software will use a simulated TH7
|
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
|
# Configure channel 1
|
||||||
[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)
|
offset = -5.6 # offset is added to the final converted value (in °C)
|
||||||
filter.samples = 50 # samples for the filter function
|
filter.samples = 50 # samples for the filter function
|
||||||
filter.type = 0 # integer representing the filter function type
|
filter.type = 0 # integer representing the filter function type
|
||||||
|
log = false # do not log this channel to DB (if applicable)
|
||||||
|
|
||||||
# Filter functions:
|
# Filter functions:
|
||||||
# 0: simple alpha-beta filter (default)
|
# 0: simple alpha-beta filter (default)
|
||||||
|
23
config.toml
23
config.toml
@ -1,20 +1,14 @@
|
|||||||
# TH7 demo configuration file.
|
|
||||||
[TH7]
|
[TH7]
|
||||||
port = 9090 # web port
|
port = 8080
|
||||||
debug = true # enable to use simulated PCB for development
|
debug = true
|
||||||
nolog = false
|
nolog = true
|
||||||
|
noweb = true
|
||||||
|
|
||||||
# all DB settings are individual to each DB
|
|
||||||
[DB]
|
[DB]
|
||||||
type = "sqlite3"
|
type = "sqlite3"
|
||||||
path = "test.db"
|
path = "test.db"
|
||||||
freq = 60
|
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]
|
[Channel_1]
|
||||||
type = 'U'
|
type = 'U'
|
||||||
filter.samples = 100
|
filter.samples = 100
|
||||||
@ -26,14 +20,15 @@ filter.samples = 20
|
|||||||
|
|
||||||
[Channel_3]
|
[Channel_3]
|
||||||
type = 'T'
|
type = 'T'
|
||||||
gain = 112.28821
|
gain = 112.5
|
||||||
|
offset = 12.4
|
||||||
|
|
||||||
[Channel_4]
|
[Channel_4]
|
||||||
type = 'K'
|
type = 'K'
|
||||||
gain = 2.8129883e2
|
gain = 2.8e2
|
||||||
|
filter.type = 3
|
||||||
|
|
||||||
[Channel_6]
|
[Channel_6]
|
||||||
type = 'J'
|
type = 'J'
|
||||||
offset = -23.1
|
offset = -23.1
|
||||||
|
log = false
|
||||||
|
|
@ -59,6 +59,7 @@ func Load() (config.Config, error) {
|
|||||||
cfg.Board.Logfreq = v.GetInt("DB.freq")
|
cfg.Board.Logfreq = v.GetInt("DB.freq")
|
||||||
cfg.Board.Debug = v.GetBool("TH7.debug")
|
cfg.Board.Debug = v.GetBool("TH7.debug")
|
||||||
cfg.Board.NoLogging = v.GetBool("TH7.nolog")
|
cfg.Board.NoLogging = v.GetBool("TH7.nolog")
|
||||||
|
cfg.Board.NoWeb = v.GetBool("TH7.noweb")
|
||||||
|
|
||||||
cfg.Channels = make([]config.Channel, 0)
|
cfg.Channels = make([]config.Channel, 0)
|
||||||
|
|
||||||
@ -82,6 +83,7 @@ func Load() (config.Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.Thermo = tc
|
c.Thermo = tc
|
||||||
|
c.Log = v.GetBool(head + ".log")
|
||||||
c.Filter.SampleSize = v.GetInt(head + ".filter.samples")
|
c.Filter.SampleSize = v.GetInt(head + ".filter.samples")
|
||||||
c.Filter.Type = v.GetInt(head + ".filter.type")
|
c.Filter.Type = v.GetInt(head + ".filter.type")
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ func SetDefaultConfig(v *viper.Viper) {
|
|||||||
v.SetDefault("TH7.LED", true)
|
v.SetDefault("TH7.LED", true)
|
||||||
v.SetDefault("TH7.debug", false)
|
v.SetDefault("TH7.debug", false)
|
||||||
v.SetDefault("TH7.nolog", false)
|
v.SetDefault("TH7.nolog", false)
|
||||||
|
v.SetDefault("TH7.noweb", false)
|
||||||
v.SetDefault("DB.freq", 600)
|
v.SetDefault("DB.freq", 600)
|
||||||
|
|
||||||
// set defaults for channels
|
// set defaults for channels
|
||||||
@ -20,7 +21,7 @@ func SetDefaultConfig(v *viper.Viper) {
|
|||||||
v.SetDefault(head+".type", "NOTSET")
|
v.SetDefault(head+".type", "NOTSET")
|
||||||
v.SetDefault(head+".gain", 106.8)
|
v.SetDefault(head+".gain", 106.8)
|
||||||
v.SetDefault(head+".offset", 0.0)
|
v.SetDefault(head+".offset", 0.0)
|
||||||
v.SetDefault(head+".unit", "U")
|
v.SetDefault(head+".log", true)
|
||||||
// filter settings
|
// filter settings
|
||||||
v.SetDefault(head+".filter.samples", 50)
|
v.SetDefault(head+".filter.samples", 50)
|
||||||
v.SetDefault(head+".filter.type", 0)
|
v.SetDefault(head+".filter.type", 0)
|
||||||
|
@ -14,6 +14,7 @@ type Channel struct {
|
|||||||
Thermo thermocouple.Type `json:"thermocouple"`
|
Thermo thermocouple.Type `json:"thermocouple"`
|
||||||
Gain float64 `json:"gain"`
|
Gain float64 `json:"gain"`
|
||||||
Offset float64 `json:"offset"`
|
Offset float64 `json:"offset"`
|
||||||
|
Log bool `json:"log"`
|
||||||
Filter Filter `json:"filter"`
|
Filter Filter `json:"filter"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,6 +25,7 @@ type TH7 struct {
|
|||||||
Logfreq int `json:"logfreq"`
|
Logfreq int `json:"logfreq"`
|
||||||
Debug bool `json:"debug"`
|
Debug bool `json:"debug"`
|
||||||
NoLogging bool `json:"nologging"`
|
NoLogging bool `json:"nologging"`
|
||||||
|
NoWeb bool `json:"noweb"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -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 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()
|
adapter, _ = NewDummyAdapter(cfg)
|
||||||
go startLoggingProcess(adapter, corePort, duration)
|
go startLoggingProcess(adapter, corePort, duration)
|
||||||
return adapter, nil
|
return adapter, nil
|
||||||
}
|
}
|
||||||
@ -35,11 +35,11 @@ func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error
|
|||||||
adapter, err = NewInfluxDBAdapter(cfg)
|
adapter, err = NewInfluxDBAdapter(cfg)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return &DummyAdapter{}, errors.New("unknown db type")
|
return &DummyAdapter{cfg: cfg}, errors.New("unknown db type")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &DummyAdapter{}, err
|
return &DummyAdapter{cfg: cfg}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
go startLoggingProcess(adapter, corePort, duration)
|
go startLoggingProcess(adapter, corePort, duration)
|
||||||
|
22
db/dummy.go
22
db/dummy.go
@ -1,26 +1,32 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
|
"th7/data/config"
|
||||||
"th7/data/core"
|
"th7/data/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DummyAdapter struct {
|
type DummyAdapter struct {
|
||||||
|
cfg config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDummyAdapter() (*DummyAdapter, error) {
|
func NewDummyAdapter(cfg config.Config) (*DummyAdapter, error) {
|
||||||
return &DummyAdapter{}, nil
|
var adapter DummyAdapter
|
||||||
|
adapter.cfg = cfg
|
||||||
|
return &adapter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DummyAdapter) Close() {
|
func (d *DummyAdapter) Close() {
|
||||||
fmt.Println("Closing DB ...")
|
log.Println("Closing DB ...")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DummyAdapter) Save(channels []core.Channel) error {
|
func (d *DummyAdapter) Save(channels []core.Channel) error {
|
||||||
fmt.Println("=======================================")
|
log.Println("===============DEBUG LOG===============")
|
||||||
for _, val := range channels {
|
for idx := range channels {
|
||||||
fmt.Printf("Saved: id=%d, value=%g\n", val.Id, val.Value)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -101,9 +101,15 @@ func (ad *InfluxDBAdapter) Save(channels []core.Channel) error {
|
|||||||
|
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
var err error
|
var err error
|
||||||
|
logged := 0
|
||||||
|
|
||||||
// influx does timestamping when data arrives to server
|
// influx does timestamping when data arrives to server
|
||||||
for c := range channels {
|
for c := range channels {
|
||||||
|
|
||||||
|
if !ad.cfg.Channels[c].Log {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
id := channels[c].Id
|
id := channels[c].Id
|
||||||
value := channels[c].Value
|
value := channels[c].Value
|
||||||
gain := ad.cfg.Channels[c].Gain
|
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",
|
sb.WriteString(fmt.Sprintf("TH7,channel=%d,thermocouple=%s gain=%g,offset=%g,value=%g\n",
|
||||||
id, thermo, gain, offset, value))
|
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)
|
saveContext, cancel := context.WithTimeout(context.Background(), INFLUXDB_MAX_SAVE_DUR)
|
||||||
@ -122,7 +136,7 @@ func (ad *InfluxDBAdapter) Save(channels []core.Channel) error {
|
|||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"th7/ports"
|
"th7/ports"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -17,7 +17,7 @@ func startLoggingProcess(db ports.DBPort, core ports.CorePort, dur time.Duration
|
|||||||
|
|
||||||
err := db.Save(val)
|
err := db.Save(val)
|
||||||
if err != nil {
|
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)
|
time.Sleep(dur)
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"th7/data/config"
|
"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 (?, ?, ?, ?, ?, ?)`
|
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 {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
timestamp := time.Now().Format(time.DateTime)
|
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)
|
saveContext, cancel := context.WithTimeout(context.Background(), SQLITE3_MAX_SAVE_DUR)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
logged := 0
|
||||||
for c := range channels {
|
for c := range channels {
|
||||||
|
|
||||||
|
if !ad.cfg.Channels[c].Log {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
id := channels[c].Id
|
id := channels[c].Id
|
||||||
value := channels[c].Value
|
value := channels[c].Value
|
||||||
gain := ad.cfg.Channels[c].Gain
|
gain := ad.cfg.Channels[c].Gain
|
||||||
@ -96,9 +101,16 @@ func (ad *SQLite3Adapter) Save(channels []core.Channel) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
6
main.go
6
main.go
@ -54,15 +54,21 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer dbPort.Close()
|
defer dbPort.Close()
|
||||||
|
|
||||||
|
// if noweb is false, then start web service
|
||||||
|
if !cfg.Board.NoWeb {
|
||||||
webPort = web.NewGinAdapter(corePort, cfg)
|
webPort = web.NewGinAdapter(corePort, cfg)
|
||||||
go webPort.Run()
|
go webPort.Run()
|
||||||
|
}
|
||||||
|
|
||||||
color.Set(color.FgHiRed)
|
color.Set(color.FgHiRed)
|
||||||
fmt.Printf("Started on: %s\n", time.Now().Format(time.DateTime))
|
fmt.Printf("Started on: %s\n", time.Now().Format(time.DateTime))
|
||||||
color.Unset()
|
color.Unset()
|
||||||
|
|
||||||
|
if !cfg.Board.NoWeb {
|
||||||
color.Set(color.FgHiGreen)
|
color.Set(color.FgHiGreen)
|
||||||
fmt.Printf("TH7 web view is live on http://localhost:%d/\n", cfg.Board.Port)
|
fmt.Printf("TH7 web view is live on http://localhost:%d/\n", cfg.Board.Port)
|
||||||
color.Unset()
|
color.Unset()
|
||||||
|
}
|
||||||
|
|
||||||
sig := <-kill
|
sig := <-kill
|
||||||
log.Printf("Caught signal %v", sig)
|
log.Printf("Caught signal %v", sig)
|
||||||
|
Loading…
Reference in New Issue
Block a user