UNPKG

magichome-platform

Version:

discover, control, and receive status for magichome devices

58 lines (57 loc) 1.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.waitForMe = exports.speedToDelay = exports.delayToSpeed = exports.loadJson = exports.parseJson = exports.clamp = exports.checksum = void 0; const fs_1 = require("fs"); //================================================= // Start checksum // //a checksum is needed at the end of the byte array otherwise the message is rejected by the light //add all bytes and chop off the beginning by & with 0xFF function checksum(buffer) { let chk = 0; for (const byte of buffer) { chk += byte; } return chk & 0xff; } exports.checksum = checksum; function clamp(value, min, max) { return Math.min(max, Math.max(min, value)); } exports.clamp = clamp; function parseJson(value, replacement) { try { return JSON.parse(value); } catch (_error) { return replacement; } } exports.parseJson = parseJson; function loadJson(file, replacement) { if (!(0, fs_1.existsSync)(file)) { return replacement; } return parseJson((0, fs_1.readFileSync)(file).toString(), replacement); } exports.loadJson = loadJson; function delayToSpeed(delay) { let clamped = clamp(delay, 1, 31); clamped -= 1; // bring into interval [0, 30] return 100 - (clamped / 30) * 100; } exports.delayToSpeed = delayToSpeed; function speedToDelay(speed) { const clamped = clamp(speed, 0, 100); return 30 - (clamped / 100) * 30 + 1; } exports.speedToDelay = speedToDelay; async function waitForMe(timeoutMS) { return new Promise((resolve) => { setTimeout(() => { if (timeoutMS < 0) { resolve(true); } }, timeoutMS); }); } exports.waitForMe = waitForMe;