isobus-name-resolver-ts
Version:
Simple tool to parse an isobus name hexstring and split it into its components.
129 lines (128 loc) • 5.08 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (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;
const isoData = __importStar(require("./isoData.json"));
class IsobusName {
nameString;
dataBuffer = new ArrayBuffer(8);
dataView = new DataView(this.dataBuffer);
constructor(isobusname) {
if (typeof isobusname !== 'string') {
isobusname = IsobusName.buf2hex(isobusname);
}
this.nameString = isobusname.toUpperCase();
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.dataView.setUint32(4, parseInt(this.nameString.substring(0, 8), 16), true);
this.dataView.setUint32(0, parseInt(this.nameString.substring(8, 16), 16), true);
}
toString() {
let 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;
}
getDeviceClass() {
let byte7 = this.getByte(6);
return byte7 >> 1;
}
getDeviceClassLabel() {
return isoData.industryGroups?.[this.getIndustryGroup()]?.vehicleSystems?.[this.getDeviceClass()]?.label;
}
getManufacturerCode() {
let byte3 = this.getByte(2);
let byte4 = this.getByte(3);
return (byte4 << 3) | 0b111 & (byte3 >> 5);
}
getManufacturerCodeLabel() {
return isoData.manufacturers?.[this.getManufacturerCode()]?.label;
}
getIdentityNumber() {
let byte1 = this.getByte(0);
let byte2 = this.getByte(1);
let byte3 = this.getByte(2);
return byte1 | byte2 << 8 | (0b11111 & byte3) << 16;
}
getEcuInstance() {
let byte5 = this.getByte(4);
return byte5 & 0b111;
}
getFunctionInstance() {
let byte5 = this.getByte(4);
return (byte5 >> 3) & 0b11111;
}
getFunction() {
return this.getByte(5);
}
getFunctionLabel() {
if (isoData.independentFunctions.hasOwnProperty(this.getFunction())) {
return isoData.independentFunctions[this.getFunction()]?.label;
}
else {
return isoData.industryGroups?.[this.getIndustryGroup()]
?.vehicleSystems?.[this.getDeviceClass()]
?.functions?.[this.getFunction()]?.label;
}
}
getDeviceClassInstance() {
let byte8 = this.getByte(7);
return byte8 & 0b1111;
}
getIndustryGroup() {
let byte8 = this.getByte(7);
return (byte8 >> 4) & 0b111;
}
getIndustryGroupLabel() {
return isoData.industryGroups?.[this.getIndustryGroup()]?.label;
}
getSelfConfigurableAddress() {
let byte8 = this.getByte(7);
return (byte8 >> 7) & 0b1;
}
getSelfConfigurableAddressLabel() {
return (this.getSelfConfigurableAddress() === 1 ? 'true' : 'false');
}
getIsoNameString() {
return this.nameString;
}
getByte(bytenum) {
return this.dataView.getUint8(bytenum);
}
static buf2hex(buffer) {
return [...new Uint8Array(buffer)]
.map(x => x.toString(16).padStart(2, '0'))
.join('');
}
}
exports.IsobusName = IsobusName;