Refactor temperature reading updates
This commit is contained in:
parent
13f6e361c1
commit
488508a431
2 changed files with 37 additions and 29 deletions
|
@ -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,30 +56,11 @@ 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:
|
||||
switch state {
|
||||
case controllers.ChamberStateIdle:
|
||||
log.Printf("Setting chamber state idle")
|
||||
metrics.State.Set(metrics.MetricStateIdle)
|
||||
gpio.StopCooler()
|
||||
gpio.StopHeater()
|
||||
|
||||
case controllers.ChamberStateCooling:
|
||||
log.Printf("Setting chamber state cooling")
|
||||
metrics.State.Set(metrics.MetricStateCooling)
|
||||
gpio.StopHeater()
|
||||
gpio.StartCooler()
|
||||
|
||||
case controllers.ChamberStateHeating:
|
||||
log.Printf("Setting chamber state heating")
|
||||
metrics.State.Set(metrics.MetricStateHeating)
|
||||
gpio.StopCooler()
|
||||
gpio.StartHeater()
|
||||
}
|
||||
|
||||
gpioSetState(state, gpio)
|
||||
ingest.AddState(state)
|
||||
|
||||
case <-ctx.Done():
|
||||
|
@ -90,6 +69,28 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
|
|||
}
|
||||
}
|
||||
|
||||
func gpioSetState(state controllers.ChamberState, gpio *hw.Gpio) {
|
||||
switch state {
|
||||
case controllers.ChamberStateIdle:
|
||||
log.Printf("Setting chamber state idle")
|
||||
metrics.State.Set(metrics.MetricStateIdle)
|
||||
gpio.StopCooler()
|
||||
gpio.StopHeater()
|
||||
|
||||
case controllers.ChamberStateCooling:
|
||||
log.Printf("Setting chamber state cooling")
|
||||
metrics.State.Set(metrics.MetricStateCooling)
|
||||
gpio.StopHeater()
|
||||
gpio.StartCooler()
|
||||
|
||||
case controllers.ChamberStateHeating:
|
||||
log.Printf("Setting chamber state heating")
|
||||
metrics.State.Set(metrics.MetricStateHeating)
|
||||
gpio.StopCooler()
|
||||
gpio.StartHeater()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Sentry
|
||||
err := sentry.Init(sentry.ClientOptions{
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue