Can publish to nexus.
Add DPT237 code.
This commit is contained in:
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);
|
||||
}
|
||||
Reference in New Issue
Block a user