ccchatbot/schalter/state.go

39 lines
910 B
Go
Raw Permalink Normal View History

2019-08-06 21:05:15 +02:00
package schalter
2019-08-07 00:08:11 +02:00
import (
"fmt"
"dev.sum7.eu/genofire/golang-lib/file"
"dev.sum7.eu/genofire/golang-lib/http"
"github.com/bdlm/log"
)
2019-08-06 21:05:15 +02:00
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 {
2019-08-07 00:08:11 +02:00
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
2019-08-06 21:05:15 +02:00
}