From 123bd0c0ba4279bc3d80b04e9c163d52876b9c23 Mon Sep 17 00:00:00 2001 From: Laur Ivan Date: Fri, 11 Mar 2022 15:36:16 +0100 Subject: [PATCH] Can publish to nexus. Add DPT237 code. --- .gitignore | 4 ++- .npmignore | 7 +++++ package.json | 3 ++ src/DPT21.ts | 20 +++++++------- src/DPT237.ts | 66 +++++++++++++++++++++++++++++++++++++++----- src/convertors.ts | 7 +++++ tests/DPT019.test.ts | 7 +++++ tests/DPT020.test.ts | 38 +++++++++++++++++++++++++ tests/DPT021.test.ts | 46 ++++++++++++++++++++++++++++++ 9 files changed, 180 insertions(+), 18 deletions(-) create mode 100644 .npmignore create mode 100644 src/convertors.ts diff --git a/.gitignore b/.gitignore index 9d628b2..e328e06 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,6 @@ mochawesome-report/ #Exclude tsc generated files js/*.map -js/ts.js \ No newline at end of file +js/ts.js + +.npmrc \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..fa67cb7 --- /dev/null +++ b/.npmignore @@ -0,0 +1,7 @@ +coverage +mochawesome-report +tests + +package-lock.json +.mocharc.json +.nycrc.json diff --git a/package.json b/package.json index bbbdd7c..5b96d71 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,9 @@ "version": "0.0.1", "description": "KNX DPT library", "main": "lib/index.js", + "publishConfig": { + "registry": "http://10.0.0.30:18081/repository/npm-private/" + }, "devDependencies": { "@istanbuljs/nyc-config-typescript": "^1.0.2", "@types/chai": "^4.3.0", diff --git a/src/DPT21.ts b/src/DPT21.ts index cf749a9..4f08f70 100644 --- a/src/DPT21.ts +++ b/src/DPT21.ts @@ -18,7 +18,7 @@ import { DPT } from './definitions'; export class DPT21 implements DPT { id = ''; name = ''; - bufferLength = 0; + bufferLength = 1; /** * Decode a buffer @@ -27,8 +27,8 @@ export class DPT21 implements DPT { * @returns the DPT value */ decoder(buffer: Buffer): boolean[] { - if (buffer.length !== 2) - throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected 2.`); + if (buffer.length !== this.bufferLength) + throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`); const result = [ !!(buffer[0] & 0x80), @@ -57,13 +57,13 @@ export class DPT21 implements DPT { throw new InvalidValueError(`Value length should be 8. Got ${value.length}.`) const b7 = value[0] ? 1 : 0 - const b6 = value[0] ? 1 : 0 - const b5 = value[0] ? 1 : 0 - const b4 = value[0] ? 1 : 0 - const b3 = value[0] ? 1 : 0 - const b2 = value[0] ? 1 : 0 - const b1 = value[0] ? 1 : 0 - const b0 = value[0] ? 1 : 0 + const b6 = value[1] ? 1 : 0 + const b5 = value[2] ? 1 : 0 + const b4 = value[3] ? 1 : 0 + const b3 = value[4] ? 1 : 0 + const b2 = value[5] ? 1 : 0 + const b1 = value[6] ? 1 : 0 + const b0 = value[7] ? 1 : 0 const buf = Buffer.from([ b7 << 7 | diff --git a/src/DPT237.ts b/src/DPT237.ts index b251b6d..264e5af 100644 --- a/src/DPT237.ts +++ b/src/DPT237.ts @@ -2,11 +2,25 @@ import { BufferLengthError } from './errors/BufferLengthError'; import { InvalidValueError } from './errors/InvalidValueError'; import { DPT } from './definitions'; +import { dec2bin, hex2bin } from './convertors'; +/** + * This is a class with flags! + */ + +export interface DPT237Result { + readResponse: boolean + addressIndicator: boolean + daliAddress: number + + lampFailure: boolean + ballastFailure: boolean + convertorError: boolean +} export class DPT237 implements DPT { - id = ''; - name = ''; - bufferLength = 0; + id = '237'; + name = '2-byte unsigned value'; + bufferLength = 2; /** @@ -15,11 +29,26 @@ export class DPT237 implements DPT { * @param buffer the buffer * @returns the DPT value */ - decoder(buffer: Buffer): number { + decoder(buffer: Buffer): DPT237Result { if (buffer.length !== this.bufferLength) throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer}. Expected ${this.bufferLength}.`); - return value; + let str = buffer.toString('hex'); + + let MSB = hex2bin(str.substring(0, 2)); // Get Binary + let LSB = hex2bin(str.substring(2, 4));// Get Binary + + return { + // LSB + readResponse: LSB.substring(0, 1) === "0" ? false : true, + addressIndicator: LSB.substring(1, 2) === "0" ? false : true, + daliAddress: parseInt("00" + LSB.substring(2, 8), 2), + + // MSB + lampFailure: MSB.substring(7, 8) === "0" ? false : true, + ballastFailure: MSB.substring(6, 7) === "0" ? false : true, + convertorError: MSB.substring(5, 6) === "0" ? false : true, + } }; /** @@ -28,12 +57,35 @@ export class DPT237 implements DPT { * @param value the value to be converted to buffer * @returns the buffer */ - encoder(value: number): Buffer { - return buf; + encoder(value: DPT237Result): Buffer { + if (value === undefined || value === null) + throw new InvalidValueError('Cannot write null value'); + + // LSB + let LSB = (value.readResponse === false ? "0" : "1") + + (value.addressIndicator === false ? "0" : "1") + + dec2bin(value.daliAddress).padStart(6, "0"); + + // MSB + let MSB = "00000" + (value.convertorError === false ? "0" : "1") + + (value.ballastFailure === false ? "0" : "1") + + (value.lampFailure === false ? "0" : "1"); + + var bufferTotal = Buffer.alloc(2) + bufferTotal[0] = parseInt(MSB, 2); + bufferTotal[1] = parseInt(LSB, 2); + return bufferTotal; } subtypes: { + "600": { + "desc": "DPT_DALI_Control_Gear_Diagnostic", + "name": "DALI control gear diagnostic", + "unit": "", + //"scalar_range": [,], + //"range": [,] + } }; } diff --git a/src/convertors.ts b/src/convertors.ts new file mode 100644 index 0000000..967ba49 --- /dev/null +++ b/src/convertors.ts @@ -0,0 +1,7 @@ + +export function hex2bin(hex: string) { + return (parseInt(hex, 16).toString(2)).padStart(8, '0'); +} +export function dec2bin(dec: number) { + return (dec >>> 0).toString(2); +} \ No newline at end of file diff --git a/tests/DPT019.test.ts b/tests/DPT019.test.ts index 439ad19..96d9b9a 100644 --- a/tests/DPT019.test.ts +++ b/tests/DPT019.test.ts @@ -79,6 +79,13 @@ describe("Test DPT019", (): void => { } expect(testFunction).to.throw(BufferLengthError); }); + it("Decode invalid buffer", async function () { + let buffer = Buffer.from([21, 9, 22, 24, 0, 1, 0, 0]) + var testFunction = function () { + const value = dpt.decoder(buffer) + } + expect(testFunction).to.throw(RangeError); + }); it("Decode oversized buffer", async function () { let bufferBiggerSize = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) diff --git a/tests/DPT020.test.ts b/tests/DPT020.test.ts index 3f664ee..da9b7a9 100644 --- a/tests/DPT020.test.ts +++ b/tests/DPT020.test.ts @@ -10,4 +10,42 @@ describe("Test DPT020", (): void => { let dpt = new DPT20(); + it("Decode buffer acceptable", async function () { + let value = 0x24 + let buffer = Buffer.from([0x24]) + const decoded = dpt.decoder(buffer) + expect(decoded).is.equal(value); + }); + + 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 oversized buffer", async function () { + let bufferBiggerSize = Buffer.from([0x20, 0x15]) + + var testFunction = function () { + const value = dpt.decoder(bufferBiggerSize) + } + expect(testFunction).to.throw(BufferLengthError); + }); + + /* Encoder tests */ + it("encode valid", async function () { + let value = 0x15 + let buffer = Buffer.from([0x15]) + + 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); + }); }); \ No newline at end of file diff --git a/tests/DPT021.test.ts b/tests/DPT021.test.ts index c4e046d..6e441a2 100644 --- a/tests/DPT021.test.ts +++ b/tests/DPT021.test.ts @@ -10,4 +10,50 @@ describe("Test DPT021", (): void => { let dpt = new DPT21(); + it("Decode buffer acceptable", async function () { + let value = [false, false, false, true, false, true, false, false] + let buffer = Buffer.from([0x14]) + const decoded = dpt.decoder(buffer) + expect(decoded).is.equal(value); + }); + + 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 oversized buffer", async function () { + let bufferBiggerSize = Buffer.from([0x20, 0x15, 0, 0, 0, 0, 0, 0, 0]) + + var testFunction = function () { + const value = dpt.decoder(bufferBiggerSize) + } + expect(testFunction).to.throw(BufferLengthError); + }); + + /* Encoder tests */ + it("encode valid", async function () { + let value: boolean[] = [false, false, false, true, false, true, false, true] + let buffer = Buffer.from([0x15]) + + 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", async function () { + var testFunction = function () { + const value = dpt.encoder([true, true, true, true, false, false, false, false, true]) + } + expect(testFunction).to.throw(InvalidValueError); + }); + }); \ No newline at end of file