@drpollio/homebridge-reolink-camera-siren-and-light-nvr
Version:
Control alarm of Reolink cameras.
99 lines • 5.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReolinkSirenAndLightAccessory = void 0;
const reolink_1 = require("./reolink");
class ReolinkSirenAndLightAccessory {
constructor(platform, accessory) {
this.platform = platform;
this.accessory = accessory;
this.lastState = {
isOn: false,
brightLevel: 100,
};
this.hasCalledGetLightStatusBackground = false;
this.cameraConfig = this.accessory.context.device;
// set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Reolink')
.setCharacteristic(this.platform.Characteristic.Model, 'Default-Model')
.setCharacteristic(this.platform.Characteristic.SerialNumber, 'Default-Serial');
// Initialize Light Service if enabled in config
if (this.cameraConfig.exposeLightToHomeKit) {
this.lightService = this.accessory.getService(this.platform.Service.Lightbulb) ||
this.accessory.addService(this.platform.Service.Lightbulb);
this.lightService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.context.device.name} Light`);
this.lightService.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setLightOn.bind(this))
.onGet(this.getLightStatus.bind(this));
this.lightService.getCharacteristic(this.platform.Characteristic.Brightness)
.onSet(this.setBrightness.bind(this));
}
// Initialize Siren Service if enabled in config
if (this.cameraConfig.exposeSirenToHomeKit) {
this.sirenService = this.accessory.getService('Siren') ||
this.accessory.addService(this.platform.Service.Switch, 'Siren');
this.sirenService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.context.device.name} Siren`);
this.sirenService.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setSirenOn.bind(this));
}
this.platform.log.debug('Finished initializing accessory:', accessory.displayName);
}
async setSirenOn(value) {
const sirenState = value === true;
this.platform.log.info('Siren:' + this.cameraConfig);
await (0, reolink_1.sirenToggle)(this.cameraConfig, sirenState).catch(x => {
this.platform.log.error(x);
throw new this.platform.api.hap.HapStatusError(-70402 /* this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
}).then((response) => {
this.platform.log.info('Alarm set to camera');
this.platform.log.info(response);
});
this.platform.log.debug('Siren:', sirenState);
}
async setLightOn(value) {
var _a;
const lightState = value === true ? 1 : 0;
const brightState = ((_a = this.lightService) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.platform.Characteristic.Brightness).value) || 100;
await (0, reolink_1.setWhiteLed)(this.cameraConfig, lightState, brightState).catch(x => {
this.platform.log.error(x);
throw new this.platform.api.hap.HapStatusError(-70402 /* this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
});
}
async getLightStatus() {
if (!this.hasCalledGetLightStatusBackground) {
this.hasCalledGetLightStatusBackground = true;
this.getLightStatusBackground().catch(() => null).then(() => {
this.hasCalledGetLightStatusBackground = false;
});
}
return this.lastState.isOn;
}
async getLightStatusBackground() {
const statusLed = await (0, reolink_1.getWhiteLed)(this.cameraConfig).catch(x => {
this.platform.log.error(`Camera: ${this.cameraConfig.ip}`, x);
});
if (statusLed === undefined) {
return;
}
this.lastState = statusLed;
this.platform.log.debug(`StatusLed ${this.cameraConfig.ip}`, statusLed);
if (this.lightService) {
this.lightService
.updateCharacteristic(this.platform.Characteristic.On, statusLed.isOn)
.updateCharacteristic(this.platform.Characteristic.Brightness, statusLed.brightLevel);
}
}
async setBrightness(value) {
let brightState = value;
const lightState = brightState > 0 ? 1 : 0;
if (brightState === 0) {
brightState = 100;
}
await (0, reolink_1.setWhiteLed)(this.cameraConfig, lightState, brightState).catch(x => {
this.platform.log.error(x);
throw new this.platform.api.hap.HapStatusError(-70402 /* this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
});
}
}
exports.ReolinkSirenAndLightAccessory = ReolinkSirenAndLightAccessory;
//# sourceMappingURL=platformAccessory.js.map