spacepanel_aggregator/httppoll.go

53 lines
871 B
Go
Raw Normal View History

2020-01-30 23:24:19 +00:00
package spacepanel_aggregator
import (
2020-01-31 00:59:39 +00:00
"encoding/json"
2020-01-30 23:24:19 +00:00
"fmt"
2020-01-31 00:59:39 +00:00
"io/ioutil"
"net/http"
2020-01-30 23:24:19 +00:00
"time"
)
func Poll(url string) {
for true {
2020-01-31 00:59:39 +00:00
resp, err := http.Get(url)
if err != nil {
ErrorinPoll(url)
fmt.Println(err.Error())
} else {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
ErrorinPoll(url)
resp.Body.Close()
} else {
resp.Body.Close()
var parsed V13
err = json.Unmarshal(data, &parsed)
if err != nil {
ErrorinPoll(url)
} else {
if parsed.State.Open != nil {
open := parsed.State.Open.(bool)
if open {
spacestates[url] = Open
} else {
spacestates[url] = Close
}
} else {
spacestates[url] = Unknown
}
}
}
}
2020-01-30 23:24:19 +00:00
time.Sleep(sleeptime)
}
}
2020-01-31 00:59:39 +00:00
func ErrorinPoll(url string) {
if spacestates[url] != Unknown {
spacestates[url] = Outdated
}
}