mirror of
https://github.com/shouptech/tempgopher.git
synced 2026-02-03 16:49:42 +00:00
Add config endpoint
This commit is contained in:
parent
5b27891ca1
commit
9ebdf41400
3 changed files with 60 additions and 17 deletions
22
config.go
22
config.go
|
|
@ -8,17 +8,17 @@ import (
|
||||||
|
|
||||||
// Sensor defines configuration for a temperature sensor.
|
// Sensor defines configuration for a temperature sensor.
|
||||||
type Sensor struct {
|
type Sensor struct {
|
||||||
ID string `yaml:"id"`
|
ID string `json:"id" yaml:"id"`
|
||||||
Alias string `yaml:"alias"`
|
Alias string `json:"alias" yaml:"alias"`
|
||||||
HighTemp float64 `yaml:"hightemp"`
|
HighTemp float64 `json:"hightemp" yaml:"hightemp"`
|
||||||
LowTemp float64 `yaml:"lowtemp"`
|
LowTemp float64 `json:"lowtemp" yaml:"lowtemp"`
|
||||||
HeatGPIO int32 `yaml:"heatgpio"`
|
HeatGPIO int32 `json:"heatgpio" yaml:"heatgpio"`
|
||||||
HeatInvert bool `yaml:"heatinvert"`
|
HeatInvert bool `json:"heatinvert" yaml:"heatinvert"`
|
||||||
HeatMinutes float64 `yaml:"heatminutes"`
|
HeatMinutes float64 `json:"heatminutes" yaml:"heatminutes"`
|
||||||
CoolGPIO int32 `yaml:"coolgpio"`
|
CoolGPIO int32 `json:"coolgpio" yaml:"coolgpio"`
|
||||||
CoolInvert bool `yaml:"coolinvert"`
|
CoolInvert bool `json:"coolinvert" yaml:"coolinvert"`
|
||||||
CoolMinutes float64 `yaml:"coolminutes"`
|
CoolMinutes float64 `json:"coolminutes" yaml:"coolminutes"`
|
||||||
Verbose bool `yaml:"verbose"`
|
Verbose bool `json:"verbose" yaml:"verbose"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config contains the applications configuration
|
// Config contains the applications configuration
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -30,7 +30,7 @@ func main() {
|
||||||
|
|
||||||
// Launch the web frontend
|
// Launch the web frontend
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
RunWeb(sc, &wg)
|
RunWeb(args.ConfigFile, sc, &wg)
|
||||||
|
|
||||||
// Wait for all threads to stop
|
// Wait for all threads to stop
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
|
||||||
53
web.go
53
web.go
|
|
@ -18,15 +18,37 @@ func PingHandler(c *gin.Context) {
|
||||||
c.String(http.StatusOK, "pong")
|
c.String(http.StatusOK, "pong")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConfigHandler responds to get requests with the current configuration
|
||||||
|
func ConfigHandler(config *Config) gin.HandlerFunc {
|
||||||
|
fn := func(c *gin.Context) {
|
||||||
|
if c.Param("alias") != "/" && c.Param("alias") != "" {
|
||||||
|
alias := c.Param("alias")[1:]
|
||||||
|
found := false
|
||||||
|
for _, v := range config.Sensors {
|
||||||
|
if v.ID == alias {
|
||||||
|
c.JSON(http.StatusOK, v)
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
c.String(http.StatusNotFound, "Not found")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, *config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return gin.HandlerFunc(fn)
|
||||||
|
}
|
||||||
|
|
||||||
// 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(states *map[string]State) gin.HandlerFunc {
|
func StatusHandler(states *map[string]State) gin.HandlerFunc {
|
||||||
fn := func(c *gin.Context) {
|
fn := func(c *gin.Context) {
|
||||||
if c.Param("alias") == "/" {
|
if c.Param("alias") == "/" || 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.String(http.StatusNotFound, "Not found")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,7 +56,7 @@ func StatusHandler(states *map[string]State) gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupRouter initializes the gin router.
|
// SetupRouter initializes the gin router.
|
||||||
func SetupRouter(states *map[string]State) *gin.Engine {
|
func SetupRouter(config *Config, states *map[string]State) *gin.Engine {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
|
@ -43,13 +65,18 @@ func SetupRouter(states *map[string]State) *gin.Engine {
|
||||||
r.GET("/ping", PingHandler)
|
r.GET("/ping", PingHandler)
|
||||||
|
|
||||||
// Status
|
// Status
|
||||||
|
r.GET("/api/status", StatusHandler(states))
|
||||||
r.GET("/api/status/*alias", StatusHandler(states))
|
r.GET("/api/status/*alias", StatusHandler(states))
|
||||||
|
|
||||||
|
// Config
|
||||||
|
r.GET("/api/config", ConfigHandler(config))
|
||||||
|
r.GET("/api/config/*alias", ConfigHandler(config))
|
||||||
|
|
||||||
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, wg *sync.WaitGroup) {
|
func RunWeb(configpath string, 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() {
|
||||||
|
|
@ -59,8 +86,24 @@ func RunWeb(sc <-chan State, wg *sync.WaitGroup) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
config, err := LoadConfig(configpath)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
hup := make(chan os.Signal)
|
||||||
|
signal.Notify(hup, os.Interrupt, syscall.SIGHUP)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
<-hup
|
||||||
|
config, err = LoadConfig(configpath)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Launch the web server
|
// Launch the web server
|
||||||
r := SetupRouter(&states)
|
r := SetupRouter(config, &states)
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: ":8080",
|
Addr: ":8080",
|
||||||
Handler: r,
|
Handler: r,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue