spaceapi update to 0.13 as OpenSpaceLint understands 0.13
[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, issuemail string) SpaceInfo {
41         nsi["contact"] = SpaceInfo{
42                 "phone":  phone,
43                 "email":  email,
44                 "ml":     ml,
45                 "jabber": jabber,
46         "issue-mail": issuemail}
47     nsi["issue-report-channels"] = [3]string{"issue-mail","email","ml"}
48         return nsi
49 }
50
51 func (nsi SpaceInfo) AddSpaceFeed(name, mimetype, url string) SpaceInfo {
52         newfeed := SpaceInfo{"name": name, "type": mimetype, "url": url}
53         if nsi["feeds"] == nil {
54                 feedlist := make([]SpaceInfo, 1)
55                 feedlist[0] = newfeed
56                 nsi["feeds"] = feedlist
57         } else {
58                 feedlist, ok := nsi["feeds"].([]SpaceInfo) //type assertion (panics if false)
59                 if ok {
60                         nsi["feeds"] = append(feedlist, newfeed)
61                 } else {
62                         panic("Wrong Type of feedlist: Should never happen")
63                 }
64         }
65         return nsi
66 }
67
68 func (nsi SpaceInfo) AddSpaceEvent(name, eventtype, extra string) SpaceInfo {
69         newevent := SpaceInfo{"name": name, "type": eventtype, "timestamp": time.Now().Unix(), "extra": extra}
70         if nsi["events"] == nil {
71                 eventlist := make([]SpaceInfo, 1)
72                 eventlist[0] = newevent
73                 nsi["events"] = eventlist
74         } else {
75                 eventlist, ok := nsi["events"].([]SpaceInfo) //type assertion
76                 if ok {
77                         if len(eventlist) >= max_num_events {
78                                 eventlist = eventlist[1:]
79                         }
80                         nsi["events"] = append(eventlist, newevent)
81                 } else {
82                         panic("Wrong Type of eventlist: Should never happen")
83                 }
84         }
85         return nsi
86 }
87
88 func (nsi SpaceInfo) AddSpaceAddress(address string) SpaceInfo {
89         nsi["address"] = address
90     if nsi["location"] != nil {
91         location, ok := nsi["location"].(SpaceInfo)
92         if ok {
93             location["address"] = address
94         }
95     }
96         return nsi
97 }
98
99 func (nsi SpaceInfo) SetStatus(open bool, status string) {
100         nsi["status"] = status
101         nsi["open"] = open
102         nsi["lastchange"] = time.Now().Unix()
103     state, ok := nsi["state"].(SpaceInfo)
104     if ok {
105         state["message"] = status
106         state["open"] = open
107         state["lastchange"] = nsi["lastchange"]
108     }
109 }
110
111 func NewSpaceInfo(space string, url string, logo string, open_icon string, closed_icon string, lat float64, lon float64) SpaceInfo {
112         nsi := map[string]interface{}{
113                 "api":        "0.13",
114                 "space":      space,
115                 "url":        url,
116                 "logo":       logo,
117                 "open":       false,
118                 "lastchange": time.Now().Unix(),
119                 "icon":       SpaceInfo{
120             "open":     open_icon,
121             "closed":   closed_icon,
122         },
123         "state":       SpaceInfo{
124             "open":      false,
125             "lastchange":time.Now().Unix(),
126             "icon":     SpaceInfo{
127                 "open":     open_icon,
128                 "closed":   closed_icon},
129             },
130         "location":   SpaceInfo{
131             "lat":      lat,
132             "lon":      lon},
133         "contact" :   SpaceInfo {},
134     }
135         return nsi
136 }
137
138 func (data SpaceInfo) MakeJSON() ([]byte, error) {
139         msg, err := json.Marshal(data)
140         if err == nil {
141                 return msg, nil
142         }
143         return nil, err
144 }