unfinished update to space api v0.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 type SpaceTempSensor struct {
14         value       string
15         unit        string
16     location    string
17     name        string
18     description string
19 }
20
21 type SpaceLightSensor struct {
22         value       string
23         //~ unit        string
24     location    string
25     name        string
26     description string
27 }
28
29 type SpacePowerConsumptionSensor struct {
30         value       string
31         unit        string
32     location    string
33     name        string
34     description string
35 }
36
37 type SpaceNetworkConnectionsSensor struct {
38         value       string
39         nettype     string
40         machines    string
41     location    string
42     name        string
43     description string
44 }
45
46 type SpaceMemberCountSensor struct {
47         value       string
48     location    string
49     name        string
50     description string
51 }
52
53 type SpaceDoorLockSensor struct {
54         value       bool
55     location    string
56     name        string
57     description string
58 }
59
60 type SpaceDoorAjarSensor struct {
61         value       bool
62     location    string
63     name        string
64     description string
65 }
66
67 func (nsi SpaceTempSensor) MakeTempSensor(what, name, where, value, unit string) {
68
69 }
70
71 func (nsi SpaceInfo) MergeInSensorData(sensortype interface{}) {
72     //todo check if what equals either "temperature", "door_locked", "barometer", "humidity", "beverage_supply", "power_consumption", "wind", "network_connections", "account_balance", "total_member_count", "people_now_present" or starts with "ext_". Else prepend "ext_"
73         var what string
74     switch sensortype := sensortype.(type) {
75         case SpaceTempSensor:
76             what = "temperature"
77         case SpaceLightSensor:
78             what = "ext_illumination"
79         case SpacePowerConsumptionSensor:
80             what = "power_consumption"
81         case SpaceNetworkConnectionsSensor:
82             what = "network_connections"
83         case SpaceMemberCountSensor:
84             what = "total_member_count"
85         case SpaceDoorLockSensor:
86             what = "door_locked"
87         case SpaceDoorAjarSensor:
88             what = "ext_door_ajar"
89         default:
90             panic("Unknown Sensor Type")
91     }
92         
93     if nsi["sensors"] == nil {
94         listofwhats := make([]SpaceInfo, 1)
95         listofwhats[0] = sensortype.(SpaceInfo)
96                 sensorobj := SpaceInfo{what: listofwhats}
97                 nsi["sensors"] = sensorobj
98         } else {
99                 sensorobj, ok := nsi["sensors"].(SpaceInfo) //type assertion (panics if false)
100                 if ok {
101             if sensorobj[what] != nil {
102                 for _, sensor := range sensorobj {
103                     //~ if sensor[what] != nil {
104                         //~ sensorinfo, ok2 := sensor[what].(SpaceInfo)
105                         //~ if ok2 {
106                             //~ sensorinfo[where] = value
107                             //~ return
108                         //~ } else {
109                             //~ panic("Wrong Type of sensorinfo: Should never happen")
110                         //~ }
111                     //~ }
112                 }
113             }
114                         //else
115                         sensorobj[what] = make([]SpaceInfo, 1)
116             sensorobj[what].([]SpaceInfo)[0] = sensortype.(SpaceInfo)
117                 } else {
118                         panic("Wrong Type of sensorobj: Should never happen")
119                 }
120         }
121 }
122
123 func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber, issuemail string) SpaceInfo {
124         nsi["contact"] = SpaceInfo{
125                 "phone":  phone,
126                 "email":  email,
127                 "ml":     ml,
128                 "jabber": jabber,
129         "issue_mail": issuemail}
130     nsi["issue_report_channels"] = [3]string{"issue_mail","email","ml"}
131         return nsi
132 }
133
134 func (nsi SpaceInfo) AddSpaceFeed(feedtype, mimetype, url string) SpaceInfo {
135         newfeed := SpaceInfo{"type": mimetype, "url": url}
136         if nsi["feeds"] == nil {
137         nsi["feeds"] = SpaceInfo{feedtype: newfeed}
138         } else {
139                 feedobj, ok := nsi["feeds"].(SpaceInfo) //type assertion (panics if false)
140                 if ok {
141             feedobj[feedtype] = newfeed
142                 } else {
143                         panic("Wrong Type of feedobj: Should never happen")
144                 }
145         }
146         return nsi
147 }
148
149 func (nsi SpaceInfo) AddSpaceEvent(name, eventtype, extra string) SpaceInfo {
150         newevent := SpaceInfo{"name": name, "type": eventtype, "timestamp": time.Now().Unix(), "extra": extra}
151         if nsi["events"] == nil {
152                 eventlist := make([]SpaceInfo, 1)
153                 eventlist[0] = newevent
154                 nsi["events"] = eventlist
155         } else {
156                 eventlist, ok := nsi["events"].([]SpaceInfo) //type assertion
157                 if ok {
158                         if len(eventlist) >= max_num_events {
159                                 eventlist = eventlist[1:]
160                         }
161                         nsi["events"] = append(eventlist, newevent)
162                 } else {
163                         panic("Wrong Type of eventlist: Should never happen")
164                 }
165         }
166         return nsi
167 }
168
169 func (nsi SpaceInfo) AddSpaceAddress(address string) SpaceInfo {
170         nsi["address"] = address
171     if nsi["location"] != nil {
172         location, ok := nsi["location"].(SpaceInfo)
173         if ok {
174             location["address"] = address
175         }
176     }
177         return nsi
178 }
179
180 func (nsi SpaceInfo) SetStatus(open bool, status string) {
181         nsi["status"] = status
182         nsi["open"] = open
183         nsi["lastchange"] = time.Now().Unix()
184     state, ok := nsi["state"].(SpaceInfo)
185     if ok {
186         state["message"] = status
187         state["open"] = open
188         state["lastchange"] = nsi["lastchange"]
189     }
190 }
191
192 func NewSpaceInfo(space string, url string, logo string, open_icon string, closed_icon string, lat float64, lon float64) SpaceInfo {
193         nsi := map[string]interface{}{
194                 "api":        "0.13",
195                 "space":      space,
196                 "url":        url,
197                 "logo":       logo,
198                 "open":       false,
199                 "lastchange": time.Now().Unix(),
200                 "icon":       SpaceInfo{
201             "open":     open_icon,
202             "closed":   closed_icon,
203         },
204         "state":       SpaceInfo{
205             "open":      false,
206             "lastchange":time.Now().Unix(),
207             "icon":     SpaceInfo{
208                 "open":     open_icon,
209                 "closed":   closed_icon},
210             },
211         "location":   SpaceInfo{
212             "lat":      lat,
213             "lon":      lon},
214         "contact" :   SpaceInfo {},
215     }
216         return nsi
217 }
218
219 func (data SpaceInfo) MakeJSON() ([]byte, error) {
220         msg, err := json.Marshal(data)
221         if err == nil {
222                 return msg, nil
223         }
224         return nil, err
225 }