@curium.rocks/udp-emitter
Version:
A IDataEmitter implementation that wraps UDP transports
134 lines • 4.44 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.UDPEmitter = void 0;
var data_emitter_base_1 = require("@curium.rocks/data-emitter-base");
/**
* UDP Data Emitter, this emits messages received directly without any modification
*/
var UDPEmitter = /** @class */ (function (_super) {
__extends(UDPEmitter, _super);
/**
*
* @param {string} id unique identifier for emitter
* @param {string} name display name of emitter
* @param {string} desc longer description of emitter
* @param {Socket} udpSocket udp socket object
*/
function UDPEmitter(id, name, desc, socket) {
var _this = _super.call(this, id, name, desc) || this;
_this.socket = socket;
_this.bindHandlers();
return _this;
}
/**
* Bind the socket event handlers
*/
UDPEmitter.prototype.bindHandlers = function () {
this.socket.on('error', this.onError.bind(this));
this.socket.on('message', this.onMessage.bind(this));
this.socket.on('listening', this.onListening.bind(this));
};
/**
* Release event handlers
*/
UDPEmitter.prototype.releaseHandlers = function () {
this.socket.off('error', this.onError);
this.socket.off('message', this.onMessage);
this.socket.off('listening', this.onListening);
};
/**
* error handler
* @param {Error} error
*/
UDPEmitter.prototype.onError = function (error) {
console.log("Error " + error);
this.faulted();
};
/**
*
* @param {Buffer} message received message
* @param {RemoteInfo} info
*/
UDPEmitter.prototype.onMessage = function (message, info) {
this.connected();
this.lastBuffer = message;
this.notifyDataListeners(this.buildDataEvent(message));
};
/**
* handler for when socket enters listening state
*/
UDPEmitter.prototype.onListening = function () {
// TODO: move out to a log interface
console.log("UDP socket listening");
};
/**
* Apply settings
* @param {ISettings} settings
* @return {IExecutionResult}
*/
UDPEmitter.prototype.applySettings = function (settings) {
this.setName(settings.name);
this.setId(settings.id);
this.setCommLink(settings.commLink);
return Promise.resolve({
actionId: settings.actionId,
success: true
});
};
/**
* send a command
* @param {ICommand} command
*/
UDPEmitter.prototype.sendCommand = function (command) {
throw new Error('Method not implemented.');
};
/**
* Probe the latest status
* @return {Promise<ISatusEvent>}
*/
UDPEmitter.prototype.probeStatus = function () {
return Promise.resolve(this.buildStatusEvent());
};
/**
* Probe the latest information
* @return {Promise<IDataEvent>}
*/
UDPEmitter.prototype.probeCurrentData = function () {
return Promise.resolve(this.buildDataEvent(this.lastBuffer));
};
/**
* Return meta data about emitter
* @return {unknown}
*/
UDPEmitter.prototype.getMetaData = function () {
return {
listeningPort: this.socket.address().port,
listeningAddress: this.socket.address().address,
};
};
/**
* Clean up resources
*/
UDPEmitter.prototype.dispose = function () {
_super.prototype.dispose.call(this);
this.releaseHandlers();
};
return UDPEmitter;
}(data_emitter_base_1.BaseEmitter));
exports.UDPEmitter = UDPEmitter;
//# sourceMappingURL=udpEmitter.js.map