59 lines
1.0 KiB
Go
59 lines
1.0 KiB
Go
package spacepanel_aggregator
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func Poll(url string, led int) {
|
|
for true {
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
ErrorPoll(url, err, led)
|
|
} else {
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
ErrorPoll(url, err, led)
|
|
_ = resp.Body.Close()
|
|
} else {
|
|
_ = resp.Body.Close()
|
|
|
|
var parsed V13
|
|
err = json.Unmarshal(data, &parsed)
|
|
if err != nil {
|
|
ErrorPoll(url, err, led)
|
|
} else {
|
|
if parsed.State.Open != nil {
|
|
open := parsed.State.Open.(bool)
|
|
if open {
|
|
SetSpaceState(url, Open)
|
|
} else {
|
|
SetSpaceState(url, Close)
|
|
}
|
|
} else {
|
|
SetSpaceState(url, Unknown)
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
time.Sleep(sleeptime)
|
|
}
|
|
}
|
|
|
|
func ErrorPoll(url string, err error, led int) {
|
|
if spacestates[url] != Unknown {
|
|
SetSpaceState(url, Outdated)
|
|
}
|
|
fmt.Println("LED", led, err.Error())
|
|
}
|
|
|
|
func SetSpaceState(url string, s State) {
|
|
lock.Lock()
|
|
spacestates[url] = s
|
|
lock.Unlock()
|
|
}
|