sonologic / SpaceState

Android app and widget showing hackerspace status
6 stars 14 forks source link

Sensor description is weird #6

Open tdfischer opened 11 years ago

tdfischer commented 11 years ago

The definition of sensors doesn't make a whole lot of sense to me. The current spec says it should be an array of arrays, and the example given is this:

"sensors":[
  {"temp":
    {"hacklab":"8.5C","handwerklokaal":"20.5C","lounge":"7.5C"}
  }
],

For starters, that doesn't look like an array of arrays to me; more like an array of hashes of hashes.

I think it would make a lot more sense to make it a hash of arrays of key-value pairs:

"sensors": {
  "temp": [
    {"hacklab":"8.5C"},
    {"handwerklokaal":"20.5C"},
    {"lounge":"7.5C"}
  ]
}

As it currently stands, you need to linearly search the list of sensors, checking the keys of each hash to find the sensor type you want. You should be able to do this (in pseudocode):

myTemps = spaceInfo['sensors']['temp']

and get the list of all temperature sensors. The current way :

sensors = []
foreach(tmp in spaceInfo["sensors"]) {
  if tmp.hasKey("temp") {
    foreach(sensor in tmp["temp"].keys()) {
      sensors.append({sensor: tmp["temp"][sensor]})
    }
  }
}

This produces the same result but offloads a lot of the work onto the developer.

rrix commented 11 years ago

I agree with you, this is a strange oddity and should be solved. I think that your key-value setup is really nice, especially as my primary consumers are all JS :)

Can you submit a pull request and we'll incorporate it in to the next spec version?