UNPKG

@canboat/canboatjs

Version:

Native javascript version of canboat

157 lines 5.89 kB
"use strict"; /** * Copyright 2025 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.SimpleCan = SimpleCan; const candevice_1 = require("./candevice"); const canId_1 = require("./canId"); const stringMsg_1 = require("./stringMsg"); const toPgn_1 = require("./toPgn"); const utilities_1 = require("./utilities"); const canSocket_1 = require("./canSocket"); const lodash_1 = __importDefault(require("lodash")); const debug = (0, utilities_1.createDebug)('canboatjs:simpleCan'); function SimpleCan(options, messageCb) { this.options = options; this.messageCb = messageCb; this.plainText = false; this.debug = (0, utilities_1.createDebug)('canboatjs:n2k-out', options); } SimpleCan.prototype.start = function () { const canDevice = this.options.canDevice || 'can0'; this.channel = new canSocket_1.CanChannel(canDevice); if (this.messageCb) { this.channel.addListener('onMessage', (msg) => { const pgn = (0, canId_1.parseCanId)(msg.id); // Drop frames the device emitted itself. Without this filter, the // device sees its own announces and heartbeats coming back in via // the socketcan loopback and treats them as real bus traffic. if (this.candevice && this.candevice.cansend && pgn.src == this.candevice.address) { return; } const timestamp = new Date().toISOString(); if (this.plainText) { this.messageCb((0, utilities_1.binToActisense)(pgn, timestamp, msg.data, msg.data.length)); } else { this.messageCb({ pgn, length: msg.data.length, data: msg.data }); } }); } this.channel.start(); this.candevice = new candevice_1.CanDevice(this, { ...this.options, disableDefaultTransmitPGNs: true, disableNAKs: true }); this.candevice.start(); }; SimpleCan.prototype.sendPGN = function (msg, force) { if (this.candevice) { if (!this.channel) { return; } if (!this.candevice.cansend && force !== true) { //we have not completed address claim yet return; } debug('sending %j', msg); if (this.options.app) { this.options.app.emit('connectionwrite', { providerId: this.options.providerId }); } const src = lodash_1.default.isString(msg) === false && msg.forceSrc ? msg.src : this.candevice.address; if (lodash_1.default.isString(msg)) { const split = msg.split(','); split[3] = src; msg = split.join(','); } else { msg.src = src; if (lodash_1.default.isUndefined(msg.prio)) { msg.prio = 3; } if (lodash_1.default.isUndefined(msg.dst)) { msg.dst = 255; } } let canid; let buffer; let pgn; if (lodash_1.default.isObject(msg)) { canid = (0, canId_1.encodeCanId)(msg); buffer = (0, toPgn_1.toPgn)(msg); pgn = msg; } else { pgn = (0, stringMsg_1.parseActisense)(msg); canid = (0, canId_1.encodeCanId)(pgn); buffer = pgn.data; } if (this.debug.enabled) { const str = (0, stringMsg_1.toActisenseSerialFormat)(pgn.pgn, buffer, pgn.dst, pgn.src); this.debug(str); } if (buffer === undefined) { this.debug("can't convert %j", msg); return; } //seems as though 126720 should always be encoded this way if (buffer.length > 8 || pgn.pgn == 126720) { const pgns = (0, utilities_1.getPlainPGNs)(buffer); pgns.forEach((pbuffer) => { this.channel.send({ id: canid, ext: true, data: pbuffer }); if (this.options.app && this.options.app.listenerCount('canboatjs:rawsend') > 0) { this.options.app.emit('canboatjs:rawsend', { knownSrc: true, data: { pgn, length: pbuffer.length, data: (0, utilities_1.byteStringArray)(pbuffer) } }); } }); } else { this.channel.send({ id: canid, ext: true, data: buffer }); if (this.options.app && this.options.app.listenerCount('canboatjs:rawsend') > 0) { this.options.app.emit('canboatjs:rawsend', { knownSrc: true, data: { pgn, length: buffer.length, data: (0, utilities_1.byteStringArray)(buffer) } }); } } } }; SimpleCan.prototype.sendActisenseFormat = function (msg) { this.sendPGN(msg); }; //# sourceMappingURL=simpleCan.js.map