Files
homebridge/lib/api.js
2015-11-03 15:02:52 -08:00

105 lines
3.5 KiB
JavaScript

var hap = require("hap-nodejs");
var hapLegacyTypes = require("hap-nodejs/accessories/types.js");
var log = require("./logger")._system;
// The official homebridge API is the object we feed the plugin's exported initializer function.
module.exports = {
API: API
}
function API() {
this._accessories = {}; // this._accessories[pluginName.accessoryName] = accessory constructor
this._platforms = {}; // this._platforms[pluginName.platformName] = platform constructor
// expose HAP-NodeJS in its entirely for plugins to use instead of making Plugins
// require() it as a dependency - it's a heavy dependency so we don't want it in
// every single plugin.
this.hap = hap;
// we also need to "bolt on" the legacy "types" constants for older accessories/platforms
// still using the "object literal" style JSON.
this.hapLegacyTypes = hapLegacyTypes;
}
API.prototype.accessory = function(name) {
// if you passed the "short form" name like "Lockitron" instead of "homebridge-lockitron.Lockitron",
// see if it matches exactly one accessory.
if (name.indexOf('.') == -1) {
var found = [];
for (var fullName in this._accessories) {
if (fullName.split(".")[1] == name)
found.push(fullName);
}
if (found.length == 1) {
return this._accessories[found[0]];
}
else if (found.length > 1) {
throw new Error("The requested accessory '" + name + "' has been registered multiple times. Please be more specific by writing one of: " + found.join(", "));
}
else {
throw new Error("The requested accessory '" + name + "' was not registered by any plugin.");
}
}
else {
if (!this._accessories[name])
throw new Error("The requested accessory '" + name + "' was not registered by any plugin.");
return this._accessories[name];
}
}
API.prototype.registerAccessory = function(pluginName, accessoryName, constructor) {
var fullName = pluginName + "." + accessoryName;
if (this._accessories[fullName])
throw new Error("Attempting to register an accessory '" + fullName + "' which has already been registered!");
log.info("Registering accessory '%s'", fullName);
this._accessories[fullName] = constructor;
}
API.prototype.platform = function(name) {
// if you passed the "short form" name like "Lockitron" instead of "homebridge-lockitron.Lockitron",
// see if it matches exactly one platform.
if (name.indexOf('.') == -1) {
var found = [];
for (var fullName in this._platforms) {
if (fullName.split(".")[1] == name)
found.push(fullName);
}
if (found.length == 1) {
return this._platforms[found[0]];
}
else if (found.length > 1) {
throw new Error("The requested platform '" + name + "' has been registered multiple times. Please be more specific by writing one of: " + found.join(", "));
}
else {
throw new Error("The requested platform '" + name + "' was not registered by any plugin.");
}
}
else {
if (!this._platforms[name])
throw new Error("The requested platform '" + name + "' was not registered by any plugin.");
return this._platforms[name];
}
}
API.prototype.registerPlatform = function(pluginName, platformName, constructor) {
var fullName = pluginName + "." + platformName;
if (this._platforms[fullName])
throw new Error("Attempting to register a platform '" + fullName + "' which has already been registered!");
log.info("Registering platform '%s'", fullName);
this._platforms[fullName] = constructor;
}