2020-02-04 20:25:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-04 21:52:29 +00:00
|
|
|
func (s *StateAggregator) GetLedStates() []string {
|
|
|
|
var states []string
|
2020-02-04 20:25:10 +00:00
|
|
|
|
|
|
|
s.mtx.RLock()
|
2020-02-04 21:52:29 +00:00
|
|
|
for _, spaceList := range s.ledList {
|
|
|
|
states = append(states, ColorMap[GetBestStateFromList(spaceList)])
|
2020-02-04 20:25:10 +00:00
|
|
|
}
|
|
|
|
s.mtx.RUnlock()
|
|
|
|
|
|
|
|
return states
|
|
|
|
}
|