diff --git a/src/DPT11.ts b/src/DPT11.ts index 26522a3..514c33a 100644 --- a/src/DPT11.ts +++ b/src/DPT11.ts @@ -3,6 +3,33 @@ import { BufferLengthError } from './errors/BufferLengthError'; import { InvalidValueError } from './errors/InvalidValueError'; import { DPT } from './definitions'; +/** + * 11.001 Date + * + *
+ * +-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 (r3 U5 r4 U4 r1 U7) + * Encoding: Day = [1 .. 31] + * Month = [1 .. 12] + * Year = [0 .. 99] + *+ *
+ * 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: + *
+ * - If Octet 3 contains value ≥ 90 : interpret as 20th century
+ * - If Octet 3 contains value < 90: interpret as 21st century
+ */
+
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),
]);
}
diff --git a/src/DataPointType.ts b/src/DataPointType.ts
index cc380fc..ff2a869 100644
--- a/src/DataPointType.ts
+++ b/src/DataPointType.ts
@@ -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);
}
}
\ No newline at end of file
diff --git a/tests/DPT011.test.ts b/tests/DPT011.test.ts
index b2fcbfc..eb0fe14 100644
--- a/tests/DPT011.test.ts
+++ b/tests/DPT011.test.ts
@@ -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);
+ });
});
\ No newline at end of file