Last active
May 17, 2016 11:25
-
-
Save germanviscuso/b567173008d03e2a9d4a29c2e3d74fec to your computer and use it in GitHub Desktop.
Functions to send a dweet.io via a Kii server extension (useful for connecting to Freeboard)
This file contains hidden or 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
function createDweetFromKiiObject(thingid, kiiObject, dweetKey, workerCallback, doneCallback) { | |
var string = null; | |
$.each(kiiObject, function(key, val){ | |
if(key == "_customInfo") | |
string = JSON.stringify(val); | |
}); | |
if (string == null){ | |
console.log("Error: can't retrieve thing custom data for dweeting"); | |
if(doneCallback) doneCallback("Dweet error"); | |
} | |
else | |
createDweet(thingid, string, dweetKey, workerCallback, doneCallback); | |
} | |
function createDweet(thingid, payload, dweetKey, workerCallback, doneCallback) { | |
var dweetSite = "https://dweet.io:443"; | |
var dweetCreateAction = "/dweet/for/"; | |
var url = dweetSite + dweetCreateAction + thingid; | |
if(dweetKey) | |
url = url + "?key=" + dweetKey; | |
console.log("Posting to " + url + "..."); | |
// Send visualization data | |
$.ajax({ | |
url: url, | |
type: "POST", | |
data: payload, //as json string | |
contentType:"application/json; charset=utf-8", | |
dataType:"json", | |
success: function(body) { | |
console.log("Dweet sent!"); | |
if(workerCallback) workerCallback(body); | |
else if(doneCallback) doneCallback("SUCCESS!"); | |
}, | |
error: function(msg) { | |
console.log("Error Dweeting"); | |
if(doneCallback) doneCallback(msg); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment