package main import ( "./spaceapi" "bufio" "fmt" "net" "net/http" "net/url" "regexp" "time" ) type SpaceState struct { present bool buttonpress_until int64 } var ( re_presence_ *regexp.Regexp = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?") re_button_ *regexp.Regexp = regexp.MustCompile("PanicButton|button\\d?") re_temp_ *regexp.Regexp = regexp.MustCompile("temp0: (\\d+\\.\\d+)") re_photo_ *regexp.Regexp = regexp.MustCompile("photo0: (\\d+)") re_querystresc_ *regexp.Regexp = regexp.MustCompile("[^\x30-\x39\x41-\x7E]") spaceapidata spaceapi.SpaceInfo = spaceapi.NewSpaceInfo("realraum", "http://realraum.at", "http://realraum.at/logo-red_250x250.png", "http://realraum.at/logo-re_open_100x100.png", "http://realraum.at/logo-re_empty_100x100.png").AddSpaceAddress("Jakoministr. 16 ground level left, 8010 Graz, Austria").AddSpaceLatLon(47.065779129, 15.442322614) statusstate *SpaceState = new(SpaceState) ) //------- func updateStatusString() { var spacestatus string if statusstate.present { if statusstate.buttonpress_until > time.Now().Unix() { spacestatus = "Panic! Present&Bored" } else { spacestatus = "Leute Anwesend" } } else { spacestatus = "Keiner Da" } spaceapidata.SetStatus(statusstate.present, spacestatus) } func publishStateToWeb() { updateStatusString() jsondata_b, err := spaceapidata.MakeJSON() if err != nil { fmt.Println("Error:", err) return } //jsondata_b := re_querystresc_.ReplaceAllFunc(jsondata_b, func(in []byte) []byte { // out := make([]byte, 4) // out[0] = '%' // copy(out[1:], []byte(strconv.FormatInt(int64(in[0]), 16))) // return out //}) jsondata := url.QueryEscape(string(jsondata_b)) resp, err := http.Get("http://www.realraum.at/cgi/status.cgi?pass=jako16&set=" + jsondata) if err != nil { fmt.Println("Error publishing realraum info", err) return } defer resp.Body.Close() } func parseSocketInputLine(line string) { match_presence := re_presence_.FindStringSubmatch(line) match_button := re_button_.FindStringSubmatch(line) match_temp := re_temp_.FindStringSubmatch(line) match_photo := re_photo_.FindStringSubmatch(line) if match_presence != nil { statusstate.present = (match_presence[1] == "yes") publishStateToWeb() } else if match_button != nil { statusstate.buttonpress_until = time.Now().Unix() + 3600 spaceapidata.AddSpaceEvent("PanicButton", "check-in", "The button has been pressed") publishStateToWeb() } else if match_temp != nil { spaceapidata.UpdateSensorData("temp", "Ceiling", match_temp[1]+"C") //newtemp, err = strconv.ParseFloat((match_temp[1]), 32) //if err == nil { // spaceapidata.UpdateSensorData("temp", "Ceiling", strconv.FormatFloat(newtemp, 'f', 2, 32)+"C") // spacestate.temperature = newtemp //} } else if match_photo != nil { spaceapidata.UpdateSensorData("light", "Front", match_photo[1]) //newphoto, err = strconv.ParseInt(match_photo[1], 10, 32) //if err == nil { // spacestate.lightlevel = newphoto //} } } func readFromUSocket(path string, c chan string) { ReOpenSocket: for { presence_socket, err := net.Dial("unix", path) if err != nil { //Waiting on Socket time.Sleep(5 * time.Second) continue ReOpenSocket } presence_reader := bufio.NewReader(presence_socket) for { line, err := presence_reader.ReadString('\n') if err != nil { //Socket closed presence_socket.Close() continue ReOpenSocket } c <- line } } } func main() { spaceapidata.AddSpaceFeed("calendar", "application/rss+xml", "http://grical.realraum.at/s/?query=!realraum&view=rss") spaceapidata.AddSpaceFeed("google+", "text/html", "https://plus.google.com/113737596421797426873") spaceapidata.AddSpaceContactInfo("+43780700888524", "irc://irc.oftc.net/#realraum", "realraum@realraum.at", "realraum@realraum.at", "realraum@realraum.at") eventqueue := make(chan string) ticker := time.NewTicker(time.Duration(15) * time.Minute) go readFromUSocket("/var/run/tuer/presence.socket", eventqueue) for { select { case e := <-eventqueue: parseSocketInputLine(e) case <-ticker.C: publishStateToWeb() } } } /* TODO: * Read config from an .ini file */