node-insim
Version:
An InSim library for NodeJS with TypeScript support
91 lines (90 loc) • 3.78 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 __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutGauge = void 0;
var lodash_defaults_1 = __importDefault(require("lodash.defaults"));
var tiny_typed_emitter_1 = require("tiny-typed-emitter");
var errors_1 = require("../errors");
var log_1 = require("../log");
var protocols_1 = require("../protocols");
var OutGaugePack_1 = require("./OutGaugePack");
var log = log_1.log.extend('outgauge');
var OutGauge = /** @class */ (function (_super) {
__extends(OutGauge, _super);
function OutGauge(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.timeout, timeout = _c === void 0 ? 0 : _c;
var _this = _super.call(this) || this;
_this._options = defaultOutGaugeOptions;
_this.connection = null;
_this.timeout = 0;
_this.timeout = timeout;
_this.on('connect', function () {
return log("Connected to ".concat(_this._options.Host, ":").concat(_this._options.Port));
});
_this.on('disconnect', function () {
return log("Disconnected from ".concat(_this._options.Host, ":").concat(_this._options.Port));
});
return _this;
}
OutGauge.prototype.connect = function (options) {
var _this = this;
this._options = (0, lodash_defaults_1.default)(options, defaultOutGaugeOptions);
log("Connecting to ".concat(this._options.Host, ":").concat(this._options.Port, "..."));
this.connection = new protocols_1.UDP({
host: this._options.Host,
port: this._options.Port,
timeout: this.timeout,
socketInitialisationMode: 'bind',
});
this.connection.connect();
this.connection.on('connect', function () {
_this.emit('connect');
});
this.connection.on('disconnect', function () {
_this.emit('disconnect');
});
this.connection.on('error', function (error) {
throw new errors_1.InSimError("UDP connection error: ".concat(error.message));
});
this.connection.on('data', function (data) { return _this.handleMessage(data); });
this.connection.on('timeout', function () {
_this.emit('timeout');
});
};
OutGauge.prototype.disconnect = function () {
log('Disconnecting...');
if (this.connection === null) {
log('Cannot disconnect - not connected');
return;
}
this.connection.disconnect();
};
OutGauge.prototype.handleMessage = function (data) {
var outGaugePack = new OutGaugePack_1.OutGaugePack();
this.emit('packet', outGaugePack.unpack(data));
};
return OutGauge;
}(tiny_typed_emitter_1.TypedEmitter));
exports.OutGauge = OutGauge;
OutGauge.defaultMaxListeners = 255;
var defaultOutGaugeOptions = {
Host: '127.0.0.1',
Port: 29998,
};