all files / Insteon/ utils.js

100% Statements 54/54
100% Branches 28/28
100% Functions 10/10
100% Lines 54/54
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                                                                                                  13×     12×   12×                   62×     62×         12× 280× 11×                   368× 368× 368× 368×     13×   11× 26×     11×     22× 11×     11×                                                      
 
 
var DEV_CAT_NAMES = ['Generalized Controllers', 'Dimmable Lighting Control',
  'Switched Lighting Control', 'Network Bridges', 'Irrigation Control',
  'Climate Control', 'Pool and Spa Control', 'Sensors and Actuators',
  'Home Entertainment', 'Energy Management', 'Built-In Appliance Control',
  'Plumbing', 'Communication', 'Computer Control', 'Window Coverings',
  'Access Control', 'Security', 'Surveillance', 'Automotive', 'Pet Care',
  'Toys', 'Timekeeping', 'Holiday'];
 
var VERSIONS = {
  '00': 'i1',
  '01': 'i2',
  '02': 'i2cs'
};
 
var RAMP_RATES = [
  2000, // shouldn't be used
  480000,
  420000,
  360000,
  300000,
  270000,
  240000,
  210000,
  180000,
  150000,
  120000,
  90000,
  60000,
  47000,
  43000,
  38500,
  34000,
  32000,
  30000,
  28000,
  26000,
  23500,
  21500,
  19000,
  8500,
  6500,
  4500,
  2000,
  500,
  300,
  200,
  100
];
 
 
function levelToHexByte(level) {
  if (level < 0 || level > 100) {
    throw new Error('level must be between 0 and 100');
  }
  // scale level to a max of 0xFF (255)
  level = ~~ (255 * level / 100);
 
  return toByte(level);
 
}
 
function levelToHexHalfByte(level) {
  if (level < 0 || level > 100) {
    throw new Error('level must be between 0 and 100');
  }
  // scale level to a max of 0xF (15)
  level = ~~ (15 * level / 100);
 
  return level.toString(16).toUpperCase();
 
}
 
function byteToLevel(byte) {
  return Math.ceil(parseInt(byte, 16) * 100 / 255);
}
 
function byteToRampRate(byte) {
  return RAMP_RATES[parseInt(byte, 16)];
}
 
 
function lookupRampRateIndex(rate) {
 
    for(var i = 1; i < RAMP_RATES.length; i++) {
      if (rate >= RAMP_RATES[i]) {
        return i;
      }
    }
 
    return RAMP_RATES.length - 1;
}
 
function rampRateToHexByte(rate) {
  return toByte(lookupRampRateIndex(rate));
}
 
function rampRateToHexHalfByte(rate) {
  return (~~((lookupRampRateIndex(rate))/2)).toString(16).toUpperCase();
}
 
function toByte(value, length) {
  length = length || 1;
  value = value.toString(16).toUpperCase();
  var pad = new Array((length * 2) + 1).join('0');
  return pad.substring(0, pad.length - value.length) + value;
}
 
function assignDefaults(defaults, options) {
  if (!options) {
    options = defaults;
    return options;
  }
  for(var key in defaults) {
    options[key] = (typeof options[key] === 'undefined') ? defaults[key] : options[key];
  }
 
  return options;
}
 
function convertTemp(from, to, val) {
  if(from === to) {
    return val;
  }
 
  if(from === 'F') {
    var c = (val - 32) * 5/9;
    if(to === 'K') {
      return c + 273.15;
    }
    return c;
  }
 
  if(from === 'K') {
    val = val - 273.15;
  }
 
  if(to === 'C') {
    return val;
  }
 
  if(to === 'K') {
    return val + 273.15;
  }
 
  return (val * 9/5) + 32;
}
 
 
 
module.exports = {
  levelToHexByte: levelToHexByte,
  levelToHexHalfByte: levelToHexHalfByte,
  rampRateToHexByte: rampRateToHexByte,
  rampRateToHexHalfByte: rampRateToHexHalfByte,
  byteToLevel: byteToLevel,
  byteToRampRate: byteToRampRate,
  toByte: toByte,
  assignDefaults: assignDefaults,
  convertTemp: convertTemp,
  DEV_CAT_NAMES: DEV_CAT_NAMES,
  VERSIONS: VERSIONS,
  RAMP_RATES: RAMP_RATES
};