node-red-contrib-vban
Version:
Nodes to interact with vban protocol
74 lines (73 loc) • 3.25 kB
JavaScript
;
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 VBANNode_1 = require("../lib/VBANNode");
const registerNode_1 = require("../lib/registerNode");
const ENodeStatus_1 = require("../lib/ENodeStatus");
const Sender_1 = require("../lib/Sender");
const vban_1 = require("vban");
const NODE_NAME = 'vban-send-text';
class SendText 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.setListener();
}
else {
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, 'VBAN server not configured');
}
});
}
setListener() {
this.node.on('input', (msg, _send, done) => {
var _a, _b, _c;
try {
const payload = msg.payload;
let text = '';
if (typeof payload === 'string' || typeof payload === 'number') {
text = payload.toString();
}
else if (typeof ((_a = payload === null || payload === void 0 ? void 0 : payload.packet) === null || _a === void 0 ? void 0 : _a.text) === 'string') {
text = (_b = payload === null || payload === void 0 ? void 0 : payload.packet) === null || _b === void 0 ? void 0 : _b.text;
}
else {
throw new Error('fail to found text from payload');
}
(_c = this.sender) === null || _c === void 0 ? void 0 : _c.send({
subProtocol: vban_1.ESubProtocol.TEXT,
text,
streamName: this.definition.streamName
}, {
address: this.definition.address,
port: Number(this.definition.port)
});
}
catch (e) {
if (e instanceof Error) {
this.node.error(e.message, { payload: { payload: msg.payload, error: e } });
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, e.message);
done(e);
}
else {
throw e;
}
}
});
}
}
module.exports = (RED) => {
(0, registerNode_1.registerNode)(RED, NODE_NAME, SendText);
};