Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
// Possible parameters:
// userMode          : "Ambient_Temperature" or "Valve_Position"
// setValue          : 0 to 40  (0.5° resolution) for "Ambient_Temperature"
// setValue          : 0 to 100 (1% resolution)   for "Valve_Position"

// safetyMode        : "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_BISBITS = 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_BISBITS;
    }
  
    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;
  }

...