command regex and who
[svn42.git] / r3-netstatus / webstatus.go
1 package main
2
3 import (
4     pubsub "github.com/tuxychandru/pubsub"
5     "./spaceapi"
6     "regexp"
7         "net/http"
8         "net/url"
9     "log"
10     "time"
11     )
12
13
14 type spaceState struct {
15         present           bool
16         buttonpress_until int64
17 }
18
19 var (
20         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")
21         statusstate     *spaceState        = new(spaceState)
22         re_querystresc_ *regexp.Regexp     = regexp.MustCompile("[^\x30-\x39\x41-\x7E]")
23 )
24
25
26 func init() {
27         spaceapidata.AddSpaceFeed("calendar", "http://grical.realraum.at/s/?query=!realraum&view=rss")
28         spaceapidata.AddSpaceFeed("blog", "https://plus.google.com/113737596421797426873")
29         spaceapidata.AddSpaceFeed("wiki", "http://realraum.at/wiki")
30         spaceapidata.AddSpaceContactInfo("+43780700888524", "irc://irc.oftc.net/#realraum", "realraum@realraum.at", "realraum@realraum.at", "realraum@realraum.at", "vorstand@realraum.at")
31 }
32
33
34 func updateStatusString() {
35         var spacestatus string
36         if statusstate.present {
37                 if statusstate.buttonpress_until > time.Now().Unix() {
38                         spacestatus = "Panic! Present&Bored"
39                 } else {
40                         spacestatus = "Leute Anwesend"
41                 }
42         } else {
43                 spacestatus = "Keiner Da"
44         }
45         spaceapidata.SetStatus(statusstate.present, spacestatus)
46 }
47
48 func publishStateToWeb() {
49         updateStatusString()
50         jsondata_b, err := spaceapidata.MakeJSON()
51         if err != nil {
52                 log.Println("Error:", err)
53                 return
54         }
55         //jsondata_b := re_querystresc_.ReplaceAllFunc(jsondata_b, func(in []byte) []byte {
56         //      out := make([]byte, 4)
57         //      out[0] = '%'
58         //      copy(out[1:], []byte(strconv.FormatInt(int64(in[0]), 16)))
59         //      return out
60         //})
61         jsondata := url.QueryEscape(string(jsondata_b))
62         resp, err := http.Get("http://www.realraum.at/cgi/status.cgi?pass=jako16&set=" + jsondata)
63         if err != nil {
64                 log.Println("Error publishing realraum info", err)
65                 return
66         }
67         resp.Body.Close()
68 }
69
70 func EventToWeb(ps *pubsub.PubSub) {
71     events := ps.Sub("presence","door","sensors","buttons","publishjson")
72
73     for eventinterface := range(events) {
74         switch event := eventinterface.(type) {
75             case TimeTick:
76                 publishStateToWeb()
77             case PresenceUpdate:
78                 statusstate.present = event.Present
79                 publishStateToWeb()
80             case DoorStatusUpdate:
81                 spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("TorwaechterLock", "Türschloß", event.Locked))
82                 spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("TorwaechterAjarSensor", "Türkontakt", event.Shut))
83                 publishStateToWeb()
84             case ButtonPressUpdate:
85                 statusstate.buttonpress_until = event.Ts + 3600
86                 spaceapidata.AddSpaceEvent("PanicButton", "check-in", "The button has been pressed")
87                 publishStateToWeb()
88             case TempSensorUpdate:
89                 spaceapidata.MergeInSensor(spaceapi.MakeTempCSensor("Temp0","Decke", event.Value))
90             case IlluminationSensorUpdate:
91                 spaceapidata.MergeInSensor(spaceapi.MakeIlluminationSensor("Photodiode","Decke","1024V/5V", event.Value))
92         }
93         }
94 }
95