UNPKG

security-camera-sdk

Version:

Universal SDK for interfacing with various security camera vendors including Hikvision, Dahua, Uniview and others

229 lines (199 loc) 6.33 kB
/** * 宇视平台 OpenAPI 常用接口封装 */ const { ParameterError } = require("../../utils/errors/cameraErrors"); const { Logger } = require("../../utils/logger"); /** * API接口常量 */ const API_PATHS = { // 认证相关 LOGIN: '/VIID/login', TOKEN_KEEP_ALIVE: '/VIID/token/alive/keep', // 资源管理 QUERY_RESOURCES: '/VIID/query', QUERY_RESOURCES_V2: '/VIID/query/V2', // 第三方设备管理 QUERY_THIRD_PARTY_IPC: '/VIID/hadesadapter/third/party/ec/v2/query', }; /** * API方法封装类 */ class UniviewAPI { constructor(client) { this.client = client; } // =================== 认证相关 =================== /** * 获取访问码 * @returns {Promise<Object>} 访问码信息 */ async getAccessCode() { const response = await this.client.rawPost(API_PATHS.LOGIN); return response.data; } /** * 执行登录操作 * @param {string} loginData 登录数据 * @returns {Promise<Object>} 登录结果 */ async performLogin(loginData) { const response = await this.client.rawPost(API_PATHS.LOGIN, loginData, { headers: { 'Content-Type': 'text/plain' }, }); return response.data; } /** * 保持Token活跃 * @returns {Promise<Object>} 保活结果 */ async keepTokenAlive() { const response = await this.client.get(API_PATHS.TOKEN_KEEP_ALIVE); return response.data; } // =================== 资源查询 =================== /** * 查询资源 * @param {Object} options 查询选项 * @param {string} options.org 组织编码 * @param {Object} options.condition 查询条件 * @returns {Promise<Object>} 查询结果 */ async queryResources(options = {}) { if (!options.condition) { throw new ParameterError("查询条件不能为空", "condition", options.condition); } // 将condition对象转换为JSON字符串 const params = { org: options.org || 'iccsid', condition: typeof options.condition === 'string' ? options.condition : JSON.stringify(options.condition) }; const response = await this.client.get(API_PATHS.QUERY_RESOURCES, params); return response.data; } /** * 查询所有资源(自动翻页) * @param {Object} options 查询选项 * @returns {Promise<Object>} 查询结果 */ async queryAllResources(options = {}) { const org = options.org || 'iccsid'; const condition = options.condition || {}; let allItems = []; let currentPage = 0; let total = 0; while (true) { // 更新当前页码 condition.PageFirstRowNumber = currentPage * (condition.PageRowNum || 100); try { const response = await this.queryResources({ org: org, condition: condition }); const items = response.Result?.InfoList || []; const pageInfo = response.Result?.RspPageInfo; allItems = allItems.concat(items); total = pageInfo?.TotalRowNum || 0; // 判断是否还有下一页 if (allItems.length >= total || items.length === 0) { break; } currentPage++; } catch (err) { throw err; } } return { ErrCode: 0, Result: { InfoList: allItems, RspPageInfo: { TotalRowNum: allItems.length, PageFirstRowNumber: 0, PageRowNum: allItems.length, } } }; } /** * 查询所有摄像机 * @param {Object} options 查询选项 * @returns {Promise<Object>} 查询结果 */ async queryAllCameras(options = {}) { const condition = { ItemNum: 2, // 相机查询 Condition: options.Condition || [ { QueryType: 256, // 资源类型:1=组织,1001=摄像机 LogicFlag: 0, QueryData: "1001" // "1001" 表示摄像机 }, { QueryType: 257, // 是否包含子组织 LogicFlag: 0, QueryData: "1" // "1" 表示查子组织 } ], QueryCount: options.QueryCount || 1, PageFirstRowNumber: options.PageFirstRowNumber || 0, PageRowNum: options.PageRowNum || 100 }; return await this.queryAllResources({ org: options.org, condition: condition }); } /** * 查询所有组织 * @param {Object} options 查询选项 * @param {string} options.org 组织编码 * @param {number} options.pageSize 每页大小 * @returns {Promise<Object>} 查询结果 */ async queryAllOrgs(options = {}) { const condition = { ItemNum: 3, // 组织查询 Condition: options.Condition || [ { QueryType: 256, // 资源类型:1=组织,1001=摄像机 LogicFlag: 0, QueryData: "1" // "1" 表示组织 }, { QueryType: 257, // 是否包含子组织 LogicFlag: 0, QueryData: "1" // "1" 表示查子组织 }, { QueryType: 1, // 组织编码(可留空或传具体编码) LogicFlag: 5, QueryData: "" // "" 表示从 org 参数指定的组织开始 } ], QueryCount: options.QueryCount || 1, PageFirstRowNumber: options.PageFirstRowNumber || 0, PageRowNum: options.PageRowNum || 200 }; return await this.queryAllResources({ org: options.org, condition: condition }); } /** * 查询第三方IPC设备信息 * @param {string} pszThirdPartyIPCCode IPC设备编码 * @returns {Promise<Object>} 查询结果 */ async queryThirdPartyIPC(pszThirdPartyIPCCode) { if (!pszThirdPartyIPCCode && typeof pszThirdPartyIPCCode !== 'string') { throw new ParameterError("IPC设备编码不能为空", "pszThirdPartyIPCCode", pszThirdPartyIPCCode); } const params = { pszThirdPartyIPCCode: pszThirdPartyIPCCode }; const response = await this.client.get(API_PATHS.QUERY_THIRD_PARTY_IPC, params); return response.data; } } module.exports = { UniviewAPI, API_PATHS };