r3 spaceapi updater daemon written in go
[svn42.git] / r3-webstatus-spaceapi / main.go
1 package main
2
3 import (
4         "./spaceapi"
5         "bufio"
6         "fmt"
7         "net"
8         "net/http"
9         "net/url"
10         "regexp"
11         "time"
12 )
13
14 type SpaceState struct {
15         present           bool
16         buttonpress_until int64
17 }
18
19 var (
20         re_presence_    *regexp.Regexp     = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?")
21         re_button_      *regexp.Regexp     = regexp.MustCompile("PanicButton|button\\d?")
22         re_temp_        *regexp.Regexp     = regexp.MustCompile("temp0: (\\d+\\.\\d+)")
23         re_photo_       *regexp.Regexp     = regexp.MustCompile("photo0: (\\d+)")
24         re_querystresc_ *regexp.Regexp     = regexp.MustCompile("[^\x30-\x39\x41-\x7E]")
25         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)
26         statusstate     *SpaceState        = new(SpaceState)
27 )
28
29 //-------
30
31 func updateStatusString() {
32         var spacestatus string
33         if statusstate.present {
34                 if statusstate.buttonpress_until > time.Now().Unix() {
35                         spacestatus = "Panic! Present&Bored"
36                 } else {
37                         spacestatus = "Leute Anwesend"
38                 }
39         } else {
40                 spacestatus = "Keiner Da"
41         }
42         spaceapidata.SetStatus(statusstate.present, spacestatus)
43 }
44
45 func publishStateToWeb() {
46         updateStatusString()
47         jsondata_b, err := spaceapidata.MakeJSON()
48         if err != nil {
49                 fmt.Println("Error:", err)
50                 return
51         }
52         //jsondata_b := re_querystresc_.ReplaceAllFunc(jsondata_b, func(in []byte) []byte {
53         //      out := make([]byte, 4)
54         //      out[0] = '%'
55         //      copy(out[1:], []byte(strconv.FormatInt(int64(in[0]), 16)))
56         //      return out
57         //})
58         jsondata := url.QueryEscape(string(jsondata_b))
59         resp, err := http.Get("http://www.realraum.at/cgi/status.cgi?pass=jako16&set=" + jsondata)
60         if err != nil {
61                 fmt.Println("Error publishing realraum info", err)
62                 return
63         }
64         defer resp.Body.Close()
65 }
66
67 func parseSocketInputLine(line string) {
68         match_presence := re_presence_.FindStringSubmatch(line)
69         match_button := re_button_.FindStringSubmatch(line)
70         match_temp := re_temp_.FindStringSubmatch(line)
71         match_photo := re_photo_.FindStringSubmatch(line)
72
73         if match_presence != nil {
74                 statusstate.present = (match_presence[1] == "yes")
75                 publishStateToWeb()
76         } else if match_button != nil {
77                 statusstate.buttonpress_until = time.Now().Unix() + 3600
78                 spaceapidata.AddSpaceEvent("PanicButton", "check-in", "The button has been pressed")
79                 publishStateToWeb()
80         } else if match_temp != nil {
81                 spaceapidata.UpdateSensorData("temp", "Ceiling", match_temp[1]+"C")
82                 //newtemp, err = strconv.ParseFloat((match_temp[1]), 32)
83                 //if err == nil {
84                 //      spaceapidata.UpdateSensorData("temp", "Ceiling", strconv.FormatFloat(newtemp, 'f', 2, 32)+"C")
85                 //      spacestate.temperature = newtemp
86                 //}
87         } else if match_photo != nil {
88                 spaceapidata.UpdateSensorData("light", "Front", match_photo[1])
89                 //newphoto, err = strconv.ParseInt(match_photo[1], 10, 32)
90                 //if err == nil {
91                 //      spacestate.lightlevel = newphoto
92                 //}
93         }
94 }
95
96 func readFromUSocket(path string, c chan string) {
97 ReOpenSocket:
98         for {
99                 presence_socket, err := net.Dial("unix", path)
100                 if err != nil {
101                         //Waiting on Socket
102                         time.Sleep(5 * time.Second)
103                         continue ReOpenSocket
104                 }
105                 presence_reader := bufio.NewReader(presence_socket)
106                 for {
107                         line, err := presence_reader.ReadString('\n')
108                         if err != nil {
109                                 //Socket closed
110                                 presence_socket.Close()
111                                 continue ReOpenSocket
112                         }
113                         c <- line
114                 }
115         }
116 }
117
118 func main() {
119         spaceapidata.AddSpaceFeed("calendar", "application/rss+xml", "http://grical.realraum.at/s/?query=!realraum&view=rss")
120         spaceapidata.AddSpaceFeed("google+", "text/html", "https://plus.google.com/113737596421797426873")
121         spaceapidata.AddSpaceContactInfo("+43780700888524", "irc://irc.oftc.net/#realraum", "realraum@realraum.at", "realraum@realraum.at", "realraum@realraum.at")
122         eventqueue := make(chan string)
123         ticker := time.NewTicker(time.Duration(15) * time.Minute)
124         go readFromUSocket("/var/run/tuer/presence.socket", eventqueue)
125         for {
126                 select {
127                 case e := <-eventqueue:
128                         parseSocketInputLine(e)
129                 case <-ticker.C:
130                         publishStateToWeb()
131                 }
132         }
133 }
134
135 /* TODO:
136 * Read config from an .ini file
137  */