formatDevice.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. "use strict";
  2. var __values = (this && this.__values) || function(o) {
  3. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  4. if (m) return m.call(o);
  5. if (o && typeof o.length === "number") return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  12. };
  13. var __importDefault = (this && this.__importDefault) || function (mod) {
  14. return (mod && mod.__esModule) ? mod : { "default": mod };
  15. };
  16. Object.defineProperty(exports, "__esModule", { value: true });
  17. exports.getFormattedDeviceList = exports.formatDevice = void 0;
  18. var CloudDeviceController_1 = __importDefault(require("../controller/CloudDeviceController"));
  19. var DiyDeviceController_1 = __importDefault(require("../controller/DiyDeviceController"));
  20. var lodash_1 = __importDefault(require("lodash"));
  21. var LanDeviceController_1 = __importDefault(require("../controller/LanDeviceController"));
  22. var CloudMultiChannelSwitchController_1 = __importDefault(require("../controller/CloudMultiChannelSwitchController"));
  23. var LanMultiChannelSwitchController_1 = __importDefault(require("../controller/LanMultiChannelSwitchController"));
  24. var Controller_1 = __importDefault(require("../controller/Controller"));
  25. var dataUtil_1 = require("./dataUtil");
  26. var CloudTandHModificationController_1 = __importDefault(require("../controller/CloudTandHModificationController"));
  27. var CloudPowerDetectionSwitchController_1 = __importDefault(require("../controller/CloudPowerDetectionSwitchController"));
  28. var CloudDualR3Controller_1 = __importDefault(require("../controller/CloudDualR3Controller"));
  29. var LanTandHModificationController_1 = __importDefault(require("../controller/LanTandHModificationController"));
  30. var LanDualR3Controller_1 = __importDefault(require("../controller/LanDualR3Controller"));
  31. var LanPowerDetectionSwitchController_1 = __importDefault(require("../controller/LanPowerDetectionSwitchController"));
  32. var CloudDW2WiFiController_1 = __importDefault(require("../controller/CloudDW2WiFiController"));
  33. var CloudRFBridgeController_1 = __importDefault(require("../controller/CloudRFBridgeController"));
  34. var LanRFBridgeController_1 = __importDefault(require("../controller/LanRFBridgeController"));
  35. var CloudZigbeeMultiSwitchController_1 = __importDefault(require("../controller/CloudZigbeeMultiSwitchController"));
  36. var LanSwitchController_1 = __importDefault(require("../controller/LanSwitchController"));
  37. var CloudUIID130Controller_1 = __importDefault(require("../controller/CloudUIID130Controller"));
  38. var CloudUIID182Controller_1 = __importDefault(require("../controller/CloudUIID182Controller"));
  39. var ghostManufacturer = function (manufacturer) {
  40. if (manufacturer === void 0) { manufacturer = 'eWeLink'; }
  41. if (~manufacturer.indexOf('松诺') || ~manufacturer.toLocaleUpperCase().indexOf('SONOFF')) {
  42. return 'SONOFF';
  43. }
  44. return 'eWeLink';
  45. };
  46. var handleLanType = function (uiid) {
  47. if (uiid === 181 || uiid === 190) {
  48. return 4;
  49. }
  50. else {
  51. return 2;
  52. }
  53. };
  54. var formatDevice = function (data) {
  55. var _a, _b, _c, _d, _e;
  56. if (data instanceof DiyDeviceController_1.default) {
  57. return {
  58. key: data.deviceId,
  59. uiid: data.uiid,
  60. deviceId: data.deviceId,
  61. deviceName: data.deviceName,
  62. disabled: data.disabled,
  63. ip: data.ip,
  64. port: data.port,
  65. type: data.type,
  66. rssi: (_a = data.txt.data1) === null || _a === void 0 ? void 0 : _a.rssi,
  67. params: data.txt,
  68. online: true,
  69. index: 25,
  70. };
  71. }
  72. if (data instanceof LanDeviceController_1.default) {
  73. var tags = void 0, unit = void 0, rate = void 0;
  74. var index = 18;
  75. if (data instanceof LanMultiChannelSwitchController_1.default ||
  76. data instanceof LanSwitchController_1.default) {
  77. tags = data.channelName;
  78. }
  79. else if (data instanceof LanRFBridgeController_1.default) {
  80. tags = data.tags;
  81. }
  82. else if (data instanceof LanTandHModificationController_1.default) {
  83. unit = data.unit;
  84. }
  85. else if (data instanceof LanDualR3Controller_1.default || data instanceof LanPowerDetectionSwitchController_1.default) {
  86. rate = data.rate;
  87. }
  88. if (data.online) {
  89. index += 8;
  90. }
  91. if (!data.selfApikey || !data.devicekey) {
  92. index -= 16;
  93. }
  94. return {
  95. key: data.deviceId,
  96. deviceId: data.deviceId,
  97. disabled: data.disabled,
  98. ip: data.ip,
  99. uiid: (_b = data.extra) === null || _b === void 0 ? void 0 : _b.uiid,
  100. port: data.port,
  101. type: handleLanType((_c = data.extra) === null || _c === void 0 ? void 0 : _c.uiid),
  102. manufacturer: ghostManufacturer((_d = data.extra) === null || _d === void 0 ? void 0 : _d.manufacturer),
  103. deviceName: data.deviceName,
  104. model: (_e = data.extra) === null || _e === void 0 ? void 0 : _e.model,
  105. apikey: data.selfApikey,
  106. params: data.params,
  107. online: data.online,
  108. index: index,
  109. tags: tags,
  110. unit: unit,
  111. rate: rate,
  112. };
  113. }
  114. if (data instanceof CloudDeviceController_1.default) {
  115. var tags = void 0, unit = void 0, rate = void 0, lowVolAlarm = void 0, eRate = void 0;
  116. var index = 20;
  117. if (data instanceof CloudMultiChannelSwitchController_1.default ||
  118. data instanceof CloudZigbeeMultiSwitchController_1.default ||
  119. data instanceof CloudUIID130Controller_1.default) {
  120. tags = data.channelName;
  121. }
  122. else if (data instanceof CloudRFBridgeController_1.default) {
  123. tags = data.tags;
  124. }
  125. else if (data instanceof CloudTandHModificationController_1.default) {
  126. unit = data.unit;
  127. }
  128. else if (data instanceof CloudPowerDetectionSwitchController_1.default ||
  129. data instanceof CloudDualR3Controller_1.default ||
  130. data instanceof CloudUIID182Controller_1.default) {
  131. rate = data.rate;
  132. }
  133. else if (data instanceof CloudDW2WiFiController_1.default) {
  134. lowVolAlarm = data.lowVolAlarm;
  135. }
  136. if (data instanceof CloudUIID182Controller_1.default) {
  137. eRate = data.eRate;
  138. }
  139. if (data.online) {
  140. index += 8;
  141. }
  142. return {
  143. key: data.deviceId,
  144. deviceId: data.deviceId,
  145. disabled: data.disabled,
  146. uiid: data.uiid,
  147. type: data.type,
  148. manufacturer: ghostManufacturer(data.extra.manufacturer),
  149. deviceName: data.deviceName,
  150. model: data.extra.model,
  151. apikey: data.apikey,
  152. params: data.params,
  153. online: data.online,
  154. index: index,
  155. tags: tags,
  156. unit: unit,
  157. rate: rate,
  158. eRate: eRate,
  159. lowVolAlarm: lowVolAlarm,
  160. };
  161. }
  162. };
  163. exports.formatDevice = formatDevice;
  164. var formatUnsupportDevice = function (data) {
  165. return {
  166. key: data.deviceId,
  167. deviceId: data.deviceId,
  168. uiid: data.uiid,
  169. deviceName: data.deviceName,
  170. params: data.params,
  171. online: data.online,
  172. };
  173. };
  174. var getFormattedDeviceList = function () {
  175. var e_1, _a, e_2, _b;
  176. var result = [];
  177. try {
  178. for (var _c = __values(Controller_1.default.deviceMap.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
  179. var item = _d.value;
  180. result.push(formatDevice(item));
  181. }
  182. }
  183. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  184. finally {
  185. try {
  186. if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
  187. }
  188. finally { if (e_1) throw e_1.error; }
  189. }
  190. try {
  191. for (var _e = __values(Controller_1.default.unsupportDeviceMap.values()), _f = _e.next(); !_f.done; _f = _e.next()) {
  192. var item = _f.value;
  193. result.push(formatUnsupportDevice(item));
  194. }
  195. }
  196. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  197. finally {
  198. try {
  199. if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
  200. }
  201. finally { if (e_2) throw e_2.error; }
  202. }
  203. var oldDiyDevices = (0, dataUtil_1.getDataSync)('diy.json', []);
  204. for (var key in oldDiyDevices) {
  205. try {
  206. if (!Controller_1.default.getDevice(key)) {
  207. result.push({
  208. online: false,
  209. type: 1,
  210. deviceId: key,
  211. deviceName: lodash_1.default.get(oldDiyDevices, [key, 'deviceName']),
  212. index: 3,
  213. });
  214. }
  215. }
  216. catch (error) {
  217. if (!Controller_1.default.getDevice(key)) {
  218. result.push({
  219. online: false,
  220. type: 1,
  221. deviceId: key,
  222. index: 3,
  223. });
  224. }
  225. }
  226. }
  227. result.sort(function (a, b) {
  228. if (!a.index) {
  229. return 1;
  230. }
  231. if (!b.index) {
  232. return -1;
  233. }
  234. return b.index - a.index;
  235. });
  236. return result;
  237. };
  238. exports.getFormattedDeviceList = getFormattedDeviceList;