camstreamerlib
Version:
Helper library for CamStreamer ACAP applications.
125 lines (124 loc) • 5.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenetecAgent = void 0;
const errors_1 = require("../../errors/errors");
const utils_1 = require("../../internal/utils");
const GenetecAgent_1 = require("../../types/GenetecAgent");
class GenetecAgent {
settings;
baseUrl;
credentials;
constructor(options = {}) {
this.settings = {
protocol: options.protocol ?? 'http',
ip: options.ip ?? '127.0.0.1',
port: options.port ?? 80,
baseUri: options.baseUri ?? 'WebSdk',
user: options.user ?? 'root',
pass: options.pass ?? '',
appId: options.appId ?? '',
timeout: options.timeout ?? 10000,
};
this.baseUrl = `${this.settings.protocol}://${this.settings.ip}:${this.settings.port}/${this.settings.baseUri}`;
this.credentials = btoa(`${this.settings.user};${this.settings.appId}:${this.settings.pass}`);
}
async checkConnection() {
const requestOptions = this.getRequestOptions('GET');
const res = await this.fetchWithTimeout(new URL(this.baseUrl), requestOptions);
if (!res.ok) {
throw new errors_1.ErrorWithResponse(res);
}
const responseBody = await res.text();
const result = await GenetecAgent_1.successResponseSchema.safeParseAsync(JSON.parse(responseBody));
if (!result.success) {
throw new Error('Genetec connection test failed: ' + JSON.stringify(result.error.issues) + '\n' + responseBody);
}
}
async getAllCameraGuids() {
const requestOptions = this.getRequestOptions('GET');
const url = new URL(`${this.baseUrl}/report/EntityConfiguration`);
url.searchParams.set('q', 'EntityTypes@Camera');
const res = await this.fetchWithTimeout(url, requestOptions);
if (!res.ok) {
throw new errors_1.ErrorWithResponse(res);
}
const responseBody = await res.text();
const result = await GenetecAgent_1.cameraGuidsResponseSchema.safeParseAsync(JSON.parse(responseBody));
if (!result.success) {
throw new Error('Genetec get camera guids failed: ' + JSON.stringify(result.error.issues) + '\n' + responseBody);
}
return result.data;
}
async getCameraDetails(guids, parameters) {
const params = parameters.join(',');
const requestOptions = this.getRequestOptions('GET');
const allCameras = [];
const chunkSize = 10;
while (guids.length > 0) {
const chunk = guids.slice(0, chunkSize);
guids.splice(0, chunkSize);
const url = new URL(`${this.baseUrl}/entity`);
url.searchParams.set('q', chunk.map((item) => `entity=${item.Guid},${params}`).join(','));
const res = await this.fetchWithTimeout(url, requestOptions);
if (!res.ok) {
throw new errors_1.ErrorWithResponse(res);
}
const responseBody = await res.text();
const result = await GenetecAgent_1.cameraDetailsResponseSchema.safeParseAsync(JSON.parse(responseBody));
if (!result.success) {
throw new Error('Genetec get camera details failed: ' + JSON.stringify(result.error.issues) + '\n' + responseBody);
}
const data = result.data;
const resultArray = Array.isArray(data.Rsp.Result) ? data.Rsp.Result : [data.Rsp.Result];
allCameras.push(...resultArray);
}
return allCameras;
}
async sendBookmark(guids, bookmarkText) {
const timeStamp = this.getTimeStamp();
const requestOptions = this.getRequestOptions('POST');
const url = new URL(`${this.baseUrl}/action`);
url.searchParams.set('q', guids.map((guid) => `AddCameraBookmark(${guid},${timeStamp},${bookmarkText})`).join(','));
const res = await this.fetchWithTimeout(url, requestOptions);
if (!res.ok) {
throw new errors_1.ErrorWithResponse(res);
}
const responseBody = await res.text();
const result = await GenetecAgent_1.successResponseSchema.safeParseAsync(JSON.parse(responseBody));
if (!result.success) {
throw new Error('Genetec send bookmark failed: ' + JSON.stringify(result.error.issues) + '\n' + responseBody);
}
}
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, utils_1.pad)(date.getUTCMonth() + 1, 2);
const day = (0, utils_1.pad)(date.getUTCDate(), 2);
const hours = (0, utils_1.pad)(date.getUTCHours(), 2);
const minutes = (0, utils_1.pad)(date.getUTCMinutes(), 2);
const seconds = (0, utils_1.pad)(date.getUTCSeconds(), 2);
const miliSeconds = (0, utils_1.pad)(date.getUTCMilliseconds(), 2);
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${miliSeconds}Z`;
}
async fetchWithTimeout(url, options) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.settings.timeout);
try {
return await fetch(url, { ...options, signal: controller.signal });
}
finally {
clearTimeout(timeoutId);
}
}
}
exports.GenetecAgent = GenetecAgent;