Today I discovered Cosm and I'm trying to get some data uploaded as a test.
I have a device (it's called HomeWizard) and I use it to remotely control switches and read weather and energy data.
Reading data from the device is done using HTTP and JSON.
I would like to read this data and send it to cosm.
If I query the device for anemometer data I receive:
{"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}
]
}
I've understood that I have to use IDs to upload this data, but there is no ID in this output. How can I upload and use the above data to Cosm?
Simple approach
From the given JSON data, take one of the elements of the
response
array:Mapping this data to Cosm data model implies a feed update of the following format:
This can be sent in a
PUT
request tohttp://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:
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:
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).