UNPKG

@canboat/canboatjs

Version:

Native javascript version of canboat

120 lines (119 loc) 3.73 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const stringMsg_1 = require("../stringMsg"); const toPgn_1 = require("../toPgn"); const utilities_1 = require("../utilities"); const canId_1 = require("../canId"); const readline_1 = __importDefault(require("readline")); const minimist_1 = __importDefault(require("minimist")); const utils_1 = require("./utils"); const canSocket_1 = require("../canSocket"); const argv = (0, minimist_1.default)(process.argv.slice(2), { boolean: ['test', 'log-output'], string: ['src'], alias: { h: 'help' } }); (0, utils_1.printVersion)(argv); if (argv['help']) { console.error(`Usage: ${process.argv[0]} [options] candevice Options: --src <src> use src for all messages --log-output log messages sent --test don't connect or send any data -h, --help output usage information`); process.exit(1); } if (argv['_'].length === 0) { console.error('Please specify a device'); process.exit(1); } const canDevice = argv['_'][0]; const srcArg = argv.src; const logOut = argv['log-output']; const test = argv.test; let channel; if (!test) { channel = new canSocket_1.CanChannel(canDevice); channel.addListener('onStopped', (msg) => { console.error(`CAN channel stopped ${msg}`); }); channel.start(); } const rl = readline_1.default.createInterface({ input: process.stdin, output: process.stdout, terminal: false }); rl.on('line', function (line) { if (line.length === 0) { return; } const msg = line[0] === '{' ? JSON.parse(line) : line; if (typeof msg !== 'string') { if (msg.prio === undefined) { msg.prio = 3; } if (msg.dst === undefined) { msg.dst = 255; } if (srcArg !== undefined) { msg.src = srcArg; } if (msg.src === undefined) { msg.src = 100; } } let pgn, canid, buffer; if (typeof msg === 'object') { canid = (0, canId_1.encodeCanId)(msg); buffer = (0, toPgn_1.toPgn)(msg); if (buffer === undefined) { console.error('invalid input: %s', line); return; } pgn = msg; } else { pgn = (0, stringMsg_1.parseN2kString)(msg); if (isNaN(pgn.prio) || isNaN(pgn.pgn) || isNaN(pgn.dst) || isNaN(pgn.src)) { console.error('invalid input: ' + line); return; } if (srcArg !== undefined) { pgn.src = srcArg; } canid = (0, canId_1.encodeCanId)(pgn); buffer = pgn.data; } const timestamp = new Date().toISOString(); if (buffer == undefined) { console.error('unable to encode: %s', line); return; } else { if (buffer.length > 8 || pgn.pgn == 126720) { const pgns = (0, utilities_1.getPlainPGNs)(buffer); pgns.forEach((pbuffer) => { if (!test) { channel.send({ id: canid, ext: true, data: pbuffer }); } if (logOut) { console.log((0, utilities_1.binToActisense)(pgn, timestamp, pbuffer, pbuffer.length)); } }); } else { if (!test) { channel.send({ id: canid, ext: true, data: buffer }); } if (logOut) { console.log((0, utilities_1.binToActisense)(pgn, timestamp, buffer, buffer.length)); } } } }); //# sourceMappingURL=cansendjs.js.map