yeelight-awesome
Version:
The node.js client api to control yeelight device over WIFI
89 lines • 3.12 kB
JavaScript
;
exports.__esModule = true;
exports.Utils = exports.getListIpAddress = exports.hexToNumber = exports.parseDeviceInfo = void 0;
/**
* 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
*/
function parseDeviceInfo(message) {
// Not found the device
if (message.indexOf("HTTP/1.1 200 OK") < 0 ||
message.indexOf("yeelight://") < 0) {
return null;
}
var getString = function (key, defaultValue) {
if (defaultValue === void 0) { defaultValue = ""; }
var regex = new RegExp(key + ": ([^\r\n]*)");
var m = message.match(regex);
if (m && m.length > 0) {
return m[1];
}
return defaultValue;
};
try {
var device = {};
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");
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);
var host = device.location.substr(11);
device.port = parseInt(host.split(":")[1], 10);
device.host = host.split(":")[0];
return device;
}
catch (err) {
return null;
}
}
exports.parseDeviceInfo = parseDeviceInfo;
/**
* 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
*/
function hexToNumber(hex) {
var hexString = hex.toUpperCase();
var result = 0;
for (var i = 1; i <= hexString.length; i++) {
var valueNumber = hexString.charCodeAt(i - 1);
valueNumber -= (valueNumber >= 65) ? 55 : 48;
result += valueNumber * Math.pow(16, hex.length - i);
}
return result;
}
exports.hexToNumber = hexToNumber;
function getListIpAddress(currentIp, from, to) {
if (from === void 0) { from = 1; }
if (to === void 0) { to = 254; }
var startNumber = parseInt(currentIp.split(".")[3], 10);
var results = [];
var before = startNumber;
var after = startNumber;
var sub = currentIp.substring(0, currentIp.lastIndexOf("."));
while (before > from || after < to) {
before--;
after++;
if (before >= from && before > 0) {
results.push(sub + "." + before);
}
if (after <= to && after < 255) {
results.push(sub + "." + after);
}
}
return results;
}
exports.getListIpAddress = getListIpAddress;
exports.Utils = {
getListIpAddress: getListIpAddress,
hexToNumber: hexToNumber,
parseDeviceInfo: parseDeviceInfo
};
//# sourceMappingURL=utils.js.map