unfinished update to space api v0.13
[svn42.git] / r3-webstatus-spaceapi / spaceapi / spaceapi.go
index 3a0f828..2588a03 100644 (file)
@@ -10,54 +10,137 @@ 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")
                }
        }
 }
 
-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, 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
@@ -98,6 +181,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 +197,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
 }