node-red-contrib-vban
Version:
Nodes to interact with vban protocol
72 lines (71 loc) • 3.3 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 });
exports.VBANNode = void 0;
const ENodeStatus_1 = require("./ENodeStatus");
const Node_1 = require("./Node");
class VBANNode extends Node_1.Node {
get server() {
if (!this._server) {
throw new Error(`can't retrieve _server before init`);
}
return this._server;
}
get serverConfigured() {
return this._server != undefined;
}
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.definition.VBANServerNodeId) {
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, 'VBAN server not configured');
return;
}
const VBANServerNode = this.nodeRED.nodes.getNode(this.definition.VBANServerNodeId);
if (!VBANServerNode) {
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, 'fail to get server configuration');
return;
}
const { server } = VBANServerNode;
if (!server) {
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, 'fail to get server configuration');
return;
}
this._server = server;
this.setStatus(ENodeStatus_1.ENodeStatus.PENDING, 'starting VBAN server');
try {
this.server.on('error', (err) => {
this.node.error(err);
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, err.message);
});
this.server.on('listening', () => {
var _a, _b;
if (((_a = this.lastStatus) === null || _a === void 0 ? void 0 : _a.status) != ENodeStatus_1.ENodeStatus.ERROR) {
const address = (_b = this.server) === null || _b === void 0 ? void 0 : _b.address();
this.setStatus(ENodeStatus_1.ENodeStatus.READY, `listening on ${address === null || address === void 0 ? void 0 : address.address}:${address === null || address === void 0 ? void 0 : address.port}`);
}
});
VBANServerNode.startVBANServer();
}
catch (e) {
this.setStatus(ENodeStatus_1.ENodeStatus.ERROR, e.message);
return;
}
});
}
send(msg, sendFn) {
(sendFn || this.node.send.bind(this.node))(msg);
}
}
exports.VBANNode = VBANNode;