protocols.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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.handleCkWsUpdateMessage = exports.sendWsUpdateMsg2Ha = exports.handleHaLight = exports.handleHaSwitch = exports.getDeviceUpdateParams = exports.initDeviceParams = exports.getEntityListByType = void 0;
  18. var const_1 = require("./const");
  19. var lodash_1 = __importDefault(require("lodash"));
  20. var init_1 = require("./init");
  21. var coolkit_ws_device_1 = __importDefault(require("coolkit-ws-device"));
  22. var utils_1 = require("./utils");
  23. var logger_1 = require("../utils/logger");
  24. var WebSocket2Ha_1 = require("./WebSocket2Ha");
  25. function checkUnavailableState(state) {
  26. return state === 'unavailable' ? 'off' : state;
  27. }
  28. function getEntityListByType(entities, type) {
  29. var e_1, _a;
  30. var entList = [];
  31. try {
  32. for (var entities_1 = __values(entities), entities_1_1 = entities_1.next(); !entities_1_1.done; entities_1_1 = entities_1.next()) {
  33. var ent = entities_1_1.value;
  34. if ((0, WebSocket2Ha_1.getEntityTypeById)(ent.entityId) === type) {
  35. entList.push(ent);
  36. }
  37. }
  38. }
  39. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  40. finally {
  41. try {
  42. if (entities_1_1 && !entities_1_1.done && (_a = entities_1.return)) _a.call(entities_1);
  43. }
  44. finally { if (e_1) throw e_1.error; }
  45. }
  46. return entList;
  47. }
  48. exports.getEntityListByType = getEntityListByType;
  49. function initDeviceParams(data) {
  50. var uiid = data.deviceUiid;
  51. var haDeviceData = data.haDeviceData;
  52. var params = null;
  53. if (uiid === const_1.CK_UIID_20001) {
  54. var entList = getEntityListByType(haDeviceData.entities, 'switch');
  55. params = {
  56. switch: checkUnavailableState(entList[0].entityState.state)
  57. };
  58. }
  59. else if (uiid === const_1.CK_UIID_20002) {
  60. var entList = getEntityListByType(haDeviceData.entities, 'switch');
  61. params = {
  62. switches: [
  63. {
  64. switch: checkUnavailableState(entList[0].entityState.state),
  65. outlet: 0
  66. },
  67. {
  68. switch: checkUnavailableState(entList[1].entityState.state),
  69. outlet: 1
  70. }
  71. ]
  72. };
  73. }
  74. else if (uiid === const_1.CK_UIID_20003) {
  75. var entList = getEntityListByType(haDeviceData.entities, 'switch');
  76. params = {
  77. switches: [
  78. {
  79. switch: checkUnavailableState(entList[0].entityState.state),
  80. outlet: 0
  81. },
  82. {
  83. switch: checkUnavailableState(entList[1].entityState.state),
  84. outlet: 1
  85. },
  86. {
  87. switch: checkUnavailableState(entList[2].entityState.state),
  88. outlet: 2
  89. }
  90. ]
  91. };
  92. }
  93. else if (uiid === const_1.CK_UIID_20004) {
  94. var entList = getEntityListByType(haDeviceData.entities, 'switch');
  95. params = {
  96. switches: [
  97. {
  98. switch: checkUnavailableState(entList[0].entityState.state),
  99. outlet: 0
  100. },
  101. {
  102. switch: checkUnavailableState(entList[1].entityState.state),
  103. outlet: 1
  104. },
  105. {
  106. switch: checkUnavailableState(entList[2].entityState.state),
  107. outlet: 2
  108. },
  109. {
  110. switch: checkUnavailableState(entList[3].entityState.state),
  111. outlet: 3
  112. },
  113. ]
  114. };
  115. }
  116. else if (uiid === const_1.CK_UIID_20005) {
  117. var entList = getEntityListByType(haDeviceData.entities, 'light');
  118. var entityState = entList[0].entityState;
  119. params = {
  120. switch: checkUnavailableState(entityState.state)
  121. };
  122. }
  123. else if (uiid === const_1.CK_UIID_20006) {
  124. var entList = getEntityListByType(haDeviceData.entities, 'light');
  125. var entityState = entList[0].entityState;
  126. if (entityState.state === 'on') {
  127. var brightness = entityState.attributes.brightness;
  128. params = {
  129. switch: checkUnavailableState(entityState.state),
  130. brightness: (0, utils_1.getCkBrightness)(brightness)
  131. };
  132. }
  133. else {
  134. params = {
  135. switch: entityState.state
  136. };
  137. }
  138. }
  139. else if (uiid === const_1.CK_UIID_20007) {
  140. var entList = getEntityListByType(haDeviceData.entities, 'light');
  141. var entityState = entList[0].entityState;
  142. if (entityState.state === 'on') {
  143. var _a = entityState.attributes, brightness = _a.brightness, min_mireds = _a.min_mireds, max_mireds = _a.max_mireds, color_temp = _a.color_temp;
  144. params = {
  145. switch: checkUnavailableState(entityState.state),
  146. brightness: (0, utils_1.getCkBrightness)(brightness),
  147. colorTemp: (0, utils_1.getCkColorTemp)(min_mireds, max_mireds, color_temp),
  148. };
  149. }
  150. else {
  151. params = {
  152. switch: entityState.state
  153. };
  154. }
  155. }
  156. else if (uiid === const_1.CK_UIID_20008) {
  157. var entList = getEntityListByType(haDeviceData.entities, 'light');
  158. var entityState = entList[0].entityState;
  159. if (entityState.state === 'on') {
  160. var _b = entityState.attributes, color_mode = _b.color_mode, brightness = _b.brightness, min_mireds = _b.min_mireds, max_mireds = _b.max_mireds, color_temp = _b.color_temp, hs_color = _b.hs_color;
  161. params = {
  162. switch: checkUnavailableState(entityState.state)
  163. };
  164. if (color_mode === const_1.HA_COLOR_MODE_XY) {
  165. lodash_1.default.set(params, 'colorMode', const_1.CK_COLOR_MODE_RGB);
  166. lodash_1.default.set(params, 'rgbBrightness', (0, utils_1.getCkBrightness)(brightness));
  167. lodash_1.default.set(params, 'hue', Math.round(hs_color[0]));
  168. lodash_1.default.set(params, 'saturation', Math.round(hs_color[1]));
  169. }
  170. else if (color_mode === const_1.HA_COLOR_MODE_COLOR_TEMP) {
  171. lodash_1.default.set(params, 'colorMode', const_1.CK_COLOR_MODE_CCT);
  172. lodash_1.default.set(params, 'cctBrightness', (0, utils_1.getCkBrightness)(brightness));
  173. lodash_1.default.set(params, 'colorTemp', (0, utils_1.getCkColorTemp)(min_mireds, max_mireds, color_temp));
  174. }
  175. }
  176. else {
  177. params = {
  178. switch: entityState.state
  179. };
  180. }
  181. }
  182. logger_1.logger.verbose("initDeviceParams: ".concat(JSON.stringify(params)));
  183. return params;
  184. }
  185. exports.initDeviceParams = initDeviceParams;
  186. function getDeviceUpdateParams(deviceData, oldState, newState, haOldState) {
  187. var uiid = deviceData.deviceUiid;
  188. var entityId = newState.entity_id;
  189. var params = null;
  190. if (uiid === const_1.CK_UIID_20001) {
  191. params = {
  192. switch: newState.state
  193. };
  194. }
  195. else if (uiid === const_1.CK_UIID_20002 || uiid === const_1.CK_UIID_20003 || uiid === const_1.CK_UIID_20004) {
  196. var entList = getEntityListByType(deviceData.haDeviceData.entities, 'switch');
  197. var index = lodash_1.default.findIndex(entList, { entityId: entityId });
  198. if (index > 3) {
  199. return;
  200. }
  201. params = {
  202. switches: oldState.switches
  203. };
  204. params.switches[index].switch = newState.state;
  205. }
  206. else if (uiid === const_1.CK_UIID_20005) {
  207. var entList = getEntityListByType(deviceData.haDeviceData.entities, 'light');
  208. if (entList[0].entityId !== entityId) {
  209. return;
  210. }
  211. params = {
  212. switch: newState.state
  213. };
  214. }
  215. else if (uiid === const_1.CK_UIID_20006) {
  216. var entList = getEntityListByType(deviceData.haDeviceData.entities, 'light');
  217. if (entList[0].entityId !== entityId) {
  218. return;
  219. }
  220. params = {
  221. switch: newState.state,
  222. brightness: (0, utils_1.getCkBrightness)(newState.attributes.brightness)
  223. };
  224. }
  225. else if (uiid === const_1.CK_UIID_20007) {
  226. var entList = getEntityListByType(deviceData.haDeviceData.entities, 'light');
  227. if (entList[0].entityId !== entityId) {
  228. return;
  229. }
  230. var _a = newState.attributes, brightness = _a.brightness, min_mireds = _a.min_mireds, max_mireds = _a.max_mireds, color_temp = _a.color_temp;
  231. if (newState.state === 'on') {
  232. params = {
  233. switch: newState.state
  234. };
  235. if (newState.attributes.brightness === haOldState.attributes.brightness) {
  236. params.colorTemp = (0, utils_1.getCkColorTemp)(min_mireds, max_mireds, color_temp);
  237. }
  238. else {
  239. params.brightness = (0, utils_1.getCkBrightness)(brightness);
  240. }
  241. }
  242. else {
  243. params = {
  244. switch: newState.state
  245. };
  246. }
  247. }
  248. else if (uiid === const_1.CK_UIID_20008) {
  249. var entList = getEntityListByType(deviceData.haDeviceData.entities, 'light');
  250. if (entList[0].entityId !== entityId) {
  251. return;
  252. }
  253. if (newState.state === 'on') {
  254. var _b = newState.attributes, color_mode = _b.color_mode, brightness = _b.brightness, min_mireds = _b.min_mireds, max_mireds = _b.max_mireds, color_temp = _b.color_temp, hs_color = _b.hs_color;
  255. params = {
  256. switch: newState.state
  257. };
  258. if (color_mode === const_1.HA_COLOR_MODE_XY) {
  259. lodash_1.default.set(params, 'colorMode', const_1.CK_COLOR_MODE_RGB);
  260. lodash_1.default.set(params, 'rgbBrightness', (0, utils_1.getCkBrightness)(brightness));
  261. lodash_1.default.set(params, 'hue', Math.round(hs_color[0]));
  262. lodash_1.default.set(params, 'saturation', Math.round(hs_color[1]));
  263. }
  264. else if (color_mode === const_1.HA_COLOR_MODE_COLOR_TEMP) {
  265. lodash_1.default.set(params, 'colorMode', const_1.CK_COLOR_MODE_CCT);
  266. lodash_1.default.set(params, 'cctBrightness', (0, utils_1.getCkBrightness)(brightness));
  267. lodash_1.default.set(params, 'colorTemp', (0, utils_1.getCkColorTemp)(min_mireds, max_mireds, color_temp));
  268. }
  269. }
  270. else {
  271. params = {
  272. switch: newState.state
  273. };
  274. }
  275. }
  276. return params;
  277. }
  278. exports.getDeviceUpdateParams = getDeviceUpdateParams;
  279. function getToggleParams(deviceData, updateParams) {
  280. var i = updateParams.switches[0].outlet;
  281. var entList = getEntityListByType(deviceData.haDeviceData.entities, 'switch');
  282. var params = {
  283. type: const_1.HA_WSMSG_TYPE_EXECUTE_SCRIPT,
  284. sequence: [
  285. {
  286. service: updateParams.switches[0].switch === 'on' ? const_1.HA_SERVICE_SWITCH_TURN_ON : const_1.HA_SERVICE_SWITCH_TURN_OFF,
  287. target: {
  288. entity_id: entList[i].entityId
  289. }
  290. }
  291. ]
  292. };
  293. return params;
  294. }
  295. function getToggleAllParams(deviceData, updateParams, uiid) {
  296. var entList = getEntityListByType(deviceData.haDeviceData.entities, 'switch');
  297. var n = 0;
  298. if (uiid === const_1.CK_UIID_20002) {
  299. n = 2;
  300. }
  301. else if (uiid === const_1.CK_UIID_20003) {
  302. n = 3;
  303. }
  304. else {
  305. n = 4;
  306. }
  307. var params = {
  308. type: const_1.HA_WSMSG_TYPE_EXECUTE_SCRIPT,
  309. sequence: []
  310. };
  311. for (var i = 0; i < n; i++) {
  312. var x = updateParams.switches[i].outlet;
  313. params.sequence.push({
  314. service: updateParams.switches[i].switch === 'on' ? const_1.HA_SERVICE_SWITCH_TURN_ON : const_1.HA_SERVICE_SWITCH_TURN_OFF,
  315. target: {
  316. entity_id: entList[x].entityId
  317. }
  318. });
  319. }
  320. return params;
  321. }
  322. function handleHaSwitch(deviceData, updateParams) {
  323. var uiid = deviceData.deviceUiid;
  324. var params;
  325. if (uiid === const_1.CK_UIID_20001) {
  326. var entList = getEntityListByType(deviceData.haDeviceData.entities, 'switch');
  327. params = {
  328. type: const_1.HA_WSMSG_TYPE_EXECUTE_SCRIPT,
  329. sequence: [
  330. {
  331. service: updateParams.switch === 'on' ? const_1.HA_SERVICE_SWITCH_TURN_ON : const_1.HA_SERVICE_SWITCH_TURN_OFF,
  332. target: {
  333. entity_id: entList[0].entityId
  334. }
  335. }
  336. ]
  337. };
  338. }
  339. else if (updateParams.switches.length === 1) {
  340. params = getToggleParams(deviceData, updateParams);
  341. }
  342. else {
  343. params = getToggleAllParams(deviceData, updateParams, uiid);
  344. }
  345. init_1.ws2ha.sendMessage(params);
  346. }
  347. exports.handleHaSwitch = handleHaSwitch;
  348. function handleHaLight(deviceData, updateParams) {
  349. var uiid = deviceData.deviceUiid;
  350. var entList = getEntityListByType(deviceData.haDeviceData.entities, 'light');
  351. if (updateParams.switch === 'off') {
  352. var params_1 = {
  353. type: const_1.HA_WSMSG_TYPE_EXECUTE_SCRIPT,
  354. sequence: [
  355. {
  356. service: const_1.HA_SERVICE_LIGHT_TURN_OFF,
  357. target: {
  358. entity_id: entList[0].entityId
  359. },
  360. data: {}
  361. }
  362. ]
  363. };
  364. init_1.ws2ha.sendMessage(params_1);
  365. return;
  366. }
  367. var params = {
  368. type: const_1.HA_WSMSG_TYPE_EXECUTE_SCRIPT,
  369. sequence: [
  370. {
  371. service: const_1.HA_SERVICE_LIGHT_TURN_ON,
  372. target: {
  373. entity_id: entList[0].entityId
  374. },
  375. data: {}
  376. }
  377. ]
  378. };
  379. if (uiid === const_1.CK_UIID_20005) {
  380. init_1.ws2ha.sendMessage(params);
  381. }
  382. else if (uiid === const_1.CK_UIID_20006) {
  383. if (typeof updateParams.brightness === 'number') {
  384. params.sequence[0].data.brightness = (0, utils_1.getHaBrightness)(updateParams.brightness);
  385. }
  386. init_1.ws2ha.sendMessage(params);
  387. }
  388. else if (uiid === const_1.CK_UIID_20007) {
  389. if (typeof updateParams.brightness === 'number') {
  390. params.sequence[0].data.brightness = (0, utils_1.getHaBrightness)(updateParams.brightness);
  391. }
  392. if (typeof updateParams.colorTemp === 'number') {
  393. var min = entList[0].entityState.attributes.min_mireds;
  394. var max = entList[0].entityState.attributes.max_mireds;
  395. params.sequence[0].data.color_temp = (0, utils_1.getHaColorTemp)(min, max, updateParams.colorTemp);
  396. }
  397. init_1.ws2ha.sendMessage(params);
  398. }
  399. else if (uiid === const_1.CK_UIID_20008) {
  400. var cctBrightness = updateParams.cctBrightness, rgbBrightness = updateParams.rgbBrightness, hue = updateParams.hue, saturation = updateParams.saturation, colorTemp = updateParams.colorTemp;
  401. if ((typeof cctBrightness === 'number') || (typeof rgbBrightness === 'number')) {
  402. var br = cctBrightness || rgbBrightness;
  403. params.sequence[0].data.brightness = (0, utils_1.getHaBrightness)(br);
  404. }
  405. if (typeof colorTemp === 'number') {
  406. var min = entList[0].entityState.attributes.min_mireds;
  407. var max = entList[0].entityState.attributes.max_mireds;
  408. params.sequence[0].data.color_temp = (0, utils_1.getHaColorTemp)(min, max, colorTemp);
  409. }
  410. if ((typeof hue === 'number') && (typeof saturation === 'number')) {
  411. params.sequence[0].data.hs_color = [hue, saturation];
  412. }
  413. init_1.ws2ha.sendMessage(params);
  414. }
  415. }
  416. exports.handleHaLight = handleHaLight;
  417. function sendWsUpdateMsg2Ha(deviceData, updateParams) {
  418. var uiid = deviceData.deviceUiid;
  419. if (!init_1.ws2ha.connected) {
  420. return;
  421. }
  422. if (uiid === const_1.CK_UIID_20001
  423. || uiid === const_1.CK_UIID_20002
  424. || uiid === const_1.CK_UIID_20003
  425. || uiid === const_1.CK_UIID_20004) {
  426. handleHaSwitch(deviceData, updateParams);
  427. }
  428. else if (uiid === const_1.CK_UIID_20005
  429. || uiid === const_1.CK_UIID_20006
  430. || uiid === const_1.CK_UIID_20007
  431. || uiid === const_1.CK_UIID_20008) {
  432. handleHaLight(deviceData, updateParams);
  433. }
  434. }
  435. exports.sendWsUpdateMsg2Ha = sendWsUpdateMsg2Ha;
  436. function handleCkWsUpdateMessage(msg) {
  437. logger_1.logger.verbose("Get CK-WS update message: ".concat(JSON.stringify(msg)));
  438. if (msg.apikey !== init_1.curUserGwData.userApiKey) {
  439. return -1;
  440. }
  441. var syncDeviceData = init_1.curUserGwData.syncDeviceData;
  442. var deviceid = msg.deviceid;
  443. var deviceData = null;
  444. for (var i = 0; i < syncDeviceData.length; i++) {
  445. if (syncDeviceData[i].syncState
  446. && syncDeviceData[i].ckDeviceData
  447. && syncDeviceData[i].ckDeviceData.deviceid === deviceid) {
  448. deviceData = syncDeviceData[i];
  449. break;
  450. }
  451. }
  452. if (!deviceData) {
  453. return -1;
  454. }
  455. if (lodash_1.default.get(init_1.ws2ckRes, 'error') !== 0) {
  456. return -1;
  457. }
  458. coolkit_ws_device_1.default.sendMessage(JSON.stringify({
  459. error: 0,
  460. sequence: msg.sequence,
  461. apikey: msg.apikey,
  462. deviceid: deviceid
  463. }));
  464. sendWsUpdateMsg2Ha(deviceData, msg.params);
  465. }
  466. exports.handleCkWsUpdateMessage = handleCkWsUpdateMessage;