spacepanel_aggregator/spaceAggregator.go

76 lines
1.1 KiB
Go

package main
import (
"sync"
)
type State int
const (
Unknown = State(iota)
Outdated
Close
Open
)
var ColorMap = map[State]string{
Unknown: "#000000",
Outdated: "#0000ff",
Close: "#ff0000",
Open: "#00ff00",
}
type Space struct {
State State
URL string
}
type StateAggregator struct {
ledList map[int][]*Space
mtx sync.RWMutex
}
func NewStateAggregator(spaceList map[string][]string) *StateAggregator {
s := &StateAggregator{
ledList: make(map[int][]*Space),
}
i := 0
for _, spaceUrls := range spaceList {
for _, url := range spaceUrls {
space := &Space{
URL: url,
}
go StartPollWorker(space)
s.ledList[i] = append(s.ledList[i], space)
}
i++
}
return s
}
func GetBestStateFromList(spaces []*Space) State {
state := Unknown
for _, space := range spaces {
if space.State > state {
state = space.State
}
}
return state
}
func (s *StateAggregator) GetLedStates() map[int]string {
states := make(map[int]string)
s.mtx.RLock()
for i, spaceList := range s.ledList {
states[i] = ColorMap[GetBestStateFromList(spaceList)]
}
s.mtx.RUnlock()
return states
}