initHaSocket.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  14. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  15. return new (P || (P = Promise))(function (resolve, reject) {
  16. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  17. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  18. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  19. step((generator = generator.apply(thisArg, _arguments || [])).next());
  20. });
  21. };
  22. var __generator = (this && this.__generator) || function (thisArg, body) {
  23. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  24. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  25. function verb(n) { return function (v) { return step([n, v]); }; }
  26. function step(op) {
  27. if (f) throw new TypeError("Generator is already executing.");
  28. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  29. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  30. if (y = 0, t) op = [op[0] & 2, t.value];
  31. switch (op[0]) {
  32. case 0: case 1: t = op; break;
  33. case 4: _.label++; return { value: op[1], done: false };
  34. case 5: _.label++; y = op[1]; op = [0]; continue;
  35. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  36. default:
  37. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  38. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  39. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  40. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  41. if (t[2]) _.ops.pop();
  42. _.trys.pop(); continue;
  43. }
  44. op = body.call(thisArg, _);
  45. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  46. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  47. }
  48. };
  49. var __read = (this && this.__read) || function (o, n) {
  50. var m = typeof Symbol === "function" && o[Symbol.iterator];
  51. if (!m) return o;
  52. var i = m.call(o), r, ar = [], e;
  53. try {
  54. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  55. }
  56. catch (error) { e = { error: error }; }
  57. finally {
  58. try {
  59. if (r && !r.done && (m = i["return"])) m.call(i);
  60. }
  61. finally { if (e) throw e.error; }
  62. }
  63. return ar;
  64. };
  65. var __values = (this && this.__values) || function(o) {
  66. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  67. if (m) return m.call(o);
  68. if (o && typeof o.length === "number") return {
  69. next: function () {
  70. if (o && i >= o.length) o = void 0;
  71. return { value: o && o[i++], done: !o };
  72. }
  73. };
  74. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  75. };
  76. var __importDefault = (this && this.__importDefault) || function (mod) {
  77. return (mod && mod.__esModule) ? mod : { "default": mod };
  78. };
  79. Object.defineProperty(exports, "__esModule", { value: true });
  80. var lodash_1 = __importDefault(require("lodash"));
  81. var HASocketClass_1 = __importDefault(require("../class/HASocketClass"));
  82. var Controller_1 = __importDefault(require("../controller/Controller"));
  83. var DiyDeviceController_1 = __importDefault(require("../controller/DiyDeviceController"));
  84. var LanSwitchController_1 = __importDefault(require("../controller/LanSwitchController"));
  85. var CloudSwitchController_1 = __importDefault(require("../controller/CloudSwitchController"));
  86. var CloudRGBBulbController_1 = __importDefault(require("../controller/CloudRGBBulbController"));
  87. var CloudDimmingController_1 = __importDefault(require("../controller/CloudDimmingController"));
  88. var CloudPowerDetectionSwitchController_1 = __importDefault(require("../controller/CloudPowerDetectionSwitchController"));
  89. var CloudMultiChannelSwitchController_1 = __importDefault(require("../controller/CloudMultiChannelSwitchController"));
  90. var CloudRGBLightStripController_1 = __importDefault(require("../controller/CloudRGBLightStripController"));
  91. var LanMultiChannelSwitchController_1 = __importDefault(require("../controller/LanMultiChannelSwitchController"));
  92. var CloudTandHModificationController_1 = __importDefault(require("../controller/CloudTandHModificationController"));
  93. var CloudDoubleColorBulbController_1 = __importDefault(require("../controller/CloudDoubleColorBulbController"));
  94. var CloudDualR3Controller_1 = __importDefault(require("../controller/CloudDualR3Controller"));
  95. var eventBus_1 = __importDefault(require("./eventBus"));
  96. var LanDualR3Controller_1 = __importDefault(require("../controller/LanDualR3Controller"));
  97. var LanTandHModificationController_1 = __importDefault(require("../controller/LanTandHModificationController"));
  98. var LanPowerDetectionSwitchController_1 = __importDefault(require("../controller/LanPowerDetectionSwitchController"));
  99. var LanDoubleColorLightController_1 = __importDefault(require("../controller/LanDoubleColorLightController"));
  100. var CloudUIID104Controller_1 = __importDefault(require("../controller/CloudUIID104Controller"));
  101. var haServiceMap_1 = __importDefault(require("../config/haServiceMap"));
  102. var CloudCoverController_1 = __importDefault(require("../controller/CloudCoverController"));
  103. var CloudUIID44Controller_1 = __importDefault(require("../controller/CloudUIID44Controller"));
  104. var CloudUIID34Controller_1 = __importDefault(require("../controller/CloudUIID34Controller"));
  105. var LanUIID34Controller_1 = __importDefault(require("../controller/LanUIID34Controller"));
  106. var logger_1 = require("./logger");
  107. var CloudZigbeeMultiSwitchController_1 = __importDefault(require("../controller/CloudZigbeeMultiSwitchController"));
  108. var CloudZigbeeDoubleColorBulbController_1 = __importDefault(require("../controller/CloudZigbeeDoubleColorBulbController"));
  109. var CloudZigbeeFiveColorBulbController_1 = __importDefault(require("../controller/CloudZigbeeFiveColorBulbController"));
  110. var CloudUIID181Controller_1 = __importDefault(require("../controller/CloudUIID181Controller"));
  111. var CloudUIID190Controller_1 = __importDefault(require("../controller/CloudUIID190Controller"));
  112. var CloudUIID130Controller_1 = __importDefault(require("../controller/CloudUIID130Controller"));
  113. var CloudUIID182Controller_1 = __importDefault(require("../controller/CloudUIID182Controller"));
  114. var CloudUIID137Controller_1 = __importDefault(require("../controller/CloudUIID137Controller"));
  115. var CloudUIID173Controller_1 = __importDefault(require("../controller/CloudUIID173Controller"));
  116. var handleDeviceByEntityId = function (entity_id, state, res, mutiSwitchState) { return __awaiter(void 0, void 0, void 0, function () {
  117. var device, _a, id, outlet, params, _b, _c, brightness_pct, _d, id, outlet, params, params, brightness_pct, switches, _e, brightness, color_temp, _f, id, outlet;
  118. return __generator(this, function (_g) {
  119. switch (_g.label) {
  120. case 0:
  121. device = Controller_1.default.getDevice(entity_id.replace(/_\d+$/, ''));
  122. if (!(device instanceof DiyDeviceController_1.default)) return [3, 2];
  123. return [4, device.setSwitch(state)];
  124. case 1:
  125. _g.sent();
  126. return [3, 58];
  127. case 2:
  128. if (!(device instanceof LanSwitchController_1.default)) return [3, 4];
  129. return [4, device.setSwitch(state)];
  130. case 3:
  131. _g.sent();
  132. return [3, 58];
  133. case 4:
  134. if (!(device instanceof LanMultiChannelSwitchController_1.default || device instanceof LanDualR3Controller_1.default)) return [3, 9];
  135. if (!mutiSwitchState) return [3, 6];
  136. return [4, device.setSwitch(mutiSwitchState)];
  137. case 5:
  138. _g.sent();
  139. return [3, 8];
  140. case 6:
  141. _a = __read(entity_id.split('_'), 2), id = _a[0], outlet = _a[1];
  142. return [4, device.setSwitch([
  143. {
  144. outlet: +outlet - 1,
  145. switch: state,
  146. },
  147. ])];
  148. case 7:
  149. _g.sent();
  150. _g.label = 8;
  151. case 8: return [3, 58];
  152. case 9:
  153. if (!(device instanceof LanTandHModificationController_1.default)) return [3, 10];
  154. device.setSwitch(state);
  155. return [3, 58];
  156. case 10:
  157. if (!(device instanceof LanPowerDetectionSwitchController_1.default)) return [3, 11];
  158. device.setSwitch(state);
  159. return [3, 58];
  160. case 11:
  161. if (!(device instanceof LanDoubleColorLightController_1.default)) return [3, 12];
  162. device.updateLight(device.parseHaData2Ck(__assign({ state: state }, res.service_data)));
  163. return [3, 58];
  164. case 12:
  165. if (!(device instanceof LanUIID34Controller_1.default)) return [3, 17];
  166. params = device.parseHaData2Lan(__assign({ state: state }, res.service_data));
  167. _b = params.fan;
  168. if (!_b) return [3, 14];
  169. return [4, device.setFan(params)];
  170. case 13:
  171. _b = (_g.sent());
  172. _g.label = 14;
  173. case 14:
  174. _b;
  175. _c = params.light;
  176. if (!_c) return [3, 16];
  177. return [4, device.toggleLight(params)];
  178. case 15:
  179. _c = (_g.sent());
  180. _g.label = 16;
  181. case 16:
  182. _c;
  183. return [3, 58];
  184. case 17:
  185. if (!(device instanceof CloudSwitchController_1.default)) return [3, 19];
  186. return [4, device.updateSwitch(state)];
  187. case 18:
  188. _g.sent();
  189. return [3, 58];
  190. case 19:
  191. if (!(device instanceof CloudRGBBulbController_1.default)) return [3, 21];
  192. return [4, device.updateLight(device.parseHaData2Ck(__assign({ state: state }, res.service_data)))];
  193. case 20:
  194. _g.sent();
  195. return [3, 58];
  196. case 21:
  197. if (!(device instanceof CloudDimmingController_1.default)) return [3, 23];
  198. brightness_pct = res.service_data.brightness_pct;
  199. return [4, device.updateLight({
  200. switch: state,
  201. bright: brightness_pct,
  202. })];
  203. case 22:
  204. _g.sent();
  205. return [3, 58];
  206. case 23:
  207. if (!(device instanceof CloudPowerDetectionSwitchController_1.default)) return [3, 25];
  208. return [4, device.updateSwitch(state)];
  209. case 24:
  210. _g.sent();
  211. return [3, 58];
  212. case 25:
  213. if (!(device instanceof CloudTandHModificationController_1.default)) return [3, 27];
  214. return [4, device.updateSwitch(state)];
  215. case 26:
  216. _g.sent();
  217. return [3, 58];
  218. case 27:
  219. if (!(device instanceof CloudMultiChannelSwitchController_1.default ||
  220. device instanceof CloudDualR3Controller_1.default ||
  221. device instanceof CloudZigbeeMultiSwitchController_1.default ||
  222. device instanceof CloudUIID130Controller_1.default ||
  223. device instanceof CloudUIID182Controller_1.default)) return [3, 32];
  224. if (!mutiSwitchState) return [3, 29];
  225. return [4, device.updateSwitch(mutiSwitchState)];
  226. case 28:
  227. _g.sent();
  228. return [3, 31];
  229. case 29:
  230. _d = __read(entity_id.split('_'), 2), id = _d[0], outlet = _d[1];
  231. return [4, device.updateSwitch([
  232. {
  233. outlet: +outlet - 1,
  234. switch: state,
  235. },
  236. ])];
  237. case 30:
  238. _g.sent();
  239. _g.label = 31;
  240. case 31: return [3, 58];
  241. case 32:
  242. if (!(device instanceof CloudRGBLightStripController_1.default)) return [3, 33];
  243. params = device.parseHaData2Ck(__assign({ state: state }, res.service_data));
  244. device.updateLight(params);
  245. return [3, 58];
  246. case 33:
  247. if (!(device instanceof CloudDoubleColorBulbController_1.default)) return [3, 35];
  248. return [4, device.updateLight(device.parseHaData2Ck(__assign({ state: state }, res.service_data)))];
  249. case 34:
  250. _g.sent();
  251. return [3, 58];
  252. case 35:
  253. if (!(device instanceof CloudUIID104Controller_1.default)) return [3, 37];
  254. return [4, device.updateLight(device.parseHaData2Ck(__assign({ state: state }, res.service_data)))];
  255. case 36:
  256. _g.sent();
  257. return [3, 58];
  258. case 37:
  259. if (!(device instanceof CloudUIID137Controller_1.default || device instanceof CloudUIID173Controller_1.default)) return [3, 39];
  260. params = device.parseHaData2Ck(__assign({ state: state }, res.service_data));
  261. logger_1.logger.info('🚀 ~ 173 返回HA的参数' + JSON.stringify(params));
  262. return [4, device.updateLight(params)];
  263. case 38:
  264. _g.sent();
  265. return [3, 58];
  266. case 39:
  267. if (!(device instanceof CloudCoverController_1.default)) return [3, 41];
  268. return [4, device.setCover({ switch: state, setclose: lodash_1.default.get(res, 'service_data.position') })];
  269. case 40:
  270. _g.sent();
  271. return [3, 58];
  272. case 41:
  273. if (!(device instanceof CloudUIID44Controller_1.default)) return [3, 43];
  274. brightness_pct = res.service_data.brightness_pct;
  275. return [4, device.updateLight({ switch: state, brightness: brightness_pct })];
  276. case 42:
  277. _g.sent();
  278. return [3, 58];
  279. case 43:
  280. if (!(device instanceof CloudUIID34Controller_1.default)) return [3, 45];
  281. switches = device.parseHaData2Ck(__assign({ state: state }, res.service_data));
  282. return [4, device.updateSwitch(switches)];
  283. case 44:
  284. _g.sent();
  285. return [3, 58];
  286. case 45:
  287. if (!(device instanceof CloudZigbeeDoubleColorBulbController_1.default)) return [3, 50];
  288. _e = res.service_data, brightness = _e.brightness, color_temp = _e.color_temp;
  289. if (!(brightness && color_temp)) return [3, 48];
  290. return [4, device.updateLight(device.parseHaData2Ck({ state: state, brightness: brightness }))];
  291. case 46:
  292. _g.sent();
  293. return [4, device.updateLight(device.parseHaData2Ck({ state: state, color_temp: color_temp }))];
  294. case 47:
  295. _g.sent();
  296. return [2];
  297. case 48: return [4, device.updateLight(device.parseHaData2Ck(__assign({ state: state }, res.service_data)))];
  298. case 49:
  299. _g.sent();
  300. return [3, 58];
  301. case 50:
  302. if (!(device instanceof CloudZigbeeFiveColorBulbController_1.default)) return [3, 52];
  303. return [4, device.updateLight(device.parseHaData2Ck(__assign({ state: state }, res.service_data)))];
  304. case 51:
  305. _g.sent();
  306. return [3, 58];
  307. case 52:
  308. if (!(device instanceof CloudUIID181Controller_1.default)) return [3, 54];
  309. return [4, device.updateSwitch(state)];
  310. case 53:
  311. _g.sent();
  312. return [3, 58];
  313. case 54:
  314. if (!(device instanceof CloudUIID190Controller_1.default)) return [3, 58];
  315. if (!mutiSwitchState) return [3, 56];
  316. return [4, device.updateSwitch(mutiSwitchState)];
  317. case 55:
  318. _g.sent();
  319. return [3, 58];
  320. case 56:
  321. _f = __read(entity_id.split('_'), 2), id = _f[0], outlet = _f[1];
  322. return [4, device.updateSwitch([
  323. {
  324. outlet: +outlet - 1,
  325. switch: state,
  326. },
  327. ])];
  328. case 57:
  329. _g.sent();
  330. _g.label = 58;
  331. case 58: return [2];
  332. }
  333. });
  334. }); };
  335. exports.default = (function (reconnect) {
  336. if (reconnect === void 0) { reconnect = false; }
  337. return __awaiter(void 0, void 0, void 0, function () {
  338. var res, err_1;
  339. return __generator(this, function (_a) {
  340. switch (_a.label) {
  341. case 0:
  342. _a.trys.push([0, 2, , 3]);
  343. return [4, HASocketClass_1.default.init(reconnect)];
  344. case 1:
  345. res = _a.sent();
  346. if (res === 0) {
  347. HASocketClass_1.default.subscribeEvents('call_service');
  348. HASocketClass_1.default.handleEvent('call_service', function (res) { return __awaiter(void 0, void 0, void 0, function () {
  349. var entity_id, service, state, tmpMap_1, _a, _b, _c, id, mutiSwitchState, e_1_1;
  350. var e_1, _d;
  351. return __generator(this, function (_e) {
  352. switch (_e.label) {
  353. case 0:
  354. logger_1.logger.info("HA emit call_service event: ".concat(JSON.stringify(res)));
  355. entity_id = res.service_data.entity_id, service = res.service;
  356. state = haServiceMap_1.default.get(service);
  357. if (!Array.isArray(entity_id)) return [3, 8];
  358. tmpMap_1 = new Map();
  359. entity_id.forEach(function (item) {
  360. var _a = __read(item.split('_'), 2), deviceid = _a[0], outlet = _a[1];
  361. var device = Controller_1.default.getDevice(deviceid);
  362. if (device instanceof LanMultiChannelSwitchController_1.default || device instanceof CloudMultiChannelSwitchController_1.default || device instanceof CloudDualR3Controller_1.default || device instanceof CloudZigbeeMultiSwitchController_1.default) {
  363. if (tmpMap_1.has(deviceid)) {
  364. tmpMap_1.get(deviceid).push({
  365. outlet: outlet - 1,
  366. switch: state,
  367. });
  368. }
  369. else {
  370. tmpMap_1.set(deviceid, [
  371. {
  372. outlet: outlet - 1,
  373. switch: state,
  374. },
  375. ]);
  376. }
  377. }
  378. else {
  379. handleDeviceByEntityId(item, state, res);
  380. }
  381. });
  382. _e.label = 1;
  383. case 1:
  384. _e.trys.push([1, 6, 7, 8]);
  385. _a = __values(tmpMap_1.entries()), _b = _a.next();
  386. _e.label = 2;
  387. case 2:
  388. if (!!_b.done) return [3, 5];
  389. _c = __read(_b.value, 2), id = _c[0], mutiSwitchState = _c[1];
  390. return [4, handleDeviceByEntityId(id, state, res, mutiSwitchState)];
  391. case 3:
  392. _e.sent();
  393. _e.label = 4;
  394. case 4:
  395. _b = _a.next();
  396. return [3, 2];
  397. case 5: return [3, 8];
  398. case 6:
  399. e_1_1 = _e.sent();
  400. e_1 = { error: e_1_1 };
  401. return [3, 8];
  402. case 7:
  403. try {
  404. if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
  405. }
  406. finally { if (e_1) throw e_1.error; }
  407. return [7];
  408. case 8:
  409. if (!(typeof entity_id === 'string')) return [3, 10];
  410. return [4, handleDeviceByEntityId(entity_id, state, res)];
  411. case 9:
  412. _e.sent();
  413. _e.label = 10;
  414. case 10:
  415. eventBus_1.default.emit('sse');
  416. return [2];
  417. }
  418. });
  419. }); });
  420. }
  421. return [3, 3];
  422. case 2:
  423. err_1 = _a.sent();
  424. logger_1.logger.error("initHaSocket error: ".concat(err_1));
  425. return [3, 3];
  426. case 3: return [2];
  427. }
  428. });
  429. });
  430. });