UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

139 lines (138 loc) 6.49 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GenetecAgent = exports.cameraDetailsResponseSchema = exports.cameraDetailSchema = void 0; const zod_1 = require("zod"); const common_1 = require("../internal/common"); const ACTION = 'AddCameraBookmark'; const GET_CAMERAS_URL = 'report/EntityConfiguration?q=EntityTypes@Camera'; const GET_CAMERAS_DETAILS_URL = '/entity?q='; const cameraGuidsResponseSchema = zod_1.z.object({ Rsp: zod_1.z.object({ Status: zod_1.z.string(), Result: zod_1.z.array(zod_1.z.object({ Guid: zod_1.z.string() })), }), }); const connectionResponseSchema = zod_1.z.object({ Rsp: zod_1.z.object({ Status: zod_1.z.string(), }), }); exports.cameraDetailSchema = zod_1.z.object({ Guid: zod_1.z.string().optional(), Name: zod_1.z.string().optional(), EntityType: zod_1.z.string().optional(), }); exports.cameraDetailsResponseSchema = zod_1.z.object({ Rsp: zod_1.z.object({ Status: zod_1.z.string(), Result: zod_1.z.union([zod_1.z.array(exports.cameraDetailSchema), exports.cameraDetailSchema]), }), }); class GenetecAgent { constructor(options = {}) { var _a, _b, _c, _d, _e, _f, _g; this.settings = { protocol: (_a = options.protocol) !== null && _a !== void 0 ? _a : 'http', ip: (_b = options.ip) !== null && _b !== void 0 ? _b : '127.0.0.1', port: (_c = options.port) !== null && _c !== void 0 ? _c : 80, base_uri: (_d = options.base_uri) !== null && _d !== void 0 ? _d : 'WebSdk', user: (_e = options.user) !== null && _e !== void 0 ? _e : 'root', pass: (_f = options.pass) !== null && _f !== void 0 ? _f : '', app_id: (_g = options.app_id) !== null && _g !== void 0 ? _g : '', }; this.baseUrl = `${this.settings.protocol}://${this.settings.ip}:${this.settings.port}/${this.settings.base_uri}`; this.credentials = btoa(`${this.settings.user};${this.settings.app_id}:${this.settings.pass}`); } checkConnection() { return __awaiter(this, void 0, void 0, function* () { const requestOptions = this.getRequestOptions('GET'); const res = yield fetch(`${this.baseUrl}/`, requestOptions); if (!res.ok) { throw new Error(yield (0, common_1.responseStringify)(res)); } return connectionResponseSchema.parse(yield res.json()); }); } getAllCameraGuids() { return __awaiter(this, void 0, void 0, function* () { const requestOptions = this.getRequestOptions('GET'); const res = yield fetch(`${this.baseUrl}/${GET_CAMERAS_URL}`, requestOptions); if (!res.ok) { throw new Error(yield (0, common_1.responseStringify)(res)); } return cameraGuidsResponseSchema.parse(yield res.json()); }); } getCameraDetails(guids, parameters) { return __awaiter(this, void 0, void 0, function* () { const params = parameters.join(','); let camerasGuids = []; const requestOptions = this.getRequestOptions('GET'); const allCameras = []; const chunkSize = 10; while (guids.length > 0) { const chunk = guids.slice(0, chunkSize); guids.splice(0, chunkSize); camerasGuids = chunk.map((item) => item.Guid); const camerasDetailsUrl = []; for (const guid of camerasGuids) { camerasDetailsUrl.push(`entity=${guid},${params}`); } const res = yield fetch(`${this.baseUrl}/${GET_CAMERAS_DETAILS_URL}${camerasDetailsUrl.join(',')}`, requestOptions); if (!res.ok) { throw new Error(yield (0, common_1.responseStringify)(res)); } const data = exports.cameraDetailsResponseSchema.parse(yield res.json()); const resultArray = Array.isArray(data.Rsp.Result) ? data.Rsp.Result : [data.Rsp.Result]; allCameras.push(...resultArray); } return allCameras; }); } sendBookmark(guids, bookmarkText) { return __awaiter(this, void 0, void 0, function* () { const cameraEntitiesUrl = []; const timeStamp = this.getTimeStamp(); const requestOptions = this.getRequestOptions('POST'); for (const guid of guids) { cameraEntitiesUrl.push(`${ACTION}(${guid},${timeStamp},${bookmarkText})`); } const res = yield fetch(`${this.baseUrl}/action?q=${cameraEntitiesUrl.join(',')}`, requestOptions); if (!res.ok) { throw new Error(yield (0, common_1.responseStringify)(res)); } return res; }); } getRequestOptions(method) { return { method, headers: new Headers({ Authorization: `Basic ${this.credentials}`, Accept: 'text/json', }), redirect: 'follow', }; } getTimeStamp() { const date = new Date(); const year = date.getUTCFullYear(); const month = (0, common_1.pad)(date.getUTCMonth() + 1, 2); const day = (0, common_1.pad)(date.getUTCDate(), 2); const hours = (0, common_1.pad)(date.getUTCHours(), 2); const minutes = (0, common_1.pad)(date.getUTCMinutes(), 2); const seconds = (0, common_1.pad)(date.getUTCSeconds(), 2); const miliSeconds = (0, common_1.pad)(date.getUTCMilliseconds(), 2); return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${miliSeconds}Z`; } } exports.GenetecAgent = GenetecAgent;