...
Code Block |
---|
|
// Encode downlink function.
//
// Input is an object with the following fields:
// - output = Object representing the payload that must be encoded.
// - variables = Object containing the configured device variables.
//
// Output must be an object with the following fields:
// - bytes = Byte array containing the downlink payload.
function encodeDownlink(input) {
varlet encodedmode = []input.data.userMode; // "ambient" if ("Valve_Position" in input.data) {
var new_vpor "valve"
let safetyMode = input.data.safetyMode; // "ambient" or "valve"
let setValue = input.data.setValue; // 0-40 for ambient, 0-100 for valve
let roomTemperature = input.data.roomTemperature; // 0-40
let safetyValue = input.data.safetyValue; // 0-40 for ambient, 0-100 for valve
let radioInterval = input.data.radioInterval; // 5, 10, 60, 120, 480
let doReferenceRunNow = input.data.Valve_Position;doReferenceRunNow; // 0 or 1
let encodedbytes = [new_vp0, 0x000, 0x260, 0x000, 0x000, 0x000];
// Byte }1: Set value
if ("Set_Point_mode === "Ambient_Temperature" in input.data) {
if (setValue < 0 || setValue > 40) {
var new_spt = (input.data.Set_Point_Ambient_Temperature) * 2;
encoded = [new_spt, 0x00, 0x26, 0x08, 0x00, 0x00];
}
return { bytes: encoded throw new Error("Set value out of range for ambient mode");
}
else {
bytes[0] = setValue *2;
}
} else if (mode === "Valve_Position") {
if (setValue < 0 || setValue > 100) {
throw new Error("Set value out of range for valve mode");
}
else {
bytes[0] = setValue;
}
} else {
throw new Error("Invalid user mode");
}
// Byte 2: Room temperature (0-80 for ambient mode)
if (roomTemperature < 0 || roomTemperature > 40) throw new Error("Room temperature out of range");
bytes[1] = roomTemperature * 4;
// Byte 3: Safety value
if (safetyMode === "Ambient_Temperature") {
if (safetyValue < 0 || safetyValue > 40) {
throw new Error("Safety value out of range for ambient mode");
}
else {
bytes[2] = safetyValue * 2;
}
} else if (safetyMode === "Valve_Position") {
if (safetyValue < 0 || safetyValue > 100) {
throw new Error("Safety value out of range for valve mode");
}
else {
bytes[2] = safetyValue;
}
} else {
throw new Error("Invalid safety mode");
}
// Byte 4: Radio interval, user mode, safety mode
let radioBits;
switch (radioInterval) {
case 5:
radioBits = 1 << 4; // Radio interval 5 minutes
break;
case 10:
radioBits = 0 << 4; // Radio interval 10 minutes
break;
case 60:
radioBits = 2 << 4; // Radio interval 60 minutes
break;
case 120:
radioBits = 3 << 4; // Radio interval 120 minutes
break;
case 480:
radioBits = 4 << 4; // Radio interval 480 minutes
break;
default:
throw new Error("Invalid radio interval");
}
let userModeBits;
if (mode === "Ambient_Temperature") {
userModeBits = 2 << 2; // User mode "ambient" in bits 3 and 4
} else {
userModeBits = 0 << 2; // User mode "valve" in bits 3 and 4
}
let safetyModeBits;
if (safetyMode === "Ambient_Temperature") {
safetyModeBits = 0 << 0; // Safety mode "ambient" in bits 1 and 2
} else {
safetyModeBits = 2 << 0; // Safety mode "valve" in bits 1 and 2
}
bytes[3] = radioBits | userModeBits | safetyModeBits;
// Byte 5: Reserved (set to 0)
bytes[4] = 0;
// Byte 6: doReferenceRunNow bit (bit 8)
if (doReferenceRunNow < 0 || doReferenceRunNow > 1) throw new Error("Invalid doReferenceRunNow value");
bytes[5] = doReferenceRunNow << 7;
return {
"bytes": bytes
};
} |
Downlink Encoder Example:
Code Block |
---|
|
// Example 1
{"Set_Point_Ambient_Temperature":22}
{"Valve_Position":100
"userMode": "Valve_Position",
"setValue": 42,
"roomTemperature": 0,
"safetyMode": "Ambient_Temperature",
"safetyValue": 19,
"radioInterval": 5,
"doReferenceRunNow": 1
}
// Example 2
{
"userMode": "Ambient_Temperature",
"setValue": 21.5,
"roomTemperature": 24,
"safetyMode": "Valve_Position",
"safetyValue": 0,
"radioInterval": 60,
"doReferenceRunNow": 0
}
// Example 3
{
"userMode": "Valve_Position",
"setValue": 36,
"roomTemperature": 0,
"safetyMode": "Valve_Position",
"safetyValue": 21,
"radioInterval": 5,
"doReferenceRunNow": 1
}
// Example 4
{
"userMode": "Ambient_Temperature",
"setValue": 20,
"roomTemperature": 0,
"safetyMode": "Ambient_Temperature",
"safetyValue": 18,
"radioInterval": 10,
"doReferenceRunNow": 0
} |