9 const max_num_events int = 4
11 type SpaceInfo map[string]interface{}
13 type SpaceDoorLockSensor struct {
20 type SpaceDoorAjarSensor struct {
27 func MakeTempSensor(name, where, unit string, value float64) SpaceInfo {
28 listofwhats := make([]SpaceInfo, 1)
29 listofwhats[0] = SpaceInfo{
35 return SpaceInfo{"temperature": listofwhats}
38 func MakeTempCSensor(name, where string, value float64) SpaceInfo {
39 return MakeTempSensor(name,where,"\u00b0C",value)
42 func MakeIlluminationSensor(name, where, unit string, value int64) SpaceInfo {
43 listofwhats := make([]SpaceInfo, 1)
44 listofwhats[0] = SpaceInfo{
50 return SpaceInfo{"ext_illumination": listofwhats}
53 func MakePowerConsumptionSensor(name, where, unit string, value int64) SpaceInfo {
54 listofwhats := make([]SpaceInfo, 1)
55 listofwhats[0] = SpaceInfo{
61 return SpaceInfo{"power_consumption": listofwhats}
64 func MakeNetworkConnectionsSensor(name, where, nettype string, value, machines int64) SpaceInfo {
65 listofwhats := make([]SpaceInfo, 1)
66 listofwhats[0] = SpaceInfo{
73 return SpaceInfo{"network_connections": listofwhats}
76 func MakeMemberCountSensor(name, where string, value int64) SpaceInfo {
77 listofwhats := make([]SpaceInfo, 1)
78 listofwhats[0] = SpaceInfo{
83 return SpaceInfo{"total_member_count": listofwhats}
86 func MakeDoorLockSensor(name, where string, value bool) SpaceInfo {
87 listofwhats := make([]SpaceInfo, 1)
88 listofwhats[0] = SpaceInfo{
93 return SpaceInfo{"door_locked": listofwhats}
96 func MakeDoorAjarSensor(name, where string, value bool) SpaceInfo {
97 listofwhats := make([]SpaceInfo, 1)
98 listofwhats[0] = SpaceInfo{
103 return SpaceInfo{"ext_door_ajar": listofwhats}
106 func (nsi SpaceInfo) MergeInSensor(sensorinfo SpaceInfo) {
107 if nsi["sensors"] == nil {
108 nsi["sensors"] = SpaceInfo{}
109 //~ listofwhats := make([]SpaceInfo, 1)
110 //~ listofwhats[0] = sensortype.(SpaceInfo)
111 //~ sensorobj := SpaceInfo{what: listofwhats}
112 //~ nsi["sensors"] = sensorobj
114 sensorobj := nsi["sensors"].(SpaceInfo)
115 for what, subsensorobjlist := range sensorinfo {
116 if sensorobj[what] == nil {
117 sensorobj[what] = subsensorobjlist
119 existingsensorobjslist := sensorobj[what].([]SpaceInfo)
120 for _, newsensorobj := range subsensorobjlist.([]SpaceInfo) {
121 foundandsubstituted := false
122 for i:=0; i< len(existingsensorobjslist); i++ {
123 if existingsensorobjslist[i]["name"] == newsensorobj["name"] {
124 existingsensorobjslist[i] = newsensorobj
125 foundandsubstituted = true
128 if foundandsubstituted == false {
129 sensorobj[what] = append(sensorobj[what].([]SpaceInfo), newsensorobj)
130 //note that we do not change existingsensorobjslist here but directly sensorobj[what] !!
131 //the implications being that, if we have several newsensorobj in the list:
132 // 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
133 // b) if the array sensorinfo[what] has several objects with the same name, nsi["sensors"] will also end up with these name conflicts
140 func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber, issuemail string) SpaceInfo {
141 nsi["contact"] = SpaceInfo{
146 "issue_mail": issuemail}
147 nsi["issue_report_channels"] = [3]string{"issue_mail","email","ml"}
151 func (nsi SpaceInfo) AddSpaceFeed(feedtype, url string) SpaceInfo {
152 newfeed := SpaceInfo{"url": url}
153 if nsi["feeds"] == nil {
154 nsi["feeds"] = SpaceInfo{feedtype: newfeed}
156 feedobj, ok := nsi["feeds"].(SpaceInfo) //type assertion (panics if false)
158 feedobj[feedtype] = newfeed
160 panic("Wrong Type of feedobj: Should never happen")
166 func (nsi SpaceInfo) AddSpaceEvent(name, eventtype, extra string) SpaceInfo {
167 newevent := SpaceInfo{"name": name, "type": eventtype, "timestamp": time.Now().Unix(), "extra": extra}
168 if nsi["events"] == nil {
169 eventlist := make([]SpaceInfo, 1)
170 eventlist[0] = newevent
171 nsi["events"] = eventlist
173 eventlist, ok := nsi["events"].([]SpaceInfo) //type assertion
175 if len(eventlist) >= max_num_events {
176 eventlist = eventlist[1:]
178 nsi["events"] = append(eventlist, newevent)
180 panic("Wrong Type of eventlist: Should never happen")
186 func (nsi SpaceInfo) AddSpaceAddress(address string) SpaceInfo {
187 nsi["address"] = address
188 if nsi["location"] != nil {
189 location, ok := nsi["location"].(SpaceInfo)
191 location["address"] = address
197 func (nsi SpaceInfo) SetStatus(open bool, status string) {
198 nsi["status"] = status
200 nsi["lastchange"] = time.Now().Unix()
201 state, ok := nsi["state"].(SpaceInfo)
203 state["message"] = status
205 state["lastchange"] = nsi["lastchange"]
209 func NewSpaceInfo(space string, url string, logo string, open_icon string, closed_icon string, lat float64, lon float64) SpaceInfo {
210 nsi := map[string]interface{}{
216 "lastchange": time.Now().Unix(),
219 "closed": closed_icon,
223 "lastchange":time.Now().Unix(),
226 "closed": closed_icon},
228 "location": SpaceInfo{
231 "contact" : SpaceInfo {},
236 func (data SpaceInfo) MakeJSON() ([]byte, error) {
237 msg, err := json.Marshal(data)