1
0
Fork 0
mirror of https://github.com/shouptech/tempgopher.git synced 2026-02-03 16:49:42 +00:00

Better handling of multiple thermostat threads

This commit is contained in:
Emma 2018-09-30 07:43:17 -06:00
parent 5d72e9d036
commit 3ba6775d9e
2 changed files with 11 additions and 10 deletions

10
main.go
View file

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

11
run.go
View file

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