camstreamerlib
Version:
Helper library for CamStreamer ACAP applications.
83 lines (82 loc) • 3.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VapixEvents = void 0;
const eventemitter2_1 = require("eventemitter2");
const WsClient_1 = require("./internal/WsClient");
class VapixEvents extends eventemitter2_1.EventEmitter2 {
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.createWsClient();
eventemitter2_1.EventEmitter2.call(this);
}
connect() {
this.ws.open();
}
disconnect() {
this.ws.close();
}
createWsClient() {
const options = {
tls: this.tls,
tlsInsecure: this.tlsInsecure,
user: this.user,
pass: this.pass,
ip: this.ip,
port: this.port,
address: '/vapix/ws-data-stream?sources=events',
};
this.ws = new WsClient_1.WsClient(options);
this.ws.on('open', () => {
const topics = [];
const eventNames = this.eventNames();
for (let i = 0; i < eventNames.length; i++) {
if (!this.isReservedEventName(eventNames[i])) {
const topic = {
topicFilter: eventNames[i],
};
topics.push(topic);
}
}
const topicFilter = {
apiVersion: '1.0',
method: 'events:configure',
params: {
eventFilterList: topics,
},
};
this.ws.send(JSON.stringify(topicFilter));
});
this.ws.on('message', (data) => {
const dataJSON = JSON.parse(data.toString());
if (dataJSON.method === 'events:configure') {
if (dataJSON.error === undefined) {
this.emit('open');
}
else {
this.emit('error', dataJSON.error);
this.disconnect();
}
return;
}
const eventName = dataJSON.params.notification.topic;
this.emit(eventName, dataJSON);
});
this.ws.on('error', (error) => {
this.emit('error', error);
});
this.ws.on('close', () => {
this.emit('close');
});
}
isReservedEventName(eventName) {
return eventName === 'open' || eventName === 'close' || eventName === 'error';
}
}
exports.VapixEvents = VapixEvents;