All files / src utils.ts

94.44% Statements 51/54
80.77% Branches 21/26
100% Functions 4/4
94% Lines 47/50

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              1x     2x       34x 22x 22x 22x 22x       2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x                   1x 19x 19x 19x 61x 61x 61x   19x   1x 1x 1x 1x 1x 1x   1x 5x 5x 5x 4x   5x 5x     1x     1x        
import { IDevice } from "./models/device";
import { ColorMode, DeviceStatus } from "./models/enums";
/**
 * Parse the raw socket message to the device information
 * @param {string} message raw HTTP message read from socket when discover the light
 * @returns {IDevice} device info
 */
export function parseDeviceInfo(message: string): IDevice {
 
    // Not found the device
    Iif (message.indexOf("HTTP/1.1 200 OK") < 0 ||
        message.indexOf("yeelight://") < 0) {
        return null;
    }
    const getString = (key: string, defaultValue = "") => {
        const regex = new RegExp(`${key}: ([^\r\n]*)`);
        const m = message.match(regex);
        Eif (m && m.length > 0) {
            return m[1];
        }
        return defaultValue;
    };
    try {
        const device: Partial<IDevice> = {};
        device.location = getString("Location");
        device.id = getString("id");
        device.model = getString("model");
        device.version = getString("fw_ver");
        device.capabilities = getString("support").split(" ");
        device.status = getString("power") as DeviceStatus;
        device.bright = parseInt(getString("bright", "0"), 10);
        device.hue = parseInt(getString("hue", "0"), 10);
        device.rgb = parseInt(getString("rgb", "0"), 10);
        device.sat = parseInt(getString("sat", "0"), 10);
        device.mode = parseInt(getString("color_mode", "0"), 10) as ColorMode;
 
        const host = device.location.substr(11);
        device.port = parseInt(host.split(":")[1], 10);
        device.host = host.split(":")[0];
        return device as IDevice;
    } catch (err) {
        return null;
    }
}
/**
 * This function to convert the hex string to the decimal number. Ex AA=> 255
 * @param {string} hex the input as heximal string ex: 12AF32CD
 * @returns {number} the number value of hex string
 */
export function hexToNumber(hex: string): number {
    const hexString = hex.toUpperCase();
    let result = 0;
    for (let i = 1; i <= hexString.length; i++) {
        let valueNumber = hexString.charCodeAt(i - 1);
        valueNumber -= (valueNumber >= 65) ? 55 : 48;
        result += valueNumber * Math.pow(16, hex.length - i);
    }
    return result;
}
export function getListIpAddress(currentIp: string, Ifrom = 1, Ito = 254): string[] {
    const startNumber = parseInt(currentIp.split(".")[3], 10);
    const results: string[] = [];
    let before = startNumber;
    let after = startNumber;
    const sub = currentIp.substring(0, currentIp.lastIndexOf("."));
 
    while (before > from || after < to) {
        before--;
        after++;
        if (before >= from && before > 0) {
            results.push(sub + "." + before);
        }
        Eif (after <= to && after < 255) {
            results.push(sub + "." + after);
        }
    }
    return results;
}
 
export const Utils = {
    getListIpAddress,
    hexToNumber,
    parseDeviceInfo,
};