UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

108 lines (107 loc) 4.17 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.CamSwitcherEvents = void 0; const EventEmitter = require("events"); const WsClient_1 = require("./internal/WsClient"); const DefaultAgent_1 = require("./DefaultAgent"); class CamSwitcherEvents extends EventEmitter { constructor(options = {}) { var _a, _b, _c, _d, _e, _f; super(); this.tls = (_a = options.tls) !== null && _a !== void 0 ? _a : false; this.tlsInsecure = (_b = options.tlsInsecure) !== null && _b !== void 0 ? _b : false; this.ip = (_c = options.ip) !== null && _c !== void 0 ? _c : '127.0.0.1'; this.port = (_d = options.port) !== null && _d !== void 0 ? _d : (this.tls ? 443 : 80); this.user = (_e = options.user) !== null && _e !== void 0 ? _e : 'root'; this.pass = (_f = options.pass) !== null && _f !== void 0 ? _f : ''; this.client = new DefaultAgent_1.DefaultAgent(options); this.createWsClient(); EventEmitter.call(this); } connect() { this.ws.open(); } disconnect() { this.ws.close(); } resendInitData() { const request = { command: 'sendInitData', }; this.ws.send(JSON.stringify(request)); } createWsClient() { const options = { ip: this.ip, port: this.port, user: this.user, pass: this.pass, tls: this.tls, tlsInsecure: this.tlsInsecure, address: '/local/camswitcher/events', protocol: 'events', }; this.ws = new WsClient_1.WsClient(options); this.ws.on('open', () => __awaiter(this, void 0, void 0, function* () { try { const token = yield this.get('/local/camswitcher/ws_authorization.cgi'); this.ws.send(JSON.stringify({ authorization: token })); } catch (err) { this.emit('error', err); } })); this.ws.on('message', (data) => { try { const parsedData = JSON.parse(data.toString()); if (parsedData.type === 'authorization') { if (parsedData.state === 'OK') { this.emit('open'); } else { this.emit('error', new Error(data.toString())); } } else { this.emit('event', parsedData); } } catch (err) { this.emit('error', err); } }); this.ws.on('error', (err) => { this.emit('error', err); }); this.ws.on('close', () => { this.emit('close'); }); } get(path) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.get(path); if (res.ok) { const responseText = JSON.parse(yield res.text()); if (responseText.status === 200) { return responseText.data; } else { throw new Error(`Request (${path}) error, response: ${JSON.stringify(responseText)}`); } } else { throw new Error(JSON.stringify(res)); } }); } } exports.CamSwitcherEvents = CamSwitcherEvents;