mirror of
https://github.com/shouptech/tempgopher.git
synced 2026-02-03 16:49:42 +00:00
Add status handler
This commit is contained in:
parent
b353b15004
commit
60c2f3c930
2 changed files with 25 additions and 8 deletions
|
|
@ -12,11 +12,11 @@ import (
|
||||||
|
|
||||||
// State represents the current state of the thermostat
|
// State represents the current state of the thermostat
|
||||||
type State struct {
|
type State struct {
|
||||||
ID string
|
Alias string `json:"alias"`
|
||||||
Temp float64
|
Temp float64 `json:"temp"`
|
||||||
Cooling bool
|
Cooling bool `json:"cooling"`
|
||||||
Heating bool
|
Heating bool `json:"heating"`
|
||||||
Changed time.Time
|
Changed time.Time `json:"changed"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadTemperature will return the current temperature (in degrees celsius) of a specific sensor.
|
// ReadTemperature will return the current temperature (in degrees celsius) of a specific sensor.
|
||||||
|
|
@ -54,7 +54,7 @@ func PinSwitch(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.
|
// RunThermostat monitors the temperature of the supplied sensor and does its best to keep it at the desired state.
|
||||||
func RunThermostat(sensor Sensor, sc chan<- State, run *bool, wg *sync.WaitGroup) {
|
func RunThermostat(sensor Sensor, sc chan<- State, run *bool, wg *sync.WaitGroup) {
|
||||||
var s State
|
var s State
|
||||||
s.ID = sensor.ID
|
s.Alias = sensor.Alias
|
||||||
s.Changed = time.Now()
|
s.Changed = time.Now()
|
||||||
|
|
||||||
cpin := rpio.Pin(sensor.CoolGPIO)
|
cpin := rpio.Pin(sensor.CoolGPIO)
|
||||||
|
|
@ -114,6 +114,7 @@ func RunThermostat(sensor Sensor, sc chan<- State, run *bool, wg *sync.WaitGroup
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("%s Shutting down thermostat", sensor.Alias)
|
||||||
PinSwitch(cpin, false, sensor.CoolInvert)
|
PinSwitch(cpin, false, sensor.CoolInvert)
|
||||||
PinSwitch(hpin, false, sensor.HeatInvert)
|
PinSwitch(hpin, false, sensor.HeatInvert)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
|
|
||||||
20
web.go
20
web.go
|
|
@ -10,11 +10,24 @@ 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
|
||||||
|
func StatusHandler(c *gin.Context) {
|
||||||
|
if c.Param("alias") == "/" {
|
||||||
|
c.JSON(http.StatusOK, states)
|
||||||
|
} else if val, ok := states[c.Param("alias")[1:]]; ok {
|
||||||
|
c.JSON(http.StatusOK, val)
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusNotFound, "Not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SetupRouter initializes the gin router.
|
// SetupRouter initializes the gin router.
|
||||||
func SetupRouter() *gin.Engine {
|
func SetupRouter() *gin.Engine {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
@ -24,17 +37,20 @@ func SetupRouter() *gin.Engine {
|
||||||
// Ping
|
// Ping
|
||||||
r.GET("/ping", PingHandler)
|
r.GET("/ping", PingHandler)
|
||||||
|
|
||||||
|
// Status
|
||||||
|
r.GET("/api/status/*alias", StatusHandler)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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, done <-chan bool, wg *sync.WaitGroup) {
|
func RunWeb(sc <-chan State, done <-chan bool, 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
|
||||||
states[s.ID] = s
|
states[s.Alias] = s
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue