basic version, statuten
[svn42.git] / r3-webstatus-spaceapi / spaceapi / spaceapi.go
1 // spaceapi.go
2 package spaceapi
3
4 import (
5         "encoding/json"
6         "time"
7 )
8
9 const max_num_events int = 4
10
11 type SpaceInfo map[string]interface{}
12
13 func (nsi SpaceInfo) UpdateSensorData(what, where, value string) {
14         if nsi["sensors"] == nil {
15                 sensorlist := make([]SpaceInfo, 1)
16                 sensorlist[0] = SpaceInfo{what: SpaceInfo{where: value}}
17                 nsi["sensors"] = sensorlist
18         } else {
19                 sensorlist, ok := nsi["sensors"].([]SpaceInfo) //type assertion (panics if false)
20                 if ok {
21                         for _, sensor := range sensorlist {
22                                 if sensor[what] != nil {
23                                         sensorinfo, ok2 := sensor[what].(SpaceInfo)
24                                         if ok2 {
25                                                 sensorinfo[where] = value
26                                                 return
27                                         } else {
28                                                 panic("Wrong Type of sensorinfo: Should never happen")
29                                         }
30                                 }
31                         }
32                         //else
33                         nsi["sensors"] = append(sensorlist, SpaceInfo{what: SpaceInfo{where: value}})
34                 } else {
35                         panic("Wrong Type of sensorlist: Should never happen")
36                 }
37         }
38 }
39
40 func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber string) SpaceInfo {
41         nsi["contact"] = SpaceInfo{
42                 "phone":  phone,
43                 "email":  email,
44                 "ml":     ml,
45                 "jabber": jabber}
46         return nsi
47 }
48
49 func (nsi SpaceInfo) AddSpaceFeed(name, mimetype, url string) SpaceInfo {
50         newfeed := SpaceInfo{"name": name, "type": mimetype, "url": url}
51         if nsi["feeds"] == nil {
52                 feedlist := make([]SpaceInfo, 1)
53                 feedlist[0] = newfeed
54                 nsi["feeds"] = feedlist
55         } else {
56                 feedlist, ok := nsi["feeds"].([]SpaceInfo) //type assertion (panics if false)
57                 if ok {
58                         nsi["feeds"] = append(feedlist, newfeed)
59                 } else {
60                         panic("Wrong Type of feedlist: Should never happen")
61                 }
62         }
63         return nsi
64 }
65
66 func (nsi SpaceInfo) AddSpaceEvent(name, eventtype, extra string) SpaceInfo {
67         newevent := SpaceInfo{"name": name, "type": eventtype, "t": time.Now().Unix(), "extra": extra}
68         if nsi["events"] == nil {
69                 eventlist := make([]SpaceInfo, 1)
70                 eventlist[0] = newevent
71                 nsi["events"] = eventlist
72         } else {
73                 eventlist, ok := nsi["events"].([]SpaceInfo) //type assertion
74                 if ok {
75                         if len(eventlist) >= max_num_events {
76                                 eventlist = eventlist[1:]
77                         }
78                         nsi["events"] = append(eventlist, newevent)
79                 } else {
80                         panic("Wrong Type of eventlist: Should never happen")
81                 }
82         }
83         return nsi
84 }
85
86 func (nsi SpaceInfo) AddSpaceAddress(address string) SpaceInfo {
87         nsi["address"] = address
88         return nsi
89 }
90
91 func (nsi SpaceInfo) AddSpaceLatLon(lat float64, lon float64) SpaceInfo {
92         nsi["lat"] = lat
93         nsi["lon"] = lon
94         return nsi
95 }
96
97 func (nsi SpaceInfo) SetStatus(open bool, status string) {
98         nsi["status"] = status
99         nsi["open"] = open
100         nsi["lastchange"] = time.Now().Unix()
101 }
102
103 func NewSpaceInfo(space string, url string, logo string, open_icon string, closed_icon string) SpaceInfo {
104         nsi := map[string]interface{}{
105                 "api":        "0.13",
106                 "space":      space,
107                 "url":        url,
108                 "logo":       logo,
109                 "open":       false,
110                 "lastchange": time.Now().Unix(),
111                 "icon": map[string]interface{}{
112                         "open":   open_icon,
113                         "closed": closed_icon}}
114         return nsi
115 }
116
117 func (data SpaceInfo) MakeJSON() ([]byte, error) {
118         msg, err := json.Marshal(data)
119         if err == nil {
120                 return msg, nil
121         }
122         return nil, err
123 }