node-red-contrib-vban
Version:
Nodes to interact with vban protocol
96 lines (95 loc) • 3.89 kB
JavaScript
"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 });
const registerNode_1 = require("../lib/registerNode");
const VBANNode_1 = require("../lib/VBANNode");
const ENodeStatus_1 = require("../lib/ENodeStatus");
const Sender_1 = require("../lib/Sender");
const vban_1 = require("vban");
const NODE_NAME = 'vban-sender';
class VBANSender extends VBANNode_1.VBANNode {
init() {
const _super = Object.create(null, {
init: { get: () => super.init }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.init.call(this);
if (this.serverConfigured) {
this.sender = new Sender_1.Sender(this.server);
this.node.on('input', (msg) => {
const { payload } = msg;
this.sendVBANPacket(payload);
});
}
else {
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, 'VBAN server not configured');
}
});
}
sendVBANPacket(payload) {
var _a, _b;
if (!payload || !payload.packet) {
const text = 'no payload.packet received';
this.node.error(text, { payload });
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, text);
return;
}
const destination = (_a = payload.to) !== null && _a !== void 0 ? _a : { address: this.definition.address, port: this.definition.port };
if (!destination || !destination.address || !destination.port) {
const text = 'destination seems to be missing, or not correctly configured';
this.node.error(text, {
payload
});
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, text);
return;
}
const { packet } = payload;
if (!packet.subProtocol && packet.subProtocol !== vban_1.ESubProtocol.AUDIO) {
const text = 'packet need to contain a subProtocol';
this.node.error(text, {
payload: packet
});
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, text);
return;
}
if (this.definition.streamName) {
packet.streamName = this.definition.streamName;
}
else if (!packet.streamName) {
const text = 'packet need to contain a streamName';
this.node.error(text, {
payload: packet
});
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, text);
return;
}
try {
(_b = this.sender) === null || _b === void 0 ? void 0 : _b.send(packet, {
address: destination.address,
port: Number(destination.port)
});
}
catch (e) {
if (e instanceof Error) {
this.node.error(e.message, {
payload: packet
});
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, e.message);
}
else {
throw e;
}
}
}
}
module.exports = (RED) => {
(0, registerNode_1.registerNode)(RED, NODE_NAME, VBANSender);
};