diff --git a/cmd/fermentord/loop.go b/cmd/fermentord/loop.go index 706893d..2ee1397 100644 --- a/cmd/fermentord/loop.go +++ b/cmd/fermentord/loop.go @@ -44,6 +44,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config } viper.OnConfigChange(func(in fsnotify.Event) { + log.Print("Reloading configuration") loadConfiguration() }) loadConfiguration() @@ -78,12 +79,14 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config switch ev { case hw.DoorClosed: + log.Print("Door closed") gpio.LightsOff() gpio.StartFan() ctrl.Resume() evs = "DOOR_CLOSED" case hw.DoorOpened: + log.Print("Door opened") ctrl.Pause() gpio.LightsOn() gpio.StopFan() diff --git a/internal/controllers/chamber.go b/internal/controllers/chamber.go index bdc57e1..820421d 100644 --- a/internal/controllers/chamber.go +++ b/internal/controllers/chamber.go @@ -89,11 +89,9 @@ func (p *ChamberController) Run(ctx context.Context, wg *sync.WaitGroup) { case <-ctx.Done(): ticker.Stop() - close(p.chPause) p.isPaused = false + // TODO Check if the line below blocks on shutdown p.setChamberState(ChamberStateIdle) - close(p.chTemp) - close(p.C) return } diff --git a/internal/hw/gpio.go b/internal/hw/gpio.go index be2d6a1..362bfe6 100644 --- a/internal/hw/gpio.go +++ b/internal/hw/gpio.go @@ -39,7 +39,7 @@ type Gpio struct { func NewGpio() (*Gpio, error) { var err error p := &Gpio{ - C: make(chan HWEvent), + C: make(chan HWEvent, 1), } p.chip, err = gpiod.NewChip("gpiochip0", gpiod.WithConsumer("fermentord")) @@ -59,6 +59,18 @@ func NewGpio() (*Gpio, error) { return nil, err } + // Add an initial door reading to the channel to ensure correct state when booting + isDoorOpen, err := p.doorOpen.Value() + if err != nil { + return nil, err + } + switch isDoorOpen { + case 0: // Line is pulled low when door is open (circuit is closed) + p.C <- DoorOpened + default: // Pull-up resistor sets line high when door is closed (open circuit) + p.C <- DoorClosed + } + p.lightsPower, err = p.chip.RequestLine(pinLightsPower, gpiod.AsOutput(0)) if err != nil { return nil, err diff --git a/pkg/temperature/ds18b20.go b/pkg/temperature/ds18b20.go index 8fc7ee1..404e140 100644 --- a/pkg/temperature/ds18b20.go +++ b/pkg/temperature/ds18b20.go @@ -48,7 +48,7 @@ var ( func init() { Reading = make(chan TemperatureReading, 30) - ConfigUpdate = make(chan configuration.Configuration) + ConfigUpdate = make(chan configuration.Configuration, 1) sensors = make([]Sensor, 0) } @@ -122,7 +122,6 @@ func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.D configure(c) case <-ctx.Done(): - close(Reading) return } } diff --git a/pkg/tilt/main.go b/pkg/tilt/main.go index 92f2541..97c08e0 100644 --- a/pkg/tilt/main.go +++ b/pkg/tilt/main.go @@ -12,24 +12,25 @@ var ( ) func init() { - C = make(chan Tilt, 10) + C = make(chan Tilt, 100) } func PollSensors(ctx context.Context, wg *sync.WaitGroup, interval time.Duration, scanDuration time.Duration) { + defer wg.Done() + if interval < scanDuration { log.Fatal("Unable to use interval < scanDuration") } - defer wg.Done() ticker := time.NewTicker(interval) for { select { - case <-ctx.Done(): - return - case <-ticker.C: scan(ctx, scanDuration) + + case <-ctx.Done(): + return } } } @@ -43,6 +44,11 @@ func scan(ctx context.Context, timeout time.Duration) { metricGravity.WithLabelValues(color).Set(t.Gravity()) metricTemp.WithLabelValues(color).Set(t.Celsius()) - C <- t + select { + case C <- t: + // Message sent + default: + // No recipients on channel + } } }