forked from mamu/spacepanel_aggregator
77 lines
1.2 KiB
Go
77 lines
1.2 KiB
Go
package spacepanel_aggregator
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/valyala/fastjson"
|
|
)
|
|
|
|
func PollWorker(url string, led int) {
|
|
for {
|
|
err := Worker(url)
|
|
if err != nil {
|
|
SetSpaceState(url, Unknown)
|
|
fmt.Println("LED", led, "URL:", url, err.Error())
|
|
}
|
|
time.Sleep(sleeptime)
|
|
}
|
|
}
|
|
|
|
var (
|
|
insecureTransport = &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
}
|
|
|
|
client = &http.Client{
|
|
Transport: insecureTransport,
|
|
}
|
|
)
|
|
|
|
func Worker(url string) error {
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
value, err := fastjson.ParseBytes(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !value.Exists("state", "open") {
|
|
return errors.New("no space state existing")
|
|
}
|
|
|
|
state := value.GetBool("state", "open")
|
|
|
|
if state {
|
|
SetSpaceState(url, Open)
|
|
} else {
|
|
SetSpaceState(url, Close)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func SetSpaceState(url string, s State) {
|
|
lock.Lock()
|
|
spacestates[url] = s
|
|
lock.Unlock()
|
|
}
|