Simple approach
From the given JSON data, take one of the elements of the response
array:
{ "t": "2013-04-27 01:40", "ws": 2.2, "gu": 0.0, "dir": 45}
Mapping this data to Cosm data model implies a feed update of the following format:
{
"version":"1.0.0",
"datastreams" : [
{
"current_value" : "2.2",
"at": "2013-04-27 01:40",
"id" : "ws"
},
{
"current_value" : "2.2",
"at": "2013-04-27 01:40",
"id" : "gu"
},
{
"current_value" : "45",
"at": "2013-04-27 01:40",
"id" : "dir"
}
]
}
This can be sent in a PUT
request to http://api.cosm.com/v2/feeds/:feed_id
(replace :feed_id
with the ID of the feed you have create via Cosm website).
You can implement remapping in a few slightly different ways, but this seems the simplest to start with. I have tested the example snippet of JSON and it works, despite the timestamp not being exactly the same format Cosm still parses it correctly.
So you will need to iterate on each of the items in response
array and remap that same way like above. First you should try this.
More efficient approach
The simple method described above can result in far too many requests being sent to Cosm. Those request will update 3 datastreams, hence it can be done with just 3 requests:
{
"version":"1.0.0",
"datapoints" : [
{ "value" : "-3.33", "at": "2013-04-28 21:40" },
{ "value" : "-3.31", "at": "2013-04-28 21:41" },
{ "value" : "-3.29", "at": "2013-04-28 21:42" },
{ "value" : "-3.27", "at": "2013-04-28 21:43" },
{ "value" : "-3.25", "at": "2013-04-28 21:44" },
{ "value" : "-3.23", "at": "2013-04-28 21:45" },
{ "value" : "-3.25", "at": "2013-04-28 21:46" },
{ "value" : "-3.27", "at": "2013-04-28 21:47" },
{ "value" : "-3.29", "at": "2013-04-28 21:48" },
{ "value" : "-3.33", "at": "2013-04-28 21:49" }
]
}
You will need to sent this in a PUT
request to the datastream endpoint and you will end-up making only 3 requests for the data given in your question:
PUT http://api.cosm.com/v2/feeds/:feed_id/datastreams/ws
PUT http://api.cosm.com/v2/feeds/:feed_id/datastreams/gu
PUT http://api.cosm.com/v2/feeds/:feed_id/datastreams/dir
JavaScript example
You can implement something similar to this:
var data_from_homewizard = {
"status": "ok",
"version": "2.352",
"request": {"route": "/wind" },
"response": [
{ "t": "2013-04-27 00:10", "ws": 0.8, "gu": 1.6, "dir": 157},
{ "t": "2013-04-27 00:25", "ws": 0.8, "gu": 1.6, "dir": 112},
{ "t": "2013-04-27 00:40", "ws": 0.9, "gu": 3.0, "dir": 112},
{ "t": "2013-04-27 00:55", "ws": 1.7, "gu": 2.7, "dir": 90},
{ "t": "2013-04-27 01:10", "ws": 1.1, "gu": 0.0, "dir": 90},
{ "t": "2013-04-27 01:25", "ws": 1.9, "gu": 0.0, "dir": 180},
{ "t": "2013-04-27 01:40", "ws": 2.2, "gu": 0.0, "dir": 45},
{ "t": "2013-04-27 01:55", "ws": 2.0, "gu": 3.0, "dir": 112},
{ "t": "2013-04-27 02:10", "ws": 2.0, "gu": 0.0, "dir": 90},
{ "t": "2013-04-27 02:25", "ws": 2.0, "gu": 3.0, "dir": 135},
{ "t": "2013-04-27 02:40", "ws": 2.4, "gu": 2.0, "dir": 67}
]
}
var ws_datapoints = [];
data_from_homewizard.response.forEach(function (i) {
ws_datapoints.push({ at: i.t, value: i.ws });
});
var gu_datapoints = [];
data_from_homewizard.response.forEach(function (i) {
gu_datapoints.push({ at: i.t, value: i.gu });
});
var dir_datapoints = [];
data_from_homewizard.response.forEach(function (i) {
dir_datapoints.push({ at: i.t, value: i.dir });
});
It should be rather easy to read, but you would probably want to write a nested loop. And, I am guessing, in this particular case you wouldn't want to run it in a browser. I used JavaScript as it seemed the easiest to describe the algorithm, instead of doing it pseudo code (so you can actually try it also).
You should also consider adjusting the timestamp format to ISO 8601, as that's the format Cosm requires to ensure the timestamps of your datapoints are stored correctly (e.g. timezone might be important to set, if the HomeWizard device time is local to wherever you are in the world).