spacepanel_aggregator/httppoll.go

59 lines
978 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)
2020-01-31 14:20:40 +00:00
_ = resp.Body.Close()
2020-01-31 00:59:39 +00:00
} else {
2020-01-31 14:20:40 +00:00
_ = resp.Body.Close()
2020-01-31 00:59:39 +00:00
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 {
2020-01-31 14:20:40 +00:00
SetSpaceState(url, Open)
2020-01-31 00:59:39 +00:00
} else {
2020-01-31 14:20:40 +00:00
SetSpaceState(url, Close)
2020-01-31 00:59:39 +00:00
}
} else {
2020-01-31 14:20:40 +00:00
SetSpaceState(url, Unknown)
2020-01-31 00:59:39 +00:00
}
}
}
}
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 {
2020-01-31 14:20:40 +00:00
SetSpaceState(url, Outdated)
2020-01-31 00:59:39 +00:00
}
}
2020-01-31 14:20:40 +00:00
func SetSpaceState(url string, s State) {
lock.Lock()
spacestates[url] = s
lock.Unlock()
}