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

Better handling of the current state of things

This commit is contained in:
Emma 2018-10-02 08:47:19 -06:00
parent f8765783c4
commit 5b27891ca1
2 changed files with 24 additions and 15 deletions

View file

@ -63,6 +63,8 @@ func ProcessSensor(sensor Sensor, state State) (State, error) {
log.Panicln(err) log.Panicln(err)
} }
state.When = time.Now()
// Initialize the pins // Initialize the pins
cpin := rpio.Pin(sensor.CoolGPIO) cpin := rpio.Pin(sensor.CoolGPIO)
cpin.Output() cpin.Output()
@ -72,6 +74,11 @@ func ProcessSensor(sensor Sensor, state State) (State, error) {
// Calculate duration // Calculate duration
duration := time.Since(state.Changed).Minutes() duration := time.Since(state.Changed).Minutes()
// When things reach the right temperature, set the duration to the future
// TODO: Better handling of this. Changed should maintain when the state changed.
// Probably need a new flag in the State struct.
future := time.Now().AddDate(10, 0, 0)
switch { switch {
case temp > sensor.HighTemp && temp < sensor.LowTemp: case temp > sensor.HighTemp && temp < sensor.LowTemp:
log.Println("Invalid state! Temperature is too high AND too low!") log.Println("Invalid state! Temperature is too high AND too low!")
@ -79,7 +86,7 @@ func ProcessSensor(sensor Sensor, state State) (State, error) {
case temp > sensor.LowTemp && state.Heating: case temp > sensor.LowTemp && state.Heating:
PinSwitch(hpin, false, sensor.HeatInvert) PinSwitch(hpin, false, sensor.HeatInvert)
state.Heating = false state.Heating = false
state.Changed = time.Unix(1<<63-62135596801, 999999999) state.Changed = future
// Temperature is too high and the cooling switch is still on, do nothing // Temperature is too high and the cooling switch is still on, do nothing
case temp > sensor.HighTemp && state.Cooling: case temp > sensor.HighTemp && state.Cooling:
break break
@ -95,7 +102,7 @@ func ProcessSensor(sensor Sensor, state State) (State, error) {
case temp < sensor.HighTemp && state.Cooling: case temp < sensor.HighTemp && state.Cooling:
PinSwitch(cpin, false, sensor.CoolInvert) PinSwitch(cpin, false, sensor.CoolInvert)
state.Cooling = false state.Cooling = false
state.Changed = time.Unix(1<<63-62135596801, 999999999) state.Changed = future
// Temperature is too low and the heating switch is on, do nothing // Temperature is too low and the heating switch is on, do nothing
case temp < sensor.LowTemp && state.Heating: case temp < sensor.LowTemp && state.Heating:
break break

18
web.go
View file

@ -13,26 +13,28 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
var states map[string]State
// PingHandler responds to get requests with the message "pong". // PingHandler responds to get requests with the message "pong".
func PingHandler(c *gin.Context) { func PingHandler(c *gin.Context) {
c.String(http.StatusOK, "pong") c.String(http.StatusOK, "pong")
} }
// StatusHandler responds to get requests with the current status of a sensor // StatusHandler responds to get requests with the current status of a sensor
func StatusHandler(c *gin.Context) { func StatusHandler(states *map[string]State) gin.HandlerFunc {
fn := func(c *gin.Context) {
if c.Param("alias") == "/" { if c.Param("alias") == "/" {
c.JSON(http.StatusOK, states) c.JSON(http.StatusOK, states)
} else if val, ok := states[c.Param("alias")[1:]]; ok { } else if val, ok := (*states)[c.Param("alias")[1:]]; ok {
c.JSON(http.StatusOK, val) c.JSON(http.StatusOK, val)
} else { } else {
c.JSON(http.StatusNotFound, "Not found") c.JSON(http.StatusNotFound, "Not found")
} }
} }
return gin.HandlerFunc(fn)
}
// SetupRouter initializes the gin router. // SetupRouter initializes the gin router.
func SetupRouter() *gin.Engine { func SetupRouter(states *map[string]State) *gin.Engine {
r := gin.Default() r := gin.Default()
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
@ -41,7 +43,7 @@ func SetupRouter() *gin.Engine {
r.GET("/ping", PingHandler) r.GET("/ping", PingHandler)
// Status // Status
r.GET("/api/status/*alias", StatusHandler) r.GET("/api/status/*alias", StatusHandler(states))
return r return r
} }
@ -49,7 +51,7 @@ func SetupRouter() *gin.Engine {
// RunWeb launches a web server. sc is used to update the states from the Thermostats. // RunWeb launches a web server. sc is used to update the states from the Thermostats.
func RunWeb(sc <-chan State, wg *sync.WaitGroup) { func RunWeb(sc <-chan State, wg *sync.WaitGroup) {
// Update sensor states when a new state comes back from the thermostat. // Update sensor states when a new state comes back from the thermostat.
states = make(map[string]State) states := make(map[string]State)
go func() { go func() {
for { for {
s := <-sc s := <-sc
@ -58,7 +60,7 @@ func RunWeb(sc <-chan State, wg *sync.WaitGroup) {
}() }()
// Launch the web server // Launch the web server
r := SetupRouter() r := SetupRouter(&states)
srv := &http.Server{ srv := &http.Server{
Addr: ":8080", Addr: ":8080",
Handler: r, Handler: r,