function Encode(fPort, obj)
{
// Possible parameters:
// userMode var encoded = []; : "Ambient_Temperature" ifor ("Valve_Position_RCI_10_minutes"
in// obj)setValue { : 0 to 40 var new_vp_10min = obj.Valve_Position_RCI_10_minutes;
(0.5° resolution) for "Ambient_Temperature"
// setValue encoded = [new_vp_10min,0x00,0x26,0x00,0x00,0x00]; : 0 }to 100 (1% resolution) iffor ("Valve_Position_RCI_5_minutes"
in
obj)// safetyMode {
var new_vp_5min = obj.Valve_Position_RCI_5_minutes;
encoded = [new_vp_5min,0x00,0x26,0x10,0x00,0x00];
}
if ("Set_Point_Ambient_Temperature_RCI_10_Minutes" in obj)
{
var new_spt_10min = (obj.Set_Point_Ambient_Temperature_RCI_10_Minutes)*2;
encoded = [new_spt_10min,0x00,0x26,0x08,0x00,0x00];
}
if ("Set_Point_Ambient_Temperature_RCI_5_Minutes" in obj)
{
var new_spt_5min = (obj.Set_Point_Ambient_Temperature_RCI_5_Minutes)*2;
encoded = [new_spt_5min,0x00,0x26,0x18,0x00,0x00];
}
return encoded; : "Ambient_Temperature" or "Valve_Position"
// safetyValue : 0 to 40 (0.5° resolution) for "Ambient_Temperature"
// safetyValue : 0 to 100 (1% resolution) for "Valve_Position"
// roomTemperature : 0 (Internal Ambient Estimate) or Temperature from External Sensor (0.25° resolution)
// radioInterval : 5 or 10 or 60 or 120 or 480 (minutes)
// doReferenceRunNow : 0 or 1 (0-False; 1-True)
/*
IMPORTANT: All of the parameters in obj is NOT required for the encoder to work
Eg. Only the "roomTemperature" parameter can be added and
For the remaining parameters, the defaults will be applied
*/
var DEFAULT_USER_MODE_BITS = 2 << 2; // "Ambient Temperature"
var DEFAULT_SET_VALUE = 38; // 19° Ambient Temperature"
var DEFAULT_SAFETY_MODE_BITS = 0 << 0; // "Ambient Temperature"
var DEFAULT_SAFETY_MODE_VALUE = 38; // 19° Ambient Temperature"
var DEFAULT_RADIO_INTERVAL_BITS = 0 << 4; // 10 minutes
var DEFAULT_ROOM_TEMPERATURE_VALUE = 0; // Use Internal Ambient Estimate
var DEFAULT_DO_REFERENCE_RUN_NOW_BIT = 0; // 0- No Reference Run
function Encode(fPort, obj) {
var mode = obj.userMode; // "Ambient_Temperature" or "Valve_Position"
var safetyMode = obj.safetyMode; // "Ambient_Temperature" or "Valve_Position"
var setValue = obj.setValue; // 0-40 for Ambient_Temperature, 0-100 for Valve_Position
var roomTemperature = obj.roomTemperature; // 0-40
var safetyValue = obj.safetyValue; // 0-40 for Ambient_Temperature, 0-100 for Valve_Position
var radioInterval = obj.radioInterval; // 5, 10, 60, 120, 480
var doReferenceRunNow = obj.doReferenceRunNow; // 0 or 1
var bytes = [0, 0, 0, 0, 0, 0];
// Byte 1: Set value
if (mode === "Ambient_Temperature") {
if (setValue < 0 || setValue > 40) {
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 {
bytes[0] = DEFAULT_SET_VALUE;
}
// Byte 2: Room temperature (0-40)
if (roomTemperature < 0 || roomTemperature > 40) {
throw new Error("Room temperature out of range");
}
else if (typeof roomTemperature === "undefined") {
bytes[1] = DEFAULT_ROOM_TEMPERATURE_VALUE;
}
else {
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 {
bytes[2] = DEFAULT_SAFETY_MODE_VALUE;
}
// Byte 4: Radio interval, user mode, safety mode
var 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:
radioBits = DEFAULT_RADIO_INTERVAL_BITS;
}
var userModeBits;
if (mode === "Ambient_Temperature") {
userModeBits = 2 << 2; // User mode "Ambient_Temperature" in bits 3 and 4
} else if (typeof mode === "undefined") {
userModeBits = DEFAULT_USER_MODE_BITS;
}
else if (mode === "Valve_Position") {
userModeBits = 0 << 2; // User mode "Valve_Position" in bits 3 and 4
}
var safetyModeBits;
if (safetyMode === "Ambient_Temperature") {
safetyModeBits = 0 << 0; // Safety mode "Ambient_Temperature" in bits 1 and 2
} else if (typeof safetyMode === "undefined") {
safetyModeBits = DEFAULT_SAFETY_MODE_BITS;
}
else if (safetyMode === "Valve_Position") {
safetyModeBits = 2 << 0; // Safety mode "Valve_Position" 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");
}
else if (typeof doReferenceRunNow === "undefined") {
bytes[5] = DEFAULT_DO_REFERENCE_RUN_NOW_BIT;
}
else {
bytes[5] = doReferenceRunNow << 7;
}
return bytes;
}
|