improved Logging

This commit is contained in:
Malte Münch 2020-01-31 15:42:04 +01:00
parent 88b94d10be
commit 94d03e1c2f
2 changed files with 7 additions and 7 deletions

View File

@ -8,16 +8,15 @@ import (
"time" "time"
) )
func Poll(url string) { func Poll(url string, led int) {
for true { for true {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
ErrorinPoll(url) ErrorPoll(url, err, led)
fmt.Println(err.Error())
} else { } else {
data, err := ioutil.ReadAll(resp.Body) data, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
ErrorinPoll(url) ErrorPoll(url, err, led)
_ = resp.Body.Close() _ = resp.Body.Close()
} else { } else {
_ = resp.Body.Close() _ = resp.Body.Close()
@ -25,7 +24,7 @@ func Poll(url string) {
var parsed V13 var parsed V13
err = json.Unmarshal(data, &parsed) err = json.Unmarshal(data, &parsed)
if err != nil { if err != nil {
ErrorinPoll(url) ErrorPoll(url, err, led)
} else { } else {
if parsed.State.Open != nil { if parsed.State.Open != nil {
open := parsed.State.Open.(bool) open := parsed.State.Open.(bool)
@ -45,10 +44,11 @@ func Poll(url string) {
} }
} }
func ErrorinPoll(url string) { func ErrorPoll(url string, err error, led int) {
if spacestates[url] != Unknown { if spacestates[url] != Unknown {
SetSpaceState(url, Outdated) SetSpaceState(url, Outdated)
} }
fmt.Println("LED", led, err.Error())
} }
func SetSpaceState(url string, s State) { func SetSpaceState(url string, s State) {

View File

@ -43,7 +43,7 @@ func Start() {
for i := 0; i < len(leds); i++ { for i := 0; i < len(leds); i++ {
for j := 0; j < len(leds[i]); j++ { for j := 0; j < len(leds[i]); j++ {
spacestates[leds[i][j]] = Unknown spacestates[leds[i][j]] = Unknown
go Poll(leds[i][j]) go Poll(leds[i][j], i)
} }
} }
fmt.Println("Loaded", len(leds), "LED-configs and", len(spacestates), "spaces.") fmt.Println("Loaded", len(leds), "LED-configs and", len(spacestates), "spaces.")