Add Dpt11 tests.
This commit is contained in:
parent
fe6751a771
commit
9b4b324e09
39
src/DPT11.ts
39
src/DPT11.ts
@ -3,6 +3,33 @@ import { BufferLengthError } from './errors/BufferLengthError';
|
||||
import { InvalidValueError } from './errors/InvalidValueError';
|
||||
import { DPT } from './definitions';
|
||||
|
||||
/**
|
||||
* <strong>11.001</strong> Date
|
||||
*
|
||||
* <pre>
|
||||
* +-7-+-6-+-5-+-4-+-3-+-2-+-1-+-0-+
|
||||
* Field Names | 0 0 0 (Day) |
|
||||
* Encoding | U U U U U |
|
||||
* +-7-+-6-+-5-+-4-+-3-+-2-+-1-+-0-+
|
||||
* | 0 0 0 0 (Month) |
|
||||
* | U U U U |
|
||||
* +-7-+-6-+-5-+-4-+-3-+-2-+-1-+-0-+
|
||||
* | 0 (Year) |
|
||||
* | U U U U U U U |
|
||||
* +---+---+---+---+---+---+---+---+
|
||||
* Format: 3 octets (r<sub>3</sub> U<sub>5</sub> r<sub>4</sub> U<sub>4</sub> r<sub>1</sub> U<sub>7</sub>)
|
||||
* Encoding: Day = [1 .. 31]
|
||||
* Month = [1 .. 12]
|
||||
* Year = [0 .. 99]
|
||||
* </pre>
|
||||
* <p>
|
||||
* This format covers the range 1990 to 2089. The following interpretation shall be carried out by devices receiving
|
||||
* the Data Point Type 11.001 and carrying out calculations on the basis of the entire 3rd octet:
|
||||
* <p>
|
||||
* - If Octet 3 contains value ≥ 90 : interpret as 20th century<br>
|
||||
* - If Octet 3 contains value < 90: interpret as 21st century<br>
|
||||
*/
|
||||
|
||||
export class DPT11 implements DPT {
|
||||
id = '11';
|
||||
name = '3-byte date value';
|
||||
@ -21,7 +48,7 @@ export class DPT11 implements DPT {
|
||||
const day = buffer[0] & 31; //0b00011111);
|
||||
const month = buffer[1] & 15; //0b00001111);
|
||||
let year = buffer[2] & 127; //0b01111111);
|
||||
year = year + (year > 89 ? 1900 : 2000);
|
||||
year = (year < 90 ? 2000 : 1900) + year
|
||||
if (
|
||||
day < 1 ||
|
||||
day > 31 ||
|
||||
@ -42,16 +69,14 @@ export class DPT11 implements DPT {
|
||||
* @returns the buffer
|
||||
*/
|
||||
encoder(value: Date | string): Buffer {
|
||||
if (value === undefined || value === null)
|
||||
throw new InvalidValueError('Cannot write null value');
|
||||
|
||||
switch (typeof value) {
|
||||
case 'string':
|
||||
case 'number':
|
||||
value = new Date(value);
|
||||
break;
|
||||
case 'object':
|
||||
// this expects the month property to be zero-based (January = 0, etc.)
|
||||
if (value instanceof Date) break;
|
||||
const { year, month, day } = value;
|
||||
value = new Date(parseInt(year), parseInt(month), parseInt(day));
|
||||
}
|
||||
|
||||
if (isNaN(value.getDate()))
|
||||
@ -61,7 +86,7 @@ export class DPT11 implements DPT {
|
||||
return Buffer.from([
|
||||
value.getDate(),
|
||||
value.getMonth() + 1,
|
||||
year - (year >= 2000 ? 2000 : 1900),
|
||||
(year % 100),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -103,11 +103,11 @@ export class DataPointType {
|
||||
return `${this.type}.${this.subtype}`;
|
||||
}
|
||||
|
||||
decode(buffer: Buffer): string | number {
|
||||
decode(buffer: Buffer): any {
|
||||
return this._decoder(buffer);
|
||||
}
|
||||
|
||||
encode(value: string | number): Buffer {
|
||||
encode(value: any): Buffer {
|
||||
return this._encoder(value);
|
||||
}
|
||||
}
|
@ -9,5 +9,56 @@ import { compareBuffers } from "./util"
|
||||
describe("Test DPT011", (): void => {
|
||||
|
||||
let dpt = new DPT11();
|
||||
let value = new Date(2021, 11, 25)
|
||||
let buffer = Buffer.from([25, 12, 21])
|
||||
|
||||
it("Decode buffer acceptable", async function () {
|
||||
const decoded = dpt.decoder(buffer)
|
||||
expect(decoded.toUTCString()).is.equal(value.toUTCString());
|
||||
});
|
||||
|
||||
it("Decode empty buffer", async function () {
|
||||
let bufferEmpty = Buffer.from([])
|
||||
var testFunction = function () {
|
||||
const value = dpt.decoder(bufferEmpty)
|
||||
}
|
||||
expect(testFunction).to.throw(BufferLengthError);
|
||||
});
|
||||
|
||||
it("Decode invalid buffer", async function () {
|
||||
let bufferEmpty = Buffer.from([25, 13, 21])
|
||||
var testFunction = function () {
|
||||
const value = dpt.decoder(bufferEmpty)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
it("Decode oversized buffer", async function () {
|
||||
let bufferBiggerSize = Buffer.from([0x20, 0x15, 0, 0])
|
||||
|
||||
var testFunction = function () {
|
||||
const value = dpt.decoder(bufferBiggerSize)
|
||||
}
|
||||
expect(testFunction).to.throw(BufferLengthError);
|
||||
});
|
||||
|
||||
/* Encoder tests */
|
||||
it("encode valid", async function () {
|
||||
expect(compareBuffers(dpt.encoder(value), buffer)).is.true;
|
||||
});
|
||||
|
||||
it("encode undefined", async function () {
|
||||
var testFunction = function () {
|
||||
const value = dpt.encoder(undefined)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
|
||||
it("encode invalid value", async function () {
|
||||
const value = "invalid"
|
||||
var testFunction = function () {
|
||||
const result = dpt.encoder(value)
|
||||
}
|
||||
expect(testFunction).to.throw(InvalidValueError);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user