Stabilize
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Søren Rasmussen 2022-07-24 09:38:40 +02:00
parent 19a79f0e59
commit 65f284a2e9
4 changed files with 37 additions and 13 deletions

View file

@ -11,7 +11,6 @@ import (
"git.joco.dk/sng/fermentord/internal/controllers" "git.joco.dk/sng/fermentord/internal/controllers"
"git.joco.dk/sng/fermentord/internal/dwingest" "git.joco.dk/sng/fermentord/internal/dwingest"
"git.joco.dk/sng/fermentord/internal/hw" "git.joco.dk/sng/fermentord/internal/hw"
"git.joco.dk/sng/fermentord/internal/lcd"
"git.joco.dk/sng/fermentord/pkg/temperature" "git.joco.dk/sng/fermentord/pkg/temperature"
"git.joco.dk/sng/fermentord/pkg/tilt" "git.joco.dk/sng/fermentord/pkg/tilt"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
@ -26,12 +25,13 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
defer wg.Done() defer wg.Done()
// Display // Display
display, err := lcd.NewLCD() /*display, err := lcd.NewLCD()
if err != nil { if err != nil {
hub.CaptureException(err) hub.CaptureException(err)
log.Fatal(err) log.Fatal(err)
} }
defer display.Close() defer display.Close()
*/
// Controller // Controller
ctrl := controllers.NewChamberController(*config) ctrl := controllers.NewChamberController(*config)
@ -40,7 +40,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
loadConfiguration := func() { loadConfiguration := func() {
temperature.ConfigUpdate <- *config temperature.ConfigUpdate <- *config
ctrl.ConfigUpdates <- *config ctrl.ConfigUpdates <- *config
display.SetSetpointTemp(config.FermentationTemperature) //display.SetSetpointTemp(config.FermentationTemperature)
} }
viper.OnConfigChange(func(in fsnotify.Event) { viper.OnConfigChange(func(in fsnotify.Event) {
@ -60,8 +60,8 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
} }
defer gpio.Close() defer gpio.Close()
wg.Add(5) wg.Add(4)
go display.Run(ctx, wg) //go display.Run(ctx, wg)
go ctrl.Run(ctx, wg) go ctrl.Run(ctx, wg)
go ingest.Run(ctx, wg, js, config) go ingest.Run(ctx, wg, js, config)
go temperature.PollSensors(ctx, wg, 1*time.Second, config.Sensors.Weight) go temperature.PollSensors(ctx, wg, 1*time.Second, config.Sensors.Weight)
@ -72,7 +72,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
case reading := <-temperature.Reading: case reading := <-temperature.Reading:
ctrl.SetTemperature(reading) ctrl.SetTemperature(reading)
ingest.AddReading(reading) ingest.AddReading(reading)
display.SetTemperature(reading) //display.SetTemperature(reading)
case ev := <-gpio.C: case ev := <-gpio.C:
var evs string var evs string
@ -81,7 +81,9 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
case hw.DoorClosed: case hw.DoorClosed:
log.Print("Door closed") log.Print("Door closed")
gpio.LightsOff() gpio.LightsOff()
if config.CoolerEnabled || config.HeaterEnabled {
gpio.StartFan() gpio.StartFan()
}
ctrl.Resume() ctrl.Resume()
evs = "DOOR_CLOSED" evs = "DOOR_CLOSED"
@ -113,7 +115,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
case state := <-ctrl.C: case state := <-ctrl.C:
gpioSetState(state, gpio, config) gpioSetState(state, gpio, config)
ingest.AddState(state) ingest.AddState(state)
display.SetState(state) //display.SetState(state)
case <-ctx.Done(): case <-ctx.Done():
return return

View file

@ -88,7 +88,7 @@ func (p *ChamberController) Run(ctx context.Context, wg *sync.WaitGroup) {
ticker.Stop() ticker.Stop()
p.isPaused = false p.isPaused = false
// TODO Check if the line below blocks on shutdown // TODO Check if the line below blocks on shutdown
p.setChamberState(ChamberStateIdle) //p.setChamberState(ChamberStateIdle)
return return
} }

View file

@ -28,11 +28,19 @@ func NewDWIngest() *DWIngest {
} }
func (p *DWIngest) AddReading(reading temperature.TemperatureReading) { func (p *DWIngest) AddReading(reading temperature.TemperatureReading) {
p.chTemperatureReading <- reading // Non-blocking send
select {
case p.chTemperatureReading <- reading:
default:
}
} }
func (p *DWIngest) AddState(state controllers.ChamberState) { func (p *DWIngest) AddState(state controllers.ChamberState) {
p.chState <- state // Non-blocking send
select {
case p.chState <- state:
default:
}
} }
func (p *DWIngest) Run(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config *configuration.Configuration) { func (p *DWIngest) Run(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config *configuration.Configuration) {

View file

@ -3,7 +3,9 @@ package lcd
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"sync" "sync"
"time"
"git.joco.dk/sng/fermentord/internal/controllers" "git.joco.dk/sng/fermentord/internal/controllers"
"git.joco.dk/sng/fermentord/pkg/temperature" "git.joco.dk/sng/fermentord/pkg/temperature"
@ -21,6 +23,7 @@ type LCD struct {
chTemp chan temperature.TemperatureReading chTemp chan temperature.TemperatureReading
chState chan controllers.ChamberState chState chan controllers.ChamberState
chSetpoint chan float64 chSetpoint chan float64
lastUpdate time.Time
} }
func NewLCD() (*LCD, error) { func NewLCD() (*LCD, error) {
@ -30,6 +33,7 @@ func NewLCD() (*LCD, error) {
chTemp: make(chan temperature.TemperatureReading, 10), chTemp: make(chan temperature.TemperatureReading, 10),
chState: make(chan controllers.ChamberState, 10), chState: make(chan controllers.ChamberState, 10),
chSetpoint: make(chan float64, 10), chSetpoint: make(chan float64, 10),
lastUpdate: time.Now(),
} }
p.bus, err = i2c.NewI2C(0x27, 0) p.bus, err = i2c.NewI2C(0x27, 0)
@ -49,13 +53,13 @@ func NewLCD() (*LCD, error) {
return nil, err return nil, err
} }
err = p.lcd.ShowMessage("Fermentor", device.SHOW_LINE_1|device.SHOW_BLANK_PADDING) err = p.lcd.ShowMessage("Fermentor", device.SHOW_LINE_1)
if err != nil { if err != nil {
p.bus.Close() p.bus.Close()
return nil, err return nil, err
} }
err = p.lcd.ShowMessage("Initializing", device.SHOW_LINE_2|device.SHOW_BLANK_PADDING) err = p.lcd.ShowMessage("Initializing", device.SHOW_LINE_2 /*|device.SHOW_BLANK_PADDING*/)
if err != nil { if err != nil {
p.bus.Close() p.bus.Close()
return nil, err return nil, err
@ -66,6 +70,10 @@ func NewLCD() (*LCD, error) {
} }
func (p *LCD) Close() error { func (p *LCD) Close() error {
if err := p.lcd.BacklightOff(); err != nil {
log.Print(err)
}
return p.bus.Close() return p.bus.Close()
} }
@ -123,6 +131,10 @@ func (p *LCD) Run(ctx context.Context, wg *sync.WaitGroup) {
} }
func (p *LCD) update() error { func (p *LCD) update() error {
if time.Since(p.lastUpdate) < 3*time.Second {
return nil
}
l1 := fmt.Sprintf("W:%4.1f C:%4.1f %s", p.wort, p.chamber, p.state) l1 := fmt.Sprintf("W:%4.1f C:%4.1f %s", p.wort, p.chamber, p.state)
l2 := fmt.Sprintf("S:%4.1f A:%4.1f", p.ambient, p.setpoint) l2 := fmt.Sprintf("S:%4.1f A:%4.1f", p.ambient, p.setpoint)
@ -142,5 +154,7 @@ func (p *LCD) update() error {
p.l2 = l2 p.l2 = l2
} }
p.lastUpdate = time.Now()
return nil return nil
} }