Created
July 16, 2015 15:00
-
-
Save digitalhuman/236bf72218bbb16cbb4a to your computer and use it in GitHub Desktop.
Philips HueLabs - Javascript library
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
/** | |
* Philips HueLabs Javascript SDK | |
* @returns {Philips} | |
* @todo Extend with other parts of existing library | |
*/ | |
var Philips = function(){ | |
this.options = { | |
connected:false, | |
max_retries:5, | |
connect_retries:0, | |
bridge_username:null, | |
bridge_ipaddress:null, | |
request_protocol:"http", | |
connectIntervalId: null | |
}; | |
}; | |
/** | |
* Setup a connection to the Philips Hue bridge | |
* @param {object} options { | |
* success: function, | |
* fail: function, | |
* attempt function | |
* } | |
* @returns {Boolean} | |
*/ | |
Philips.prototype.connect = function(options){ | |
$.extend(this.options, options); | |
var self = this; | |
//Check and connect to the bridge | |
if(options.bridge_ipaddress !== null && options.bridge_username !== null){ | |
//Validate if current does not exists/authorized | |
$.getJSON(this.options.request_protocol + "://" + options.bridge_ipaddress + "/api/" + options.bridge_username, {}, | |
function(res){ | |
//Check if user is already authorized | |
if(typeof(res.length) === "undefined" || typeof(res[0].error) !== "object"){ | |
//We are connected | |
options.connected = true; | |
if(typeof(options.success) == "function"){ | |
options.success(res); | |
} | |
}else{ | |
if(typeof(res[0].error) === "object" && res[0].error.description.indexOf('unauthorized') >= 0){ | |
//We are not authorized. Let get authorized | |
self.connectIntervalId = setInterval(self._connectAttempt, 3000, self.options); | |
}else{ | |
if(typeof(options.fail) == "function"){ | |
options.fail(res); | |
} | |
} | |
} | |
}, | |
function(res){ | |
clearInterval(self.connectIntervalId); | |
if(typeof(options.fail) === "function"){ | |
options.fail("Existing user validation request failed."); | |
} | |
} | |
); | |
}else{ | |
if(typeof(options.fail) === "function"){ | |
options.fail("Bridge username or IP address not found."); | |
return false; | |
} | |
} | |
}; | |
/** | |
* Connect attemp | |
* @param {object} options | |
* @returns {Boolean} | |
*/ | |
Philips.prototype._connectAttempt = function(options){ | |
$.extend(this.options, options); | |
//Check max retries | |
if(options.connect_retries === options.max_retries){ | |
clearInterval(options.connectIntervalId); | |
if(typeof(options.fail) === "function"){ | |
options.fail("Maximum retries"); | |
} | |
return false; | |
} | |
//Attempt to connect | |
$.ajax({ | |
method:"POST", | |
url: options.request_protocol + "://" + options.bridge_ipaddress + "/api", | |
contentType:"text/plain; charset=UTF-8", | |
data: JSON.stringify({ | |
devicetype: "huelabs-user", | |
username: options.bridge_username | |
}), | |
dataType:"json", | |
processData: false, | |
crossDomain: true | |
}) | |
.done(function(res){ | |
//Response validation | |
if(typeof(res[0].error) === "object"){ | |
}else if(typeof(res[0].success) === "object"){ | |
clearInterval(options.connectIntervalId); | |
//Set connected | |
options.connected = true; | |
if(typeof(options.success) === "function"){ | |
options.success(res[0].success); | |
} | |
} | |
}) | |
.fail(function(res){ | |
clearInterval(options.connectIntervalId); | |
if(typeof(options.fail) === "function"){ | |
options.fail(JSON.stringify(res)); | |
} | |
}) | |
.always(function(res){ | |
if(typeof(options.attempt) === "function"){ | |
options.attempt(options.connect_retries); | |
} | |
}); | |
//Next attempt | |
options.connect_retries++; | |
}; | |
window.Philips = new Philips(); | |
/*** Example connection ****/ | |
function init(){ | |
Philips.connect({ | |
bridge_username: "philips-huelabs", | |
bridge_ipaddress: "192.168.0.1", | |
success: function(res){ | |
console.log(res); | |
}, | |
fail: function(res){ | |
// | |
}, | |
attempt: function(attempt){ | |
// | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment