UNPKG

homebridge-stagekit

Version:

Homebridge Plugin for the RockBand StageKit

507 lines 20.6 kB
"use strict"; const stagekit_1 = require("stagekit"); let hap; let Accessory; const PLUGIN_NAME = 'homebridge-stagekit'; const PLATFORM_NAME = 'stagekit'; class StageKitPlatform { constructor(log, config, api) { this.log = log; this.config = config; this.api = api; try { this.stageKit = new stagekit_1.StageKit(); } catch (ex) { this.log.error('Error connecting to StageKit: ' + ex); return; } api.on("didFinishLaunching", this.addAccessory.bind(this)); } ledsToInt(leds) { let binary = ''; for (let i = 0; i < leds; i++) { binary += '1'; } if (this.config.random_leds) { for (let i = 0; i < 8 - leds; i++) { const pos = Math.round(Math.random() * binary.length); binary = binary.slice(0, pos) + '0' + binary.slice(pos); } } return parseInt(binary, 2); } intToLeds(leds) { const binary = leds.toString(2); return (binary.match(/1/g) || []).length; } panic(state, callback) { var _a; if (!this.accessory) { return; } if (this.panicTimeout) { clearTimeout(this.panicTimeout); this.panicTimeout = undefined; } const party = this.accessory.getService('Party Mode'); if (party) { party.setCharacteristic(hap.Characteristic.On, false); } (_a = this.stageKit) === null || _a === void 0 ? void 0 : _a.AllOff(); const fog = this.accessory.getService('Fog Machine'); if (fog) { fog.updateCharacteristic(hap.Characteristic.On, false); } const strobe = this.accessory.getService('Strobe Light'); if (strobe) { strobe .updateCharacteristic(hap.Characteristic.On, false) .updateCharacteristic(hap.Characteristic.Brightness, 100); } const red = this.accessory.getService('Red Lights'); if (red) { red .updateCharacteristic(hap.Characteristic.On, false) .updateCharacteristic(hap.Characteristic.Brightness, 100); } const yellow = this.accessory.getService('Yellow Lights'); if (yellow) { yellow .updateCharacteristic(hap.Characteristic.On, false) .updateCharacteristic(hap.Characteristic.Brightness, 100); } const green = this.accessory.getService('Green Lights'); if (green) { green .updateCharacteristic(hap.Characteristic.On, false) .updateCharacteristic(hap.Characteristic.Brightness, 100); } const blue = this.accessory.getService('Blue Lights'); if (blue) { blue .updateCharacteristic(hap.Characteristic.On, false) .updateCharacteristic(hap.Characteristic.Brightness, 100); } if (callback) { callback(); } this.panicTimeout = setTimeout(() => { if (this.accessory) { const panic = this.accessory.getService('Panic'); if (panic) { panic.updateCharacteristic(hap.Characteristic.On, false); } this.panicTimeout = undefined; } }, 1000); } setFog(state, callback) { var _a; if (!this.accessory) { callback(); return; } if (this.fogTimeout) { clearTimeout(this.fogTimeout); this.fogTimeout = undefined; } (_a = this.stageKit) === null || _a === void 0 ? void 0 : _a.SetFog(state); callback(); if (state && this.config.fog_pulse_seconds) { this.fogTimeout = setTimeout(() => { if (this.accessory) { const fog = this.accessory.getService('Fog Machine'); if (fog) { fog.updateCharacteristic(hap.Characteristic.On, false); } this.fogTimeout = undefined; } }, this.config.fog_pulse_seconds * 1000); } } setStrobe(state, callback) { var _a; if (!this.accessory) { callback(); return; } if (this.strobeTimeout) { clearTimeout(this.strobeTimeout); this.strobeTimeout = undefined; } const strobeVal = Math.ceil(state / 25); (_a = this.stageKit) === null || _a === void 0 ? void 0 : _a.SetStrobe(strobeVal); callback(); this.strobeTimeout = setTimeout(() => { if (this.accessory) { const strobe = this.accessory.getService('Strobe Light'); if (strobe) { strobe.updateCharacteristic(hap.Characteristic.Brightness, strobeVal * 25); } this.strobeTimeout = undefined; } }, 1000); } setStrobeToggle(state, callback) { if (!this.accessory) { callback(); return; } const strobe = this.accessory.getService('Strobe Light'); if (strobe) { const on = strobe.getCharacteristic(hap.Characteristic.On).value; if (state && !on) { const strobeVal = strobe.getCharacteristic(hap.Characteristic.Brightness).value; this.setStrobe(strobeVal, callback); } else if (!state && on) { this.setStrobe(0, callback); } else { callback(); } } else { callback(); } } setRed(state, callback) { var _a; if (!this.accessory) { callback(); return; } if (this.redTimeout) { clearTimeout(this.redTimeout); this.redTimeout = undefined; } const redVal = Math.ceil(state / 12.5); (_a = this.stageKit) === null || _a === void 0 ? void 0 : _a.SetRed(this.ledsToInt(redVal)); callback(); this.redTimeout = setTimeout(() => { if (this.accessory) { const red = this.accessory.getService('Red Lights'); if (red) { red.updateCharacteristic(hap.Characteristic.Brightness, redVal * 12.5); } this.redTimeout = undefined; } }, 1000); } setRedToggle(state, callback) { if (!this.accessory) { callback(); return; } const red = this.accessory.getService('Red Lights'); if (red) { const on = red.getCharacteristic(hap.Characteristic.On).value; if (state && !on) { const redVal = red.getCharacteristic(hap.Characteristic.Brightness).value; this.setRed(redVal, callback); } else if (!state && on) { this.setRed(0, callback); } else { callback(); } } else { callback(); } } setYellow(state, callback) { var _a; if (!this.accessory) { callback(); return; } if (this.yellowTimeout) { clearTimeout(this.yellowTimeout); this.yellowTimeout = undefined; } const yellowVal = Math.ceil(state / 12.5); (_a = this.stageKit) === null || _a === void 0 ? void 0 : _a.SetYellow(this.ledsToInt(yellowVal)); callback(); this.yellowTimeout = setTimeout(() => { if (this.accessory) { const yellow = this.accessory.getService('Yellow Lights'); if (yellow) { yellow.updateCharacteristic(hap.Characteristic.Brightness, yellowVal * 12.5); } this.yellowTimeout = undefined; } }, 1000); } setYellowToggle(state, callback) { if (!this.accessory) { callback(); return; } const yellow = this.accessory.getService('Yellow Lights'); if (yellow) { const on = yellow.getCharacteristic(hap.Characteristic.On).value; if (state && !on) { const yellowVal = yellow.getCharacteristic(hap.Characteristic.Brightness).value; this.setYellow(yellowVal, callback); } else if (!state && on) { this.setYellow(0, callback); } else { callback(); } } else { callback(); } } setGreen(state, callback) { var _a; if (!this.accessory) { callback(); return; } if (this.greenTimeout) { clearTimeout(this.greenTimeout); this.greenTimeout = undefined; } const greenVal = Math.ceil(state / 12.5); (_a = this.stageKit) === null || _a === void 0 ? void 0 : _a.SetGreen(this.ledsToInt(greenVal)); callback(); this.greenTimeout = setTimeout(() => { if (this.accessory) { const green = this.accessory.getService('Green Lights'); if (green) { green.updateCharacteristic(hap.Characteristic.Brightness, greenVal * 12.5); } this.greenTimeout = undefined; } }, 1000); } setGreenToggle(state, callback) { if (!this.accessory) { callback(); return; } const green = this.accessory.getService('Green Lights'); if (green) { const on = green.getCharacteristic(hap.Characteristic.On).value; if (state && !on) { const greenVal = green.getCharacteristic(hap.Characteristic.Brightness).value; this.setGreen(greenVal, callback); } else if (!state && on) { this.setGreen(0, callback); } else { callback(); } } else { callback(); } } setBlue(state, callback) { var _a; if (!this.accessory) { callback(); return; } if (this.blueTimeout) { clearTimeout(this.blueTimeout); this.blueTimeout = undefined; } const blueVal = Math.ceil(state / 12.5); (_a = this.stageKit) === null || _a === void 0 ? void 0 : _a.SetBlue(this.ledsToInt(blueVal)); callback(); this.blueTimeout = setTimeout(() => { if (this.accessory) { const blue = this.accessory.getService('Blue Lights'); if (blue) { blue.updateCharacteristic(hap.Characteristic.Brightness, blueVal * 12.5); } this.blueTimeout = undefined; } }, 1000); } setBlueToggle(state, callback) { if (!this.accessory) { callback(); return; } const blue = this.accessory.getService('Blue Lights'); if (blue) { const on = blue.getCharacteristic(hap.Characteristic.On).value; if (state && !on) { const blueVal = blue.getCharacteristic(hap.Characteristic.Brightness).value; this.setBlue(blueVal, callback); } else if (!state && on) { this.setBlue(0, callback); } else { callback(); } } else { callback(); } } randomLeds() { var _a, _b, _c, _d; if (!this.accessory) { return; } const redVal = Math.round(Math.random() * 255); const yellowVal = Math.round(Math.random() * 255); const greenVal = Math.round(Math.random() * 255); const blueVal = Math.round(Math.random() * 255); (_a = this.stageKit) === null || _a === void 0 ? void 0 : _a.SetRed(redVal); (_b = this.stageKit) === null || _b === void 0 ? void 0 : _b.SetYellow(yellowVal); (_c = this.stageKit) === null || _c === void 0 ? void 0 : _c.SetGreen(greenVal); (_d = this.stageKit) === null || _d === void 0 ? void 0 : _d.SetBlue(blueVal); const red = this.accessory.getService('Red Lights'); if (red) { red .updateCharacteristic(hap.Characteristic.On, true) .updateCharacteristic(hap.Characteristic.Brightness, this.intToLeds(redVal) * 12.5); } const yellow = this.accessory.getService('Yellow Lights'); if (yellow) { yellow .updateCharacteristic(hap.Characteristic.On, true) .updateCharacteristic(hap.Characteristic.Brightness, this.intToLeds(yellowVal) * 12.5); } const green = this.accessory.getService('Green Lights'); if (green) { green .updateCharacteristic(hap.Characteristic.On, true) .updateCharacteristic(hap.Characteristic.Brightness, this.intToLeds(greenVal) * 12.5); } const blue = this.accessory.getService('Blue Lights'); if (blue) { blue .updateCharacteristic(hap.Characteristic.On, true) .updateCharacteristic(hap.Characteristic.Brightness, this.intToLeds(blueVal) * 12.5); } } partyMode(state, callback) { var _a, _b, _c, _d; if (!this.accessory) { callback(); return; } if (state) { if (!this.partyInterval) { this.partyInterval = setInterval(this.randomLeds.bind(this), this.config.party_mode_seconds * 1000); } } else { if (this.partyInterval) { clearInterval(this.partyInterval); this.partyInterval = undefined; } (_a = this.stageKit) === null || _a === void 0 ? void 0 : _a.SetRed(0); (_b = this.stageKit) === null || _b === void 0 ? void 0 : _b.SetYellow(0); (_c = this.stageKit) === null || _c === void 0 ? void 0 : _c.SetGreen(0); (_d = this.stageKit) === null || _d === void 0 ? void 0 : _d.SetBlue(0); const red = this.accessory.getService('Red Lights'); if (red) { red .updateCharacteristic(hap.Characteristic.On, false) .updateCharacteristic(hap.Characteristic.Brightness, 100); } const yellow = this.accessory.getService('Yellow Lights'); if (yellow) { yellow .updateCharacteristic(hap.Characteristic.On, false) .updateCharacteristic(hap.Characteristic.Brightness, 100); } const green = this.accessory.getService('Green Lights'); if (green) { green .updateCharacteristic(hap.Characteristic.On, false) .updateCharacteristic(hap.Characteristic.Brightness, 100); } const blue = this.accessory.getService('Blue Lights'); if (blue) { blue .updateCharacteristic(hap.Characteristic.On, false) .updateCharacteristic(hap.Characteristic.Brightness, 100); } } callback(); } configureAccessory(accessory) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; accessory.on("identify", () => { this.log(accessory.displayName + ' identify requested!'); }); (_a = accessory.getService('Panic')) === null || _a === void 0 ? void 0 : _a.getCharacteristic(hap.Characteristic.On).on('set', this.panic.bind(this)); (_b = accessory.getService('Fog Machine')) === null || _b === void 0 ? void 0 : _b.getCharacteristic(hap.Characteristic.On).on('set', this.setFog.bind(this)); (_c = accessory.getService('Strobe Light')) === null || _c === void 0 ? void 0 : _c.getCharacteristic(hap.Characteristic.Brightness).on('set', this.setStrobe.bind(this)); (_d = accessory.getService('Strobe Light')) === null || _d === void 0 ? void 0 : _d.getCharacteristic(hap.Characteristic.On).on('set', this.setStrobeToggle.bind(this)); (_e = accessory.getService('Red Lights')) === null || _e === void 0 ? void 0 : _e.getCharacteristic(hap.Characteristic.Brightness).on('set', this.setRed.bind(this)); (_f = accessory.getService('Red Lights')) === null || _f === void 0 ? void 0 : _f.getCharacteristic(hap.Characteristic.On).on('set', this.setRedToggle.bind(this)); (_g = accessory.getService('Yellow Lights')) === null || _g === void 0 ? void 0 : _g.getCharacteristic(hap.Characteristic.Brightness).on('set', this.setYellow.bind(this)); (_h = accessory.getService('Yellow Lights')) === null || _h === void 0 ? void 0 : _h.getCharacteristic(hap.Characteristic.On).on('set', this.setYellowToggle.bind(this)); (_j = accessory.getService('Green Lights')) === null || _j === void 0 ? void 0 : _j.getCharacteristic(hap.Characteristic.Brightness).on('set', this.setGreen.bind(this)); (_k = accessory.getService('Green Lights')) === null || _k === void 0 ? void 0 : _k.getCharacteristic(hap.Characteristic.On).on('set', this.setGreenToggle.bind(this)); (_l = accessory.getService('Blue Lights')) === null || _l === void 0 ? void 0 : _l.getCharacteristic(hap.Characteristic.Brightness).on('set', this.setBlue.bind(this)); (_m = accessory.getService('Blue Lights')) === null || _m === void 0 ? void 0 : _m.getCharacteristic(hap.Characteristic.On).on('set', this.setBlueToggle.bind(this)); let party = accessory.getService('Party Mode'); if (party && !this.config.party_mode_seconds) { accessory.removeService(party); party = undefined; } else if (!party && this.config.party_mode_seconds) { party = accessory.addService(hap.Service.Switch, 'Party Mode', 'Party Mode'); } if (party) { party.getCharacteristic(hap.Characteristic.On) .on('set', this.partyMode.bind(this)); } const eventfile = (_o = this.stageKit) === null || _o === void 0 ? void 0 : _o.eventfile; const accInfo = accessory.getService(hap.Service.AccessoryInformation); if (accInfo && eventfile) { accInfo.setCharacteristic(hap.Characteristic.SerialNumber, eventfile); } this.accessory = accessory; this.panic(); } addAccessory() { if (!this.accessory) { const uuid = hap.uuid.generate('RockBand StageKit'); const accessory = new Accessory('StageKit', uuid); const accInfo = accessory.getService(hap.Service.AccessoryInformation); if (accInfo) { accInfo .setCharacteristic(hap.Characteristic.Manufacturer, 'PDP') .setCharacteristic(hap.Characteristic.Model, 'RockBand StageKit'); } accessory.addService(hap.Service.Switch, 'Panic', 'Panic'); accessory.addService(hap.Service.Switch, 'Fog Machine', 'Fog Machine'); accessory.addService(hap.Service.Lightbulb, 'Strobe Light', 'Strobe Light') .addCharacteristic(hap.Characteristic.Brightness); accessory.addService(hap.Service.Lightbulb, 'Red Lights', 'Red Lights') .addCharacteristic(hap.Characteristic.Brightness); accessory.addService(hap.Service.Lightbulb, 'Yellow Lights', 'Yellow Lights') .addCharacteristic(hap.Characteristic.Brightness); accessory.addService(hap.Service.Lightbulb, 'Green Lights', 'Green Lights') .addCharacteristic(hap.Characteristic.Brightness); accessory.addService(hap.Service.Lightbulb, 'Blue Lights', 'Blue Lights') .addCharacteristic(hap.Characteristic.Brightness); this.configureAccessory(accessory); this.api.registerPlatformAccessories('homebridge-stagekit', 'stagekit', [accessory]); } } } module.exports = (api) => { hap = api.hap; Accessory = api.platformAccessory; api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, StageKitPlatform); }; //# sourceMappingURL=index.js.map