9 const max_num_events int = 4
11 type SpaceInfo map[string]interface{}
13 type SpaceTempSensor struct {
21 type SpaceLightSensor struct {
29 type SpacePowerConsumptionSensor struct {
37 type SpaceNetworkConnectionsSensor struct {
46 type SpaceMemberCountSensor struct {
53 type SpaceDoorLockSensor struct {
60 type SpaceDoorAjarSensor struct {
67 func (nsi SpaceTempSensor) MakeTempSensor(what, name, where, value, unit string) {
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_"
74 switch sensortype := sensortype.(type) {
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:
87 case SpaceDoorAjarSensor:
88 what = "ext_door_ajar"
90 panic("Unknown Sensor Type")
93 if nsi["sensors"] == nil {
94 listofwhats := make([]SpaceInfo, 1)
95 listofwhats[0] = sensortype.(SpaceInfo)
96 sensorobj := SpaceInfo{what: listofwhats}
97 nsi["sensors"] = sensorobj
99 sensorobj, ok := nsi["sensors"].(SpaceInfo) //type assertion (panics if false)
101 if sensorobj[what] != nil {
102 for _, sensor := range sensorobj {
103 //~ if sensor[what] != nil {
104 //~ sensorinfo, ok2 := sensor[what].(SpaceInfo)
106 //~ sensorinfo[where] = value
109 //~ panic("Wrong Type of sensorinfo: Should never happen")
115 sensorobj[what] = make([]SpaceInfo, 1)
116 sensorobj[what].([]SpaceInfo)[0] = sensortype.(SpaceInfo)
118 panic("Wrong Type of sensorobj: Should never happen")
123 func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber, issuemail string) SpaceInfo {
124 nsi["contact"] = SpaceInfo{
129 "issue_mail": issuemail}
130 nsi["issue_report_channels"] = [3]string{"issue_mail","email","ml"}
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}
139 feedobj, ok := nsi["feeds"].(SpaceInfo) //type assertion (panics if false)
141 feedobj[feedtype] = newfeed
143 panic("Wrong Type of feedobj: Should never happen")
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
156 eventlist, ok := nsi["events"].([]SpaceInfo) //type assertion
158 if len(eventlist) >= max_num_events {
159 eventlist = eventlist[1:]
161 nsi["events"] = append(eventlist, newevent)
163 panic("Wrong Type of eventlist: Should never happen")
169 func (nsi SpaceInfo) AddSpaceAddress(address string) SpaceInfo {
170 nsi["address"] = address
171 if nsi["location"] != nil {
172 location, ok := nsi["location"].(SpaceInfo)
174 location["address"] = address
180 func (nsi SpaceInfo) SetStatus(open bool, status string) {
181 nsi["status"] = status
183 nsi["lastchange"] = time.Now().Unix()
184 state, ok := nsi["state"].(SpaceInfo)
186 state["message"] = status
188 state["lastchange"] = nsi["lastchange"]
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{}{
199 "lastchange": time.Now().Unix(),
202 "closed": closed_icon,
206 "lastchange":time.Now().Unix(),
209 "closed": closed_icon},
211 "location": SpaceInfo{
214 "contact" : SpaceInfo {},
219 func (data SpaceInfo) MakeJSON() ([]byte, error) {
220 msg, err := json.Marshal(data)