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) {
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()

View file

@ -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
}

View file

@ -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

View file

@ -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
}
}

View file

@ -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
}
}
}