All files / src/utils function.ts

30.64% Statements 19/62
13.88% Branches 5/36
37.5% Functions 3/8
38.77% Lines 19/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1431x                                                                                                                           2x     2x   1x   1x                                                                                     2x 2x   2x                   2x 2x   2x         1x 1x 1x 1x 1x 1x 1x 1x    
import { ColorName, colors, MAX_TEMP, MIN_TEMP } from './const';
import { IState, ITemp } from './lib';
 
/**
 * convert temp to api
 * @param temp
 */
function temp2api(temp: unknown) {
  let res;
 
  if (temp === 'on' || temp === true) res = 254;
  else if (temp === 'off' || temp === false) res = 253;
  else if (typeof temp === 'number') {
    // 0.5C accuracy
    res =
      Math.round((Math.min(Math.max(temp, MIN_TEMP), MAX_TEMP) - 8) * 2) + 16;
  } else {
    throw new Error('Invalid Type');
  }
 
  return res;
}
 
/**
 * convert api to temp
 * @param param
 */
function api2temp(param: string | number): ITemp {
  Iif (param === 254) return 'on';
  Iif (param === 253) return 'off';
 
  Iif (typeof param === 'number') {
    throw new Error('Invalid Argument');
  }
 
  // 0.5C accuracy
  return (parseFloat(param) - 16) / 2 + 8;
}
 
/**
 * time to api
 * @param seconds
 */
function time2api(seconds: number) {
  Iif (seconds <= 0) {
    return 0;
  }
 
  return Date.now() / 1000 + Math.min(60 * 60 * 24, seconds);
}
 
// function api2time(param)
// {
//     // convert the input to a readable timestamp
//     return 0;
// }
/**
 * state to api
 * @param param
 */
function state2api(param: IState): number {
  // convert the input to an allowed value
  Iif (typeof param === 'number') {
    return param;
  }
  switch (param) {
    case 'off':
      return 0;
    case 'on':
      return 1;
    case 'toggle':
    default:
      return 2;
  }
}
 
/**
 * level to api
 * @param param
 * @param isPercent
 */
function level2api(param: number, isPercent: boolean) {
  // convert the input to an allowed value
  Iif (isPercent) return param > 100 ? 100 : param;
  return param > 255 ? 255 : param;
  return 0;
}
 
// The fritzbox accepts only a predefined set of color temperatures
// Setting the color temperature to other values fails silently.
function colortemp2api(param: number) {
  Iif (param > 6200) return 6500;
  Iif (param > 5600) return 5900;
  Iif (param > 5000) return 5300;
  Iif (param > 4500) return 4700;
  Iif (param > 4000) return 4200;
  Iif (param > 3600) return 3800;
  Iif (param > 3200) return 3400;
  Iif (param > 2850) return 3000;
  return 2700;
}
 
/**
 *  Fritz color schemes
 * The fritzbox accepts only a limited range of hue/saturation combinations
 * to set the color of a bulb. The hue value must be one of the 12 predefined
 * values for the base colors and each of the hue values has its own set of
 * three saturation values.
 * Any attempt to use other hue/saturaion values fails silently.
 * @param color
 */
function color2apihue(color: ColorName) {
  const col = colors.get(color);
  if (col !== undefined)
    // convert the input to an allowed value
    return col.hue;
  return 0;
}
 
/**
 * resolve satindex
 * @param color
 * @param satindex
 */
function satindex2apisat(color: ColorName, satindex: number) {
  const col = colors.get(color);
  if (col !== undefined)
    // convert the input to an allowed value
    return col.sat[satindex > 2 ? 0 : satindex];
  return 0;
}
 
export {
  temp2api,
  api2temp,
  time2api,
  state2api,
  level2api,
  colortemp2api,
  color2apihue,
  satindex2apisat,
};