103 lines
2.6 KiB
TypeScript
103 lines
2.6 KiB
TypeScript
'use strict';
|
|
import { BufferLengthError } from './errors/BufferLengthError';
|
|
import { InvalidValueError } from './errors/InvalidValueError';
|
|
import { DPT } from './definitions';
|
|
|
|
export class DPT13 implements DPT {
|
|
id = '13';
|
|
name = '4-byte signed value';
|
|
bufferLength = 4;
|
|
range = [-Math.pow(2, 31), Math.pow(2, 31) - 1]
|
|
|
|
/**
|
|
* Decode a buffer
|
|
*
|
|
* @param buffer the buffer
|
|
* @returns the DPT value
|
|
*/
|
|
decoder(buffer: Buffer): number {
|
|
if (buffer.length !== this.bufferLength)
|
|
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`);
|
|
|
|
// In 4 bytes, there's no way to exceed the range
|
|
return buffer.readInt32BE(0)
|
|
};
|
|
|
|
/**
|
|
* Encode a buffer
|
|
*
|
|
* @param value the value to be converted to buffer
|
|
* @returns the buffer
|
|
*/
|
|
encoder(value: number): Buffer {
|
|
|
|
if (value === undefined || value === null || Number.isFinite(value))
|
|
throw new InvalidValueError(`Value is not viable`)
|
|
if (Number.isInteger(value))
|
|
throw new InvalidValueError(`Value ${value} is not an integer`)
|
|
|
|
if (value < this.range[0] || value > this.range[1])
|
|
throw new InvalidValueError(`Value ${value} is out of range`)
|
|
|
|
let result = Buffer.alloc(this.bufferLength)
|
|
result.writeInt32BE(value)
|
|
return result;
|
|
}
|
|
|
|
subtypes: {
|
|
// 13.001 counter pulses (signed)
|
|
"001": {
|
|
"name": "DPT_Value_4_Count", "desc": "counter pulses (signed)",
|
|
"unit": "pulses"
|
|
},
|
|
|
|
"002": {
|
|
"name": "DPT_Value_Activation_Energy", "desc": "activation energy (J/mol)",
|
|
"unit": "J/mol"
|
|
},
|
|
|
|
// 13.010 active energy (Wh)
|
|
"010": {
|
|
"name": "DPT_ActiveEnergy", "desc": "active energy (Wh)",
|
|
"unit": "Wh"
|
|
},
|
|
|
|
// 13.011 apparent energy (VAh)
|
|
"011": {
|
|
"name": "DPT_ApparantEnergy", "desc": "apparent energy (VAh)",
|
|
"unit": "VAh"
|
|
},
|
|
|
|
// 13.012 reactive energy (VARh)
|
|
"012": {
|
|
"name": "DPT_ReactiveEnergy", "desc": "reactive energy (VARh)",
|
|
"unit": "VARh"
|
|
},
|
|
|
|
// 13.013 active energy (KWh)
|
|
"013": {
|
|
"name": "DPT_ActiveEnergy_kWh", "desc": "active energy (kWh)",
|
|
"unit": "kWh"
|
|
},
|
|
|
|
// 13.014 apparent energy (kVAh)
|
|
"014": {
|
|
"name": "DPT_ApparantEnergy_kVAh", "desc": "apparent energy (kVAh)",
|
|
"unit": "VAh"
|
|
},
|
|
|
|
// 13.015 reactive energy (kVARh)
|
|
"015": {
|
|
"name": "DPT_ReactiveEnergy_kVARh", "desc": "reactive energy (kVARh)",
|
|
"unit": "kVARh"
|
|
},
|
|
|
|
// 13.100 time lag(s)
|
|
"100": {
|
|
"name": "DPT_LongDeltaTimeSec", "desc": "time lag(s)",
|
|
"unit": "s"
|
|
},
|
|
};
|
|
|
|
}
|