UNPKG

@canboat/canboatjs

Version:

Native javascript version of canboat

124 lines 4.42 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; } 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); 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) { if (this.candevice) { if (!this.candevice.cansend && msg.pgn !== 59904 && msg.pgn !== 60928 && msg.pgn !== 126996) { debug('ignoring %j', msg); return; } debug('sending %j', msg); const src = msg.pgn === 59904 || 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 (debug.enabled) { const str = (0, stringMsg_1.toActisenseSerialFormat)(pgn.pgn, buffer, pgn.dst, pgn.src); debug(str); } if (buffer) { //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 }); }); } else { this.channel.send({ id: canid, ext: true, data: buffer }); } } } }; SimpleCan.prototype.sendActisenseFormat = function (msg) { this.sendPGN(msg); }; //# sourceMappingURL=simpleCan.js.map