fix presence bug
[svn42.git] / go / r3-netstatus / webstatus.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6     pubsub "github.com/tuxychandru/pubsub"
7     "./spaceapi"
8     "regexp"
9         "net/http"
10         "net/url"
11     "time"
12     "fmt"
13     r3events "svn.spreadspace.org/realraum/go.svn/r3events"
14     )
15
16
17 type spaceState struct {
18         present           bool
19         buttonpress_until int64
20 }
21
22 var (
23         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.065554, 15.450435).AddSpaceAddress("Brockmanngasse 15, 8010 Graz, Austria")
24         statusstate     *spaceState        = new(spaceState)
25         re_querystresc_ *regexp.Regexp     = regexp.MustCompile("[^\x30-\x39\x41-\x7E]")
26 )
27
28
29 func init() {
30         spaceapidata.AddSpaceFeed("calendar", "http://grical.realraum.at/s/?query=!realraum&view=rss")
31         spaceapidata.AddSpaceFeed("blog", "https://plus.google.com/113737596421797426873")
32         spaceapidata.AddSpaceFeed("wiki", "http://realraum.at/wiki")
33         spaceapidata.AddSpaceContactInfo("+43780700888524", "irc://irc.oftc.net/#realraum", "realraum@realraum.at", "realraum@realraum.at", "realraum@realraum.at", "vorstand@realraum.at")
34 }
35
36
37 func updateStatusString() {
38         var spacestatus string
39         if statusstate.present {
40                 if statusstate.buttonpress_until > time.Now().Unix() {
41                         spacestatus = "Panic! Present&Bored"
42                 } else {
43                         spacestatus = "Leute Anwesend"
44                 }
45         } else {
46                 spacestatus = "Keiner Da"
47         }
48         spaceapidata.SetStatus(statusstate.present, spacestatus)
49 }
50
51 func publishStateToWeb() {
52         updateStatusString()
53         jsondata_b, err := spaceapidata.MakeJSON()
54         if err != nil {
55                 Syslog_.Println("Error:", err)
56                 return
57         }
58         //jsondata_b := re_querystresc_.ReplaceAllFunc(jsondata_b, func(in []byte) []byte {
59         //      out := make([]byte, 4)
60         //      out[0] = '%'
61         //      copy(out[1:], []byte(strconv.FormatInt(int64(in[0]), 16)))
62         //      return out
63         //})
64         jsondata := url.QueryEscape(string(jsondata_b))
65         resp, err := http.Get("http://www.realraum.at/cgi/status.cgi?pass=jako16&set=" + jsondata)
66         if err != nil {
67                 Syslog_.Println("Error publishing realraum info", err)
68                 return
69         }
70         resp.Body.Close()
71 }
72
73 func EventToWeb(ps *pubsub.PubSub) {
74     events := ps.Sub("presence","door","sensors","buttons","updateinterval")
75
76     for eventinterface := range(events) {
77         //Debug_.Printf("EventToWeb: %s" , eventinterface)
78         switch event := eventinterface.(type) {
79             case r3events.TimeTick:
80                 publishStateToWeb()
81             case r3events.PresenceUpdate:
82                 statusstate.present = event.Present
83                 publishStateToWeb()
84             case r3events.BackdoorAjarUpdate:
85                 spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("HintertorwaechterAjarSensor", "Hintertürkontakt", event.Shut))
86                 publishStateToWeb()
87             case r3events.DoorAjarUpdate:
88                 spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("TorwaechterAjarSensor", "Türkontakt", event.Shut))
89                 publishStateToWeb()
90             case r3events.DoorLockUpdate:
91                 spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("TorwaechterLock", "Türschloß", event.Locked))
92                 publishStateToWeb()
93             case r3events.BoreDoomButtonPressEvent:
94                 statusstate.buttonpress_until = event.Ts + 3600
95                 spaceapidata.AddSpaceEvent("BoreDOOMButton", "check-in", "The button has been pressed")
96                 publishStateToWeb()
97             case r3events.TempSensorUpdate:
98                 var tempsensorlocation string
99                 switch event.Sensorindex {
100                     case 0:
101                         tempsensorlocation = "LoTHR"
102                     case 1:
103                         tempsensorlocation = "CX"
104                     default:
105                         tempsensorlocation = "Sonstwo"
106                 }
107                spaceapidata.MergeInSensor(spaceapi.MakeTempCSensor(fmt.Sprintf("Temp%d",event.Sensorindex),tempsensorlocation, event.Value))
108             case r3events.IlluminationSensorUpdate:
109                 spaceapidata.MergeInSensor(spaceapi.MakeIlluminationSensor("Photodiode","LoTHR","1024V/5V", event.Value))
110             case r3events.GasLeakAlert:
111                 spaceapidata.AddSpaceEvent("GasLeak", "alert", "GasLeak Alert has been triggered")
112                 publishStateToWeb()                
113         }
114         }
115 }
116