spacepanel_aggregator/spaceAggregator.go

74 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[int][]string) *StateAggregator {
s := &StateAggregator{
ledList: make(map[int][]*Space),
}
for i, spaceUrls := range spaceList {
for _, url := range spaceUrls {
space := &Space{
URL: url,
}
go StartPollWorker(space)
s.ledList[i] = append(s.ledList[i], space)
}
}
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() []string {
states := make([]string, len(s.ledList))
s.mtx.RLock()
for i, spaceList := range s.ledList {
states[i] = ColorMap[GetBestStateFromList(spaceList)]
}
s.mtx.RUnlock()
return states
}