import base64ToArrayBuffer from 'base64-arraybuffer'; import { decimalToBitfield, obisCodeValueToNumeric, numericObisFormat } from './binary'; import { ParserError } from './errors'; import { ChecksumValidation, ObisParser } from './ObisParser'; import { Parser } from './Parser'; import { ParserResult } from './ParserResult'; import type { RegisterModel } from './registers'; export class HexingParserBase extends Parser { anotherPossibleObis: object = { 'C.1.0': 'C.1.0.2', '15.8.0': '1.8.0', }; getCodeMapper() { switch (this.interpretationCode) { default: throw new ParserError('Invalid interpretation code'); } } getChecksumValidation() { return ChecksumValidation.NONE; } parse(aBase64String: string, recoverMeterNumber?: Function): ParserResult { const arrayBuffer = new Uint8Array(base64ToArrayBuffer.decode(aBase64String)); const arraySlice = arrayBuffer; const obisParser = new ObisParser(); const obisRegisters = obisParser.parse(arraySlice, { checksumValidation: this.getChecksumValidation(), }); if (recoverMeterNumber && obisRegisters.getMeterNumber()) { return recoverMeterNumber(numericObisFormat(obisRegisters.getMeterNumber())); } const result = new ParserResult(); const addValue = (entry: [RegisterModel, string]) => { const [recordModel, targetObis] = entry; let rawValue = obisRegisters.getValueOfObis(targetObis); if (rawValue == null) { const anotherPossibleValue = obisRegisters.getValueOfObis(this.anotherPossibleObis[targetObis]); if (anotherPossibleValue) { rawValue = anotherPossibleValue; } else { result.add(recordModel, null); return; } } let finalValue: string | null = rawValue; if (recordModel.format === 'numeric') { finalValue = obisCodeValueToNumeric(rawValue); } if (recordModel.format === 'bitfield') { finalValue = decimalToBitfield(rawValue); } if (recordModel.format === 'text') { finalValue = rawValue.trim(); } result.add(recordModel, finalValue); }; const mapperToUse: any = this.getCodeMapper(); for (const entry of mapperToUse) { addValue(entry); } return result; } }