UNPKG

homebridge-ezviz

Version:

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

137 lines 6.07 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.auth = exports.getCameras = exports.getDomain = void 0; const axios_1 = __importDefault(require("axios")); const crypto_1 = __importDefault(require("crypto")); const querystring_1 = __importDefault(require("querystring")); const endpoints_1 = require("./endpoints"); function getDomain(id) { return __awaiter(this, void 0, void 0, function* () { const headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': endpoints_1.EZVIZEndpoints.USER_AGENT, clientType: 1, }; const domainReq = { headers: headers, method: 'POST', url: 'https://api.ezvizlife.com/api/area/domain', data: querystring_1.default.stringify({ areaId: id, }), }; const domain = (yield axios_1.default(domainReq)).data; return `https://${domain.domain}`; }); } exports.getDomain = getDomain; function getCameras(sessionId, domain, log) { return __awaiter(this, void 0, void 0, function* () { const cameras = []; const query = querystring_1.default.stringify({ filter: 'CONNECTION,SWITCH,STATUS,WIFI,NODISTURB,P2P,KMS,FEATURE,DETECTOR,VIDEO_QUALITY', groupId: -1, limit: 30, offset: 0, }); try { const info = yield endpoints_1.sendRequest(sessionId, domain, `${endpoints_1.EZVIZEndpoints.API_ENDPOINT_PAGELIST}?${query}`, 'GET'); if (info.deviceInfos && info.deviceInfos.length > 0) { const connection = info.CONNECTION; const status = info.STATUS; const switchInfo = info.SWITCH; for (const item of info.deviceInfos) { const camera = item; if (connection && connection.hasOwnProperty(camera.deviceSerial)) { camera.connection = connection[camera.deviceSerial]; } if (status && status.hasOwnProperty(camera.deviceSerial)) { camera.statusInfo = status[camera.deviceSerial]; } if (switchInfo && switchInfo.hasOwnProperty(camera.deviceSerial)) { camera.switch = switchInfo[camera.deviceSerial]; } cameras.push(camera); } } } catch (error) { endpoints_1.handleError(log, error, 'Error fetching cameras'); } return cameras; }); } exports.getCameras = getCameras; function auth(domain, email, password, log) { return __awaiter(this, void 0, void 0, function* () { const headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': endpoints_1.EZVIZEndpoints.USER_AGENT, clientType: 1, }; const emailHash = crypto_1.default.createHash('md5').update(password).digest('hex'); const passHash = crypto_1.default.createHash('md5').update(password).digest('hex'); const payload = { account: email, featureCode: emailHash, password: passHash, }; const req = { headers: headers, method: 'POST', url: `${domain}${endpoints_1.EZVIZEndpoints.API_ENDPOINT_AUTH}`, data: querystring_1.default.stringify(payload), }; try { const response = (yield axios_1.default(req)).data; if (response.retcode) { if (response.retcode === '1001') { log === null || log === void 0 ? void 0 : log.error('Login error: Incorrect login details'); } else if (response.retcode === '1002') { log === null || log === void 0 ? void 0 : log.error('Login error: Captcha required'); } else if (response.retcode === '1005') { log === null || log === void 0 ? void 0 : log.error('Login error: Incorrect Captcha code'); } else { log === null || log === void 0 ? void 0 : log.error(`Login error: ${response.retcode}`); } return ''; } if (response.meta && response.meta.code) { const code = response.meta.code; if (code === 6002) { log === null || log === void 0 ? void 0 : log.error('2 Factor Authentication accounts are not supported at this time.'); return ''; } } if (response.loginSession && response.loginSession.sessionId) { const login = response; return login.loginSession.sessionId; } else { log === null || log === void 0 ? void 0 : log.error(response); return ''; } } catch (error) { endpoints_1.handleError(log, error, 'Unable to login'); return ''; } }); } exports.auth = auth; //# sourceMappingURL=connection.js.map