2020-01-30 23:24:19 +00:00
|
|
|
package spacepanel_aggregator
|
|
|
|
|
|
|
|
import (
|
2020-02-04 18:32:54 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
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"
|
2020-02-04 18:32:54 +00:00
|
|
|
|
|
|
|
"github.com/valyala/fastjson"
|
2020-01-30 23:24:19 +00:00
|
|
|
)
|
|
|
|
|
2020-02-04 18:32:54 +00:00
|
|
|
func PollWorker(url string, led int) {
|
|
|
|
for {
|
|
|
|
err := Worker(url)
|
2020-01-31 00:59:39 +00:00
|
|
|
if err != nil {
|
2020-02-04 18:32:54 +00:00
|
|
|
SetSpaceState(url, Unknown)
|
|
|
|
fmt.Println("LED", led, "URL:", url, err.Error())
|
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
|
|
|
|
2020-02-04 18:32:54 +00:00
|
|
|
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")
|
2020-01-31 00:59:39 +00:00
|
|
|
}
|
2020-02-04 18:32:54 +00:00
|
|
|
|
|
|
|
state := value.GetBool("state", "open")
|
|
|
|
|
|
|
|
if state {
|
|
|
|
SetSpaceState(url, Open)
|
|
|
|
} else {
|
|
|
|
SetSpaceState(url, Close)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
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()
|
|
|
|
}
|