UNPKG

isobus-name-resolver-ts

Version:

Simple tool to parse an isobus name hexstring and split it into its components.

125 lines (124 loc) 6.28 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IsobusName = void 0; var buffer_1 = require("buffer/"); var isoData = __importStar(require("./isoData.json")); var IsobusName = /** @class */ (function () { function IsobusName(isobusname) { this.dataBuffer = buffer_1.Buffer.alloc(8); if (buffer_1.Buffer.isBuffer(isobusname)) { isobusname = isobusname.toString('hex'); } else if (typeof isobusname === 'object') { throw new TypeError('parameter "isobusname" is not a valid buffer, use https://github.com/feross/buffer'); } this.nameString = isobusname; if (this.nameString.match(new RegExp(/^[A-Fa-f0-9]+$/i)) == null || this.nameString.length !== 16) { throw new RangeError('Invalid ISOBUS NAME given. It must be a 64 bit number.'); } this.dataBuffer.writeUInt32LE(parseInt(this.nameString.substring(0, 8), 16), 4); this.dataBuffer.writeUInt32LE(parseInt(this.nameString.substring(8, 16), 16), 0); } IsobusName.prototype.toString = function () { var str = 'ISOBUS Name String: ' + this.nameString + '\n'; str += 'Device Class: ' + this.getDeviceClassLabel() + ' (' + this.getDeviceClass() + ')\n'; str += 'Manufacturer Code: ' + this.getManufacturerCodeLabel() + ' (' + this.getManufacturerCode() + ')\n'; str += 'Identity Number: ' + this.getIdentityNumber() + '\n'; str += 'ECU Instance: ' + this.getEcuInstance() + '\n'; str += 'Function Instance: ' + this.getFunctionInstance() + '\n'; str += 'Function: ' + this.getFunctionLabel() + ' (' + this.getFunction() + ')\n'; str += 'Device Class Instance: ' + this.getDeviceClassInstance() + '\n'; str += 'Industry Group: ' + this.getIndustryGroupLabel() + ' (' + this.getIndustryGroup() + ')\n'; str += 'Self Configurable Address: ' + this.getSelfConfigurableAddressLabel() + '\n'; return str; }; IsobusName.prototype.getDeviceClass = function () { var byte7 = this.getByte(6); return byte7 >> 1; }; IsobusName.prototype.getDeviceClassLabel = function () { var _a, _b, _c, _d; return (_d = (_c = (_b = (_a = isoData.industryGroups) === null || _a === void 0 ? void 0 : _a[this.getIndustryGroup()]) === null || _b === void 0 ? void 0 : _b.vehicleSystems) === null || _c === void 0 ? void 0 : _c[this.getDeviceClass()]) === null || _d === void 0 ? void 0 : _d.label; }; IsobusName.prototype.getManufacturerCode = function () { var byte3 = this.getByte(2); var byte4 = this.getByte(3); return (byte4 << 3) | 7 & (byte3 >> 5); }; IsobusName.prototype.getManufacturerCodeLabel = function () { var _a, _b; return (_b = (_a = isoData.manufacturers) === null || _a === void 0 ? void 0 : _a[this.getManufacturerCode()]) === null || _b === void 0 ? void 0 : _b.label; }; IsobusName.prototype.getIdentityNumber = function () { var byte1 = this.getByte(0); var byte2 = this.getByte(1); var byte3 = this.getByte(2); return byte1 | byte2 << 8 | (31 & byte3) << 16; }; IsobusName.prototype.getEcuInstance = function () { var byte5 = this.getByte(4); return byte5 & 7; }; IsobusName.prototype.getFunctionInstance = function () { var byte5 = this.getByte(4); return (byte5 >> 3) & 31; }; IsobusName.prototype.getFunction = function () { return this.getByte(5); }; IsobusName.prototype.getFunctionLabel = function () { var _a, _b, _c, _d, _e, _f, _g; if (isoData.independentFunctions.hasOwnProperty(this.getFunction())) { return (_a = isoData.independentFunctions[this.getFunction()]) === null || _a === void 0 ? void 0 : _a.label; } else { return (_g = (_f = (_e = (_d = (_c = (_b = isoData.industryGroups) === null || _b === void 0 ? void 0 : _b[this.getIndustryGroup()]) === null || _c === void 0 ? void 0 : _c.vehicleSystems) === null || _d === void 0 ? void 0 : _d[this.getDeviceClass()]) === null || _e === void 0 ? void 0 : _e.functions) === null || _f === void 0 ? void 0 : _f[this.getFunction()]) === null || _g === void 0 ? void 0 : _g.label; } }; IsobusName.prototype.getDeviceClassInstance = function () { var byte8 = this.getByte(7); return byte8 & 15; }; IsobusName.prototype.getIndustryGroup = function () { var byte8 = this.getByte(7); return (byte8 >> 4) & 7; }; IsobusName.prototype.getIndustryGroupLabel = function () { var _a, _b; return (_b = (_a = isoData.industryGroups) === null || _a === void 0 ? void 0 : _a[this.getIndustryGroup()]) === null || _b === void 0 ? void 0 : _b.label; }; IsobusName.prototype.getSelfConfigurableAddress = function () { var byte8 = this.getByte(7); return (byte8 >> 7) & 1; }; IsobusName.prototype.getSelfConfigurableAddressLabel = function () { return (this.getSelfConfigurableAddress() === 1 ? 'true' : 'false'); }; IsobusName.prototype.getIsoNameString = function () { return this.nameString; }; IsobusName.prototype.getByte = function (bytenum) { return this.dataBuffer.readUInt8(bytenum); }; return IsobusName; }()); exports.IsobusName = IsobusName;