rework data structures and caching service to not overload pi
This commit is contained in:
parent
731f827dbf
commit
a1a03f5fbc
@ -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++ {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
74
core/th7/service.go
Normal file
74
core/th7/service.go
Normal 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)
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -3,7 +3,7 @@ package temperature
|
||||
type Unit int
|
||||
|
||||
const (
|
||||
None Unit = iota
|
||||
None Unit = iota // None is UV
|
||||
K
|
||||
C
|
||||
F
|
||||
|
@ -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 ...")
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
||||
|
6
main.go
6
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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
11
pcb/dummy.go
11
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
|
||||
}
|
||||
|
100
pcb/led/led.go
100
pcb/led/led.go
@ -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()
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package ports
|
||||
|
||||
import "th7/data/core"
|
||||
|
||||
type DBPort interface {
|
||||
Close()
|
||||
Save()
|
||||
Save([]core.Channel)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user