initCkWs.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 __importDefault = (this && this.__importDefault) || function (mod) {
  50. return (mod && mod.__esModule) ? mod : { "default": mod };
  51. };
  52. Object.defineProperty(exports, "__esModule", { value: true });
  53. var coolkit_ws_1 = __importDefault(require("coolkit-ws"));
  54. var dataUtil_1 = require("./dataUtil");
  55. var Controller_1 = __importDefault(require("../controller/Controller"));
  56. var app_1 = require("../config/app");
  57. var CloudSwitchController_1 = __importDefault(require("../controller/CloudSwitchController"));
  58. var CloudTandHModificationController_1 = __importDefault(require("../controller/CloudTandHModificationController"));
  59. var CloudRGBBulbController_1 = __importDefault(require("../controller/CloudRGBBulbController"));
  60. var CloudDimmingController_1 = __importDefault(require("../controller/CloudDimmingController"));
  61. var CloudPowerDetectionSwitchController_1 = __importDefault(require("../controller/CloudPowerDetectionSwitchController"));
  62. var CloudMultiChannelSwitchController_1 = __importDefault(require("../controller/CloudMultiChannelSwitchController"));
  63. var CloudRGBLightStripController_1 = __importDefault(require("../controller/CloudRGBLightStripController"));
  64. var restApi_1 = require("../apis/restApi");
  65. var CloudDoubleColorBulbController_1 = __importDefault(require("../controller/CloudDoubleColorBulbController"));
  66. var eventBus_1 = __importDefault(require("./eventBus"));
  67. var CloudDualR3Controller_1 = __importDefault(require("../controller/CloudDualR3Controller"));
  68. var LanDualR3Controller_1 = __importDefault(require("../controller/LanDualR3Controller"));
  69. var CloudDW2WiFiController_1 = __importDefault(require("../controller/CloudDW2WiFiController"));
  70. var CloudUIID104Controller_1 = __importDefault(require("../controller/CloudUIID104Controller"));
  71. var CloudZigbeeUIID1770Controller_1 = __importDefault(require("../controller/CloudZigbeeUIID1770Controller"));
  72. var CloudZigbeeUIID2026Controller_1 = __importDefault(require("../controller/CloudZigbeeUIID2026Controller"));
  73. var CloudZigbeeUIID3026Controller_1 = __importDefault(require("../controller/CloudZigbeeUIID3026Controller"));
  74. var CloudZigbeeUIID4026Controller_1 = __importDefault(require("../controller/CloudZigbeeUIID4026Controller"));
  75. var CloudZigbeeUIID1000Controller_1 = __importDefault(require("../controller/CloudZigbeeUIID1000Controller"));
  76. var CloudCoverController_1 = __importDefault(require("../controller/CloudCoverController"));
  77. var LanTandHModificationController_1 = __importDefault(require("../controller/LanTandHModificationController"));
  78. var CloudRFBridgeController_1 = __importDefault(require("../controller/CloudRFBridgeController"));
  79. var CloudUIID44Controller_1 = __importDefault(require("../controller/CloudUIID44Controller"));
  80. var CloudUIID34Controller_1 = __importDefault(require("../controller/CloudUIID34Controller"));
  81. var process_1 = __importDefault(require("process"));
  82. var logger_1 = require("./logger");
  83. var CloudZigbeeMultiSwitchController_1 = __importDefault(require("../controller/CloudZigbeeMultiSwitchController"));
  84. var CloudZigbeeDoubleColorBulbController_1 = __importDefault(require("../controller/CloudZigbeeDoubleColorBulbController"));
  85. var CloudZigbeeFiveColorBulbController_1 = __importDefault(require("../controller/CloudZigbeeFiveColorBulbController"));
  86. var CloudUIID181Controller_1 = __importDefault(require("../controller/CloudUIID181Controller"));
  87. var CloudUIID190Controller_1 = __importDefault(require("../controller/CloudUIID190Controller"));
  88. var CloudUIID137Controller_1 = __importDefault(require("../controller/CloudUIID137Controller"));
  89. var CloudUIID173Controller_1 = __importDefault(require("../controller/CloudUIID173Controller"));
  90. var mergeDeviceParams_1 = __importDefault(require("./mergeDeviceParams"));
  91. var LanDeviceController_1 = __importDefault(require("../controller/LanDeviceController"));
  92. var CloudDeviceController_1 = __importDefault(require("../controller/CloudDeviceController"));
  93. var CloudUIID130Controller_1 = __importDefault(require("../controller/CloudUIID130Controller"));
  94. var CloudUIID182Controller_1 = __importDefault(require("../controller/CloudUIID182Controller"));
  95. var lodash_1 = __importDefault(require("lodash"));
  96. var channelMap_1 = require("../config/channelMap");
  97. exports.default = (function () { return __awaiter(void 0, void 0, void 0, function () {
  98. var apikey, at, region, res;
  99. return __generator(this, function (_a) {
  100. switch (_a.label) {
  101. case 0:
  102. apikey = (0, dataUtil_1.getDataSync)('user.json', ['user', 'apikey']);
  103. at = (0, dataUtil_1.getDataSync)('user.json', ['at']);
  104. region = (0, dataUtil_1.getDataSync)('user.json', ['region']);
  105. logger_1.logger.debug("initCkWs.ts at: ".concat(at, " region: ").concat(region));
  106. if (!at || !apikey) {
  107. return [2, -1];
  108. }
  109. return [4, coolkit_ws_1.default.init({
  110. appid: app_1.appId,
  111. at: at,
  112. apikey: apikey,
  113. region: region,
  114. userAgent: 'app',
  115. reqTimeout: 30000,
  116. useTestEnv: process_1.default.env.CK_API_ENV === 'test',
  117. maxRetry: 10000,
  118. })];
  119. case 1:
  120. res = _a.sent();
  121. logger_1.logger.debug('initCkWs.ts res: ' + JSON.stringify(res));
  122. logger_1.logger.debug("initCkWs at: ".concat(at));
  123. coolkit_ws_1.default.on('message', function (ws) { return __awaiter(void 0, void 0, void 0, function () {
  124. var type, data, tmp, device, _a, currentTemperature, currentHumidity, state, _b, bright, status_1, _c, current, voltage, power, status_2, switches, switches, ids, switches, switches, switches, _d, online, name_1, updateUnavailable2Ha, switches, channelNum, i;
  125. return __generator(this, function (_e) {
  126. try {
  127. type = ws.type, data = ws.data;
  128. logger_1.logger.debug("receive CK-WS msg: type: ".concat(type));
  129. logger_1.logger.debug("receive CK-WS msg: data: ".concat(JSON.stringify(data)));
  130. if (type === 'message' && data !== 'pong') {
  131. tmp = JSON.parse(data);
  132. if (!tmp.deviceid) {
  133. return [2];
  134. }
  135. device = Controller_1.default.getDevice(tmp.deviceid);
  136. if (tmp.action === 'update') {
  137. if (device instanceof LanDeviceController_1.default || device instanceof CloudDeviceController_1.default) {
  138. (0, mergeDeviceParams_1.default)(device.params, tmp.params);
  139. }
  140. if (device instanceof CloudSwitchController_1.default) {
  141. device.updateState(tmp.params.switch);
  142. }
  143. else if (device instanceof CloudTandHModificationController_1.default || device instanceof LanTandHModificationController_1.default) {
  144. _a = tmp.params, currentTemperature = _a.currentTemperature, currentHumidity = _a.currentHumidity, state = _a.switch;
  145. if (currentHumidity || currentTemperature) {
  146. device.updateTandH(currentTemperature, currentHumidity);
  147. }
  148. else if (state) {
  149. device.updateState(state);
  150. }
  151. }
  152. else if (device instanceof CloudRGBBulbController_1.default) {
  153. device.updateState(device.parseCkData2Ha(tmp.params));
  154. }
  155. else if (device instanceof CloudDimmingController_1.default) {
  156. _b = tmp.params, bright = _b.bright, status_1 = _b.switch;
  157. device.updateState({
  158. status: status_1,
  159. bright: bright,
  160. });
  161. }
  162. else if (device instanceof CloudPowerDetectionSwitchController_1.default) {
  163. _c = tmp.params, current = _c.current, voltage = _c.voltage, power = _c.power, status_2 = _c.switch;
  164. logger_1.logger.info("Get power detection switch message, params: ".concat(JSON.stringify(tmp.params)));
  165. device.updateState({
  166. status: status_2,
  167. current: current,
  168. voltage: voltage,
  169. power: power,
  170. });
  171. }
  172. else if (device instanceof CloudMultiChannelSwitchController_1.default) {
  173. switches = tmp.params.switches;
  174. if (Array.isArray(switches)) {
  175. device.updateState(switches);
  176. }
  177. }
  178. else if (device instanceof CloudZigbeeMultiSwitchController_1.default) {
  179. switches = tmp.params.switches;
  180. if (Array.isArray(switches)) {
  181. device.updateState(switches);
  182. }
  183. }
  184. else if (device instanceof CloudRGBLightStripController_1.default) {
  185. logger_1.logger.info("Get lamp strip message, params: ".concat(JSON.stringify(tmp.params)));
  186. device.updateState(device.parseCkData2Ha(tmp.params));
  187. }
  188. else if (device instanceof CloudDoubleColorBulbController_1.default) {
  189. logger_1.logger.info("Get double color bulb message, params: ".concat(JSON.stringify(tmp.params)));
  190. device.updateState(tmp.params);
  191. }
  192. else if (device instanceof CloudUIID104Controller_1.default) {
  193. logger_1.logger.info("Get UIID 104 message, params: ".concat(tmp.params));
  194. device.updateState(tmp.params);
  195. }
  196. else if (device instanceof CloudDualR3Controller_1.default || device instanceof LanDualR3Controller_1.default) {
  197. logger_1.logger.info("Get DualR3 message, params: ".concat(tmp.params));
  198. if (tmp.params && tmp.params.switches) {
  199. device.updateState(tmp.params.switches);
  200. }
  201. }
  202. else if (device instanceof CloudDW2WiFiController_1.default) {
  203. logger_1.logger.info("Get DW2 message, params: ".concat(tmp.params));
  204. if (tmp.params) {
  205. device.updateState(tmp.params);
  206. }
  207. }
  208. else if (device instanceof CloudZigbeeUIID1000Controller_1.default) {
  209. logger_1.logger.info("Get Zigbee UIID 1000 message, params: ".concat(tmp.params));
  210. if (tmp.params) {
  211. device.updateState(tmp.params);
  212. }
  213. }
  214. else if (device instanceof CloudZigbeeUIID1770Controller_1.default) {
  215. logger_1.logger.info("Get Zigbee UIID 1770 message, params: ".concat(tmp.params));
  216. if (tmp.params) {
  217. device.updateState(tmp.params);
  218. }
  219. }
  220. else if (device instanceof CloudZigbeeUIID2026Controller_1.default) {
  221. logger_1.logger.info("Get Zigbee UIID 2026 message, params: ".concat(tmp.params));
  222. if (tmp.params) {
  223. device.updateState(tmp.params);
  224. }
  225. }
  226. else if (device instanceof CloudZigbeeUIID3026Controller_1.default) {
  227. logger_1.logger.info("Get Zigbee UIID 3026 message, params: ".concat(tmp.params));
  228. if (tmp.params) {
  229. device.updateState(tmp.params);
  230. }
  231. }
  232. else if (device instanceof CloudZigbeeUIID4026Controller_1.default) {
  233. logger_1.logger.info("Get Zigbee UIID 4026 message, params: ".concat(tmp.params));
  234. if (tmp.params) {
  235. device.updateState(tmp.params);
  236. }
  237. }
  238. else if (device instanceof CloudCoverController_1.default) {
  239. logger_1.logger.info("Get cover message, params: ".concat(tmp.params));
  240. if (tmp.params) {
  241. device.updateState(tmp.params);
  242. }
  243. }
  244. else if (device instanceof CloudRFBridgeController_1.default) {
  245. logger_1.logger.info("Get RF-Bridge message, params: ".concat(tmp.params));
  246. ids = device.parseCkData2Ha(tmp.params);
  247. device.updateState(ids);
  248. }
  249. else if (device instanceof CloudUIID34Controller_1.default) {
  250. logger_1.logger.info("Get UIID 34 message, params: ".concat(tmp.params));
  251. device.updateState(tmp.params.switches);
  252. }
  253. else if (device instanceof CloudUIID44Controller_1.default) {
  254. logger_1.logger.info("Get UIID 44 message, params: ".concat(tmp.params));
  255. device.updateState(tmp.params);
  256. }
  257. else if (device instanceof CloudZigbeeDoubleColorBulbController_1.default || device instanceof CloudZigbeeFiveColorBulbController_1.default) {
  258. logger_1.logger.info("Get UIID 1258 message, params: ".concat(JSON.stringify(tmp.params)));
  259. device.updateState(tmp.params);
  260. }
  261. else if (device instanceof CloudUIID181Controller_1.default) {
  262. device.updateState(tmp.params.switch);
  263. }
  264. else if (device instanceof CloudUIID190Controller_1.default) {
  265. logger_1.logger.info("Get UIID 190 message, params: ".concat(JSON.stringify(tmp.params)));
  266. switches = tmp.params.switches;
  267. if (Array.isArray(switches)) {
  268. device.updateState(switches);
  269. }
  270. }
  271. else if (device instanceof CloudUIID137Controller_1.default) {
  272. logger_1.logger.info("Get UIID 137 message, params: ".concat(JSON.stringify(tmp.params)));
  273. device.updateState(tmp.params);
  274. }
  275. else if (device instanceof CloudUIID173Controller_1.default) {
  276. logger_1.logger.info("Get UIID 173 message, params: ".concat(JSON.stringify(tmp.params)));
  277. device.updateState(tmp.params);
  278. }
  279. else if (device instanceof CloudUIID130Controller_1.default) {
  280. logger_1.logger.info("Get UIID 130 message, params: ".concat(JSON.stringify(tmp.params)));
  281. switches = tmp.params.switches;
  282. if (Array.isArray(switches)) {
  283. device.updateState(switches);
  284. }
  285. }
  286. else if (device instanceof CloudUIID182Controller_1.default) {
  287. logger_1.logger.info("Get UIID 182 message, params: ".concat(JSON.stringify(tmp.params)));
  288. switches = tmp.params.switches;
  289. if (Array.isArray(switches)) {
  290. device.updateState(switches);
  291. }
  292. }
  293. eventBus_1.default.emit('update-controller', data);
  294. }
  295. if (tmp.action === 'sysmsg' && (device === null || device === void 0 ? void 0 : device.entityId)) {
  296. _d = tmp.params, online = _d.online, name_1 = _d.name;
  297. if (device instanceof LanDeviceController_1.default || device instanceof CloudDeviceController_1.default) {
  298. (0, mergeDeviceParams_1.default)(device, { online: online, deviceName: name_1 });
  299. }
  300. if (online === false) {
  301. updateUnavailable2Ha = function (entityId) { return __awaiter(void 0, void 0, void 0, function () {
  302. var state, entityRes;
  303. return __generator(this, function (_a) {
  304. switch (_a.label) {
  305. case 0:
  306. state = 'unavailable';
  307. return [4, (0, restApi_1.getStateByEntityId)(entityId)];
  308. case 1:
  309. entityRes = _a.sent();
  310. if (entityRes && entityRes.data) {
  311. (0, restApi_1.updateStates)(entityId, {
  312. entity_id: entityId,
  313. state: state,
  314. attributes: __assign(__assign({}, entityRes.data.attributes), { state: state })
  315. });
  316. }
  317. return [2];
  318. }
  319. });
  320. }); };
  321. switches = lodash_1.default.get(device, ['params', 'switches'], undefined);
  322. if (switches && switches.length > 0) {
  323. channelNum = (0, channelMap_1.getMaxChannelByUiid)(device.uiid);
  324. for (i = 0; i < channelNum; i++) {
  325. updateUnavailable2Ha("".concat(device.entityId, "_").concat(i + 1));
  326. }
  327. }
  328. else {
  329. updateUnavailable2Ha(device.entityId);
  330. }
  331. eventBus_1.default.emit('device-offline', device.deviceId);
  332. }
  333. }
  334. eventBus_1.default.emit('sse');
  335. }
  336. }
  337. catch (error) {
  338. logger_1.logger.error("initCkWs error: ".concat(error));
  339. }
  340. return [2];
  341. });
  342. }); });
  343. return [2];
  344. }
  345. });
  346. }); });