node-red-contrib-vban
Version:
Nodes to interact with vban protocol
80 lines (79 loc) • 3.66 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 Sender_1 = require("../lib/Sender");
const ENodeStatus_1 = require("../lib/ENodeStatus");
const vban_1 = require("vban");
const NODE_NAME = 'vban-send-midi-or-serial';
class SendMidiOrSerial 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, _d;
try {
const payload = msg.payload;
let packet = {};
let data;
if (Buffer.isBuffer(payload)) {
data = payload;
}
else if (((_a = payload === null || payload === void 0 ? void 0 : payload.packet) === null || _a === void 0 ? void 0 : _a.data) && Buffer.isBuffer((_b = payload === null || payload === void 0 ? void 0 : payload.packet) === null || _b === void 0 ? void 0 : _b.data)) {
packet = payload === null || payload === void 0 ? void 0 : payload.packet;
data = packet.data;
}
else {
throw new Error('fail to found data from payload');
}
(_c = this.sender) === null || _c === void 0 ? void 0 : _c.send({
bps: packet.bps,
channelsIdents: packet.channelsIdents,
formatBit: packet.formatBit,
streamType: (_d = packet.streamType) !== null && _d !== void 0 ? _d : vban_1.ESerialStreamType.VBAN_SERIAL_GENERIC,
subProtocol: vban_1.ESubProtocol.SERIAL,
data,
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, SendMidiOrSerial);
};