dptlib/src/DPT2.ts
2022-03-09 22:38:02 +01:00

140 lines
3.3 KiB
TypeScript

'use strict';
import { BufferLengthError } from './errors/BufferLengthError';
import { InvalidValueError } from './errors/InvalidValueError';
import { DPT } from './definitions';
export interface DPT2Result {
priority: boolean;
data: boolean;
}
export class DPT2 implements DPT {
id = '2';
name = '1-bit value with priority';
bufferLength = 1;
/**
* Decode a buffer
*
* @param buffer the buffer
* @returns the DPT value
*/
decoder(buffer: Buffer): DPT2Result {
if (buffer.length !== this.bufferLength)
throw new BufferLengthError(`Invalid buffer length ${buffer.length}/${buffer} for DPT1. Expected ${this.bufferLength}.`);
const value = buffer.readUInt8(0)
return {
priority: ((value & 0b00000010) >> 1) === 1,
data: (value & 0b00000001) === 1,
};
};
/**
* Encode a buffer
*
* @param value the value to be converted to buffer
* @returns the buffer
*/
encoder(value: DPT2Result): Buffer {
if (!value) throw new InvalidValueError('DPT2: cannot write null value');
return Buffer.from([((value.priority ? 1 : 0) << 1) + ((value.data ? 1 : 0) & 0b00000001)]);
}
subtypes: {
// 2.001 switch control
'001': {
use: 'G',
name: 'DPT_Switch_Control',
desc: 'switch with priority',
enc: { 0: 'Off', 1: 'On' },
},
// 2.002 boolean control
'002': {
use: 'G',
name: 'DPT_Bool_Control',
desc: 'boolean with priority',
enc: { 0: 'false', 1: 'true' },
},
// 2.003 enable control
'003': {
use: 'FB',
name: 'DPT_Emable_Control',
desc: 'enable with priority',
enc: { 0: 'Disabled', 1: 'Enabled' },
},
// 2.004 ramp control
'004': {
use: 'FB',
name: 'DPT_Ramp_Control',
desc: 'ramp with priority',
enc: { 0: 'No ramp', 1: 'Ramp' },
},
// 2.005 alarm control
'005': {
use: 'FB',
name: 'DPT_Alarm_Control',
desc: 'alarm with priority',
enc: { 0: 'No alarm', 1: 'Alarm' },
},
// 2.006 binary value control
'006': {
use: 'FB',
name: 'DPT_BinaryValue_Control',
desc: 'binary value with priority',
enc: { 0: 'Off', 1: 'On' },
},
// 2.007 step control
'007': {
use: 'FB',
name: 'DPT_Step_Control',
desc: 'step with priority',
enc: { 0: 'Off', 1: 'On' },
},
// 2.008 Direction1 control
'008': {
use: 'FB',
name: 'DPT_Direction1_Control',
desc: 'direction 1 with priority',
enc: { 0: 'Off', 1: 'On' },
},
// 2.009 Direction2 control
'009': {
use: 'FB',
name: 'DPT_Direction2_Control',
desc: 'direction 2 with priority',
enc: { 0: 'Off', 1: 'On' },
},
// 2.010 start control
'010': {
use: 'FB',
name: 'DPT_Start_Control',
desc: 'start with priority',
enc: { 0: 'No control', 1: 'No control', 2: 'Off', 3: 'On' },
},
// 2.011 state control
'011': {
use: 'FB',
name: 'DPT_Switch_Control',
desc: 'switch',
enc: { 0: 'No control', 1: 'No control', 2: 'Off', 3: 'On' },
},
// 2.012 invert control
'012': {
use: 'FB',
name: 'DPT_Switch_Control',
desc: 'switch',
enc: { 0: 'No control', 1: 'No control', 2: 'Off', 3: 'On' },
},
};
}