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

This commit is contained in:
Søren Rasmussen 2022-07-24 01:16:40 +02:00
parent c0dc0dabef
commit 56d33782c8
5 changed files with 30 additions and 12 deletions

View file

@ -44,6 +44,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
} }
viper.OnConfigChange(func(in fsnotify.Event) { viper.OnConfigChange(func(in fsnotify.Event) {
log.Print("Reloading configuration")
loadConfiguration() loadConfiguration()
}) })
loadConfiguration() loadConfiguration()
@ -78,12 +79,14 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream, config
switch ev { switch ev {
case hw.DoorClosed: case hw.DoorClosed:
log.Print("Door closed")
gpio.LightsOff() gpio.LightsOff()
gpio.StartFan() gpio.StartFan()
ctrl.Resume() ctrl.Resume()
evs = "DOOR_CLOSED" evs = "DOOR_CLOSED"
case hw.DoorOpened: case hw.DoorOpened:
log.Print("Door opened")
ctrl.Pause() ctrl.Pause()
gpio.LightsOn() gpio.LightsOn()
gpio.StopFan() gpio.StopFan()

View file

@ -89,11 +89,9 @@ func (p *ChamberController) Run(ctx context.Context, wg *sync.WaitGroup) {
case <-ctx.Done(): case <-ctx.Done():
ticker.Stop() ticker.Stop()
close(p.chPause)
p.isPaused = false p.isPaused = false
// TODO Check if the line below blocks on shutdown
p.setChamberState(ChamberStateIdle) p.setChamberState(ChamberStateIdle)
close(p.chTemp)
close(p.C)
return return
} }

View file

@ -39,7 +39,7 @@ type Gpio struct {
func NewGpio() (*Gpio, error) { func NewGpio() (*Gpio, error) {
var err error var err error
p := &Gpio{ p := &Gpio{
C: make(chan HWEvent), C: make(chan HWEvent, 1),
} }
p.chip, err = gpiod.NewChip("gpiochip0", gpiod.WithConsumer("fermentord")) p.chip, err = gpiod.NewChip("gpiochip0", gpiod.WithConsumer("fermentord"))
@ -59,6 +59,18 @@ func NewGpio() (*Gpio, error) {
return nil, err 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)) p.lightsPower, err = p.chip.RequestLine(pinLightsPower, gpiod.AsOutput(0))
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -48,7 +48,7 @@ var (
func init() { func init() {
Reading = make(chan TemperatureReading, 30) Reading = make(chan TemperatureReading, 30)
ConfigUpdate = make(chan configuration.Configuration) ConfigUpdate = make(chan configuration.Configuration, 1)
sensors = make([]Sensor, 0) sensors = make([]Sensor, 0)
} }
@ -122,7 +122,6 @@ func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.D
configure(c) configure(c)
case <-ctx.Done(): case <-ctx.Done():
close(Reading)
return return
} }
} }

View file

@ -12,24 +12,25 @@ var (
) )
func init() { 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) { func PollSensors(ctx context.Context, wg *sync.WaitGroup, interval time.Duration, scanDuration time.Duration) {
defer wg.Done()
if interval < scanDuration { if interval < scanDuration {
log.Fatal("Unable to use interval < scanDuration") log.Fatal("Unable to use interval < scanDuration")
} }
defer wg.Done()
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
for { for {
select { select {
case <-ctx.Done():
return
case <-ticker.C: case <-ticker.C:
scan(ctx, scanDuration) 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()) metricGravity.WithLabelValues(color).Set(t.Gravity())
metricTemp.WithLabelValues(color).Set(t.Celsius()) metricTemp.WithLabelValues(color).Set(t.Celsius())
C <- t select {
case C <- t:
// Message sent
default:
// No recipients on channel
}
} }
} }