UNPKG

homebridge-ezviz

Version:

EZVIZ plugin for homebridge: https://homebridge.io/

79 lines 3.83 kB
"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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EZVIZCam = void 0; const events_1 = require("events"); const connection_1 = require("./connection"); const endpoints_1 = require("./endpoints"); const querystring_1 = __importDefault(require("querystring")); class EZVIZCam extends events_1.EventEmitter { constructor(config, info, log) { super(); this.log = log; this.config = config; this.info = info; this.lastUpdatedTime = new Date(); } setSwitchProperty(type, value) { var _a; return __awaiter(this, void 0, void 0, function* () { const query = querystring_1.default.stringify({ channel: 0, clientType: 1, enable: value, serial: this.info.deviceSerial, sessionId: this.config.sessionId, type: type, }); const response = yield endpoints_1.sendRequest(this.config.sessionId, this.config.domain, endpoints_1.EZVIZEndpoints.API_ENDPOINT_SWITCH_STATUS, 'POST', query); try { if (response.resultCode && response.resultCode !== '0') { (_a = this.log) === null || _a === void 0 ? void 0 : _a.error(`Unable to set property '${type}' for ${this.info.name} to ${value}`); return false; } const switchSwitch = this.info.switch.find((x) => x.type === type); if (switchSwitch) { switchSwitch.enable = value === 1; } return true; } catch (error) { endpoints_1.handleError(this.log, error, `Error setting property for ${this.info.name}`); } return false; }); } updateData() { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { const checkTime = new Date(this.lastUpdatedTime); checkTime.setSeconds(checkTime.getSeconds() + 1); if (new Date().getTime() < checkTime.getTime()) { return this.info; } const cameras = yield connection_1.getCameras(this.config.sessionId || '', this.config.domain, this.log); const camera = cameras.find((x) => x.deviceSerial === this.info.deviceSerial); if (camera) { camera.code = (_b = (_a = this.config.cameras) === null || _a === void 0 ? void 0 : _a.find((x) => x.serial === camera.deviceSerial)) === null || _b === void 0 ? void 0 : _b.code; if (!camera.code) { (_c = this.log) === null || _c === void 0 ? void 0 : _c.error(`Please add the verification code for ${camera.name} to your config.json. Instructions can be found on the Github.`); } this.info = camera; } return this.info; }); } } exports.EZVIZCam = EZVIZCam; //# sourceMappingURL=camera.js.map