From 7c22f62515e41750db14fff3181ab3452a6506bb Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Mon, 17 Jun 2013 00:35:19 +0000 Subject: [PATCH] unfinished update to space api v0.13 --- r3-webstatus-spaceapi/spaceapi/spaceapi.go | 139 ++++++++++++++++++++++------ 1 file changed, 110 insertions(+), 29 deletions(-) diff --git a/r3-webstatus-spaceapi/spaceapi/spaceapi.go b/r3-webstatus-spaceapi/spaceapi/spaceapi.go index 9455e84..2588a03 100644 --- a/r3-webstatus-spaceapi/spaceapi/spaceapi.go +++ b/r3-webstatus-spaceapi/spaceapi/spaceapi.go @@ -10,29 +10,112 @@ const max_num_events int = 4 type SpaceInfo map[string]interface{} -func (nsi SpaceInfo) UpdateSensorData(what, where, value string) { - if nsi["sensors"] == nil { - sensorlist := make([]SpaceInfo, 1) - sensorlist[0] = SpaceInfo{what: SpaceInfo{where: value}} - nsi["sensors"] = sensorlist +type SpaceTempSensor struct { + value string + unit string + location string + name string + description string +} + +type SpaceLightSensor struct { + value string + //~ unit string + location string + name string + description string +} + +type SpacePowerConsumptionSensor struct { + value string + unit string + location string + name string + description string +} + +type SpaceNetworkConnectionsSensor struct { + value string + nettype string + machines string + location string + name string + description string +} + +type SpaceMemberCountSensor struct { + value string + location string + name string + description string +} + +type SpaceDoorLockSensor struct { + value bool + location string + name string + description string +} + +type SpaceDoorAjarSensor struct { + value bool + location string + name string + description string +} + +func (nsi SpaceTempSensor) MakeTempSensor(what, name, where, value, unit string) { + +} + +func (nsi SpaceInfo) MergeInSensorData(sensortype interface{}) { + //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_" + var what string + switch sensortype := sensortype.(type) { + case SpaceTempSensor: + what = "temperature" + case SpaceLightSensor: + what = "ext_illumination" + case SpacePowerConsumptionSensor: + what = "power_consumption" + case SpaceNetworkConnectionsSensor: + what = "network_connections" + case SpaceMemberCountSensor: + what = "total_member_count" + case SpaceDoorLockSensor: + what = "door_locked" + case SpaceDoorAjarSensor: + what = "ext_door_ajar" + default: + panic("Unknown Sensor Type") + } + + if nsi["sensors"] == nil { + listofwhats := make([]SpaceInfo, 1) + listofwhats[0] = sensortype.(SpaceInfo) + sensorobj := SpaceInfo{what: listofwhats} + nsi["sensors"] = sensorobj } else { - sensorlist, ok := nsi["sensors"].([]SpaceInfo) //type assertion (panics if false) + sensorobj, ok := nsi["sensors"].(SpaceInfo) //type assertion (panics if false) if ok { - for _, sensor := range sensorlist { - if sensor[what] != nil { - sensorinfo, ok2 := sensor[what].(SpaceInfo) - if ok2 { - sensorinfo[where] = value - return - } else { - panic("Wrong Type of sensorinfo: Should never happen") - } - } - } + if sensorobj[what] != nil { + for _, sensor := range sensorobj { + //~ if sensor[what] != nil { + //~ sensorinfo, ok2 := sensor[what].(SpaceInfo) + //~ if ok2 { + //~ sensorinfo[where] = value + //~ return + //~ } else { + //~ panic("Wrong Type of sensorinfo: Should never happen") + //~ } + //~ } + } + } //else - nsi["sensors"] = append(sensorlist, SpaceInfo{what: SpaceInfo{where: value}}) + sensorobj[what] = make([]SpaceInfo, 1) + sensorobj[what].([]SpaceInfo)[0] = sensortype.(SpaceInfo) } else { - panic("Wrong Type of sensorlist: Should never happen") + panic("Wrong Type of sensorobj: Should never happen") } } } @@ -43,23 +126,21 @@ func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber, issuemai "email": email, "ml": ml, "jabber": jabber, - "issue-mail": issuemail} - nsi["issue-report-channels"] = [3]string{"issue-mail","email","ml"} + "issue_mail": issuemail} + nsi["issue_report_channels"] = [3]string{"issue_mail","email","ml"} return nsi } -func (nsi SpaceInfo) AddSpaceFeed(name, mimetype, url string) SpaceInfo { - newfeed := SpaceInfo{"name": name, "type": mimetype, "url": url} +func (nsi SpaceInfo) AddSpaceFeed(feedtype, mimetype, url string) SpaceInfo { + newfeed := SpaceInfo{"type": mimetype, "url": url} if nsi["feeds"] == nil { - feedlist := make([]SpaceInfo, 1) - feedlist[0] = newfeed - nsi["feeds"] = feedlist + nsi["feeds"] = SpaceInfo{feedtype: newfeed} } else { - feedlist, ok := nsi["feeds"].([]SpaceInfo) //type assertion (panics if false) + feedobj, ok := nsi["feeds"].(SpaceInfo) //type assertion (panics if false) if ok { - nsi["feeds"] = append(feedlist, newfeed) + feedobj[feedtype] = newfeed } else { - panic("Wrong Type of feedlist: Should never happen") + panic("Wrong Type of feedobj: Should never happen") } } return nsi -- 1.7.10.4