Created
January 3, 2014 14:05
-
-
Save carlossg/8238325 to your computer and use it in GitHub Desktop.
Callback for NinjaBlocks devices that will post device data to Xively through Parse.com
http://forums.ninjablocks.com/index.php?p=/discussion/490/unofficial-how-to-posting-from-the-dashboard-to-cosm-pachube/p1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Parse.Cloud.define("hello", function(request, response) { | |
var xivelyKey = 'YOUR XIVELY KEY'; | |
var feedId = 713578059; | |
var device = request.params.D; | |
var channels = { | |
31: 'Temperature', | |
30: 'Humidity' | |
}; | |
var channel = channels[device]; | |
var incomingdata = request.params.DA; | |
var timest = new Date(request.params.timestamp); | |
var timest1 = timest.getFullYear()+'-'+("0" + (timest.getMonth() + 1)).slice(-2)+'-'+("0" + timest.getDate()).slice(-2) +'T'+("0"+timest.getHours()).slice(-2)+':'+("0"+timest.getMinutes()).slice(-2)+':'+("0"+timest.getSeconds()).slice(-2)+'Z'; | |
var datap = { datapoints: []}; | |
datap.datapoints.push | |
({ | |
"at" : timest1, | |
"value" : incomingdata | |
}); | |
var requestOptions = { | |
method: 'POST', | |
url: 'http://api.xively.com/v2/feeds/' + feedId + '/datastreams/' + channel + '/datapoints', | |
headers:{ | |
'X-ApiKey': xivelyKey, | |
'Content-Type': 'application/json' | |
}, | |
body: datap, | |
success: function(httpResponse) { | |
console.log(httpResponse.text); | |
response.success('Xively ' + channel + ' Updated!'); | |
}, | |
error: function(httpResponse) { | |
console.error('Request failed with response code ' + httpResponse.status); | |
response.error('Xively ' + channel + ' not updated'); | |
} | |
}; | |
Parse.Cloud.httpRequest(requestOptions); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment