123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- "use strict";
- var __read = (this && this.__read) || function (o, n) {
- var m = typeof Symbol === "function" && o[Symbol.iterator];
- if (!m) return o;
- var i = m.call(o), r, ar = [], e;
- try {
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
- }
- catch (error) { e = { error: error }; }
- finally {
- try {
- if (r && !r.done && (m = i["return"])) m.call(i);
- }
- finally { if (e) throw e.error; }
- }
- return ar;
- };
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- var multicast_dns_1 = __importDefault(require("multicast-dns"));
- var Controller_1 = __importDefault(require("../controller/Controller"));
- var ELanType_1 = __importDefault(require("../ts/enum/ELanType"));
- var logger_1 = require("../utils/logger");
- var Mdns = (function () {
- function Mdns(params) {
- var onResponseCb = params.onResponseCb, queryParams = params.queryParams, queryCb = params.queryCb;
- this.mdns = new multicast_dns_1.default();
- this.onResponse(onResponseCb);
- this.query(queryParams, queryCb);
- }
- Mdns.createInstance = function (params) {
- if (!Mdns.instance) {
- Mdns.instance = new Mdns(params);
- }
- return Mdns.instance;
- };
- Mdns.prototype.query = function (params, callback) {
- if (!this.mdns) {
- this.mdns = new multicast_dns_1.default();
- }
- this.mdns.query(params, callback);
- };
- Mdns.prototype.onResponse = function (callback) {
- this.mdns.on('response', function (packet) {
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
- var answers = packet.answers;
- if (Array.isArray(answers)) {
- var tmp = {};
- var key = '';
- var _loop_1 = function (i) {
- var data = answers[i].data;
- switch (answers[i].type) {
- case 'PTR':
- if ("".concat(data).indexOf('ewelink') === -1) {
- return { value: void 0 };
- }
- tmp.ptr = data;
- break;
- case 'A':
- tmp.a = data;
- break;
- case 'SRV':
- tmp.srv = data;
- break;
- case 'TXT':
- var arr = data.toString().split(/(?<!\{.*),(?!\}.*)/);
- var txtData_1 = {};
- arr.map(function (str) {
- var _a = __read(str.split('='), 2), key = _a[0], value = _a[1];
- try {
- txtData_1[key] = JSON.parse(value);
- }
- catch (_b) {
- txtData_1[key] = value;
- }
- });
- tmp.txt = txtData_1;
- key = txtData_1.id;
- break;
- default:
- break;
- }
- };
- for (var i = 0; i < answers.length; i++) {
- var state_1 = _loop_1(i);
- if (typeof state_1 === "object")
- return state_1.value;
- }
- if (!key) {
- return;
- }
- if (((_a = tmp.txt) === null || _a === void 0 ? void 0 : _a.type) === 'diy_plug') {
- logger_1.logger.info('Found DIY switch');
- var diyDevice = Controller_1.default.setDevice({
- id: key,
- data: tmp,
- type: 1,
- });
- callback && callback(diyDevice);
- }
- else if (((_b = tmp.txt) === null || _b === void 0 ? void 0 : _b.type) === ELanType_1.default.Plug) {
- logger_1.logger.info('Found LAN switch');
- var lanDevice = Controller_1.default.setDevice({
- id: key,
- data: tmp,
- type: 2,
- lanType: ELanType_1.default.Plug,
- });
- callback && callback(lanDevice);
- }
- else if (((_c = tmp.txt) === null || _c === void 0 ? void 0 : _c.type) === ELanType_1.default.Strip) {
- logger_1.logger.info('Found LAN multi-channel switch');
- var lanDevice = Controller_1.default.setDevice({
- id: key,
- data: tmp,
- type: 2,
- lanType: ELanType_1.default.Strip,
- });
- callback && callback(lanDevice);
- }
- else if (((_d = tmp.txt) === null || _d === void 0 ? void 0 : _d.type) === ELanType_1.default.MultifunSwitch) {
- logger_1.logger.info('Found LAN DualR3');
- var dualR3 = Controller_1.default.setDevice({
- id: key,
- data: tmp,
- type: 2,
- lanType: ELanType_1.default.MultifunSwitch,
- });
- callback && callback(dualR3);
- }
- else if (((_e = tmp.txt) === null || _e === void 0 ? void 0 : _e.type) === ELanType_1.default.EnhancedPlug) {
- logger_1.logger.info('Found LAN enhanced plug');
- var device = Controller_1.default.setDevice({
- id: key,
- data: tmp,
- type: 2,
- lanType: ELanType_1.default.EnhancedPlug,
- });
- callback && callback(device);
- }
- else if (((_f = tmp.txt) === null || _f === void 0 ? void 0 : _f.type) === ELanType_1.default.THPlug) {
- logger_1.logger.info('Found LAN TH plug');
- var device = Controller_1.default.setDevice({
- id: key,
- data: tmp,
- type: 2,
- lanType: ELanType_1.default.THPlug,
- });
- callback && callback(device);
- }
- else if (((_g = tmp.txt) === null || _g === void 0 ? void 0 : _g.type) === ELanType_1.default.RF) {
- logger_1.logger.info('Found LAN RF-Bridge');
- var device = Controller_1.default.setDevice({
- id: key,
- data: tmp,
- type: 2,
- lanType: ELanType_1.default.RF,
- });
- callback && callback(device);
- }
- else if (((_h = tmp.txt) === null || _h === void 0 ? void 0 : _h.type) === ELanType_1.default.FanLight) {
- logger_1.logger.info('Found LAN fan light');
- var device = Controller_1.default.setDevice({
- id: key,
- data: tmp,
- type: 2,
- lanType: ELanType_1.default.FanLight,
- });
- callback && callback(device);
- }
- else if (((_j = tmp.txt) === null || _j === void 0 ? void 0 : _j.type) === ELanType_1.default.Light) {
- logger_1.logger.info('Found LAN multi-color lamp');
- }
- }
- });
- };
- Mdns.prototype.destroy = function () {
- this.mdns.destroy();
- this.mdns = null;
- };
- return Mdns;
- }());
- exports.default = Mdns;
|