package main import ( "./spaceapi" "bufio" "fmt" "net" "net/http" "net/url" "regexp" "time" "strconv" ) type SpaceState struct { present bool buttonpress_until int64 } var ( re_presence_ *regexp.Regexp = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?") re_status_ *regexp.Regexp = regexp.MustCompile("Status: (closed|opened), (opening|waiting|closing|idle), (ajar|shut).*") 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",47.065779129, 15.442322614).AddSpaceAddress("Jakoministr. 16 ground level left, 8010 Graz, Austria") 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_status := re_status_.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") //spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("Torwaechter", "Front Door", match_presence[2] == "closed")) spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("TorwaechterLock", "Türschloß", match_presence[2] == "closed")) publishStateToWeb() } else if match_status != nil { spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("TorwaechterLock", "Türschloß", match_status[1] == "closed")) spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("TorwaechterAjarSensor", "Türkontakt", match_status[3] == "shut")) //spaceapidata.MergeInSensor(spaceapi.MakeDoorAjarSensor("Torwaechter", "Front Door", match_presence[3] == "shut")) 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 { newtemp, err := strconv.ParseFloat((match_temp[1]), 32) if err == nil { spaceapidata.MergeInSensor(spaceapi.MakeTempCSensor("Temp0","Decke",newtemp)) } } else if match_photo != nil { newphoto, err := strconv.ParseInt(match_photo[1], 10, 32) if err == nil { spaceapidata.MergeInSensor(spaceapi.MakeIlluminationSensor("Photodiode","Decke","1024V/5V",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", "http://grical.realraum.at/s/?query=!realraum&view=rss") spaceapidata.AddSpaceFeed("blog", "https://plus.google.com/113737596421797426873") spaceapidata.AddSpaceFeed("wiki", "http://realraum.at/wiki") spaceapidata.AddSpaceContactInfo("+43780700888524", "irc://irc.oftc.net/#realraum", "realraum@realraum.at", "realraum@realraum.at", "realraum@realraum.at", "vorstand@realraum.at") eventqueue := make(chan string) ticker := time.NewTicker(time.Duration(7) * 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 */