UNPKG

hap-nodejs

Version:

HAP-NodeJS is a Node.js implementation of HomeKit Accessory Server.

145 lines 6.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = require(".."); class LightControllerClass { name = "Simple Light"; //name of accessory pincode = "031-45-154"; username = "FA:3C:ED:5A:1A:1A"; // MAC like address used by HomeKit to differentiate accessories. manufacturer = "HAP-NodeJS"; //manufacturer (optional) model = "v1.0"; //model (optional) serialNumber = "A12S345KGB"; //serial number (optional) power = false; //current power status brightness = 100; //current brightness hue = 0; //current hue saturation = 0; //current saturation outputLogs = true; //output logs setPower(status) { if (this.outputLogs) { console.log("Turning the '%s' %s", this.name, status ? "on" : "off"); } this.power = status; } getPower() { if (this.outputLogs) { console.log("'%s' is %s.", this.name, this.power ? "on" : "off"); } return this.power; } setBrightness(brightness) { if (this.outputLogs) { console.log("Setting '%s' brightness to %s", this.name, brightness); } this.brightness = brightness; } getBrightness() { if (this.outputLogs) { console.log("'%s' brightness is %s", this.name, this.brightness); } return this.brightness; } setSaturation(saturation) { if (this.outputLogs) { console.log("Setting '%s' saturation to %s", this.name, saturation); } this.saturation = saturation; } getSaturation() { if (this.outputLogs) { console.log("'%s' saturation is %s", this.name, this.saturation); } return this.saturation; } setHue(hue) { if (this.outputLogs) { console.log("Setting '%s' hue to %s", this.name, hue); } this.hue = hue; } getHue() { if (this.outputLogs) { console.log("'%s' hue is %s", this.name, this.hue); } return this.hue; } identify() { if (this.outputLogs) { console.log("Identify the '%s'", this.name); } } } const LightController = new LightControllerClass(); // Generate a consistent UUID for our light Accessory that will remain the same even when // restarting our server. We use the `uuid.generate` helper function to create a deterministic // UUID based on an arbitrary "namespace" and the word "light". const lightUUID = __1.uuid.generate("hap-nodejs:accessories:light" + LightController.name); // This is the Accessory that we'll return to HAP-NodeJS that represents our light. const lightAccessory = exports.accessory = new __1.Accessory(LightController.name, lightUUID); // Add properties for publishing (in case we're using Core.js and not BridgedCore.js) // @ts-expect-error: Core/BridgeCore API lightAccessory.username = LightController.username; // @ts-expect-error: Core/BridgeCore API lightAccessory.pincode = LightController.pincode; lightAccessory.category = 5 /* Categories.LIGHTBULB */; // set some basic properties (these values are arbitrary and setting them is optional) lightAccessory .getService(__1.Service.AccessoryInformation) .setCharacteristic(__1.Characteristic.Manufacturer, LightController.manufacturer) .setCharacteristic(__1.Characteristic.Model, LightController.model) .setCharacteristic(__1.Characteristic.SerialNumber, LightController.serialNumber); // listen for the "identify" event for this Accessory lightAccessory.on("identify" /* AccessoryEventTypes.IDENTIFY */, (paired, callback) => { LightController.identify(); callback(); }); // services exposed to the user should have "names" like "Light" for this case const lightbulb = lightAccessory.addService(__1.Service.Lightbulb, LightController.name); lightbulb.getCharacteristic(__1.Characteristic.On) .on("set" /* CharacteristicEventTypes.SET */, (value, callback) => { LightController.setPower(value); // Our light is synchronous - this value has been successfully set // Invoke the callback when you finished processing the request // If it's going to take more than 1s to finish the request, try to invoke the callback // after getting the request instead of after finishing it. This avoids blocking other // requests from HomeKit. callback(); }) // We want to intercept requests for our current power state, so we can query the hardware itself instead of // allowing HAP-NodeJS to return the cached Characteristic.value. .on("get" /* CharacteristicEventTypes.GET */, (callback) => { callback(null, LightController.getPower()); }); // To inform HomeKit about changes occurred outside of HomeKit (like user physically turn on the light) // Please use Characteristic.updateValue // // lightAccessory // .getService(Service.Lightbulb) // .getCharacteristic(Characteristic.On) // .updateValue(true); // also add an "optional" Characteristic for Brightness lightbulb.addCharacteristic(__1.Characteristic.Brightness) .on("set" /* CharacteristicEventTypes.SET */, (value, callback) => { LightController.setBrightness(value); callback(); }) .on("get" /* CharacteristicEventTypes.GET */, (callback) => { callback(null, LightController.getBrightness()); }); // also add an "optional" Characteristic for Saturation lightbulb.addCharacteristic(__1.Characteristic.Saturation) .on("set" /* CharacteristicEventTypes.SET */, (value, callback) => { LightController.setSaturation(value); callback(); }) .on("get" /* CharacteristicEventTypes.GET */, (callback) => { callback(null, LightController.getSaturation()); }); // also add an "optional" Characteristic for Hue lightbulb.addCharacteristic(__1.Characteristic.Hue) .on("set" /* CharacteristicEventTypes.SET */, (value, callback) => { LightController.setHue(value); callback(); }) .on("get" /* CharacteristicEventTypes.GET */, (callback) => { callback(null, LightController.getHue()); }); //# sourceMappingURL=Light_accessory.js.map