node-red-dashboard-2-t86
Version:
Set of Node-RED nodes to controll home automation based on Unipi Patron and DALI.
66 lines (65 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.daliFrame = daliFrame;
exports.foxtronDaliFrame = foxtronDaliFrame;
const foxtron_types_1 = require("./foxtron-types");
const SOH = String.fromCharCode(0x01);
const ETB = String.fromCharCode(0x17);
const FoxCommand = '0B'; // 11 - distinc send
const FoxPriority = '00'; // 0 is auto
const FoxDataLength = '10'; // 16 bits - 2 bytes
const FoxFlags = '00'; // no flags are set
function calcCheckSum(frame) {
let sum = 0;
for (let i = 0; i < frame.length; i += 2) {
const number = parseInt(frame[i] + frame[i + 1], 16);
if (isNaN(number) || number < 0 || number > 255) {
throw new Error(`Foxtron DALI ASCII checkum calc error. Wrong input (must be between between 0 and F)`);
}
sum += number;
}
const mod = sum % 0x100;
const negmod = 0xFF - mod;
let csStr = '';
if (negmod < 0x10) {
csStr = '0';
}
csStr += negmod.toString(16).toUpperCase();
return csStr;
}
function daliFrame(cmd) {
if (!cmd.value)
cmd.value = 0;
let firstByte = cmd.opcode === foxtron_types_1.Opcode.DAPC ? 0 : 1;
switch (cmd.address.type) {
case foxtron_types_1.AddressType.Broadcast:
firstByte += 0xFE;
break;
case foxtron_types_1.AddressType.Group:
firstByte += 0x80 + (cmd.address.value << 1);
break;
case foxtron_types_1.AddressType.Short:
firstByte += (cmd.address.value << 1);
break;
}
let secondByte = cmd.opcode === foxtron_types_1.Opcode.DAPC ? cmd.value : cmd.opcode;
if (cmd.opcode === foxtron_types_1.Opcode.DAPC && (cmd.value < foxtron_types_1.MIN_DALI_LEVEL || cmd.value > foxtron_types_1.MAX_DALI_LEVEL)) {
throw new Error(`DAPC value ${cmd.value} out of range <0;254>`);
}
const frameInt = (firstByte << 8) + secondByte;
let frameStr = frameInt.toString(16).toUpperCase();
while (frameStr.length < 4) {
frameStr = '0' + frameStr;
}
return frameStr;
}
function foxtronDaliFrame(cmd) {
const df = daliFrame(cmd);
const foxDataFrame = FoxCommand
+ FoxPriority
+ FoxDataLength
+ df
+ FoxFlags;
const cs = calcCheckSum(foxDataFrame);
return SOH + foxDataFrame + cs + ETB;
}