forked from mamu/spacepanel_aggregator
polling
This commit is contained in:
parent
cae862d8bb
commit
0b753454ee
|
@ -0,0 +1,13 @@
|
|||
package spacepanel_aggregator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Poll(url string) {
|
||||
for true {
|
||||
fmt.Println("Polling", url)
|
||||
time.Sleep(sleeptime)
|
||||
}
|
||||
}
|
43
setup.go
43
setup.go
|
@ -2,13 +2,18 @@ package spacepanel_aggregator
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
io "io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var listen = ":8080"
|
||||
var conffile = "conf.yml"
|
||||
var sleeptime time.Duration = 60000000000 // nanoseconds
|
||||
var leds [][]string
|
||||
var spacestates map[string]State
|
||||
|
||||
func SetConf(s string) {
|
||||
conffile = s
|
||||
|
@ -30,14 +35,46 @@ func Start() {
|
|||
|
||||
if err != nil {
|
||||
ce(err)
|
||||
panic("An error occured while parsing the conffile, quitting...")
|
||||
}
|
||||
fmt.Println("So, lief anscheinend durch, schüss")
|
||||
spacestates = make(map[string]State)
|
||||
for i := 0; i < len(leds); i++ {
|
||||
fmt.Println("-")
|
||||
for j := 0; j < len(leds[i]); j++ {
|
||||
fmt.Println(" -", leds[i][j])
|
||||
spacestates[leds[i][j]] = Unknown
|
||||
go Poll(leds[i][j])
|
||||
}
|
||||
}
|
||||
fmt.Println("Loaded", len(leds), "LED-configs and", len(spacestates), "spaces.")
|
||||
|
||||
r := setupRouter()
|
||||
_ = r.Run(listen)
|
||||
}
|
||||
|
||||
func getBestState(states []string) State {
|
||||
returner := Unknown
|
||||
for i := 0; i < len(states); i++ {
|
||||
if spacestates[states[i]] < returner {
|
||||
returner = spacestates[states[i]]
|
||||
}
|
||||
}
|
||||
return returner
|
||||
}
|
||||
|
||||
func getLedStates(c *gin.Context) {
|
||||
returner := make([]string, len(leds))
|
||||
for i := 0; i < len(leds); i++ {
|
||||
returner[i] = colors[getBestState(leds[i])]
|
||||
}
|
||||
c.JSON(http.StatusOK, returner)
|
||||
}
|
||||
|
||||
func setupRouter() *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.Default()
|
||||
// Ping test
|
||||
//r.GET("/leds", getLedStates())
|
||||
r.GET("/leds", getLedStates)
|
||||
return r
|
||||
}
|
||||
|
||||
func ce(err error) {
|
||||
|
|
7
types.go
7
types.go
|
@ -3,7 +3,7 @@ package spacepanel_aggregator
|
|||
type State int
|
||||
|
||||
const (
|
||||
Open State = iota
|
||||
Open = State(iota)
|
||||
Close
|
||||
Outdated
|
||||
Unknown
|
||||
|
@ -11,6 +11,5 @@ const (
|
|||
|
||||
var colors = [4]string{"#00ff00", "#ff0000", "#0000ff", "#000000"}
|
||||
|
||||
type Config struct {
|
||||
Root [][]string
|
||||
}
|
||||
//var colors = make(map[State]string)
|
||||
//colors[Open] = "#00ff00"
|
||||
|
|
Loading…
Reference in New Issue