UNPKG

@canboat/canboatjs

Version:

Native javascript version of canboat

119 lines 4.2 kB
"use strict"; /** * Copyright 2018 Scott Bender (scott@scottbender.net) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.W2K01Stream = W2K01Stream; const utilities_1 = require("./utilities"); const stream_1 = require("stream"); const toPgn_1 = require("./toPgn"); const n2k_actisense_1 = require("./n2k-actisense"); const util_1 = __importDefault(require("util")); //const pgnsSent = {} const N2K_ASCII = 0; const N2K_ACTISENSE = 1; function W2K01Stream(options, type, outEvent) { if (this === undefined) { return new W2K01Stream(options, type, outEvent); } stream_1.Transform.call(this, { objectMode: true }); this.debug = (0, utilities_1.createDebug)('canboatjs:w2k01', options); this.debugData = (0, utilities_1.createDebug)('canboatjs:w2k01-data', options); this.sentAvailable = false; this.options = options; this.outEvent = outEvent || 'w2k-1-out'; this.format = type === 'ascii' ? N2K_ASCII : N2K_ACTISENSE; if (this.format === N2K_ASCII) { if (options.app) { options.app.on(this.options.outEevent || 'nmea2000out', (msg) => { if (typeof msg === 'string') { this.sendW2KPGN(msg); } else { this.sendPGN(msg); } options.app.emit('connectionwrite', { providerId: options.providerId }); }); options.app.on(options.jsonOutEvent || 'nmea2000JsonOut', (msg) => { this.sendPGN(msg); options.app.emit('connectionwrite', { providerId: options.providerId }); }); } } this.debug('started'); } W2K01Stream.prototype.send = function (msg) { this.debug('sending %s', msg); this.options.app.emit(this.outEvent, msg); }; W2K01Stream.prototype.sendPGN = function (pgn) { //const now = Date.now() //let lastSent = pgnsSent[pgn.pgn] if (this.format === N2K_ASCII) { const ascii = (0, toPgn_1.pgnToActisenseN2KAsciiFormat)(pgn); this.send(ascii + '\r\n'); } else { const buf = (0, toPgn_1.pgnToN2KActisenseFormat)(pgn); this.send(buf); } //pgnsSent[pgn.pgn] = now }; W2K01Stream.prototype.sendW2KPGN = function (msg) { if (this.format === N2K_ASCII) { const ascii = (0, toPgn_1.actisenseToN2KAsciiFormat)(msg); this.send(ascii + '\r\n'); } else { const buf = toPgn_1.actisenseToN2KActisenseFormat; this.send(buf); } }; util_1.default.inherits(W2K01Stream, stream_1.Transform); W2K01Stream.prototype._transform = function (chunk, encoding, done) { if (!this.sentAvailable && this.format === N2K_ASCII) { this.debug('emit nmea2000OutAvailable'); this.options.app.emit('nmea2000OutAvailable'); this.sentAvailable = true; } if (this.format === N2K_ASCII) { if (this.debugData.enabled) { this.debugData('Received: ' + chunk); } this.push(chunk); } else { (0, n2k_actisense_1.readN2KActisense)(chunk, this.plainText, this, (data) => { this.push(data); }); } done(); }; W2K01Stream.prototype.pipe = function (pipeTo) { if (!pipeTo.fromPgn) { this.plainText = true; } else { this.plainText = false; } return W2K01Stream.super_.prototype.pipe.call(this, pipeTo); }; W2K01Stream.prototype.end = function () { }; //# sourceMappingURL=w2k01.js.map