From 0b753454eea0e1434f7af040ee94ec22eae4e8db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20M=C3=BCnch?= Date: Fri, 31 Jan 2020 00:24:19 +0100 Subject: [PATCH] polling --- httppoll.go | 13 +++++++++++++ setup.go | 43 ++++++++++++++++++++++++++++++++++++++++--- types.go | 7 +++---- 3 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 httppoll.go diff --git a/httppoll.go b/httppoll.go new file mode 100644 index 0000000..0203e00 --- /dev/null +++ b/httppoll.go @@ -0,0 +1,13 @@ +package spacepanel_aggregator + +import ( + "fmt" + "time" +) + +func Poll(url string) { + for true { + fmt.Println("Polling", url) + time.Sleep(sleeptime) + } +} diff --git a/setup.go b/setup.go index 9b59175..c1ce58a 100644 --- a/setup.go +++ b/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) { diff --git a/types.go b/types.go index a705daa..952f218 100644 --- a/types.go +++ b/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"