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