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

Catch interrupts

This commit is contained in:
Emma 2018-09-28 20:21:39 -06:00
parent 56b810ce83
commit 62a0e67ce8
2 changed files with 22 additions and 3 deletions

View file

@ -2,7 +2,10 @@ package main
import ( import (
"log" "log"
"os"
"os/signal"
"sync" "sync"
"syscall"
"github.com/alexflint/go-arg" "github.com/alexflint/go-arg"
"github.com/stianeikeland/go-rpio" "github.com/stianeikeland/go-rpio"
@ -31,10 +34,14 @@ func main() {
} }
// Launch the thermostat go routines // Launch the thermostat go routines
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
signal.Notify(sig, os.Interrupt, syscall.SIGINT)
var wg sync.WaitGroup var wg sync.WaitGroup
for _, sensor := range config.Sensors { for _, sensor := range config.Sensors {
wg.Add(1) wg.Add(1)
go RunThermostat(sensor) go RunThermostat(sensor, sig, &wg)
} }
wg.Wait() wg.Wait()

16
run.go
View file

@ -3,6 +3,8 @@ package main
import ( import (
"errors" "errors"
"log" "log"
"os"
"sync"
"time" "time"
"github.com/stianeikeland/go-rpio" "github.com/stianeikeland/go-rpio"
@ -61,7 +63,7 @@ func GetPinState(pin rpio.Pin, invert bool) bool {
} }
// RunThermostat monitors the temperature of the supplied sensor and does its best to keep it at the desired state. // RunThermostat monitors the temperature of the supplied sensor and does its best to keep it at the desired state.
func RunThermostat(sensor Sensor) { func RunThermostat(sensor Sensor, sig chan os.Signal, wg *sync.WaitGroup) {
var s State var s State
s.Changed = time.Now() s.Changed = time.Now()
@ -74,7 +76,13 @@ func RunThermostat(sensor Sensor) {
SetPinState(cpin, false, sensor.CoolInvert) SetPinState(cpin, false, sensor.CoolInvert)
SetPinState(hpin, false, sensor.HeatInvert) SetPinState(hpin, false, sensor.HeatInvert)
for { run := true
go func() {
<-sig
run = false
}()
for run {
t, err := ReadTemperature(sensor.ID) t, err := ReadTemperature(sensor.ID)
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
@ -115,4 +123,8 @@ func RunThermostat(sensor Sensor) {
//s.Heating = GetPinState(hpin, sensor.HeatInvert) //s.Heating = GetPinState(hpin, sensor.HeatInvert)
log.Printf("%s Temp: %.2f, Cooling: %t, Heating: %t, Duration: %.1f", sensor.Alias, s.Temp, s.Cooling, s.Heating, min) log.Printf("%s Temp: %.2f, Cooling: %t, Heating: %t, Duration: %.1f", sensor.Alias, s.Temp, s.Cooling, s.Heating, min)
} }
SetPinState(cpin, false, sensor.CoolInvert)
SetPinState(hpin, false, sensor.HeatInvert)
wg.Done()
} }