UNPKG

@canboat/canboatjs

Version:

Native javascript version of canboat

184 lines 6.54 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.Ydgw02Stream = Ydgw02Stream; const utilities_1 = require("./utilities"); const stream_1 = require("stream"); const fromPgn_1 = require("./fromPgn"); const yddevice_1 = require("./yddevice"); const toPgn_1 = require("./toPgn"); const canId_1 = require("./canId"); const util_1 = __importDefault(require("util")); //const pgnsSent = {} function Ydgw02Stream(options, type) { if (this === undefined) { return new Ydgw02Stream(options, type); } stream_1.Transform.call(this, { objectMode: true }); this.debug = (0, utilities_1.createDebug)('canboatjs:ydgw02', options); this.debugOut = (0, utilities_1.createDebug)('canboatjs:n2k-out', options); this.sentAvailable = false; this.options = options; this.outEvent = options.ydgwOutEvent || 'ydwg02-out'; this.device = undefined; this.fromPgn = new fromPgn_1.Parser(options); this.fromPgn.on('warning', (_pgn, _warning) => { //debug(`[warning] ${pgn.pgn} ${warning}`) }); this.fromPgn.on('error', (pgn, error) => { this.debug(`[error] ${pgn.pgn} ${error}`); }); if (options.app) { const outEvents = (this.options.outEvent || 'nmea2000out') .split(',') .map((event) => event.trim()); outEvents.forEach((event) => { options.app.on(event, (msg) => { if (typeof msg === 'string') { this.sendYdgwPGN(msg); } else { this.sendPGN(msg); } options.app.emit('connectionwrite', { providerId: options.providerId }); }); }); const jsonOutEvents = (options.jsonOutEvent || 'nmea2000JsonOut') .split(',') .map((event) => event.trim()); jsonOutEvents.forEach((event) => { options.app.on(event, (msg) => { this.sendPGN(msg); options.app.emit('connectionwrite', { providerId: options.providerId }); }); }); options.app.on('ydFullRawOut', (msgs) => { this.sendYdgwFullPGN(msgs); options.app.emit('connectionwrite', { providerId: options.providerId }); }); //this.sendString('$PDGY,N2NET_OFFLINE') if (type === 'usb') { // set ydnu to RAW mode options.app.emit(this.outEvent, Buffer.from([0x30, 0x0a])); } this.debug('started'); } } Ydgw02Stream.prototype.cansend = function (_msg) { return this.options.createDevice ? this.device && this.device.cansend : this.sentAvailable; }; Ydgw02Stream.prototype.sendString = function (msg, forceSend) { if (this.cansend() || forceSend === true) { this.debugOut('sending %s', msg); if (this.options.app.listenerCount('canboatjs:rawsend') > 0) { this.options.app.emit('canboatjs:rawsend', { knownSrc: this.device !== undefined, data: msg }); } this.options.app.emit(this.outEvent, msg); } }; Ydgw02Stream.prototype.sendPGN = function (pgn) { if (this.cansend() || pgn.forceSend === true) { //let now = Date.now() //let lastSent = pgnsSent[pgn.pgn] let msgs; if (pgn.ydFullFormat === true || this.device !== undefined) { if (pgn.src !== 254) { pgn.src = this.device.address; } msgs = (0, toPgn_1.pgnToYdgwFullRawFormat)(pgn); } else { msgs = (0, toPgn_1.pgnToYdgwRawFormat)(pgn); } msgs.forEach((raw) => { this.sendString(raw + '\r\n', pgn.forceSend); }); //pgnsSent[pgn.pgn] = now } }; Ydgw02Stream.prototype.sendYdgwFullPGN = function (msgs) { msgs.forEach((raw) => { this.sendString(raw + '\r\n'); }); }; Ydgw02Stream.prototype.sendYdgwPGN = function (msg) { let msgs; if (this.device != undefined) { msgs = (0, toPgn_1.actisenseToYdgwFullRawFormat)(msg); } else { msgs = (0, toPgn_1.actisenseToYdgwRawFormat)(msg); } msgs.forEach((raw) => { this.sendString(raw + '\r\n'); }); }; util_1.default.inherits(Ydgw02Stream, stream_1.Transform); Ydgw02Stream.prototype._transform = function (chunk, encoding, done) { const line = chunk.toString().trim(); if (line.length < 23) { //bad data done(); return; } if (this.options.createDevice === true) { if (this.device === undefined) { this.device = new yddevice_1.YdDevice(this.options); this.device.start(); } if (this.cansend() && line.charAt(13) === 'T') { //11:54:07.833 R 18eafffe 00 ee 00 const canId = (0, canId_1.parseCanIdStr)(line.slice(15, 23)); if (canId.src === this.device.address) { //ignore pgn we're transmitting done(); return; } } } else { if (this.sentAvailable === false) { this.sentAvailable = true; this.debug('emit nmea2000OutAvailable'); this.options.app.emit('nmea2000OutAvailable'); } if (line.charAt(13) === 'T') { //ignore pgns we're transmitting done(); return; } } const pgn = this.fromPgn.parseYDGW02(line); this.options.app.emit('canboatjs:rawoutput', line); if (pgn !== undefined) { this.push(pgn); this.options.app.emit(this.options.analyzerOutEvent || 'N2KAnalyzerOut', pgn); } done(); }; Ydgw02Stream.prototype.end = function () { }; //# sourceMappingURL=ydgw02.js.map