From a86c3dc3b92a7eb1850280958c008edb1b8feff2 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Sun, 30 Sep 2018 09:00:20 -0600 Subject: [PATCH] Skeleton for web frontend --- main.go | 10 ++++++++++ run.go | 8 +++++++- web.go | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 web.go diff --git a/main.go b/main.go index 66f043d..e159522 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,9 @@ func main() { // run is tracking whether or not the thermostats should run run := true + // done is used to signal the web frontend to stop + done := make(chan bool) + sig := make(chan os.Signal) signal.Notify(sig, os.Interrupt, syscall.SIGTERM) signal.Notify(sig, os.Interrupt, syscall.SIGINT) @@ -43,6 +46,7 @@ func main() { go func() { <-sig run = false + done <- true }() sc := make(chan State) @@ -53,6 +57,12 @@ func main() { wg.Add(1) go RunThermostat(sensor, sc, &run, &wg) } + + // Launch the web frontend + wg.Add(1) + RunWeb(sc, done, &wg) + + // Wait for all threads to stop wg.Wait() // Close the GPIO access diff --git a/run.go b/run.go index a16c6c3..2342e59 100644 --- a/run.go +++ b/run.go @@ -107,7 +107,13 @@ func RunThermostat(sensor Sensor, sc chan<- State, run *bool, wg *sync.WaitGroup s.Temp = t log.Printf("%s Temp: %.2f, Cooling: %t, Heating: %t, Duration: %.1f", sensor.Alias, s.Temp, s.Cooling, s.Heating, min) - sc <- s + + select { + case sc <- s: + break + default: + break + } } PinSwitch(cpin, false, sensor.CoolInvert) diff --git a/web.go b/web.go new file mode 100644 index 0000000..489bd25 --- /dev/null +++ b/web.go @@ -0,0 +1,21 @@ +package main + +import ( + "sync" +) + +// RunWeb launches a web server +func RunWeb(sc <-chan State, done <-chan bool, wg *sync.WaitGroup) { + states := make(map[string]State) + + go func() { + for { + s := <-sc + states[s.ID] = s + } + }() + + <-done + + wg.Done() +}