39 lines
910 B
Go
39 lines
910 B
Go
package schalter
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/file"
|
|
"dev.sum7.eu/genofire/golang-lib/http"
|
|
"github.com/bdlm/log"
|
|
)
|
|
|
|
func (s *Schalter) stateString() string {
|
|
if s.state {
|
|
return fmt.Sprintf("%s is open", s.spaceName)
|
|
}
|
|
return fmt.Sprintf("%s is closed", s.spaceName)
|
|
}
|
|
func (s *Schalter) fetchState() bool {
|
|
if s.Path != "" && s.URL != "" {
|
|
log.Panic("it is not possible to set path and url to ")
|
|
}
|
|
api := SpaceAPI{}
|
|
if s.Path != "" {
|
|
if err := file.ReadJSON(s.Path, &api); err != nil {
|
|
log.WithField("path", s.Path).Errorf("unable to read spaceapi from file: %s", err)
|
|
return false
|
|
}
|
|
} else {
|
|
if err := http.JSONRequest(s.URL, &api); err != nil {
|
|
log.WithField("url", s.URL).Errorf("unable to http request spaceapi: %s", err)
|
|
return false
|
|
}
|
|
}
|
|
|
|
changed := api.State.Open != s.state
|
|
s.state = api.State.Open
|
|
s.spaceName = api.Space
|
|
return changed
|
|
}
|