@canboat/canboatjs
Version:
Native javascript version of canboat
166 lines (163 loc) • 5.81 kB
JavaScript
"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 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.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) {
options.app.on(this.options.outEevent || 'nmea2000out', (msg) => {
if (typeof msg === 'string') {
this.sendYdgwPGN(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 });
});
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.debug('sending %s', 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) {
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');
});
/*
if ( !this.parser ) {
this.parser = new Parser()
let that = this
this.parser.on('error', (pgn, error) => {
console.error(`Error parsing ${pgn.pgn} ${error}`)
console.error(error.stack)
})
this.parser.on('pgn', (pgn) => {
let now = Date.now()
let lastSent = pgnsSent[pgn.pgn]
if ( !lastSent || now - lastSent > rateLimit ) {
pgnToYdwgRawFormat(pgn).forEach(raw => {
this.sendString(raw)
})
pgnsSent[pgn.pgn] = now
}
})
}
this.parser.parseString(msg)
*/
};
util_1.default.inherits(Ydgw02Stream, stream_1.Transform);
Ydgw02Stream.prototype._transform = function (chunk, encoding, done) {
const line = chunk.toString().trim();
//line = line.substring(0, line.length) // take off the \r
if (this.device === undefined && !this.sentAvailable) {
this.debug('emit nmea2000OutAvailable');
this.options.app.emit('nmea2000OutAvailable');
this.sentAvailable = true;
if (this.options.createDevice === true) {
this.device = new yddevice_1.YdDevice(this.options);
this.device.start();
}
}
this.options.app.emit('canboatjs:rawoutput', line);
const pgn = this.fromPgn.parseYDGW02(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