UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

195 lines (194 loc) 7.92 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.CamOverlayAPI = exports.ImageType = void 0; const common_1 = require("./internal/common"); const DefaultAgent_1 = require("./DefaultAgent"); var ImageType; (function (ImageType) { ImageType[ImageType["PNG"] = 0] = "PNG"; ImageType[ImageType["JPEG"] = 1] = "JPEG"; })(ImageType || (exports.ImageType = ImageType = {})); class CamOverlayAPI { constructor(options = {}) { if ((0, common_1.isClient)(options)) { this.client = options; } else { this.client = new DefaultAgent_1.DefaultAgent(options); } } checkCameraTime() { return __awaiter(this, void 0, void 0, function* () { const cameraTime = yield this.get('/local/camoverlay/api/camera_time.cgi'); return cameraTime.state; }); } listImages() { return __awaiter(this, void 0, void 0, function* () { const images = yield this.get('/local/camoverlay/api/upload_image.cgi?action=list'); return images.list; }); } uploadImage(file, fileName) { return __awaiter(this, void 0, void 0, function* () { const formData = new FormData(); formData.append('target', 'SD0'); formData.append('uploadedFile[]', file, fileName); const path = '/local/camoverlay/api/upload_image.cgi?action=upload'; yield this.post(path, formData); }); } getNetworkCameraList() { return __awaiter(this, void 0, void 0, function* () { const response = yield this.get('/local/camoverlay/api/network_camera_list.cgi'); return response.camera_list; }); } updateInfoticker(serviceID, text) { return __awaiter(this, void 0, void 0, function* () { const path = `/local/camoverlay/api/infoticker.cgi?service_id=${serviceID}&text=${text}`; const res = yield this.client.get(path); if (!res.ok) { throw new Error(yield (0, common_1.responseStringify)(res)); } }); } setEnabled(serviceID, enabled) { return __awaiter(this, void 0, void 0, function* () { const path = `/local/camoverlay/api/enabled.cgi?id_${serviceID}=${enabled ? 1 : 0}`; const res = yield this.client.post(path, ''); if (!res.ok) { throw new Error(yield (0, common_1.responseStringify)(res)); } }); } isEnabled(serviceID) { return __awaiter(this, void 0, void 0, function* () { const path = '/local/camoverlay/api/services.cgi?action=get'; const res = yield this.client.get(path); if (res.ok) { const data = JSON.parse(yield res.text()); for (const service of data.services) { if (service.id === serviceID) { return service.enabled === 1; } } throw new Error('Service not found.'); } else { throw new Error(yield (0, common_1.responseStringify)(res)); } }); } getSingleService(serviceId) { return __awaiter(this, void 0, void 0, function* () { return this.get('/local/camoverlay/api/services.cgi', { action: 'get', service_id: serviceId.toString() }); }); } getServices() { return __awaiter(this, void 0, void 0, function* () { const serviceList = yield this.get('/local/camoverlay/api/services.cgi?action=get'); return serviceList.services; }); } updateSingleService(serviceId, serviceJson) { return __awaiter(this, void 0, void 0, function* () { const path = '/local/camoverlay/api/services.cgi'; yield this.post(path, JSON.stringify(serviceJson), { action: 'set', service_id: serviceId.toString(), }); }); } updateServices(servicesJson) { return __awaiter(this, void 0, void 0, function* () { const path = '/local/camoverlay/api/services.cgi?action=set'; const res = yield this.client.post(path, JSON.stringify(servicesJson)); if (!res.ok) { throw new Error(yield (0, common_1.responseStringify)(res)); } }); } updateCGText(serviceID, fields) { const params = {}; for (const field of fields) { const name = field.field_name; params[name] = field.text; if (field.color !== undefined) { params[`${name}_color`] = field.color; } } return this.promiseCGUpdate(serviceID, 'update_text', params); } updateCGImagePos(serviceID, coordinates = '', x = 0, y = 0) { const params = { coord_system: coordinates, pos_x: x, pos_y: y, }; return this.promiseCGUpdate(serviceID, 'update_image', params); } updateCGImage(serviceID, path, coordinates = '', x = 0, y = 0) { const params = { coord_system: coordinates, pos_x: x, pos_y: y, image: path, }; return this.promiseCGUpdate(serviceID, 'update_image', params); } updateCGImageFromData(serviceID, imageType, imageData, coordinates = '', x = 0, y = 0) { const contentType = imageType === ImageType.PNG ? 'image/png' : 'image/jpeg'; const params = { coord_system: coordinates, pos_x: x, pos_y: y, }; return this.promiseCGUpdate(serviceID, 'update_image', params, contentType, imageData); } promiseCGUpdate(serviceID, action, params = {}, contentType, data) { return __awaiter(this, void 0, void 0, function* () { const path = `/local/camoverlay/api/customGraphics.cgi`; let headers = {}; if (contentType !== undefined && data) { headers = { 'Content-Type': contentType }; } const res = yield this.client.post(path, data !== null && data !== void 0 ? data : '', Object.assign({ action: action, service_id: serviceID.toString() }, params), headers); if (!res.ok) { throw new Error(yield (0, common_1.responseStringify)(res)); } }); } get(path, params) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.get(path, params); if (res.ok) { return yield res.json(); } else { throw new Error(yield (0, common_1.responseStringify)(res)); } }); } post(path, data, params) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.post(path, data, params); if (res.ok) { return yield res.json(); } else { throw new Error(yield (0, common_1.responseStringify)(res)); } }); } } exports.CamOverlayAPI = CamOverlayAPI;