rework data structures and caching service to not overload pi

This commit is contained in:
William Clark 2022-11-13 17:21:03 +00:00
parent 731f827dbf
commit a1a03f5fbc
14 changed files with 126 additions and 135 deletions

View File

@ -17,19 +17,21 @@ var (
var ( var (
thermocoupleTypes = map[string]thermocouple.Type{ thermocoupleTypes = map[string]thermocouple.Type{
"B": thermocouple.B, "B": thermocouple.B,
"E": thermocouple.E, "E": thermocouple.E,
"J": thermocouple.J, "J": thermocouple.J,
"K": thermocouple.K, "K": thermocouple.K,
"N": thermocouple.N, "N": thermocouple.N,
"R": thermocouple.R, "R": thermocouple.R,
"S": thermocouple.S, "S": thermocouple.S,
"T": thermocouple.T, "T": thermocouple.T,
"UV": thermocouple.None,
} }
temperatureUnits = map[string]temperature.Unit{ temperatureUnits = map[string]temperature.Unit{
"C": temperature.C, "C": temperature.C,
"F": temperature.F, "F": temperature.F,
"K": temperature.K, "K": temperature.K,
"UV": temperature.None,
} }
) )
@ -83,7 +85,7 @@ func Load() (config.Config, error) {
var head = fmt.Sprintf("Channel_%d", i) var head = fmt.Sprintf("Channel_%d", i)
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", "C") v.SetDefault(head+".unit", "UV")
} }
for i := 1; i < 8; i++ { for i := 1; i < 8; i++ {

View File

@ -1,6 +1,7 @@
package core package core
import ( import (
"th7/core/th7"
"th7/data/config" "th7/data/config"
"th7/ports" "th7/ports"
) )
@ -10,6 +11,6 @@ func NewAdapter(pcb ports.PCBPort, cfg config.Config) (ports.CorePort, error) {
if cfg.Board.Debug { if cfg.Board.Debug {
return NewDummyAdapter(pcb), nil return NewDummyAdapter(pcb), nil
} else { } else {
return NewTH7Adapter(pcb, cfg), nil return th7.NewTH7Adapter(pcb, cfg), nil
} }
} }

74
core/th7/service.go Normal file
View File

@ -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)
}
}

View File

@ -1,4 +1,4 @@
package core package th7
import ( import (
"errors" "errors"
@ -14,6 +14,8 @@ type TH7Adapter struct {
data []core.Channel data []core.Channel
lookup map[int]int lookup map[int]int
table core.Ratio table core.Ratio
cfg config.Config
run bool
} }
func NewTH7Adapter(pcbPort ports.PCBPort, cfg config.Config) *TH7Adapter { 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 var adapter TH7Adapter
adapter.pcbPort = pcbPort adapter.pcbPort = pcbPort
adapter.cfg = cfg
adapter.run = true
adapter.lookup = make(map[int]int) adapter.lookup = make(map[int]int)
adapter.data = make([]core.Channel, 0) 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) adapter.data = append(adapter.data, channel)
} }
go startCacheService(&adapter)
return &adapter return &adapter
} }

View File

@ -1,9 +1,11 @@
package core package core
import "th7/data/temperature"
type Channel struct { type Channel struct {
Id int `json:"id"` Id int `json:"id"`
Uv float64 `json:"uv"` Value float64 `json:"value"`
Celsius float64 `json:"celsius"` Unit temperature.Unit `json:"unit"`
} }
type Ratio struct { type Ratio struct {

View File

@ -3,7 +3,7 @@ package temperature
type Unit int type Unit int
const ( const (
None Unit = iota None Unit = iota // None is UV
K K
C C
F F

View File

@ -2,6 +2,7 @@ package db
import ( import (
"fmt" "fmt"
"th7/data/core"
) )
type DummyAdapter struct { type DummyAdapter struct {
@ -15,6 +16,6 @@ func (d *DummyAdapter) Close() {
fmt.Println("Closing DB ...") fmt.Println("Closing DB ...")
} }
func (d *DummyAdapter) Save() { func (d *DummyAdapter) Save([]core.Channel) {
fmt.Println("Saving data to DB ...") fmt.Println("Saving data to DB ...")
} }

View File

@ -1,6 +1,7 @@
package logger package logger
import ( import (
"fmt"
"th7/ports" "th7/ports"
"time" "time"
) )
@ -19,7 +20,12 @@ func doLogging(log *Logger) {
for log.run { for log.run {
time.Sleep(duration) 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)
} }
} }

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"sync" "sync"
"th7/data/core"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@ -52,7 +53,7 @@ func (ad *SQLite3Adapter) Close() {
ad.db.Close() ad.db.Close()
} }
func (ad *SQLite3Adapter) Save() { func (ad *SQLite3Adapter) Save([]core.Channel) {
ad.mu.Lock() ad.mu.Lock()
defer ad.mu.Unlock() defer ad.mu.Unlock()

View File

@ -24,7 +24,11 @@ func main() {
var err error var err error
kill := make(chan os.Signal, 1) 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() config, err := config.Load()
if err != nil { if err != nil {

View File

@ -1,13 +1,10 @@
package pcb package pcb
import ( import (
"th7/pcb/led"
"th7/ports" "th7/ports"
) )
func NewAdapter(LED bool) (ports.PCBPort, error) { func NewAdapter(LED bool) (ports.PCBPort, error) {
ledController := led.NewLEDController() return NewDummyAdapter(LED)
return NewDummyAdapter(LED, ledController)
} }

View File

@ -3,22 +3,17 @@ package pcb
import ( import (
"fmt" "fmt"
"th7/data/pcb" "th7/data/pcb"
"th7/pcb/led"
) )
type DummyAdapter struct { type DummyAdapter struct {
table pcb.Table table pcb.Table
use_led bool use_led bool
ledController *led.LEDController
} }
func NewDummyAdapter(led bool, controller *led.LEDController) (*DummyAdapter, error) { func NewDummyAdapter(led bool) (*DummyAdapter, error) {
var adapter DummyAdapter var adapter DummyAdapter
adapter.use_led = led adapter.use_led = led
adapter.ledController = controller
adapter.ledController.Off()
return &adapter, nil return &adapter, nil
} }

View File

@ -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()
}

View File

@ -1,6 +1,8 @@
package ports package ports
import "th7/data/core"
type DBPort interface { type DBPort interface {
Close() Close()
Save() Save([]core.Channel)
} }