diff --git a/README.md b/README.md index 4966e46..2a40d93 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/config.toml b/config.toml index 85b4799..87ab9cd 100644 --- a/config.toml +++ b/config.toml @@ -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 \ No newline at end of file diff --git a/config/config.go b/config/config.go index c78e9b6..89c6fec 100644 --- a/config/config.go +++ b/config/config.go @@ -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") diff --git a/config/defaults.go b/config/defaults.go index 664d382..4eb2b69 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -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) diff --git a/data/config/data.go b/data/config/data.go index eba9d24..0e003f6 100644 --- a/data/config/data.go +++ b/data/config/data.go @@ -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 { diff --git a/db/adapter.go b/db/adapter.go index 77e8a46..a322b8c 100644 --- a/db/adapter.go +++ b/db/adapter.go @@ -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) diff --git a/db/dummy.go b/db/dummy.go index 7e2567c..b3aa8fb 100644 --- a/db/dummy.go +++ b/db/dummy.go @@ -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 } diff --git a/db/influxdb.go b/db/influxdb.go index e4480f4..c309740 100644 --- a/db/influxdb.go +++ b/db/influxdb.go @@ -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 } diff --git a/db/logging.go b/db/logging.go index 60d455f..c833373 100644 --- a/db/logging.go +++ b/db/logging.go @@ -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) diff --git a/db/sqlite3.go b/db/sqlite3.go index 5a6c2a9..5d86299 100644 --- a/db/sqlite3.go +++ b/db/sqlite3.go @@ -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 } diff --git a/main.go b/main.go index 334fdc3..41a71c9 100644 --- a/main.go +++ b/main.go @@ -54,15 +54,21 @@ func main() { } defer dbPort.Close() - webPort = web.NewGinAdapter(corePort, cfg) - go webPort.Run() + // 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() - color.Set(color.FgHiGreen) - fmt.Printf("TH7 web view is live on http://localhost:%d/\n", cfg.Board.Port) - 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)