api.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __generator = (this && this.__generator) || function (thisArg, body) {
  12. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  13. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  14. function verb(n) { return function (v) { return step([n, v]); }; }
  15. function step(op) {
  16. if (f) throw new TypeError("Generator is already executing.");
  17. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  18. 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;
  19. if (y = 0, t) op = [op[0] & 2, t.value];
  20. switch (op[0]) {
  21. case 0: case 1: t = op; break;
  22. case 4: _.label++; return { value: op[1], done: false };
  23. case 5: _.label++; y = op[1]; op = [0]; continue;
  24. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  25. default:
  26. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  27. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  28. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  29. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  30. if (t[2]) _.ops.pop();
  31. _.trys.pop(); continue;
  32. }
  33. op = body.call(thisArg, _);
  34. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  35. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  36. }
  37. };
  38. var __importDefault = (this && this.__importDefault) || function (mod) {
  39. return (mod && mod.__esModule) ? mod : { "default": mod };
  40. };
  41. Object.defineProperty(exports, "__esModule", { value: true });
  42. exports.syncHaDevice2Ck = exports.getHaDeviceList = exports.addHaGateway = void 0;
  43. var coolkit_api_1 = __importDefault(require("coolkit-api"));
  44. var coolkit_api_device_1 = __importDefault(require("coolkit-api-device"));
  45. var init_1 = require("./init");
  46. var app_1 = require("../config/app");
  47. var lodash_1 = __importDefault(require("lodash"));
  48. var dataUtil_1 = require("../utils/dataUtil");
  49. var utils_1 = require("./utils");
  50. var protocols_1 = require("./protocols");
  51. var coolkit_ws_device_1 = __importDefault(require("coolkit-ws-device"));
  52. var process_1 = __importDefault(require("process"));
  53. var logger_1 = require("../utils/logger");
  54. function addHaGateway(uniqueID) {
  55. return __awaiter(this, void 0, void 0, function () {
  56. return __generator(this, function (_a) {
  57. switch (_a.label) {
  58. case 0: return [4, coolkit_api_1.default.device.addThirdPartyDevice({
  59. type: 23,
  60. partnerDevice: [{
  61. uniqueID: uniqueID
  62. }]
  63. })];
  64. case 1: return [2, _a.sent()];
  65. }
  66. });
  67. });
  68. }
  69. exports.addHaGateway = addHaGateway;
  70. function regHaGateway() {
  71. return __awaiter(this, void 0, void 0, function () {
  72. var res, apikey, deviceid, region, gwData, index;
  73. return __generator(this, function (_a) {
  74. switch (_a.label) {
  75. case 0:
  76. logger_1.logger.info('start regHaGateway()');
  77. return [4, addHaGateway(init_1.curUserGwData.gwuuid)];
  78. case 1:
  79. res = _a.sent();
  80. if (!(res.error === 0)) return [3, 4];
  81. apikey = lodash_1.default.get(res.data.thingList[0], 'itemData.devicekey');
  82. deviceid = lodash_1.default.get(res.data.thingList[0], 'itemData.deviceid');
  83. region = (0, dataUtil_1.getDataSync)('user.json', ['region']);
  84. coolkit_api_device_1.default.init({
  85. apikey: apikey,
  86. appSecret: app_1.appSecret,
  87. deviceid: deviceid,
  88. region: region,
  89. useTestEnv: process_1.default.env.CK_API_ENV === 'test'
  90. });
  91. init_1.curUserGwData.gwInList = true;
  92. init_1.curUserGwData.gwApikey = apikey;
  93. init_1.curUserGwData.gwDeviceid = deviceid;
  94. return [4, (0, dataUtil_1.getGwData)()];
  95. case 2:
  96. gwData = _a.sent();
  97. index = lodash_1.default.findIndex(gwData, { userApiKey: init_1.curUserGwData.userApiKey });
  98. gwData[index] = init_1.curUserGwData;
  99. return [4, (0, dataUtil_1.setGwData)(gwData)];
  100. case 3:
  101. _a.sent();
  102. return [3, 5];
  103. case 4:
  104. logger_1.logger.warn("regHaGateway error: addHaGateway failed");
  105. return [2, -1];
  106. case 5:
  107. logger_1.logger.info('end regHaGateway()');
  108. return [2];
  109. }
  110. });
  111. });
  112. }
  113. function getHaDeviceList() {
  114. return __awaiter(this, void 0, void 0, function () {
  115. var haDeviceList, subDeviceListRes, subDeviceList, result, i, uiid, data, j, result, i, uiid, err_1;
  116. return __generator(this, function (_a) {
  117. switch (_a.label) {
  118. case 0:
  119. logger_1.logger.info('start getHaDeviceList()');
  120. _a.label = 1;
  121. case 1:
  122. _a.trys.push([1, 6, , 7]);
  123. return [4, init_1.ws2ha.getHaDeviceEntityMap()];
  124. case 2:
  125. haDeviceList = _a.sent();
  126. if (!init_1.curUserGwData.gwInList) return [3, 4];
  127. return [4, coolkit_api_device_1.default.getGateWayAndSubDevice()];
  128. case 3:
  129. subDeviceListRes = _a.sent();
  130. if (subDeviceListRes.error !== 0) {
  131. logger_1.logger.error('getGatewayAndSubDevice failed');
  132. return [2, -1];
  133. }
  134. subDeviceList = subDeviceListRes.data.subDevicesInfo;
  135. result = [];
  136. for (i = 0; i < haDeviceList.length; i++) {
  137. uiid = (0, utils_1.getHaDeviceUiid)(haDeviceList[i]);
  138. if (uiid === -1) {
  139. continue;
  140. }
  141. else {
  142. data = {
  143. deviceUiid: uiid,
  144. deviceNameHa: haDeviceList[i].deviceData.name,
  145. deviceNameCk: '',
  146. haDeviceId: haDeviceList[i].deviceId,
  147. syncState: false,
  148. haDeviceData: haDeviceList[i],
  149. ckDeviceData: null
  150. };
  151. for (j = 0; j < subDeviceList.length; j++) {
  152. if (subDeviceList[j].itemData.params.uniqueID === haDeviceList[i].deviceId) {
  153. data.deviceNameCk = subDeviceList[j].itemData.name;
  154. data.ckDeviceData = subDeviceList[j].itemData;
  155. data.syncState = true;
  156. break;
  157. }
  158. }
  159. result.push(data);
  160. }
  161. }
  162. if (result.length !== 0) {
  163. init_1.curUserGwData.syncDeviceData = result;
  164. }
  165. return [2, result];
  166. case 4:
  167. result = [];
  168. for (i = 0; i < haDeviceList.length; i++) {
  169. uiid = (0, utils_1.getHaDeviceUiid)(haDeviceList[i]);
  170. if (uiid === -1) {
  171. continue;
  172. }
  173. else {
  174. result.push({
  175. deviceUiid: uiid,
  176. deviceNameHa: haDeviceList[i].deviceData.name,
  177. deviceNameCk: '',
  178. haDeviceId: haDeviceList[i].deviceId,
  179. syncState: false,
  180. haDeviceData: haDeviceList[i],
  181. ckDeviceData: null
  182. });
  183. }
  184. }
  185. if (result.length !== 0) {
  186. init_1.curUserGwData.syncDeviceData = result;
  187. }
  188. logger_1.logger.info('end getHaDeviceList()');
  189. return [2, result];
  190. case 5: return [3, 7];
  191. case 6:
  192. err_1 = _a.sent();
  193. logger_1.logger.error("getHaDeviceList error: ".concat(err_1));
  194. return [2, -1];
  195. case 7: return [2];
  196. }
  197. });
  198. });
  199. }
  200. exports.getHaDeviceList = getHaDeviceList;
  201. function syncHaDevice2Ck(states) {
  202. return __awaiter(this, void 0, void 0, function () {
  203. var res, i, found, modelId, res, entities, online, params, err_2;
  204. return __generator(this, function (_a) {
  205. switch (_a.label) {
  206. case 0:
  207. logger_1.logger.info('start syncHaDevice2Ck()');
  208. _a.label = 1;
  209. case 1:
  210. _a.trys.push([1, 13, , 14]);
  211. if (!!init_1.curUserGwData.gwInList) return [3, 3];
  212. return [4, regHaGateway()];
  213. case 2:
  214. res = _a.sent();
  215. if (res === -1) {
  216. return [2, res];
  217. }
  218. _a.label = 3;
  219. case 3:
  220. if (!(lodash_1.default.get(init_1.ws2ckRes, 'error') !== 0)) return [3, 5];
  221. return [4, (0, init_1.initWs2Ck)({
  222. apikey: init_1.curUserGwData.gwApikey,
  223. region: (0, dataUtil_1.getDataSync)('user.json', ['region']),
  224. deviceid: init_1.curUserGwData.gwDeviceid
  225. })];
  226. case 4:
  227. _a.sent();
  228. _a.label = 5;
  229. case 5:
  230. i = 0;
  231. _a.label = 6;
  232. case 6:
  233. if (!(i < states.length)) return [3, 12];
  234. found = lodash_1.default.find(init_1.curUserGwData.syncDeviceData, { haDeviceId: states[i].haDeviceId });
  235. modelId = (0, utils_1.getCkDeviceModelIdByUiid)(states[i].deviceUiid);
  236. if (!states[i].state) return [3, 9];
  237. if (!!found.ckDeviceData) return [3, 8];
  238. return [4, coolkit_api_device_1.default.addSubDevice({
  239. type: 2,
  240. subDevices: [
  241. {
  242. name: found.haDeviceData.deviceData.name,
  243. productModelId: modelId,
  244. deviceParams: {
  245. uniqueID: found.haDeviceData.deviceId
  246. }
  247. }
  248. ]
  249. })];
  250. case 7:
  251. res = _a.sent();
  252. if (res.error !== 0) {
  253. states[i].state = false;
  254. }
  255. else {
  256. entities = found.haDeviceData.entities;
  257. online = true;
  258. if (entities.some(function (entity) { return entity.entityState.state === 'unavailable'; })) {
  259. online = false;
  260. }
  261. (0, init_1.setCkDeviceOnlineState)({
  262. subDevId: found.haDeviceData.deviceId,
  263. uiid: found.deviceUiid,
  264. deviceid: res.data.thingList[0].itemData.deviceid,
  265. online: online
  266. });
  267. params = (0, protocols_1.initDeviceParams)(found);
  268. coolkit_ws_device_1.default.sendMessage(JSON.stringify({
  269. action: 'update',
  270. apikey: init_1.curUserGwData.userApiKey,
  271. deviceid: res.data.thingList[0].itemData.deviceid,
  272. userAgent: 'device',
  273. params: params
  274. }));
  275. }
  276. return [3, 8];
  277. case 8: return [3, 11];
  278. case 9: return [4, coolkit_api_device_1.default.deleteSubDevice(found.ckDeviceData.deviceid)];
  279. case 10:
  280. _a.sent();
  281. _a.label = 11;
  282. case 11:
  283. i++;
  284. return [3, 6];
  285. case 12:
  286. logger_1.logger.info('end syncHaDevice2Ck()');
  287. return [2, states];
  288. case 13:
  289. err_2 = _a.sent();
  290. logger_1.logger.error("syncHaDevice2Ck error: ".concat(err_2));
  291. return [2, -1];
  292. case 14: return [2];
  293. }
  294. });
  295. });
  296. }
  297. exports.syncHaDevice2Ck = syncHaDevice2Ck;