@tantchen/fritzapi
Version:
Home automation node API for Fritz!Box, Fritz!DECT and FRITZ!Powerline devices
143 lines (142 loc) • 3.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.satindex2apisat = exports.color2apihue = exports.colortemp2api = exports.level2api = exports.state2api = exports.time2api = exports.api2temp = exports.temp2api = void 0;
var const_1 = require("./const");
/**
* convert temp to api
* @param temp
*/
function temp2api(temp) {
var 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, const_1.MIN_TEMP), const_1.MAX_TEMP) - 8) * 2) + 16;
}
else {
throw new Error('Invalid Type');
}
return res;
}
exports.temp2api = temp2api;
/**
* convert api to temp
* @param param
*/
function api2temp(param) {
if (param === 254)
return 'on';
if (param === 253)
return 'off';
if (typeof param === 'number') {
throw new Error('Invalid Argument');
}
// 0.5C accuracy
return (parseFloat(param) - 16) / 2 + 8;
}
exports.api2temp = api2temp;
/**
* time to api
* @param seconds
*/
function time2api(seconds) {
if (seconds <= 0) {
return 0;
}
return Date.now() / 1000 + Math.min(60 * 60 * 24, seconds);
}
exports.time2api = time2api;
// function api2time(param)
// {
// // convert the input to a readable timestamp
// return 0;
// }
/**
* state to api
* @param param
*/
function state2api(param) {
// convert the input to an allowed value
if (typeof param === 'number') {
return param;
}
switch (param) {
case 'off':
return 0;
case 'on':
return 1;
case 'toggle':
default:
return 2;
}
}
exports.state2api = state2api;
/**
* level to api
* @param param
* @param isPercent
*/
function level2api(param, isPercent) {
// convert the input to an allowed value
if (isPercent)
return param > 100 ? 100 : param;
return param > 255 ? 255 : param;
return 0;
}
exports.level2api = level2api;
// The fritzbox accepts only a predefined set of color temperatures
// Setting the color temperature to other values fails silently.
function colortemp2api(param) {
if (param > 6200)
return 6500;
if (param > 5600)
return 5900;
if (param > 5000)
return 5300;
if (param > 4500)
return 4700;
if (param > 4000)
return 4200;
if (param > 3600)
return 3800;
if (param > 3200)
return 3400;
if (param > 2850)
return 3000;
return 2700;
}
exports.colortemp2api = colortemp2api;
/**
* 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) {
var col = const_1.colors.get(color);
if (col !== undefined)
// convert the input to an allowed value
return col.hue;
return 0;
}
exports.color2apihue = color2apihue;
/**
* resolve satindex
* @param color
* @param satindex
*/
function satindex2apisat(color, satindex) {
var col = const_1.colors.get(color);
if (col !== undefined)
// convert the input to an allowed value
return col.sat[satindex > 2 ? 0 : satindex];
return 0;
}
exports.satindex2apisat = satindex2apisat;