From 3ac7d60864a8e35860c85d082bd37f187ff92e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20M=C3=BCnch?= Date: Fri, 31 Jan 2020 01:59:39 +0100 Subject: [PATCH] polling --- httppoll.go | 41 +++++++++- types.go | 228 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 266 insertions(+), 3 deletions(-) diff --git a/httppoll.go b/httppoll.go index 0203e00..51ce254 100644 --- a/httppoll.go +++ b/httppoll.go @@ -1,13 +1,52 @@ package spacepanel_aggregator import ( + "encoding/json" "fmt" + "io/ioutil" + "net/http" "time" ) func Poll(url string) { for true { - fmt.Println("Polling", url) + resp, err := http.Get(url) + if err != nil { + ErrorinPoll(url) + fmt.Println(err.Error()) + } else { + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + ErrorinPoll(url) + resp.Body.Close() + } else { + resp.Body.Close() + + var parsed V13 + err = json.Unmarshal(data, &parsed) + if err != nil { + ErrorinPoll(url) + } else { + if parsed.State.Open != nil { + open := parsed.State.Open.(bool) + if open { + spacestates[url] = Open + } else { + spacestates[url] = Close + } + } else { + spacestates[url] = Unknown + } + + } + } + } time.Sleep(sleeptime) } } + +func ErrorinPoll(url string) { + if spacestates[url] != Unknown { + spacestates[url] = Outdated + } +} diff --git a/types.go b/types.go index 952f218..9d01e67 100644 --- a/types.go +++ b/types.go @@ -11,5 +11,229 @@ const ( var colors = [4]string{"#00ff00", "#ff0000", "#0000ff", "#000000"} -//var colors = make(map[State]string) -//colors[Open] = "#00ff00" +type V13 struct { + API string `json:"api"` + Cache struct { + Schedule string `json:"schedule"` + } `json:"cache"` + Cam []string `json:"cam"` + Contact struct { + Email string `json:"email"` + Facebook string `json:"facebook"` + Foursquare string `json:"foursquare"` + Google struct { + Plus string `json:"plus"` + } `json:"google"` + Identica string `json:"identica"` + Irc string `json:"irc"` + IssueMail string `json:"issue_mail"` + Jabber string `json:"jabber"` + Keymasters []struct { + Email string `json:"email"` + IrcNick string `json:"irc_nick"` + Name string `json:"name"` + Phone string `json:"phone"` + Twitter string `json:"twitter"` + } `json:"keymasters"` + Ml string `json:"ml"` + Phone string `json:"phone"` + Sip string `json:"sip"` + Twitter string `json:"twitter"` + } `json:"contact"` + Events []struct { + Extra string `json:"extra"` + Name string `json:"name"` + Timestamp float64 `json:"timestamp"` + Type string `json:"type"` + } `json:"events"` + Feeds struct { + Blog struct { + Type string `json:"type"` + URL string `json:"url"` + } `json:"blog"` + Calendar struct { + Type string `json:"type"` + URL string `json:"url"` + } `json:"calendar"` + Flickr struct { + Type string `json:"type"` + URL string `json:"url"` + } `json:"flickr"` + Wiki struct { + Type string `json:"type"` + URL string `json:"url"` + } `json:"wiki"` + } `json:"feeds"` + IssueReportChannels []string `json:"issue_report_channels"` + Location struct { + Address string `json:"address"` + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` + } `json:"location"` + Logo string `json:"logo"` + Projects []string `json:"projects"` + RadioShow []struct { + End string `json:"end"` + Name string `json:"name"` + Start string `json:"start"` + Type string `json:"type"` + URL string `json:"url"` + } `json:"radio_show"` + Sensors struct { + AccountBalance []struct { + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"account_balance"` + Barometer []struct { + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"barometer"` + BeverageSupply []struct { + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"beverage_supply"` + DoorLocked []struct { + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Value bool `json:"value"` + } `json:"door_locked"` + Humidity []struct { + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"humidity"` + NetworkConnections []struct { + Description string `json:"description"` + Location string `json:"location"` + Machines []struct { + Mac string `json:"mac"` + Name string `json:"name"` + } `json:"machines"` + Name string `json:"name"` + Type string `json:"type"` + Value float64 `json:"value"` + } `json:"network_connections"` + PeopleNowPresent []struct { + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Names []string `json:"names"` + Value float64 `json:"value"` + } `json:"people_now_present"` + PowerConsumption []struct { + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"power_consumption"` + Radiation struct { + Alpha []struct { + ConversionFactor float64 `json:"conversion_factor"` + DeadTime float64 `json:"dead_time"` + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"alpha"` + Beta []struct { + ConversionFactor float64 `json:"conversion_factor"` + DeadTime float64 `json:"dead_time"` + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"beta"` + BetaGamma []struct { + ConversionFactor float64 `json:"conversion_factor"` + DeadTime float64 `json:"dead_time"` + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"beta_gamma"` + Gamma []struct { + ConversionFactor float64 `json:"conversion_factor"` + DeadTime float64 `json:"dead_time"` + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"gamma"` + } `json:"radiation"` + Temperature []struct { + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"temperature"` + TotalMemberCount []struct { + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Value float64 `json:"value"` + } `json:"total_member_count"` + Wind []struct { + Description string `json:"description"` + Location string `json:"location"` + Name string `json:"name"` + Properties struct { + Direction struct { + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"direction"` + Elevation struct { + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"elevation"` + Gust struct { + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"gust"` + Speed struct { + Unit string `json:"unit"` + Value float64 `json:"value"` + } `json:"speed"` + } `json:"properties"` + } `json:"wind"` + } `json:"sensors"` + Space string `json:"space"` + Spacefed struct { + Spacenet bool `json:"spacenet"` + Spacephone bool `json:"spacephone"` + Spacesaml bool `json:"spacesaml"` + } `json:"spacefed"` + State struct { + Icon struct { + Closed string `json:"closed"` + Open string `json:"open"` + } `json:"icon"` + Lastchange float64 `json:"lastchange"` + Message string `json:"message"` + Open interface{} `json:"open"` + TriggerPerson string `json:"trigger_person"` + } `json:"state"` + Stream struct { + M4 string `json:"m4"` + Mjpeg string `json:"mjpeg"` + Ustream string `json:"ustream"` + } `json:"stream"` + URL string `json:"url"` +}