MdnsClass.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __importDefault = (this && this.__importDefault) || function (mod) {
  19. return (mod && mod.__esModule) ? mod : { "default": mod };
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. var multicast_dns_1 = __importDefault(require("multicast-dns"));
  23. var Controller_1 = __importDefault(require("../controller/Controller"));
  24. var ELanType_1 = __importDefault(require("../ts/enum/ELanType"));
  25. var logger_1 = require("../utils/logger");
  26. var Mdns = (function () {
  27. function Mdns(params) {
  28. var onResponseCb = params.onResponseCb, queryParams = params.queryParams, queryCb = params.queryCb;
  29. this.mdns = new multicast_dns_1.default();
  30. this.onResponse(onResponseCb);
  31. this.query(queryParams, queryCb);
  32. }
  33. Mdns.createInstance = function (params) {
  34. if (!Mdns.instance) {
  35. Mdns.instance = new Mdns(params);
  36. }
  37. return Mdns.instance;
  38. };
  39. Mdns.prototype.query = function (params, callback) {
  40. if (!this.mdns) {
  41. this.mdns = new multicast_dns_1.default();
  42. }
  43. this.mdns.query(params, callback);
  44. };
  45. Mdns.prototype.onResponse = function (callback) {
  46. this.mdns.on('response', function (packet) {
  47. var _a, _b, _c, _d, _e, _f, _g, _h, _j;
  48. var answers = packet.answers;
  49. if (Array.isArray(answers)) {
  50. var tmp = {};
  51. var key = '';
  52. var _loop_1 = function (i) {
  53. var data = answers[i].data;
  54. switch (answers[i].type) {
  55. case 'PTR':
  56. if ("".concat(data).indexOf('ewelink') === -1) {
  57. return { value: void 0 };
  58. }
  59. tmp.ptr = data;
  60. break;
  61. case 'A':
  62. tmp.a = data;
  63. break;
  64. case 'SRV':
  65. tmp.srv = data;
  66. break;
  67. case 'TXT':
  68. var arr = data.toString().split(/(?<!\{.*),(?!\}.*)/);
  69. var txtData_1 = {};
  70. arr.map(function (str) {
  71. var _a = __read(str.split('='), 2), key = _a[0], value = _a[1];
  72. try {
  73. txtData_1[key] = JSON.parse(value);
  74. }
  75. catch (_b) {
  76. txtData_1[key] = value;
  77. }
  78. });
  79. tmp.txt = txtData_1;
  80. key = txtData_1.id;
  81. break;
  82. default:
  83. break;
  84. }
  85. };
  86. for (var i = 0; i < answers.length; i++) {
  87. var state_1 = _loop_1(i);
  88. if (typeof state_1 === "object")
  89. return state_1.value;
  90. }
  91. if (!key) {
  92. return;
  93. }
  94. if (((_a = tmp.txt) === null || _a === void 0 ? void 0 : _a.type) === 'diy_plug') {
  95. logger_1.logger.info('Found DIY switch');
  96. var diyDevice = Controller_1.default.setDevice({
  97. id: key,
  98. data: tmp,
  99. type: 1,
  100. });
  101. callback && callback(diyDevice);
  102. }
  103. else if (((_b = tmp.txt) === null || _b === void 0 ? void 0 : _b.type) === ELanType_1.default.Plug) {
  104. logger_1.logger.info('Found LAN switch');
  105. var lanDevice = Controller_1.default.setDevice({
  106. id: key,
  107. data: tmp,
  108. type: 2,
  109. lanType: ELanType_1.default.Plug,
  110. });
  111. callback && callback(lanDevice);
  112. }
  113. else if (((_c = tmp.txt) === null || _c === void 0 ? void 0 : _c.type) === ELanType_1.default.Strip) {
  114. logger_1.logger.info('Found LAN multi-channel switch');
  115. var lanDevice = Controller_1.default.setDevice({
  116. id: key,
  117. data: tmp,
  118. type: 2,
  119. lanType: ELanType_1.default.Strip,
  120. });
  121. callback && callback(lanDevice);
  122. }
  123. else if (((_d = tmp.txt) === null || _d === void 0 ? void 0 : _d.type) === ELanType_1.default.MultifunSwitch) {
  124. logger_1.logger.info('Found LAN DualR3');
  125. var dualR3 = Controller_1.default.setDevice({
  126. id: key,
  127. data: tmp,
  128. type: 2,
  129. lanType: ELanType_1.default.MultifunSwitch,
  130. });
  131. callback && callback(dualR3);
  132. }
  133. else if (((_e = tmp.txt) === null || _e === void 0 ? void 0 : _e.type) === ELanType_1.default.EnhancedPlug) {
  134. logger_1.logger.info('Found LAN enhanced plug');
  135. var device = Controller_1.default.setDevice({
  136. id: key,
  137. data: tmp,
  138. type: 2,
  139. lanType: ELanType_1.default.EnhancedPlug,
  140. });
  141. callback && callback(device);
  142. }
  143. else if (((_f = tmp.txt) === null || _f === void 0 ? void 0 : _f.type) === ELanType_1.default.THPlug) {
  144. logger_1.logger.info('Found LAN TH plug');
  145. var device = Controller_1.default.setDevice({
  146. id: key,
  147. data: tmp,
  148. type: 2,
  149. lanType: ELanType_1.default.THPlug,
  150. });
  151. callback && callback(device);
  152. }
  153. else if (((_g = tmp.txt) === null || _g === void 0 ? void 0 : _g.type) === ELanType_1.default.RF) {
  154. logger_1.logger.info('Found LAN RF-Bridge');
  155. var device = Controller_1.default.setDevice({
  156. id: key,
  157. data: tmp,
  158. type: 2,
  159. lanType: ELanType_1.default.RF,
  160. });
  161. callback && callback(device);
  162. }
  163. else if (((_h = tmp.txt) === null || _h === void 0 ? void 0 : _h.type) === ELanType_1.default.FanLight) {
  164. logger_1.logger.info('Found LAN fan light');
  165. var device = Controller_1.default.setDevice({
  166. id: key,
  167. data: tmp,
  168. type: 2,
  169. lanType: ELanType_1.default.FanLight,
  170. });
  171. callback && callback(device);
  172. }
  173. else if (((_j = tmp.txt) === null || _j === void 0 ? void 0 : _j.type) === ELanType_1.default.Light) {
  174. logger_1.logger.info('Found LAN multi-color lamp');
  175. }
  176. }
  177. });
  178. };
  179. Mdns.prototype.destroy = function () {
  180. this.mdns.destroy();
  181. this.mdns = null;
  182. };
  183. return Mdns;
  184. }());
  185. exports.default = Mdns;