diff --git a/src/codetables.ts b/src/codetables.ts index 331b7b1..33055b9 100644 --- a/src/codetables.ts +++ b/src/codetables.ts @@ -71,25 +71,5 @@ export namespace MeterbusLibCodeTables { new DCFt('Special Function', 0, (x)=>{ return x}) ] -/* - DATA_FIELD_CODES = ( - ('No Data', 0, None), - ('8 Bit Integer', 1, lambda x: x[0]), - ('16 Bit Integer', 2, lambda x: (x[1] << 8) + x[0]), - ('24 Bit Integer', 3, lambda x: (x[2] << 16) + (x[1] << 8) + x[0]), - ('32 Bit Integer', 4, lambda x: (x[3] << 24) + (x[2] << 16) + (x[1] << 8) + x[0]), - ('32 Bit Real', 4, lambda x: struct.unpack('f', str(bytearray(x)))[0]), - ('48 Bit Integer', 6, lambda x: (x[5] << 40) + (x[4] << 32) + (x[3] << 24) + (x[2] << 16) + (x[1] << 8) + x[0]), - ('64 Bit Integer', 8, lambda x: (x[7] << 56) + (x[6] << 48) + (x[5] << 40) + (x[4] << 32) + (x[3] << 24) + (x[2] << 16) + (x[1] << 8) + x[0]), - ('Selection for Readout', 0, None), - ('2 Digit BCD', 1, lambda x: MeterbusTypeConversion.bcd(x)), - ('4 Digit BCD', 2, lambda x: MeterbusTypeConversion.bcd(x)), - ('6 Digit BCD', 3, lambda x: MeterbusTypeConversion.bcd(x)), - ('8 Digit BCD', 4, lambda x: MeterbusTypeConversion.bcd(x)), - ('variable length', -1, None), - ('12 Digit BCD', 6, lambda x: MeterbusTypeConversion.bcd(x)), - ('Special Function', 0, None), - ) -*/ } \ No newline at end of file diff --git a/src/longframe.ts b/src/longframe.ts index 89acf8e..2477514 100644 --- a/src/longframe.ts +++ b/src/longframe.ts @@ -16,24 +16,30 @@ export namespace MeterbusLibLongFrame { parse2() { super.parse2() // control frame parse2 method - this._fixedDataHeader = new FixedDataHeader(this._telegram.slice(7, 19)) + + let consumed : number = 7 // 68 L L 68 C A CI + this._fixedDataHeader = new FixedDataHeader(this._telegram.slice(consumed)) this._fixedDataHeader.parse() - let consumed : number = 0 + consumed += this._fixedDataHeader.consumed + let count : number = 0 while (true) { let die : DataBlock = - new DataBlock(this._telegram.slice(19 + consumed)) + new DataBlock(this._telegram.slice(consumed)) die.parse() this._variableDataBlocks.push(die) consumed += die.consumed - break //FIXME + count++ + if ((consumed) >= (this._telegram.length - 2)) { + break + } } } } export class FixedDataHeader { private _data : number[] - + private __consumed : number = 0 private _identNo : number private _manufacturer : string private _version : number @@ -42,6 +48,7 @@ export namespace MeterbusLibLongFrame { private _status : number private _signature : number[] + get identNo() : number { return this._identNo } get manufacturer() : string { return this._manufacturer } get version() : number { return this._version } @@ -54,23 +61,30 @@ export namespace MeterbusLibLongFrame { this._data = data } + get consumed() : number { + return this.__consumed + } + parse() : void { this._identNo = MeterbusLibUtils.bcd(this._data.slice(0, 4)) + this.__consumed += 4 this._manufacturer = MeterbusLibUtils.manufCode(this._data.slice(4, 6)) + this.__consumed += 2 this._version = this._data[6] + this.__consumed += 1 this._medium = MeterbusLibCodeTables.MEDIUM_CODE[this._data[7]] + this.__consumed += 1 this._accessNo = this._data[8] + this.__consumed += 1 this._status = this._data[9] + this.__consumed += 1 this._signature = this._data.slice(10, 12) + this.__consumed += 2 } toJSON() : any { return MeterbusLibUtils.jsonPrepaper(this, ["_data"]) } - - getJSON() : string { - return JSON.stringify(this) - } } export abstract class ADataBlock { @@ -83,20 +97,18 @@ export namespace MeterbusLibLongFrame { abstract parse() : void toJSON() : any { - console.log("x") return MeterbusLibUtils.jsonPrepaper(this, []) } - - getJSON() : string { - return JSON.stringify(this) - } } export class DataBlock extends ADataBlock { private _content : VariableDataBlock | ManufacturerSpecificData + private _hex : string = "" + private __data : number[] constructor(data : number[]) { super() + this.__data = data if ((data[0] & 0x0f) == 0x0f) { this._content = new ManufacturerSpecificData(data) } else { @@ -104,13 +116,21 @@ export namespace MeterbusLibLongFrame { } } + get consumed() : number { + return this._content.consumed + } parse() : void { this._content.parse() + + for (let d of this.__data.slice(0, this.consumed)) { + this._hex += "" + d.toString(16) + " " + } + } toJSON() : any { - return MeterbusLibUtils.jsonPrepaper(this._content, []) + return MeterbusLibUtils.jsonPrepaper(this, []) } } @@ -202,23 +222,26 @@ export namespace MeterbusLibLongFrame { } let dcf : MeterbusLibCodeTables.DCFt = MeterbusLibCodeTables.DIF_CODING_FIELD[this._dif.coding] + let actualLength = dcf.length + if (dcf.length == -1) { + actualLength = this.__indata[this.__consumed] + this.__consumed++ + } this._data = this.__indata.slice(this.__consumed, - this.__consumed + dcf.length) - this.__consumed += dcf.length + this.__consumed + actualLength) + this.__consumed += actualLength - console.log(dcf.encoder) this._value = dcf.encoder(this._data) - console.log(this._value) } } export class ManufacturerSpecificData extends ADataBlock { private _type : string = "MSD" - private _data : number[] + private __indata : number[] constructor(data : number[]) { super() - this._data = data + this.__indata = data } parse() : void { diff --git a/src/meterbus.test.ts b/src/meterbus.test.ts index b578df2..2d36aa1 100644 --- a/src/meterbus.test.ts +++ b/src/meterbus.test.ts @@ -180,7 +180,7 @@ describe('The Meterbus Longframe Library', () => { .to.deep.equal([0,0]) }) it('should prepare itself as JSON', () => { - console.log((telegram.frame as MeterbusLibLongFrame.LongFrame).getJSON()) - expect((telegram.frame as MeterbusLibLongFrame.LongFrame).getJSON()).to.equal(json_1phase_electric) + console.log(JSON.stringify(telegram.frame as MeterbusLibLongFrame.LongFrame, null, 2)) + expect(JSON.stringify(telegram.frame as MeterbusLibLongFrame.LongFrame)).to.equal(json_1phase_electric) }) }) diff --git a/src/simpleframes.ts b/src/simpleframes.ts index c76b557..2b33b18 100644 --- a/src/simpleframes.ts +++ b/src/simpleframes.ts @@ -24,10 +24,6 @@ export namespace MeterbusLibFrames { return MeterbusLibUtils.jsonPrepaper(this, []) } - getJSON() : string { - return JSON.stringify(this) - } - verifyChecksumAndEnd() { if (this.__firstPayload != 0) { if (this._telegram[this._telegram.length - 1] != 0x16) {