Can publish to nexus.
Add DPT237 code.
This commit is contained in:
parent
aa52933d15
commit
123bd0c0ba
2
.gitignore
vendored
2
.gitignore
vendored
@ -13,3 +13,5 @@ mochawesome-report/
|
||||
#Exclude tsc generated files
|
||||
js/*.map
|
||||
js/ts.js
|
||||
|
||||
.npmrc
|
7
.npmignore
Normal file
7
.npmignore
Normal file
@ -0,0 +1,7 @@
|
||||
coverage
|
||||
mochawesome-report
|
||||
tests
|
||||
|
||||
package-lock.json
|
||||
.mocharc.json
|
||||
.nycrc.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",
|
||||
|
20
src/DPT21.ts
20
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 |
|
||||
|
@ -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": [,]
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
7
src/convertors.ts
Normal file
7
src/convertors.ts
Normal file
@ -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);
|
||||
}
|
@ -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])
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
@ -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);
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user