WebSocket2Ha.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. exports.WebSocket2Ha = exports.getEntityTypeById = void 0;
  54. var ws_1 = __importDefault(require("ws"));
  55. var lodash_1 = __importDefault(require("lodash"));
  56. var init_1 = require("./init");
  57. var protocols_1 = require("./protocols");
  58. var coolkit_api_device_1 = __importDefault(require("coolkit-api-device"));
  59. var coolkit_ws_device_1 = __importDefault(require("coolkit-ws-device"));
  60. var process_1 = __importDefault(require("process"));
  61. var logger_1 = require("../utils/logger");
  62. var ENTITY_TYPE_ALLOW_LIST = ['switch', 'light'];
  63. function allowedEntityType(entityType) {
  64. return ENTITY_TYPE_ALLOW_LIST.includes(entityType);
  65. }
  66. function getEntityTypeById(entityId) {
  67. return entityId.split('.')[0];
  68. }
  69. exports.getEntityTypeById = getEntityTypeById;
  70. function entityStateFilter(states) {
  71. var result = [];
  72. for (var i = 0; i < states.length; i++) {
  73. var entityType = getEntityTypeById(states[i]['entity_id']);
  74. if (allowedEntityType(entityType)) {
  75. result.push(states[i]);
  76. }
  77. }
  78. return result;
  79. }
  80. function entityFilter(list, states) {
  81. var result = [];
  82. for (var i = 0; i < list.length; i++) {
  83. var entityType = getEntityTypeById(list[i]['entity_id']);
  84. var deviceId = list[i]['device_id'];
  85. var entityState = lodash_1.default.find(states, { 'entity_id': list[i]['entity_id'] });
  86. if (allowedEntityType(entityType) && deviceId) {
  87. result.push(list[i]);
  88. }
  89. }
  90. return result;
  91. }
  92. function getHaDeviceDataById(deviceList, id) {
  93. for (var i = 0; i < deviceList.length; i++) {
  94. for (var j = 0; j < deviceList[i].haDeviceData.entities.length; j++) {
  95. if (deviceList[i].haDeviceData.entities[j].entityId === id) {
  96. return deviceList[i];
  97. }
  98. }
  99. }
  100. return null;
  101. }
  102. var WebSocket2Ha = (function () {
  103. function WebSocket2Ha() {
  104. this.wsUrl = '';
  105. this.connected = false;
  106. this.cmdId = 1;
  107. this.wsUrl = process_1.default.env.DEBUG_MODE ? process_1.default.env.HASS_WS_URL : (process_1.default.env.HA_URL ? process_1.default.env.HA_URL + '/api/websocket' : 'http://supervisor/core/websocket');
  108. this.connect();
  109. }
  110. WebSocket2Ha.prototype.connect = function () {
  111. var ws = new ws_1.default(this.wsUrl);
  112. this.ws = ws;
  113. ws.on('open', this.handleOpen.bind(this));
  114. ws.on('close', this.handleClose.bind(this));
  115. ws.on('error', this.handleError.bind(this));
  116. ws.on('message', this.handleMessage.bind(this));
  117. };
  118. WebSocket2Ha.prototype.handleOpen = function () {
  119. logger_1.logger.info('WebSocket2Ha connecting...');
  120. };
  121. WebSocket2Ha.prototype.handleClose = function () {
  122. logger_1.logger.info('WebSocket2Ha will close');
  123. this.ws.removeEventListener('open');
  124. this.ws.removeEventListener('close');
  125. this.ws.removeEventListener('error');
  126. this.ws.removeEventListener('message');
  127. };
  128. WebSocket2Ha.prototype.handleError = function (err) {
  129. logger_1.logger.error("WebSocket2Ha error: ".concat(err));
  130. };
  131. WebSocket2Ha.prototype.handleMessage = function (msg) {
  132. try {
  133. var data = JSON.parse(msg);
  134. switch (data.type) {
  135. case 'auth_required':
  136. var resMsg = {
  137. type: 'auth',
  138. access_token: process_1.default.env.DEBUG_MODE ? process_1.default.env.HASS_LLAT : process_1.default.env.SUPERVISOR_TOKEN,
  139. };
  140. this.ws.send(JSON.stringify(resMsg));
  141. break;
  142. case 'auth_ok':
  143. this.connected = true;
  144. logger_1.logger.info('WebSocket2Ha connect success');
  145. this.subscribeEvents('state_changed');
  146. this.heartBeat();
  147. break;
  148. case 'result':
  149. logger_1.logger.verbose('HA WebSocket get result message');
  150. break;
  151. case 'event':
  152. if (data.event.event_type === 'state_changed') {
  153. this.handleHaStateChangedEvent(data);
  154. }
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. catch (err) {
  161. logger_1.logger.error("WebSocket2Ha error: ".concat(err));
  162. }
  163. };
  164. WebSocket2Ha.prototype.heartBeat = function () {
  165. var _this = this;
  166. setInterval(function () {
  167. _this.sendMessage({
  168. id: _this.cmdId++,
  169. type: 'ping'
  170. });
  171. }, 10000);
  172. };
  173. WebSocket2Ha.prototype.handleHaStateChangedEvent = function (e) {
  174. return __awaiter(this, void 0, void 0, function () {
  175. var entityId, gwInList, syncDeviceData, deviceList, deviceData, subDeviceListRes, subDeviceList, oldState, i, newState, haOldState, params;
  176. return __generator(this, function (_a) {
  177. switch (_a.label) {
  178. case 0:
  179. entityId = e.event.data.entity_id;
  180. if (!init_1.curUserGwData) {
  181. return [2];
  182. }
  183. gwInList = init_1.curUserGwData.gwInList, syncDeviceData = init_1.curUserGwData.syncDeviceData;
  184. if (!(gwInList && syncDeviceData && allowedEntityType(getEntityTypeById(entityId)))) return [3, 2];
  185. deviceList = syncDeviceData.filter(function (item) { return item.syncState && item.ckDeviceData; });
  186. deviceData = getHaDeviceDataById(deviceList, entityId);
  187. if (!deviceData) {
  188. return [2, -1];
  189. }
  190. return [4, coolkit_api_device_1.default.getGateWayAndSubDevice()];
  191. case 1:
  192. subDeviceListRes = _a.sent();
  193. if (subDeviceListRes.error !== 0) {
  194. return [2, -1];
  195. }
  196. subDeviceList = subDeviceListRes.data.subDevicesInfo;
  197. oldState = null;
  198. for (i = 0; i < subDeviceList.length; i++) {
  199. if (subDeviceList[i].itemData.deviceid === deviceData.ckDeviceData.deviceid) {
  200. oldState = subDeviceList[i].itemData.params;
  201. break;
  202. }
  203. }
  204. if (!oldState) {
  205. return [2, -1];
  206. }
  207. newState = e.event.data.new_state;
  208. if (newState.state === 'unavailable') {
  209. logger_1.logger.info("HA device unavailable, new state: ".concat(JSON.stringify(newState)));
  210. (0, init_1.setCkDeviceOnlineState)({
  211. uiid: deviceData.deviceUiid,
  212. subDevId: deviceData.haDeviceData.deviceId,
  213. online: false,
  214. deviceid: deviceData.ckDeviceData.deviceid
  215. });
  216. return [2];
  217. }
  218. haOldState = e.event.data.old_state;
  219. params = (0, protocols_1.getDeviceUpdateParams)(deviceData, oldState, newState, haOldState);
  220. logger_1.logger.verbose("Send message to CK, params: ".concat(JSON.stringify(params)));
  221. if (!params) {
  222. return [2, -1];
  223. }
  224. if (lodash_1.default.get(init_1.ws2ckRes, 'error') === 0) {
  225. (0, init_1.setCkDeviceOnlineState)({
  226. uiid: deviceData.deviceUiid,
  227. subDevId: deviceData.haDeviceData.deviceId,
  228. online: true,
  229. deviceid: deviceData.ckDeviceData.deviceid
  230. });
  231. coolkit_ws_device_1.default.sendMessage(JSON.stringify({
  232. action: 'update',
  233. apikey: init_1.curUserGwData.userApiKey,
  234. deviceid: deviceData.ckDeviceData.deviceid,
  235. userAgent: 'device',
  236. params: params
  237. }));
  238. }
  239. _a.label = 2;
  240. case 2: return [2];
  241. }
  242. });
  243. });
  244. };
  245. WebSocket2Ha.prototype.sendMessage = function (data) {
  246. logger_1.logger.verbose("Send message to HA Core, data: ".concat(JSON.stringify(data)));
  247. this.ws.send(JSON.stringify(__assign({ id: this.cmdId++ }, data)));
  248. };
  249. WebSocket2Ha.prototype.subscribeEvents = function (eventType) {
  250. var msg = {
  251. type: 'subscribe_events',
  252. event_type: eventType,
  253. id: this.cmdId++
  254. };
  255. this.ws.send(JSON.stringify(msg));
  256. };
  257. WebSocket2Ha.prototype.getHaData = function (type) {
  258. var _this = this;
  259. return new Promise(function (resolve) {
  260. var id = _this.cmdId++;
  261. var handler = function (data) {
  262. _this.ws.removeEventListener('message', handler);
  263. var msg = JSON.parse(data);
  264. if (msg.success) {
  265. resolve(msg.result);
  266. }
  267. else {
  268. resolve(-1);
  269. }
  270. };
  271. _this.ws.send(JSON.stringify({ id: id, type: type }));
  272. _this.ws.on('message', handler);
  273. setTimeout(function () {
  274. _this.ws.removeEventListener('message', handler);
  275. resolve(-1);
  276. }, 5000);
  277. });
  278. };
  279. WebSocket2Ha.prototype.getEntityStates = function () {
  280. return __awaiter(this, void 0, void 0, function () {
  281. return __generator(this, function (_a) {
  282. switch (_a.label) {
  283. case 0: return [4, this.getHaData('get_states')];
  284. case 1: return [2, _a.sent()];
  285. }
  286. });
  287. });
  288. };
  289. WebSocket2Ha.prototype.getConfigDeviceRegistryList = function () {
  290. return __awaiter(this, void 0, void 0, function () {
  291. return __generator(this, function (_a) {
  292. switch (_a.label) {
  293. case 0: return [4, this.getHaData('config/device_registry/list')];
  294. case 1: return [2, _a.sent()];
  295. }
  296. });
  297. });
  298. };
  299. WebSocket2Ha.prototype.getConfigEntityRegistryList = function () {
  300. return __awaiter(this, void 0, void 0, function () {
  301. return __generator(this, function (_a) {
  302. switch (_a.label) {
  303. case 0: return [4, this.getHaData('config/entity_registry/list')];
  304. case 1: return [2, _a.sent()];
  305. }
  306. });
  307. });
  308. };
  309. WebSocket2Ha.prototype.getHaDeviceEntityMap = function () {
  310. return __awaiter(this, void 0, void 0, function () {
  311. var result, entityStatesRes, entityStateList, deviceRes, deviceList, entityRes, entityList, i, device, deviceId, entityId, entityState, index;
  312. return __generator(this, function (_a) {
  313. switch (_a.label) {
  314. case 0:
  315. result = [];
  316. return [4, this.getEntityStates()];
  317. case 1:
  318. entityStatesRes = _a.sent();
  319. logger_1.logger.verbose("getHaDeviceEntityMap: entityStatesRes: ".concat(JSON.stringify(entityStatesRes)));
  320. if (entityStatesRes === -1) {
  321. return [2, result];
  322. }
  323. entityStateList = entityStateFilter(entityStatesRes);
  324. logger_1.logger.verbose("getHaDeviceEntityMap: entityStateList: ".concat(JSON.stringify(entityStateList)));
  325. return [4, this.getConfigDeviceRegistryList()];
  326. case 2:
  327. deviceRes = _a.sent();
  328. if (deviceRes === -1) {
  329. return [2, result];
  330. }
  331. deviceList = deviceRes;
  332. logger_1.logger.verbose("getHaDeviceEntityMap: deviceList: ".concat(JSON.stringify(deviceList)));
  333. return [4, this.getConfigEntityRegistryList()];
  334. case 3:
  335. entityRes = _a.sent();
  336. if (entityRes === -1) {
  337. return [2, result];
  338. }
  339. entityList = entityFilter(entityRes, entityStateList);
  340. logger_1.logger.verbose("getHaDeviceEntityMap: entityList: ".concat(JSON.stringify(entityList)));
  341. for (i = 0; i < entityList.length; i++) {
  342. device = lodash_1.default.find(deviceList, { id: entityList[i]['device_id'] });
  343. deviceId = device['id'];
  344. entityId = entityList[i]['entity_id'];
  345. entityState = lodash_1.default.find(entityStateList, { entity_id: entityId });
  346. index = lodash_1.default.findIndex(result, { deviceId: deviceId });
  347. if (index === -1) {
  348. result.push({
  349. deviceId: deviceId,
  350. deviceData: device,
  351. entities: [{ entityId: entityId, entityData: entityList[i], entityState: entityState }]
  352. });
  353. }
  354. else {
  355. result[index].entities.push({ entityId: entityId, entityData: entityList[i], entityState: entityState });
  356. }
  357. }
  358. logger_1.logger.verbose("getHaDeviceEntityMap: result: ".concat(JSON.stringify(result)));
  359. return [2, result];
  360. }
  361. });
  362. });
  363. };
  364. return WebSocket2Ha;
  365. }());
  366. exports.WebSocket2Ha = WebSocket2Ha;