package spacepanel_aggregator import ( "fmt" "github.com/gin-gonic/gin" yaml "gopkg.in/yaml.v2" io "io/ioutil" "net/http" "sync" "time" ) var listen = ":8080" var conffile = "conf.yml" var sleeptime time.Duration = 60000000000 // nanoseconds var leds [][]string var spacestates map[string]State var lock = sync.RWMutex{} func SetConf(s string) { conffile = s } func SetIf(s string) { listen = s } func Start() { fmt.Println("Welcome to Spacepanel Aggregator!\n") fmt.Println("Listen Interface: ", listen) fmt.Println("Config-File: ", conffile) bytes, err := io.ReadFile(conffile) if err != nil { ce(err) } err = yaml.Unmarshal(bytes, &leds) if err != nil { ce(err) panic("An error occured while parsing the conffile, quitting...") } spacestates = make(map[string]State) for i := 0; i < len(leds); i++ { for j := 0; j < len(leds[i]); j++ { spacestates[leds[i][j]] = Unknown go Poll(leds[i][j], i) } } 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) { if err != nil { fmt.Println("ERROR: ", err.Error()) } }