UNPKG

@iotize/device-com-socket.node

Version:

Socket communication protocol to communicate with an iotize

202 lines 8.02 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SocketProtocol = void 0; const byte_converter_1 = require("@iotize/common/byte-converter"); const api_1 = require("@iotize/tap/protocol/api"); const core_1 = require("@iotize/tap/protocol/core"); const net = __importStar(require("net")); const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const debug_1 = __importDefault(require("./debug")); const TAG = 'SocketProtocol'; class SocketProtocol extends core_1.QueueComProtocol { constructor(serverOptions) { super(); if (serverOptions) { this.setSocketOptions(serverOptions); } else { this._socketOptions = { host: SocketProtocol.DEFAULT_HOSTNAME, port: 2000, }; } this._receiveStream = new rxjs_1.Subject(); } get socketOptions() { return this._socketOptions; } setSocketOptions(options) { this._socketOptions = options; if (!this._socketOptions.host) { this._socketOptions.host = SocketProtocol.DEFAULT_HOSTNAME; } return this; } get options() { return this._options; } _createSocket() { const socket = new net.Socket(); socket.on('data', (data) => { debug_1.default(TAG, 'onData', data); this.notifyNewMessage(new Uint8Array(data)); }); socket.on('close', (data) => { debug_1.default(TAG, 'socket closed event. parameter: ', data); if (this.connectionState !== api_1.ConnectionState.DISCONNECTED) { this.setConnectionState(api_1.ConnectionState.DISCONNECTED); } }); socket.on('end', () => { debug_1.default(TAG, 'socket end event'); if (this.connectionState !== api_1.ConnectionState.DISCONNECTED) { this.setConnectionState(api_1.ConnectionState.DISCONNECTED); } }); socket.on('error', (error) => { const errorCode = error.code; debug_1.default(TAG, 'socket error: ', error); switch (errorCode) { case 'ECONNRESET': this.disconnect() .toPromise() .catch((err) => { debug_1.default(TAG, `Cannot disconnect error`, err); }); break; case 'EHOSTUNREACH': case 'EINVAL': default: if (this._connectionEmitter) { debug_1.default(TAG, 'error during connection, notify failure...'); this._connectionEmitter.error(error); this._connectionEmitter = undefined; } } }); return socket; } _connect(options) { if (this.socket) { this.socket.end(); } this.socket = this._createSocket(); return new rxjs_1.Observable((emitter) => { debug_1.default(TAG, 'connecting...', this._options); if (!this.socket) { emitter.error(new Error('Socket should be defined')); return; } this._connectionEmitter = emitter; this.socket.connect(this._socketOptions.port, this._socketOptions.host || 'localhost', () => { this._connectionEmitter = undefined; emitter.next(); emitter.complete(); }); }).pipe(operators_1.share()); } _disconnect(options) { return rxjs_1.defer(() => { if (this.socket) { this.socket.end(); } // if (this._receiveStream) { // this._receiveStream.complete(); // } return 'ok'; }); } // send(data: Uint8Array): Observable<Uint8Array> { // debug(TAG, "send()", data, " (length: ", data.length, ") buffer => ", data.buffer); // var p = this.write(data) // .then(() => { // return this.readOneValue(); // }) // .then((data) => { // debug("SocketProtocol", "Value read: ", data); // return data; // }); // return Observable.from(p); // } write(data) { const sendBuffer = Buffer.from(data.buffer, 0, data.length); const writePromise = new Promise((resolve, reject) => { try { if (!this.socket) { throw new Error('Socket should be defined'); // TODO proper error } if (!this.socket.writable) { throw new Error('Socket is not open or not writable'); // TODO proper error } debug_1.default(TAG, 'write()', sendBuffer); const writeSuccess = this.socket.write(sendBuffer, () => { debug_1.default(TAG, 'write', 'DONE'); resolve(sendBuffer); }); if (!writeSuccess) { throw new Error('Socket cannot write data due to unknown error'); // TODO proper error } } catch (err) { reject(err); } }); return writePromise; } read() { return this.readOneValue(this.options.send.timeout); } readOneValue(timeoutMs = 5000) { debug_1.default(TAG, 'readOneValue', 'INIT'); const startTime = new Date(); return this._receiveStream .pipe(operators_1.take(1), operators_1.timeout(timeoutMs), operators_1.tap((data) => debug_1.default(TAG, 'readOneValue', 'RESOLVING...', byte_converter_1.bufferToHexString(data))), operators_1.catchError((err) => { if (err.name === 'TimeoutError') { err = api_1.ComProtocol.Errors.timeoutError({ timeout: timeoutMs, protocol: this, startTime, msg: `Waiting for Tap response timeout after ${timeoutMs}ms`, }); } return rxjs_1.throwError(err); })) .toPromise(); } notifyNewMessage(message) { // if (!this._receiveStream){ // throw new Error("IllegalState no receive stream"); // } debug_1.default(`notifyNewMessage() ${byte_converter_1.bufferToHexString(message)}`); this._receiveStream.next(message); } receiveStream() { return this._receiveStream; } } exports.SocketProtocol = SocketProtocol; SocketProtocol.DEFAULT_HOSTNAME = 'localhost'; //# sourceMappingURL=socket-protocol.js.map