diff --git a/config/config.go b/config/config.go index c5caf0c..a14cf45 100644 --- a/config/config.go +++ b/config/config.go @@ -17,19 +17,21 @@ var ( 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, + "B": thermocouple.B, + "E": thermocouple.E, + "J": thermocouple.J, + "K": thermocouple.K, + "N": thermocouple.N, + "R": thermocouple.R, + "S": thermocouple.S, + "T": thermocouple.T, + "UV": thermocouple.None, } temperatureUnits = map[string]temperature.Unit{ - "C": temperature.C, - "F": temperature.F, - "K": temperature.K, + "C": temperature.C, + "F": temperature.F, + "K": temperature.K, + "UV": temperature.None, } ) @@ -83,7 +85,7 @@ func Load() (config.Config, error) { var head = fmt.Sprintf("Channel_%d", i) v.SetDefault(head+".gain", 106.8) v.SetDefault(head+".offset", 0.0) - v.SetDefault(head+".unit", "C") + v.SetDefault(head+".unit", "UV") } for i := 1; i < 8; i++ { diff --git a/core/adapter.go b/core/adapter.go index a2ce4fa..7bdf9fe 100644 --- a/core/adapter.go +++ b/core/adapter.go @@ -1,6 +1,7 @@ package core import ( + "th7/core/th7" "th7/data/config" "th7/ports" ) @@ -10,6 +11,6 @@ func NewAdapter(pcb ports.PCBPort, cfg config.Config) (ports.CorePort, error) { if cfg.Board.Debug { return NewDummyAdapter(pcb), nil } else { - return NewTH7Adapter(pcb, cfg), nil + return th7.NewTH7Adapter(pcb, cfg), nil } } diff --git a/core/th7/service.go b/core/th7/service.go new file mode 100644 index 0000000..075bb49 --- /dev/null +++ b/core/th7/service.go @@ -0,0 +1,74 @@ +package th7 + +import ( + "fmt" + "log" + "time" +) + +const ( + DurWaitBetweenChannel = 3 * time.Millisecond + DurWaitBetweenSample = 1 * time.Millisecond + DurWaitRestart = 500 * time.Millisecond +) + +const ( + Samples = 10 +) + +func alphaBetaFilter(arr []float64, init float64) float64 { + + value := init + for i := range arr { + gain := 1.0 / (float64(i + 1)) + value += (gain*arr[i] - value) + } + return value +} + +func startCacheService(t *TH7Adapter) { + + data := t.data + + samples := make([][]float64, len(t.cfg.Channels)) + + // init + for i := range samples { + samples[i] = make([]float64, Samples) + } + + for t.run { + + for i := 0; i < Samples; i++ { + + // update Table + t.pcbPort.UpdateTable() + + for k := range t.cfg.Channels { + val, err := t.pcbPort.ReadChannel(t.cfg.Channels[k].Id) + if err != nil { + log.Printf("Error reading channel %d: %v\n", t.cfg.Channels[k].Id, err) + } + + samples[k][i] = val + time.Sleep(DurWaitBetweenSample) + } + time.Sleep(DurWaitBetweenChannel) + } + + // do alpha~beta on each channel, + // putting the value in + + for i := range samples { + data[i].Value = alphaBetaFilter(samples[i], data[i].Value) + } + + t.mu.Lock() + t.data = data + t.mu.Unlock() + + fmt.Println("data updated ...") + + time.Sleep(DurWaitRestart) + } +} diff --git a/core/th7.go b/core/th7/th7.go similarity index 91% rename from core/th7.go rename to core/th7/th7.go index 4acb0b3..36e2b00 100644 --- a/core/th7.go +++ b/core/th7/th7.go @@ -1,4 +1,4 @@ -package core +package th7 import ( "errors" @@ -14,6 +14,8 @@ type TH7Adapter struct { data []core.Channel lookup map[int]int table core.Ratio + cfg config.Config + run bool } func NewTH7Adapter(pcbPort ports.PCBPort, cfg config.Config) *TH7Adapter { @@ -21,6 +23,8 @@ func NewTH7Adapter(pcbPort ports.PCBPort, cfg config.Config) *TH7Adapter { var adapter TH7Adapter adapter.pcbPort = pcbPort + adapter.cfg = cfg + adapter.run = true adapter.lookup = make(map[int]int) adapter.data = make([]core.Channel, 0) @@ -32,6 +36,8 @@ func NewTH7Adapter(pcbPort ports.PCBPort, cfg config.Config) *TH7Adapter { adapter.data = append(adapter.data, channel) } + go startCacheService(&adapter) + return &adapter } diff --git a/data/core/data.go b/data/core/data.go index 68e31a9..cd75f2a 100644 --- a/data/core/data.go +++ b/data/core/data.go @@ -1,9 +1,11 @@ package core +import "th7/data/temperature" + type Channel struct { - Id int `json:"id"` - Uv float64 `json:"uv"` - Celsius float64 `json:"celsius"` + Id int `json:"id"` + Value float64 `json:"value"` + Unit temperature.Unit `json:"unit"` } type Ratio struct { diff --git a/data/temperature/data.go b/data/temperature/data.go index 77b421b..0bd0482 100644 --- a/data/temperature/data.go +++ b/data/temperature/data.go @@ -3,7 +3,7 @@ package temperature type Unit int const ( - None Unit = iota + None Unit = iota // None is UV K C F diff --git a/db/dummy.go b/db/dummy.go index a1767d1..9e921a3 100644 --- a/db/dummy.go +++ b/db/dummy.go @@ -2,6 +2,7 @@ package db import ( "fmt" + "th7/data/core" ) type DummyAdapter struct { @@ -15,6 +16,6 @@ func (d *DummyAdapter) Close() { fmt.Println("Closing DB ...") } -func (d *DummyAdapter) Save() { +func (d *DummyAdapter) Save([]core.Channel) { fmt.Println("Saving data to DB ...") } diff --git a/db/logger/logger.go b/db/logger/logger.go index 1d94aa2..ee9689e 100644 --- a/db/logger/logger.go +++ b/db/logger/logger.go @@ -1,6 +1,7 @@ package logger import ( + "fmt" "th7/ports" "time" ) @@ -19,7 +20,12 @@ func doLogging(log *Logger) { for log.run { time.Sleep(duration) - log.db.Save() + + ch, err := log.core.GetChannels() + if err != nil { + fmt.Printf("error getting data for logger: %v\n", err) + } + log.db.Save(ch) } } diff --git a/db/sqlite3.go b/db/sqlite3.go index c1d721e..8a0d43b 100644 --- a/db/sqlite3.go +++ b/db/sqlite3.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "sync" + "th7/data/core" _ "github.com/mattn/go-sqlite3" ) @@ -52,7 +53,7 @@ func (ad *SQLite3Adapter) Close() { ad.db.Close() } -func (ad *SQLite3Adapter) Save() { +func (ad *SQLite3Adapter) Save([]core.Channel) { ad.mu.Lock() defer ad.mu.Unlock() diff --git a/main.go b/main.go index 08b3c24..7fcde3c 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,11 @@ func main() { var err error kill := make(chan os.Signal, 1) - signal.Notify(kill, syscall.SIGTERM, syscall.SIGINT) + signal.Notify(kill, + syscall.SIGTERM, + syscall.SIGINT, + syscall.SIGHUP, + syscall.SIGQUIT) config, err := config.Load() if err != nil { diff --git a/pcb/adapter.go b/pcb/adapter.go index 058be87..645dfaf 100644 --- a/pcb/adapter.go +++ b/pcb/adapter.go @@ -1,13 +1,10 @@ package pcb import ( - "th7/pcb/led" "th7/ports" ) func NewAdapter(LED bool) (ports.PCBPort, error) { - ledController := led.NewLEDController() - - return NewDummyAdapter(LED, ledController) + return NewDummyAdapter(LED) } diff --git a/pcb/dummy.go b/pcb/dummy.go index 991583f..8a34a39 100644 --- a/pcb/dummy.go +++ b/pcb/dummy.go @@ -3,22 +3,17 @@ package pcb import ( "fmt" "th7/data/pcb" - "th7/pcb/led" ) type DummyAdapter struct { - table pcb.Table - use_led bool - ledController *led.LEDController + table pcb.Table + use_led bool } -func NewDummyAdapter(led bool, controller *led.LEDController) (*DummyAdapter, error) { +func NewDummyAdapter(led bool) (*DummyAdapter, error) { var adapter DummyAdapter adapter.use_led = led - adapter.ledController = controller - - adapter.ledController.Off() return &adapter, nil } diff --git a/pcb/led/led.go b/pcb/led/led.go deleted file mode 100644 index 96f0bbe..0000000 --- a/pcb/led/led.go +++ /dev/null @@ -1,100 +0,0 @@ -package led - -import ( - "sync" - "time" -) - -const ( - DurationFlick = 40 * time.Millisecond -) - -type LEDController struct { - led1 bool - led2 bool - mu1 sync.Mutex - mu2 sync.Mutex -} - -func NewLEDController() *LEDController { - - var controller LEDController - controller.led1 = false - controller.led2 = false - - return &controller -} - -// turn both off -func (c *LEDController) Off() { - - c.led1 = true - c.led2 = true - - c.Disable1() - c.Disable2() -} - -func (c *LEDController) Enable1() { - - if c.led1 { - return - } - - c.led1 = true - - // gpio code to turn LED 1 on -} - -func (c *LEDController) Enable2() { - - if c.led2 { - return - } - - c.led2 = true - - // gpio code to turn LED 2 on -} - -func (c *LEDController) Disable1() { - - if !c.led1 { - return - } - - c.led1 = false - - // gpio code to turn LED 1 off -} - -func (c *LEDController) Disable2() { - - if !c.led2 { - return - } - - c.led2 = false - - // gpio code to turn LED 2 off -} - -func (c *LEDController) Flick1() { - c.mu1.Lock() - defer c.mu1.Unlock() - - // assume it is off - c.Enable1() - time.Sleep(DurationFlick) - c.Disable1() -} - -func (c *LEDController) Flick2() { - c.mu2.Lock() - defer c.mu2.Unlock() - - // assume it is off - c.Enable2() - time.Sleep(DurationFlick) - c.Disable2() -} diff --git a/ports/db.go b/ports/db.go index fd75e65..9a33913 100644 --- a/ports/db.go +++ b/ports/db.go @@ -1,6 +1,8 @@ package ports +import "th7/data/core" + type DBPort interface { Close() - Save() + Save([]core.Channel) }