Refactor temperature reading updates

This commit is contained in:
Søren Rasmussen 2022-07-19 11:24:39 +02:00
parent 13f6e361c1
commit 488508a431
2 changed files with 37 additions and 29 deletions

View file

@ -29,9 +29,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
defer wg.Done() defer wg.Done()
// Controller // Controller
chCtrlTemp := make(chan temperature.TemperatureReading, 10) ctrl := controllers.NewChamberController("Chamber 1", *config)
defer close(chCtrlTemp)
ctrl := controllers.NewChamberController("Chamber 1", *config, chCtrlTemp)
// NATS // NATS
ingest := dwingest.NewDWIngest() ingest := dwingest.NewDWIngest()
@ -58,10 +56,20 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
for { for {
select { select {
case reading := <-temperature.C: case reading := <-temperature.C:
chCtrlTemp <- reading ctrl.SetTemperature(reading)
ingest.AddReading(reading) ingest.AddReading(reading)
case state := <-ctrl.C: case state := <-ctrl.C:
gpioSetState(state, gpio)
ingest.AddState(state)
case <-ctx.Done():
return
}
}
}
func gpioSetState(state controllers.ChamberState, gpio *hw.Gpio) {
switch state { switch state {
case controllers.ChamberStateIdle: case controllers.ChamberStateIdle:
log.Printf("Setting chamber state idle") log.Printf("Setting chamber state idle")
@ -81,13 +89,6 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
gpio.StopCooler() gpio.StopCooler()
gpio.StartHeater() gpio.StartHeater()
} }
ingest.AddState(state)
case <-ctx.Done():
return
}
}
} }
func main() { func main() {

View file

@ -43,19 +43,19 @@ type ChamberController struct {
chamberTemperature float64 chamberTemperature float64
wortTemperature float64 wortTemperature float64
chTemp <-chan temperature.TemperatureReading chTemp chan temperature.TemperatureReading
pid *PIDController pid *PIDController
hub *sentry.Hub hub *sentry.Hub
} }
func NewChamberController(name string, config configuration.ControllerConfig, chTemp <-chan temperature.TemperatureReading) *ChamberController { func NewChamberController(name string, config configuration.ControllerConfig) *ChamberController {
return &ChamberController{ return &ChamberController{
C: make(chan ChamberState), C: make(chan ChamberState),
name: name, name: name,
config: config, config: config,
pid: NewPIDController(config.PID.Kp, config.PID.Ki, config.PID.Kd), pid: NewPIDController(config.PID.Kp, config.PID.Ki, config.PID.Kd),
chTemp: chTemp, chTemp: make(chan temperature.TemperatureReading),
chamberState: ChamberStateIdle, chamberState: ChamberStateIdle,
ConfigUpdates: make(chan configuration.ControllerConfig, 1), ConfigUpdates: make(chan configuration.ControllerConfig, 1),
hub: sentry.CurrentHub().Clone(), hub: sentry.CurrentHub().Clone(),
@ -75,7 +75,7 @@ func (p *ChamberController) Run(ctx context.Context, wg *sync.WaitGroup) {
p.setChamberState(state) p.setChamberState(state)
case temp := <-p.chTemp: case temp := <-p.chTemp:
p.setTemperature(temp) p.updateTemperature(temp)
case c := <-p.ConfigUpdates: case c := <-p.ConfigUpdates:
p.config = c p.config = c
@ -83,12 +83,19 @@ func (p *ChamberController) Run(ctx context.Context, wg *sync.WaitGroup) {
case <-ctx.Done(): case <-ctx.Done():
ticker.Stop() ticker.Stop()
p.setChamberState(ChamberStateIdle) p.setChamberState(ChamberStateIdle)
close(p.chTemp)
close(p.C)
return return
} }
} }
} }
func (p *ChamberController) setTemperature(t temperature.TemperatureReading) { func (p *ChamberController) SetTemperature(t temperature.TemperatureReading) {
p.chTemp <- t
}
func (p *ChamberController) updateTemperature(t temperature.TemperatureReading) {
switch t.Sensor { switch t.Sensor {
case p.config.Sensors.Ambient: case p.config.Sensors.Ambient:
p.ambientTemperature = t.Degrees p.ambientTemperature = t.Degrees