homebridge-ezviz
Version:
EZVIZ plugin for homebridge: https://homebridge.io/
158 lines • 7.41 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const camera_1 = require("./ezviz/camera");
const camera_2 = require("./ezviz/models/camera");
const connection_1 = require("./ezviz/connection");
const accessory_1 = require("./accessory");
const HOUR = 3600000;
class Options {
constructor() {
this.sleepSwitch = true;
this.audioSwitch = true;
}
}
let hap;
let Accessory;
const PLUGIN_NAME = 'homebridge-ezviz';
const PLATFORM_NAME = 'EZVIZ';
class EZVIZPlatform {
constructor(log, config, api) {
this.ezvizObjects = [];
this.log = log;
this.api = api;
this.config = config;
this.options = new Options();
if (!config) {
return;
}
this.initDefaultOptions();
api.on("didFinishLaunching", this.didFinishLaunching.bind(this));
api.on("shutdown", this.isShuttingDown.bind(this));
}
initDefaultOptions() {
Object.keys(this.options).forEach((opt) => {
const key = opt;
if (this.config.options) {
const configVal = this.config.options[key];
if (typeof configVal === 'undefined') {
this.options[key] = true;
this.log.debug(`Defaulting ${key} to true`);
}
else {
this.options[key] = configVal;
this.log.debug(`Using ${key} from config: ${configVal}`);
}
}
});
}
configureAccessory(accessory) {
this.log.info(`Configuring accessory ${accessory.displayName}`);
accessory.on("identify", () => {
this.log.info(`${accessory.displayName} identified!`);
});
const cameraInfo = accessory.context.cameraInfo;
const camera = new camera_1.EZVIZCam(this.config, cameraInfo, this.log);
const ezvizAccessory = new accessory_1.EAVIZAccessory(accessory, camera, this.config, this.log, hap);
ezvizAccessory.configureController();
const sleepSwitch = camera.info.switch.find((x) => x.type === camera_2.SwitchTypes.Sleep);
if (sleepSwitch && this.options.sleepSwitch) {
ezvizAccessory.createSwitchService('Sleep Mode', hap.Service.Switch, camera_2.SwitchTypes.Sleep, (value) => __awaiter(this, void 0, void 0, function* () {
yield ezvizAccessory.toggleSleep(value);
}));
}
else {
ezvizAccessory.removeService(hap.Service.Switch, 'Sleep Mode');
}
const audioSwitch = camera.info.switch.find((x) => x.type === camera_2.SwitchTypes.Audio);
if (audioSwitch && this.options.audioSwitch) {
ezvizAccessory.createSwitchService('Audio', hap.Service.Switch, camera_2.SwitchTypes.Audio, (value) => __awaiter(this, void 0, void 0, function* () {
yield ezvizAccessory.toggleAudio(value);
}));
}
else {
ezvizAccessory.removeService(hap.Service.Switch, 'Audio');
}
this.ezvizObjects.push({ accessory: accessory, camera: camera });
}
addCameras(cameras) {
return __awaiter(this, void 0, void 0, function* () {
cameras.forEach((cameraInfo) => {
var _a, _b;
const uuid = hap.uuid.generate(cameraInfo.deviceSerial);
const displayName = cameraInfo.name.replace('(', '').replace(')', '');
const accessory = new Accessory(displayName, uuid);
cameraInfo.code = (_b = (_a = this.config.cameras) === null || _a === void 0 ? void 0 : _a.find((x) => x.serial === cameraInfo.deviceSerial)) === null || _b === void 0 ? void 0 : _b.code;
accessory.context.cameraInfo = cameraInfo;
const accessoryInformation = accessory.getService(hap.Service.AccessoryInformation);
if (accessoryInformation) {
accessoryInformation.setCharacteristic(hap.Characteristic.Manufacturer, 'EZVIZ');
accessoryInformation.setCharacteristic(hap.Characteristic.Model, cameraInfo.deviceSubCategory);
accessoryInformation.setCharacteristic(hap.Characteristic.SerialNumber, cameraInfo.deviceSerial);
accessoryInformation.setCharacteristic(hap.Characteristic.FirmwareRevision, cameraInfo.version);
}
if (!this.ezvizObjects.find((x) => x.accessory.UUID === uuid)) {
this.log.debug(`New camera found: ${cameraInfo.name}`);
this.configureAccessory(accessory);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
else {
const obj = this.ezvizObjects.find((x) => x.accessory.UUID === uuid);
if (obj) {
obj.camera.info = cameraInfo;
}
}
});
});
}
getSessionId() {
return __awaiter(this, void 0, void 0, function* () {
const region = this.config.region;
const email = this.config.email;
const password = this.config.password;
if (!email || !password) {
this.log.error('You must provide your email and password in config.json.');
return '';
}
this.config.domain = yield connection_1.getDomain(region);
return yield connection_1.auth(this.config.domain, email, password, this.log);
});
}
didFinishLaunching() {
return __awaiter(this, void 0, void 0, function* () {
const self = this;
const sessionId = yield this.getSessionId();
if (sessionId) {
this.config.sessionId = sessionId;
setInterval(() => __awaiter(this, void 0, void 0, function* () {
self.log.debug('Reauthenticating with config credentials');
this.config.sessionId = yield this.getSessionId();
}), HOUR * 12);
const cameras = yield connection_1.getCameras(this.config.sessionId, this.config.domain, this.log);
yield this.addCameras(cameras);
}
else {
this.log.error('Unable to retrieve access token.');
}
});
}
isShuttingDown() {
const accessoryObjects = this.ezvizObjects.map((x) => {
return x.accessory;
});
this.api.updatePlatformAccessories(accessoryObjects);
}
}
module.exports = (api) => {
hap = api.hap;
Accessory = api.platformAccessory;
api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, EZVIZPlatform);
};
//# sourceMappingURL=index.js.map