untested update to space api v0.13
authorBernhard Tittelbach <xro@realraum.at>
Mon, 17 Jun 2013 13:03:34 +0000 (13:03 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Mon, 17 Jun 2013 13:03:34 +0000 (13:03 +0000)
r3-webstatus-spaceapi/main.go
r3-webstatus-spaceapi/spaceapi/spaceapi.go

index 07bb194..114777e 100644 (file)
@@ -9,6 +9,7 @@ import (
        "net/url"
        "regexp"
        "time"
+    "strconv"
 )
 
 type SpaceState struct {
@@ -72,24 +73,22 @@ func parseSocketInputLine(line string) {
 
        if match_presence != nil {
                statusstate.present = (match_presence[1] == "yes")
+        spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("Torwaechter", "Front Door", match_presence[2] == "closed"))
                publishStateToWeb()
        } else if match_button != nil {
                statusstate.buttonpress_until = time.Now().Unix() + 3600
                spaceapidata.AddSpaceEvent("PanicButton", "check-in", "The button has been pressed")
                publishStateToWeb()
        } else if match_temp != nil {
-               spaceapidata.UpdateSensorData("temp", "Ceiling", match_temp[1]+"C")
-               //newtemp, err = strconv.ParseFloat((match_temp[1]), 32)
-               //if err == nil {
-               //      spaceapidata.UpdateSensorData("temp", "Ceiling", strconv.FormatFloat(newtemp, 'f', 2, 32)+"C")
-               //      spacestate.temperature = newtemp
-               //}
+               newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
+               if err == nil {
+            spaceapidata.MergeInSensor(spaceapi.MakeTempCSensor("Temp0","Ceiling",newtemp))
+               }
        } else if match_photo != nil {
-               spaceapidata.UpdateSensorData("light", "Front", match_photo[1])
-               //newphoto, err = strconv.ParseInt(match_photo[1], 10, 32)
-               //if err == nil {
-               //      spacestate.lightlevel = newphoto
-               //}
+               newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
+               if err == nil {
+                       spaceapidata.MergeInSensor(spaceapi.MakeIlluminationSensor("Photodiode","Ceiling","1024V/5V",newphoto))
+               }
        }
 }
 
index 2588a03..d32aa79 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 {