knx-dpt
Version:
Serialize and deserialize KNX datapoints
153 lines (148 loc) • 3.77 kB
JavaScript
const test = require('tape');
const DPTLib = require('../../../src');
var dpt4 = DPTLib.resolve('DPT4');
var tests = [
[[0x00], String.fromCharCode(0)],
[[0x01], String.fromCharCode(1)],
[[0x02], String.fromCharCode(2)],
[[0x03], String.fromCharCode(3)],
[[0x04], String.fromCharCode(4)],
[[0x05], String.fromCharCode(5)],
[[0x06], String.fromCharCode(6)],
[[0x07], String.fromCharCode(7)],
[[0x08], String.fromCharCode(8)],
[[0x09], String.fromCharCode(9)],
[[0x0A], String.fromCharCode(10)],
[[0x0B], String.fromCharCode(11)],
[[0x0C], String.fromCharCode(12)],
[[0x0D], String.fromCharCode(13)],
[[0x0E], String.fromCharCode(14)],
[[0x0F], String.fromCharCode(15)],
[[0x10], String.fromCharCode(16)],
[[0x11], String.fromCharCode(17)],
[[0x12], String.fromCharCode(18)],
[[0x13], String.fromCharCode(19)],
[[0x14], String.fromCharCode(20)],
[[0x15], String.fromCharCode(21)],
[[0x16], String.fromCharCode(22)],
[[0x17], String.fromCharCode(23)],
[[0x18], String.fromCharCode(24)],
[[0x19], String.fromCharCode(25)],
[[0x1A], String.fromCharCode(26)],
[[0x1B], String.fromCharCode(27)],
[[0x1C], String.fromCharCode(28)],
[[0x1D], String.fromCharCode(29)],
[[0x1E], String.fromCharCode(30)],
[[0x1F], String.fromCharCode(31)],
[[0x20], ' '],
[[0x21], '!'],
[[0x22], '"'],
[[0x23], '#'],
[[0x24], '$'],
[[0x25], '%'],
[[0x26], '&'],
[[0x27], '\''],
[[0x28], '('],
[[0x29], ')'],
[[0x2A], '*'],
[[0x2B], '+'],
[[0x2C], ','],
[[0x2D], '-'],
[[0x2E], '.'],
[[0x2F], '/'],
[[0x30], '0'],
[[0x31], '1'],
[[0x32], '2'],
[[0x33], '3'],
[[0x34], '4'],
[[0x35], '5'],
[[0x36], '6'],
[[0x37], '7'],
[[0x38], '8'],
[[0x39], '9'],
[[0x3A], ':'],
[[0x3B], ';'],
[[0x3C], '<'],
[[0x3D], '='],
[[0x3E], '>'],
[[0x3F], '?'],
[[0x40], '@'],
[[0x41], 'A'],
[[0x42], 'B'],
[[0x43], 'C'],
[[0x44], 'D'],
[[0x45], 'E'],
[[0x46], 'F'],
[[0x47], 'G'],
[[0x48], 'H'],
[[0x49], 'I'],
[[0x4A], 'J'],
[[0x4B], 'K'],
[[0x4C], 'L'],
[[0x4D], 'M'],
[[0x4E], 'N'],
[[0x4F], 'O'],
[[0x50], 'P'],
[[0x51], 'Q'],
[[0x52], 'R'],
[[0x53], 'S'],
[[0x54], 'T'],
[[0x55], 'U'],
[[0x56], 'V'],
[[0x57], 'W'],
[[0x58], 'X'],
[[0x59], 'Y'],
[[0x5A], 'Z'],
[[0x5B], '['],
[[0x5C], '\\'],
[[0x5D], ']'],
[[0x5E], '^'],
[[0x5F], '_'],
[[0x60], '`'],
[[0x61], 'a'],
[[0x62], 'b'],
[[0x63], 'c'],
[[0x64], 'd'],
[[0x65], 'e'],
[[0x66], 'f'],
[[0x67], 'g'],
[[0x68], 'h'],
[[0x69], 'i'],
[[0x6A], 'j'],
[[0x6B], 'k'],
[[0x6C], 'l'],
[[0x6D], 'm'],
[[0x6E], 'n'],
[[0x6F], 'o'],
[[0x70], 'p'],
[[0x71], 'q'],
[[0x72], 'r'],
[[0x73], 's'],
[[0x74], 't'],
[[0x75], 'u'],
[[0x76], 'v'],
[[0x77], 'w'],
[[0x78], 'x'],
[[0x79], 'y'],
[[0x7A], 'z'],
[[0x7B], '{'],
[[0x7C], '|'],
[[0x7D], '}'],
[[0x7E], '~'],
[[0x7F], String.fromCharCode(127)],
];
test('DPT4 conversion', function (t) {
t.plan(tests.length * 2);
for (var i = 0; i < tests.length; i++) {
var buf = new Buffer(tests[i][0]);
var obj = tests[i][1];
// backward test (object to raw data)
converted = dpt4.formatAPDU(obj);
t.deepEqual(converted, buf, `DPT4 formatAPDU ${JSON.stringify(obj)}`);
// forward test (raw data to object)
var converted = dpt4.fromBuffer(buf);
t.deepEqual(converted, obj, `DPT4 fromBuffer ${JSON.stringify(buf)}`
);
}
t.end();
});