diff --git a/main.go b/main.go index 9f5bcd5..434e689 100644 --- a/main.go +++ b/main.go @@ -33,15 +33,23 @@ func main() { log.Fatal(err) } + // run is tracking whether or not the thermostats should run + run := true + // Launch the thermostat go routines sig := make(chan os.Signal) signal.Notify(sig, os.Interrupt, syscall.SIGTERM) signal.Notify(sig, os.Interrupt, syscall.SIGINT) + go func() { + <-sig + run = false + }() + var wg sync.WaitGroup for _, sensor := range config.Sensors { wg.Add(1) - go RunThermostat(sensor, sig, &wg) + go RunThermostat(sensor, &run, &wg) } wg.Wait() diff --git a/run.go b/run.go index dd028cd..76d36dd 100644 --- a/run.go +++ b/run.go @@ -3,7 +3,6 @@ package main import ( "errors" "log" - "os" "sync" "time" @@ -52,7 +51,7 @@ func SetPinState(pin rpio.Pin, on bool, invert bool) { } // RunThermostat monitors the temperature of the supplied sensor and does its best to keep it at the desired state. -func RunThermostat(sensor Sensor, sig <-chan os.Signal, wg *sync.WaitGroup) { +func RunThermostat(sensor Sensor, run *bool, wg *sync.WaitGroup) { var s State s.Changed = time.Now() @@ -65,13 +64,7 @@ func RunThermostat(sensor Sensor, sig <-chan os.Signal, wg *sync.WaitGroup) { SetPinState(cpin, false, sensor.CoolInvert) SetPinState(hpin, false, sensor.HeatInvert) - run := true - go func() { - <-sig - run = false - }() - - for run { + for *run { t, err := ReadTemperature(sensor.ID) if err != nil { log.Panicln(err)