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
|
|
|
|
}
|
|
|
|
|
2020-02-04 21:58:26 +00:00
|
|
|
func NewStateAggregator(spaceList map[int][]string) *StateAggregator {
|
2020-02-04 20:25:10 +00:00
|
|
|
s := &StateAggregator{
|
|
|
|
ledList: make(map[int][]*Space),
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:25:20 +00:00
|
|
|
for i, spaceUrls := range spaceList {
|
2020-02-04 20:25:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-04 21:52:29 +00:00
|
|
|
func (s *StateAggregator) GetLedStates() []string {
|
2020-02-04 21:58:26 +00:00
|
|
|
states := make([]string, len(s.ledList))
|
2020-02-04 20:25:10 +00:00
|
|
|
|
|
|
|
s.mtx.RLock()
|
2020-02-04 21:58:26 +00:00
|
|
|
for i, spaceList := range s.ledList {
|
|
|
|
states[i] = ColorMap[GetBestStateFromList(spaceList)]
|
2020-02-04 20:25:10 +00:00
|
|
|
}
|
|
|
|
s.mtx.RUnlock()
|
|
|
|
|
|
|
|
return states
|
|
|
|
}
|