nanoleaf-cove
Version:
Simple NanoLeaf Api for Node.js
190 lines (189 loc) • 7.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.discover = void 0;
var tslib_1 = require("tslib");
var electron_dns_sd_1 = require("electron-dns-sd");
var getBroadcastNetworkInterfaces = require('network-cove').getBroadcastNetworkInterfaces;
var dgram;
if (typeof window !== 'undefined' && typeof window.dgram !== undefined)
dgram = window.dgram;
else
dgram = require('dgram');
var DefaultAddress = '239.255.255.250';
var broadcastInterval;
var discover = function (_a) {
var _this = this;
var _b = _a.type, type = _b === void 0 ? 'mdns' : _b, _c = _a.allInfo, allInfo = _c === void 0 ? false : _c, _d = _a.timer, timer = _d === void 0 ? 4000 : _d;
return new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var _a, _b, e_1;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 5, , 6]);
if (!(type === 'udp')) return [3, 2];
_a = resolve;
return [4, udpDiscovery({ allInfo: allInfo, timer: timer })];
case 1:
_a.apply(void 0, [_c.sent()]);
return [3, 4];
case 2:
_b = resolve;
return [4, mdnsDiscovery({ allInfo: allInfo })];
case 3:
_b.apply(void 0, [_c.sent()]);
_c.label = 4;
case 4: return [3, 6];
case 5:
e_1 = _c.sent();
reject(e_1);
return [3, 6];
case 6: return [2];
}
});
}); });
};
exports.discover = discover;
var mdnsDiscovery = function (_a) {
var allInfo = _a.allInfo;
return new Promise(function (resolve, reject) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var found_1, devices, e_2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
found_1 = [];
return [4, electron_dns_sd_1.discover({ name: '_nanoleafapi._tcp.local' })];
case 1:
devices = _a.sent();
devices.map(function (device) {
var _a;
if (!found_1.some(function (light) { return light.address === device.address; })) {
var ip = "http://" + device.address + ":" + device.service.port;
if (allInfo) {
found_1.push({
name: device.modelName || device.familyName,
id: ((_a = device.fqdn) === null || _a === void 0 ? void 0 : _a.replace(/\s/g, '-', '').toLowerCase()) || device.address,
ip: ip,
type: _getNanoleafType(device.fqdn),
});
}
else {
found_1.push(ip);
}
}
});
resolve(found_1);
return [3, 3];
case 2:
e_2 = _a.sent();
reject(e_2);
return [3, 3];
case 3: return [2];
}
});
}); });
};
var udpDiscovery = function (_a) {
var allInfo = _a.allInfo, timer = _a.timer;
return new Promise(function (resolve, reject) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var socket_1, found_2;
return tslib_1.__generator(this, function (_a) {
try {
socket_1 = dgram.createSocket('udp4');
found_2 = [];
socket_1.on('message', function (description, rinfo) {
if (_isNanoleaf(description)) {
var ip_1 = _getNanoleafIp(description);
if (allInfo) {
if (!found_2.some(function (light) {
return light.ip === ip_1;
})) {
var light = { ip: ip_1, type: _getNanoleafType(description), name: _getNanoleafName(description) };
found_2.push(light);
}
}
else {
if (!found_2.some(function (light) {
return light === ip_1;
})) {
found_2.push(ip_1);
}
}
}
});
socket_1.bind(function () {
socket_1.setBroadcast(true);
var broadcastList = tslib_1.__spreadArrays([DefaultAddress], getBroadcastNetworkInterfaces());
broadcastInterval = setInterval(function () {
broadcastList.map(function (address) {
var message = 'M-SEARCH * HTTP/1.1\r\n' +
'HOST:' +
address +
':1900\r\n' +
'MAN:"ssdp:discover"\r\n' +
'ST:nanoleaf_aurora:light\r\n' +
'ST:nanoleaf:nl29\r\n' +
'ST:nanoleaf:nl42\r\n' +
'MX:3\r\n' +
'\r\n';
socket_1.send(message, 0, message.length, 1900, address);
});
}, 800);
setTimeout(function () {
clearInterval(broadcastInterval);
socket_1.close();
resolve(found_2);
}, timer);
});
}
catch (e) {
reject(e);
}
return [2];
});
}); });
};
function _isNanoleaf(description) {
return /nanoleaf/i.test(description) && /location:/i.test(description);
}
function _getNanoleafIp(description) {
return /location:(.*)/i.exec(description)[1].trim();
}
function _getNanoleafType(description) {
if (typeof description === 'string') {
if (description === null || description === void 0 ? void 0 : description.toLowerCase().includes('light panels')) {
return 'aurora';
}
else if (description === null || description === void 0 ? void 0 : description.toLowerCase().includes('canvas')) {
return 'canvas';
}
else if (description === null || description === void 0 ? void 0 : description.toLowerCase().includes('shapes')) {
return 'shapes';
}
else {
return 'aurora';
}
}
else {
if (/aurora:light/i.test(description)) {
return 'aurora';
}
else if (/nl29/i.test(description)) {
return 'canvas';
}
else if (/nl42/i.test(description)) {
return 'shapes';
}
else {
return 'aurora';
}
}
}
function _getNanoleafName(description) {
try {
return /nl-devicename:(.*)/i.exec(description)[1].trim();
}
catch (_a) {
return '';
}
}