This commit is contained in:
Malte Münch 2020-01-31 00:24:19 +01:00
parent cae862d8bb
commit 0b753454ee
3 changed files with 56 additions and 7 deletions

13
httppoll.go Normal file
View File

@ -0,0 +1,13 @@
package spacepanel_aggregator
import (
"fmt"
"time"
)
func Poll(url string) {
for true {
fmt.Println("Polling", url)
time.Sleep(sleeptime)
}
}

View File

@ -2,13 +2,18 @@ package spacepanel_aggregator
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
io "io/ioutil" io "io/ioutil"
"net/http"
"time"
) )
var listen = ":8080" var listen = ":8080"
var conffile = "conf.yml" var conffile = "conf.yml"
var sleeptime time.Duration = 60000000000 // nanoseconds
var leds [][]string var leds [][]string
var spacestates map[string]State
func SetConf(s string) { func SetConf(s string) {
conffile = s conffile = s
@ -30,14 +35,46 @@ func Start() {
if err != nil { if err != nil {
ce(err) 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++ { for i := 0; i < len(leds); i++ {
fmt.Println("-")
for j := 0; j < len(leds[i]); j++ { 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) { func ce(err error) {

View File

@ -3,7 +3,7 @@ package spacepanel_aggregator
type State int type State int
const ( const (
Open State = iota Open = State(iota)
Close Close
Outdated Outdated
Unknown Unknown
@ -11,6 +11,5 @@ const (
var colors = [4]string{"#00ff00", "#ff0000", "#0000ff", "#000000"} var colors = [4]string{"#00ff00", "#ff0000", "#0000ff", "#000000"}
type Config struct { //var colors = make(map[State]string)
Root [][]string //colors[Open] = "#00ff00"
}