removed kicad-libs and teenstep after moving to github
[svn42.git] / go / r3-netstatus / spaceapi / spaceapi.go
1 // (c) Bernhard Tittelbach, 2013
2
3 // spaceapi.go
4 package spaceapi
5
6 import (
7         "encoding/json"
8         "time"
9 )
10
11 const max_num_events int = 4
12
13 type SpaceInfo map[string]interface{}
14
15 type SpaceDoorLockSensor struct {
16         value       bool
17     location    string
18     name        string
19     description string
20 }
21
22 type SpaceDoorAjarSensor struct {
23         value       bool
24     location    string
25     name        string
26     description string
27 }
28
29 func MakeTempSensor(name, where, unit string, value float64) SpaceInfo {
30     listofwhats := make([]SpaceInfo, 1)
31     listofwhats[0] = SpaceInfo{
32                 "value":    value,
33                 "unit":     unit,
34                 "location": where,
35                 "name":     name,
36                 "description": ""}
37     return SpaceInfo{"temperature": listofwhats}
38 }
39
40 func MakeTempCSensor(name, where string, value float64) SpaceInfo {
41     return MakeTempSensor(name,where,"\u00b0C",value)
42 }
43
44 func MakeIlluminationSensor(name, where, unit string, value int64) SpaceInfo {
45     listofwhats := make([]SpaceInfo, 1)
46     listofwhats[0] = SpaceInfo{
47                 "value":    value,
48                 "unit":     unit,
49                 "location": where,
50                 "name":     name,
51                 "description": ""}
52     return SpaceInfo{"ext_illumination": listofwhats}
53 }
54
55 func MakePowerConsumptionSensor(name, where, unit string, value int64) SpaceInfo {
56     listofwhats := make([]SpaceInfo, 1)
57     listofwhats[0] = SpaceInfo{
58                 "value":    value,
59                 "unit":     unit,
60                 "location": where,
61                 "name":     name,
62                 "description": ""}
63     return SpaceInfo{"power_consumption": listofwhats}
64 }
65
66 func MakeNetworkConnectionsSensor(name, where, nettype string, value, machines int64) SpaceInfo {
67     listofwhats := make([]SpaceInfo, 1)
68     listofwhats[0] = SpaceInfo{
69                 "value":    value,
70         "type":     nettype,
71         "machines": machines,
72                 "location": where,
73                 "name":     name,
74                 "description": ""}
75     return SpaceInfo{"network_connections": listofwhats}
76 }
77
78 func MakeMemberCountSensor(name, where string, value int64) SpaceInfo {
79     listofwhats := make([]SpaceInfo, 1)
80     listofwhats[0] = SpaceInfo{
81                 "value":    value,
82                 "location": where,
83                 "name":     name,
84                 "description": ""}
85     return SpaceInfo{"total_member_count": listofwhats}
86 }
87
88 func MakeDoorLockSensor(name, where string, value bool) SpaceInfo {
89     listofwhats := make([]SpaceInfo, 1)
90     listofwhats[0] = SpaceInfo{
91                 "value":    value,
92                 "location": where,
93                 "name":     name,
94                 "description": ""}
95     return SpaceInfo{"door_locked": listofwhats}
96 }
97
98 func MakeDoorAjarSensor(name, where string, value bool) SpaceInfo {
99     listofwhats := make([]SpaceInfo, 1)
100     listofwhats[0] = SpaceInfo{
101                 "value":    value,
102                 "location": where,
103                 "name":     name,
104                 "description": ""}
105     return SpaceInfo{"ext_door_ajar": listofwhats}
106 }
107
108 func (nsi SpaceInfo) MergeInSensor(sensorinfo SpaceInfo) {
109     if nsi["sensors"] == nil {
110         nsi["sensors"] = SpaceInfo{}
111         //~ listofwhats := make([]SpaceInfo, 1)
112         //~ listofwhats[0] = sensortype.(SpaceInfo)
113                 //~ sensorobj := SpaceInfo{what: listofwhats}
114                 //~ nsi["sensors"] = sensorobj
115         }
116     sensorobj := nsi["sensors"].(SpaceInfo)
117     for what, subsensorobjlist := range sensorinfo {
118         if sensorobj[what] == nil {
119             sensorobj[what] = subsensorobjlist
120         } else {
121             existingsensorobjslist := sensorobj[what].([]SpaceInfo)
122             for _, newsensorobj := range subsensorobjlist.([]SpaceInfo) {
123                 foundandsubstituted := false
124                 for i:=0; i< len(existingsensorobjslist); i++ {
125                     if existingsensorobjslist[i]["name"] == newsensorobj["name"] {
126                         existingsensorobjslist[i] = newsensorobj
127                         foundandsubstituted = true
128                     }
129                 }
130                 if foundandsubstituted == false {
131                     sensorobj[what] = append(sensorobj[what].([]SpaceInfo), newsensorobj)
132                     //note that we do not change existingsensorobjslist here but directly sensorobj[what] !!
133                     //the implications being that, if we have several newsensorobj in the list:
134                     //  a) optimisation: we only check them against the existing ones and spare ourselves the work of checking a newsensorobj's name against a just added other newsensorobjs's name
135                     //  b) if the array sensorinfo[what] has several objects with the same name, nsi["sensors"] will also end up with these name conflicts
136                 }
137             }
138         }
139     }
140 }
141
142 func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber, issuemail string) SpaceInfo {
143         nsi["contact"] = SpaceInfo{
144                 "phone":  phone,
145                 "email":  email,
146                 "ml":     ml,
147                 "jabber": jabber,
148         "issue_mail": issuemail}
149     nsi["issue_report_channels"] = [3]string{"issue_mail","email","ml"}
150         return nsi
151 }
152
153 func (nsi SpaceInfo) AddSpaceFeed(feedtype, url string) SpaceInfo {
154         newfeed := SpaceInfo{"url": url}
155         if nsi["feeds"] == nil {
156         nsi["feeds"] = SpaceInfo{feedtype: newfeed}
157         } else {
158                 feedobj, ok := nsi["feeds"].(SpaceInfo) //type assertion (panics if false)
159                 if ok {
160             feedobj[feedtype] = newfeed
161                 } else {
162                         panic("Wrong Type of feedobj: Should never happen")
163                 }
164         }
165         return nsi
166 }
167
168 func (nsi SpaceInfo) AddSpaceEvent(name, eventtype, extra string) SpaceInfo {
169         newevent := SpaceInfo{"name": name, "type": eventtype, "timestamp": time.Now().Unix(), "extra": extra}
170         if nsi["events"] == nil {
171                 eventlist := make([]SpaceInfo, 1)
172                 eventlist[0] = newevent
173                 nsi["events"] = eventlist
174         } else {
175                 eventlist, ok := nsi["events"].([]SpaceInfo) //type assertion
176                 if ok {
177                         if len(eventlist) >= max_num_events {
178                                 eventlist = eventlist[1:]
179                         }
180                         nsi["events"] = append(eventlist, newevent)
181                 } else {
182                         panic("Wrong Type of eventlist: Should never happen")
183                 }
184         }
185         return nsi
186 }
187
188 func (nsi SpaceInfo) AddSpaceAddress(address string) SpaceInfo {
189         nsi["address"] = address
190     if nsi["location"] != nil {
191         location, ok := nsi["location"].(SpaceInfo)
192         if ok {
193             location["address"] = address
194         }
195     }
196         return nsi
197 }
198
199 func (nsi SpaceInfo) SetStatus(open bool, status string) {
200         nsi["status"] = status
201         nsi["open"] = open
202         nsi["lastchange"] = time.Now().Unix()
203     state, ok := nsi["state"].(SpaceInfo)
204     if ok {
205         state["message"] = status
206         state["open"] = open
207         state["lastchange"] = nsi["lastchange"]
208     }
209 }
210
211 func NewSpaceInfo(space string, url string, logo string, open_icon string, closed_icon string, lat float64, lon float64) SpaceInfo {
212         nsi := map[string]interface{}{
213                 "api":        "0.13",
214                 "space":      space,
215                 "url":        url,
216                 "logo":       logo,
217                 "open":       false,
218                 "lastchange": time.Now().Unix(),
219                 "icon":       SpaceInfo{
220             "open":     open_icon,
221             "closed":   closed_icon,
222         },
223         "state":       SpaceInfo{
224             "open":      false,
225             "lastchange":time.Now().Unix(),
226             "icon":     SpaceInfo{
227                 "open":     open_icon,
228                 "closed":   closed_icon},
229             },
230         "location":   SpaceInfo{
231             "lat":      lat,
232             "lon":      lon},
233         "contact" :   SpaceInfo {},
234     }
235         return nsi
236 }
237
238 func (data SpaceInfo) MakeJSON() ([]byte, error) {
239         msg, err := json.Marshal(data)
240         if err == nil {
241                 return msg, nil
242         }
243         return nil, err
244 }