X-Git-Url: https://git.realraum.at/?p=svn42.git;a=blobdiff_plain;f=r3-webstatus-spaceapi%2Fspaceapi%2Fspaceapi.go;h=4162295dcb29aeca8ec91dbf9facf012fe674d2b;hp=3a0f828098a3c2b1e4cc71ba8a9590e2f9d423ac;hb=098333f9a1d3fa389378a212dea39cf48030cd91;hpb=b30fa8619ab41857c9c15aa939e05f2e406b8192 diff --git a/r3-webstatus-spaceapi/spaceapi/spaceapi.go b/r3-webstatus-spaceapi/spaceapi/spaceapi.go index 3a0f828..4162295 100644 --- a/r3-webstatus-spaceapi/spaceapi/spaceapi.go +++ b/r3-webstatus-spaceapi/spaceapi/spaceapi.go @@ -10,54 +10,154 @@ 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 - } else { - sensorlist, 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") - } - } - } - //else - nsi["sensors"] = append(sensorlist, SpaceInfo{what: SpaceInfo{where: value}}) - } else { - panic("Wrong Type of sensorlist: Should never happen") - } +type SpaceDoorLockSensor struct { + value bool + location string + name string + description string +} + +type SpaceDoorAjarSensor struct { + value bool + location string + name string + description string +} + +func MakeTempSensor(name, where, unit string, value float64) SpaceInfo { + listofwhats := make([]SpaceInfo, 1) + listofwhats[0] = SpaceInfo{ + "value": value, + "unit": unit, + "location": where, + "name": name, + "description": ""} + return SpaceInfo{"temperature": listofwhats} +} + +func MakeTempCSensor(name, where string, value float64) SpaceInfo { + return MakeTempSensor(name,where,"\u00b0C",value) +} + +func MakeIlluminationSensor(name, where, unit string, value int64) SpaceInfo { + listofwhats := make([]SpaceInfo, 1) + listofwhats[0] = SpaceInfo{ + "value": value, + "unit": unit, + "location": where, + "name": name, + "description": ""} + return SpaceInfo{"ext_illumination": listofwhats} +} + +func MakePowerConsumptionSensor(name, where, unit string, value int64) SpaceInfo { + listofwhats := make([]SpaceInfo, 1) + listofwhats[0] = SpaceInfo{ + "value": value, + "unit": unit, + "location": where, + "name": name, + "description": ""} + return SpaceInfo{"power_consumption": listofwhats} +} + +func MakeNetworkConnectionsSensor(name, where, nettype string, value, machines int64) SpaceInfo { + listofwhats := make([]SpaceInfo, 1) + listofwhats[0] = SpaceInfo{ + "value": value, + "type": nettype, + "machines": machines, + "location": where, + "name": name, + "description": ""} + return SpaceInfo{"network_connections": listofwhats} +} + +func MakeMemberCountSensor(name, where string, value int64) SpaceInfo { + listofwhats := make([]SpaceInfo, 1) + listofwhats[0] = SpaceInfo{ + "value": value, + "location": where, + "name": name, + "description": ""} + return SpaceInfo{"total_member_count": listofwhats} +} + +func MakeDoorLockSensor(name, where string, value bool) SpaceInfo { + listofwhats := make([]SpaceInfo, 1) + listofwhats[0] = SpaceInfo{ + "value": value, + "location": where, + "name": name, + "description": ""} + return SpaceInfo{"door_locked": listofwhats} +} + +func MakeDoorAjarSensor(name, where string, value bool) SpaceInfo { + listofwhats := make([]SpaceInfo, 1) + listofwhats[0] = SpaceInfo{ + "value": value, + "location": where, + "name": name, + "description": ""} + return SpaceInfo{"ext_door_ajar": listofwhats} +} + +func (nsi SpaceInfo) MergeInSensor(sensorinfo SpaceInfo) { + if nsi["sensors"] == nil { + nsi["sensors"] = SpaceInfo{} + //~ listofwhats := make([]SpaceInfo, 1) + //~ listofwhats[0] = sensortype.(SpaceInfo) + //~ sensorobj := SpaceInfo{what: listofwhats} + //~ nsi["sensors"] = sensorobj } + sensorobj := nsi["sensors"].(SpaceInfo) + for what, subsensorobjlist := range sensorinfo { + if sensorobj[what] == nil { + sensorobj[what] = subsensorobjlist + } else { + existingsensorobjslist := sensorobj[what].([]SpaceInfo) + for _, newsensorobj := range subsensorobjlist.([]SpaceInfo) { + foundandsubstituted := false + for i:=0; i< len(existingsensorobjslist); i++ { + if existingsensorobjslist[i]["name"] == newsensorobj["name"] { + existingsensorobjslist[i] = newsensorobj + foundandsubstituted = true + } + } + if foundandsubstituted == false { + sensorobj[what] = append(sensorobj[what].([]SpaceInfo), newsensorobj) + //note that we do not change existingsensorobjslist here but directly sensorobj[what] !! + //the implications being that, if we have several newsensorobj in the list: + // 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 + // b) if the array sensorinfo[what] has several objects with the same name, nsi["sensors"] will also end up with these name conflicts + } + } + } + } } -func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber string) SpaceInfo { +func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber, issuemail string) SpaceInfo { nsi["contact"] = SpaceInfo{ "phone": phone, "email": email, "ml": ml, - "jabber": jabber} + "jabber": jabber, + "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, url string) SpaceInfo { + newfeed := SpaceInfo{"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 @@ -98,6 +198,12 @@ func (nsi SpaceInfo) SetStatus(open bool, status string) { nsi["status"] = status nsi["open"] = open nsi["lastchange"] = time.Now().Unix() + state, ok := nsi["state"].(SpaceInfo) + if ok { + state["message"] = status + state["open"] = open + state["lastchange"] = nsi["lastchange"] + } } func NewSpaceInfo(space string, url string, logo string, open_icon string, closed_icon string, lat float64, lon float64) SpaceInfo { @@ -108,13 +214,22 @@ func NewSpaceInfo(space string, url string, logo string, open_icon string, close "logo": logo, "open": false, "lastchange": time.Now().Unix(), - "icon": SpaceInfo{ - "open": open_icon, - "closed": closed_icon}, - "location": SpaceInfo{ - "lat": lat, - "lon": lon}, - "contact" : SpaceInfo {} } + "icon": SpaceInfo{ + "open": open_icon, + "closed": closed_icon, + }, + "state": SpaceInfo{ + "open": false, + "lastchange":time.Now().Unix(), + "icon": SpaceInfo{ + "open": open_icon, + "closed": closed_icon}, + }, + "location": SpaceInfo{ + "lat": lat, + "lon": lon}, + "contact" : SpaceInfo {}, + } return nsi }