Skip to content

Instantly share code, notes, and snippets.

@francois-dibulo
Last active April 23, 2022 17:00
Show Gist options
  • Save francois-dibulo/57da35da200769ffed770e798bc2bed4 to your computer and use it in GitHub Desktop.
Save francois-dibulo/57da35da200769ffed770e798bc2bed4 to your computer and use it in GitHub Desktop.
Helper functions for the AirConsole API
/**
* Extending AirConsole with useful helper functions
* Include this script after the AirConsole API
* @author francois@n-dream.com
*/
(function() {
if (!window['AirConsole']) {
throw "AirConsole is not defined. Did you not include the API file?";
}
// ===================================================================================
// CUSTOM DEVICE STATES HELPER
// ===================================================================================
/**
* Merges the current custom data with new data of this device
* @param {Object} custom_data - E.g. { new_key: true, name: "Update me" }
*/
AirConsole.prototype.mergeCustomData = function(custom_data) {
var data = this.getCustomDeviceStateProperty() || {};
for (var prop in custom_data) {
data[prop] = custom_data[prop];
}
this.setCustomDeviceState(data);
};
/**
* Returns a certain property of the custom data or all custom data of a device
* @param {Number} device_id - The device ID of which you want the custom state. Default is this device.
* @param {String} key - The property name (Optional)
* @return {Mixed}
*/
AirConsole.prototype.getCustomDeviceStateProperty = function(device_id, key) {
var custom_data = this.getCustomDeviceState(device_id);
if (custom_data && key !== undefined) {
custom_data = custom_data[key];
}
return custom_data;
};
/**
* Returns the custom data (or a certain property) of the Screen device
* @param {String} key - The property name (Optional)
* @return {Mixed}
*/
AirConsole.prototype.getScreenCustomData = function(key) {
return this.getCustomDeviceStateProperty(AirConsole.SCREEN, key);
};
// ===================================================================================
// DEVICE HELPER FUNCTIONS
// ===================================================================================
/**
* Returns profile data and more for a controller device
* @param {Number|undefined} device_id
* @return {Object}
*/
AirConsole.prototype.getDeviceData = function(device_id) {
device_id = device_id || this.getDeviceId();
var data = {
uid: this.getUID(device_id),
device_id: device_id,
nickname: this.getNickname(device_id),
picture: this.getProfilePicture(device_id),
is_master: this.isMasterController(device_id),
is_premium: this.isPremium(device_id),
is_auth: this.isUserLoggedIn(device_id)
};
return data;
};
/**
* Returns True if this device is a master device
* @param {Number|undefined} device_id
* @return {Boolean}
*/
AirConsole.prototype.isMasterController = function(device_id) {
device_id = device_id || this.getDeviceId();
return this.getMasterControllerDeviceId() === device_id;
};
/**
* Returns device data of the master device
* @return {Boolean}
*/
AirConsole.prototype.getMasterController = function() {
var master_id = this.getMasterControllerDeviceId();
return this.getDeviceData(master_id);
};
/**
* Returns True if at least one premium device is connected
* @return {Boolean}
*/
AirConsole.prototype.hasPremiumDevice = function() {
return this.getPremiumDeviceIds().length > 0;
};
/**
* Returns True if this device is the screen
* @return {Boolean}
*/
AirConsole.prototype.isScreen = function() {
return this.getDeviceId() === AirConsole.SCREEN;
};
// ===================================================================================
// GENERAL HELPER FUNCTIONS
// ===================================================================================
/**
* Returns True if we are developing locally
* @param {Array} - (Optional) developer_hosts E.g. ["127.0.0.1", "localhost"] your localhost
* @return {Boolean}
*/
AirConsole.prototype.isDeveloperMode = function(developer_hosts) {
var screen_data = this.devices[AirConsole.SCREEN];
var is_developer = false;
if (screen_data) {
var url = screen_data.url;
developer_hosts = developer_hosts || ["10.0", "192.168", "127.0"];
for (var i = 0; i < developer_hosts.length; i++) {
if (url.indexOf(developer_hosts[i]) > -1) {
is_developer = true;
break;
}
}
}
return is_developer;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment