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

View file

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