1 // (c) Bernhard Tittelbach, 2013
11 const max_num_events int = 4
13 type SpaceInfo map[string]interface{}
15 type SpaceDoorLockSensor struct {
22 type SpaceDoorAjarSensor struct {
29 func MakeTempSensor(name, where, unit string, value float64) SpaceInfo {
30 listofwhats := make([]SpaceInfo, 1)
31 listofwhats[0] = SpaceInfo{
37 return SpaceInfo{"temperature": listofwhats}
40 func MakeTempCSensor(name, where string, value float64) SpaceInfo {
41 return MakeTempSensor(name,where,"\u00b0C",value)
44 func MakeIlluminationSensor(name, where, unit string, value int64) SpaceInfo {
45 listofwhats := make([]SpaceInfo, 1)
46 listofwhats[0] = SpaceInfo{
52 return SpaceInfo{"ext_illumination": listofwhats}
55 func MakePowerConsumptionSensor(name, where, unit string, value int64) SpaceInfo {
56 listofwhats := make([]SpaceInfo, 1)
57 listofwhats[0] = SpaceInfo{
63 return SpaceInfo{"power_consumption": listofwhats}
66 func MakeNetworkConnectionsSensor(name, where, nettype string, value, machines int64) SpaceInfo {
67 listofwhats := make([]SpaceInfo, 1)
68 listofwhats[0] = SpaceInfo{
75 return SpaceInfo{"network_connections": listofwhats}
78 func MakeMemberCountSensor(name, where string, value int64) SpaceInfo {
79 listofwhats := make([]SpaceInfo, 1)
80 listofwhats[0] = SpaceInfo{
85 return SpaceInfo{"total_member_count": listofwhats}
88 func MakeDoorLockSensor(name, where string, value bool) SpaceInfo {
89 listofwhats := make([]SpaceInfo, 1)
90 listofwhats[0] = SpaceInfo{
95 return SpaceInfo{"door_locked": listofwhats}
98 func MakeDoorAjarSensor(name, where string, value bool) SpaceInfo {
99 listofwhats := make([]SpaceInfo, 1)
100 listofwhats[0] = SpaceInfo{
105 return SpaceInfo{"ext_door_ajar": listofwhats}
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
116 sensorobj := nsi["sensors"].(SpaceInfo)
117 for what, subsensorobjlist := range sensorinfo {
118 if sensorobj[what] == nil {
119 sensorobj[what] = subsensorobjlist
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
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
142 func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber, issuemail string) SpaceInfo {
143 nsi["contact"] = SpaceInfo{
148 "issue_mail": issuemail}
149 nsi["issue_report_channels"] = [3]string{"issue_mail","email","ml"}
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}
158 feedobj, ok := nsi["feeds"].(SpaceInfo) //type assertion (panics if false)
160 feedobj[feedtype] = newfeed
162 panic("Wrong Type of feedobj: Should never happen")
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
175 eventlist, ok := nsi["events"].([]SpaceInfo) //type assertion
177 if len(eventlist) >= max_num_events {
178 eventlist = eventlist[1:]
180 nsi["events"] = append(eventlist, newevent)
182 panic("Wrong Type of eventlist: Should never happen")
188 func (nsi SpaceInfo) AddSpaceAddress(address string) SpaceInfo {
189 nsi["address"] = address
190 if nsi["location"] != nil {
191 location, ok := nsi["location"].(SpaceInfo)
193 location["address"] = address
199 func (nsi SpaceInfo) SetStatus(open bool, status string) {
200 nsi["status"] = status
202 nsi["lastchange"] = time.Now().Unix()
203 state, ok := nsi["state"].(SpaceInfo)
205 state["message"] = status
207 state["lastchange"] = nsi["lastchange"]
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{}{
218 "lastchange": time.Now().Unix(),
221 "closed": closed_icon,
225 "lastchange":time.Now().Unix(),
228 "closed": closed_icon},
230 "location": SpaceInfo{
233 "contact" : SpaceInfo {},
238 func (data SpaceInfo) MakeJSON() ([]byte, error) {
239 msg, err := json.Marshal(data)