more cosmetic changes
[svn42.git] / r3-webstatus-spaceapi / spaceapi / spaceapi.go
index 2588a03..4162295 100644 (file)
@@ -10,46 +10,6 @@ const max_num_events int = 4
 
 type SpaceInfo map[string]interface{}
 
-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
@@ -64,60 +24,117 @@ type SpaceDoorAjarSensor struct {
     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")
-    }
-        
+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 {
-        listofwhats := make([]SpaceInfo, 1)
-        listofwhats[0] = sensortype.(SpaceInfo)
-               sensorobj := SpaceInfo{what: listofwhats}
-               nsi["sensors"] = sensorobj
-       } else {
-               sensorobj, ok := nsi["sensors"].(SpaceInfo) //type assertion (panics if false)
-               if ok {
-            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")
-                        //~ }
-                    //~ }
+        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
                 }
             }
-                       //else
-                       sensorobj[what] = make([]SpaceInfo, 1)
-            sensorobj[what].([]SpaceInfo)[0] = sensortype.(SpaceInfo)
-               } else {
-                       panic("Wrong Type of sensorobj: Should never happen")
-               }
-       }
+        }
+    }
 }
 
 func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber, issuemail string) SpaceInfo {
@@ -131,8 +148,8 @@ func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber, issuemai
        return nsi
 }
 
-func (nsi SpaceInfo) AddSpaceFeed(feedtype, mimetype, url string) SpaceInfo {
-       newfeed := SpaceInfo{"type": mimetype, "url": url}
+func (nsi SpaceInfo) AddSpaceFeed(feedtype, url string) SpaceInfo {
+       newfeed := SpaceInfo{"url": url}
        if nsi["feeds"] == nil {
         nsi["feeds"] = SpaceInfo{feedtype: newfeed}
        } else {