homebridge-blynk-plugin
Version:
Based on Peter J Wojciechowski but updated to use the new API
98 lines • 4.16 kB
JavaScript
;
const poller_1 = require("./poller");
const accessories_1 = require("./accessories");
const config_1 = require("./config");
const PLUGIN_NAME = "homebridge-blynk-platform";
const PLATFORM_NAME = "BlynkPlatform";
let api;
let hap;
let Accessory;
let myConfig;
class BlynkPlatform {
constructor(log, config, homebridge) {
this.accs = [];
this.plugins = new Array();
this.platAccessories = [];
this.needToFetchConfigs = 0;
this.log = log;
myConfig = new config_1.BlynkConfig(homebridge.hap, this.log, config);
this.poll = new poller_1.BlynkPoller(log, myConfig.pollerSeconds, []);
this.needToFetchConfigs = myConfig.devices.length;
this.isConfigurtionReady();
api.on("shutdown", () => {
this.log.info(`${PLATFORM_NAME} is shutting down.`);
this.poll.shutdown();
});
api.on("didFinishLaunching", () => {
this.log.info(`${PLATFORM_NAME} has finishing launching.`);
this.fetchConfigs();
});
}
configureAccessory(accessory) {
this.accs.push(accessory);
}
fetchConfigs() {
myConfig.devices.forEach((device) => {
(async () => {
if (device.discover) {
await device.readProject().catch((error) => { this.log.error(`error reading project: ${error}`); });
}
this.needToFetchConfigs--;
device.widgets.forEach((widget) => {
const plugin = new accessories_1.BlynkAccessory(hap, this.log, widget);
const uuidSeed = (widget.getId() > 0)
? `${device.token}-${widget.getId().toString()}`
: `${device.deviceId}-${device.manufacturer}-${widget.getModel()}-${widget.getPinType()}-${widget.getPinNumber()}`;
const accId = hap.uuid.generate(uuidSeed);
this.log.debug(`PlatformAccessory: identified ${plugin.name}(${widget.getTypeOf()}) - ${widget.getId()} [${accId}] seed: ${uuidSeed}`);
let haveAcc = this.accs.find(accessory => accessory.UUID === accId);
if (!haveAcc) {
haveAcc = new Accessory(plugin.name, accId);
plugin.attachAccessory(haveAcc);
api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [haveAcc]);
}
else {
plugin.attachAccessory(haveAcc);
}
this.platAccessories.push(haveAcc);
this.plugins.push(plugin);
});
})();
});
}
isConfigurtionReady() {
const waitForConfigInMilliSeconds = 500;
this.log.debug(`Checking if config is ready: waiting for ${waitForConfigInMilliSeconds} ms remaining configs to fetch: ${this.needToFetchConfigs}`);
if (this.needToFetchConfigs <= 0) {
this.cleanUpAccessories();
}
else {
setTimeout(() => { this.isConfigurtionReady(); }, waitForConfigInMilliSeconds);
}
}
cleanUpAccessories() {
this.log.info(`Checking if accessories have been removed to clean up cache.`);
this.accs
.filter(orphan => !this.platAccessories.includes(orphan))
.forEach(orphan => {
this.log.info(`Removing accessory: ${orphan.displayName} - ${orphan.UUID}`);
api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [orphan]);
});
this.accs.length = 0;
this.platAccessories.forEach(acc => {
this.log.debug(`Committing defined accessory(${acc.displayName}) to cache`);
this.accs.push(acc);
});
this.poll
.setPollerAccessoryList(this.plugins)
.poll();
}
}
module.exports = (homebridge) => {
api = homebridge;
hap = homebridge.hap;
myConfig;
Accessory = homebridge.platformAccessory;
homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, BlynkPlatform);
};
//# sourceMappingURL=index.js.map