devices.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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.updateElectricRate = exports.getDevTempHumHistory = exports.updateLanDevice = exports.setRate = exports.changeUnit = exports.removeDiyDevice = exports.updateDiyDevice = exports.upgradeDevice = exports.getOTAinfo = exports.proxy2ws = exports.updateChannelName = exports.updateDeviceName = exports.disableDevice = exports.getDeviceById = exports.getDevices = void 0;
  54. var lodash_1 = __importDefault(require("lodash"));
  55. var coolkit_ws_1 = __importDefault(require("coolkit-ws"));
  56. var eventBus_1 = __importDefault(require("../utils/eventBus"));
  57. var initMdns_1 = __importDefault(require("../utils/initMdns"));
  58. var Controller_1 = __importDefault(require("../controller/Controller"));
  59. var syncDevice2Ha_1 = __importDefault(require("../utils/syncDevice2Ha"));
  60. var dataUtil_1 = require("../utils/dataUtil");
  61. var mergeDeviceParams_1 = __importDefault(require("../utils/mergeDeviceParams"));
  62. var DiyDeviceController_1 = __importDefault(require("../controller/DiyDeviceController"));
  63. var updateDiyDeviceName_1 = __importDefault(require("../utils/updateDiyDeviceName"));
  64. var removeEntityByDevice_1 = __importDefault(require("../utils/removeEntityByDevice"));
  65. var LanSwitchController_1 = __importDefault(require("../controller/LanSwitchController"));
  66. var LanDualR3Controller_1 = __importDefault(require("../controller/LanDualR3Controller"));
  67. var LanDeviceController_1 = __importDefault(require("../controller/LanDeviceController"));
  68. var CloudDeviceController_1 = __importDefault(require("../controller/CloudDeviceController"));
  69. var CloudDualR3Controller_1 = __importDefault(require("../controller/CloudDualR3Controller"));
  70. var CloudSwitchController_1 = __importDefault(require("../controller/CloudSwitchController"));
  71. var formatDevice_1 = require("../utils/formatDevice");
  72. var ckApi_1 = require("../apis/ckApi");
  73. var LanTandHModificationController_1 = __importDefault(require("../controller/LanTandHModificationController"));
  74. var LanMultiChannelSwitchController_1 = __importDefault(require("../controller/LanMultiChannelSwitchController"));
  75. var CloudTandHModificationController_1 = __importDefault(require("../controller/CloudTandHModificationController"));
  76. var CloudMultiChannelSwitchController_1 = __importDefault(require("../controller/CloudMultiChannelSwitchController"));
  77. var modifyDeviceStatus_1 = require("../utils/modifyDeviceStatus");
  78. var CloudPowerDetectionSwitchController_1 = __importDefault(require("../controller/CloudPowerDetectionSwitchController"));
  79. var diyDeviceApi_1 = require("../apis/diyDeviceApi");
  80. var LanPowerDetectionSwitchController_1 = __importDefault(require("../controller/LanPowerDetectionSwitchController"));
  81. var LanRFBridgeController_1 = __importDefault(require("../controller/LanRFBridgeController"));
  82. var CloudRFBridgeController_1 = __importDefault(require("../controller/CloudRFBridgeController"));
  83. var CloudUIID44Controller_1 = __importDefault(require("../controller/CloudUIID44Controller"));
  84. var CloudUIID34Controller_1 = __importDefault(require("../controller/CloudUIID34Controller"));
  85. var LanUIID34Controller_1 = __importDefault(require("../controller/LanUIID34Controller"));
  86. var logger_1 = require("../utils/logger");
  87. var CloudZigbeeMultiSwitchController_1 = __importDefault(require("../controller/CloudZigbeeMultiSwitchController"));
  88. var CloudZigbeeDoubleColorBulbController_1 = __importDefault(require("../controller/CloudZigbeeDoubleColorBulbController"));
  89. var CloudZigbeeFiveColorBulbController_1 = __importDefault(require("../controller/CloudZigbeeFiveColorBulbController"));
  90. var CloudUIID181Controller_1 = __importDefault(require("../controller/CloudUIID181Controller"));
  91. var CloudUIID190Controller_1 = __importDefault(require("../controller/CloudUIID190Controller"));
  92. var getThings_1 = __importDefault(require("../utils/getThings"));
  93. var CloudUIID130Controller_1 = __importDefault(require("../controller/CloudUIID130Controller"));
  94. var CloudUIID137Controller_1 = __importDefault(require("../controller/CloudUIID137Controller"));
  95. var CloudUIID173Controller_1 = __importDefault(require("../controller/CloudUIID173Controller"));
  96. var CloudUIID182Controller_1 = __importDefault(require("../controller/CloudUIID182Controller"));
  97. var mdns = (0, initMdns_1.default)();
  98. var getDevTempHumHistory = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  99. var _a, deviceid, last, format, data, err_1;
  100. return __generator(this, function (_b) {
  101. switch (_b.label) {
  102. case 0:
  103. _a = req.body, deviceid = _a.deviceid, last = _a.last, format = _a.format;
  104. _b.label = 1;
  105. case 1:
  106. _b.trys.push([1, 3, , 4]);
  107. return [4, (0, ckApi_1.getTempHumHistory)(deviceid, last, format)];
  108. case 2:
  109. data = _b.sent();
  110. res.json({
  111. error: 0,
  112. data: data.data
  113. });
  114. return [3, 4];
  115. case 3:
  116. err_1 = _b.sent();
  117. logger_1.logger.error("getDevTempHumHistory error", err_1);
  118. res.json({
  119. error: 500,
  120. data: null
  121. });
  122. return [3, 4];
  123. case 4: return [2];
  124. }
  125. });
  126. }); };
  127. exports.getDevTempHumHistory = getDevTempHumHistory;
  128. var getDevices = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  129. var type, refresh, data, err_2;
  130. return __generator(this, function (_a) {
  131. switch (_a.label) {
  132. case 0:
  133. _a.trys.push([0, 4, , 5]);
  134. type = req.query.type;
  135. refresh = req.body.refresh;
  136. if (type === undefined) {
  137. res.json({
  138. error: 401,
  139. data: null,
  140. });
  141. }
  142. if (!refresh) return [3, 2];
  143. return [4, (0, syncDevice2Ha_1.default)({
  144. syncLovelace: false,
  145. sleepTime: 2000,
  146. })];
  147. case 1:
  148. _a.sent();
  149. _a.label = 2;
  150. case 2: return [4, (0, getThings_1.default)()];
  151. case 3:
  152. _a.sent();
  153. data = (0, formatDevice_1.getFormattedDeviceList)();
  154. res.json({
  155. error: 0,
  156. data: data,
  157. });
  158. return [3, 5];
  159. case 4:
  160. err_2 = _a.sent();
  161. logger_1.logger.error("getDevices error: ".concat(err_2));
  162. res.json({
  163. error: 500,
  164. data: null,
  165. });
  166. return [3, 5];
  167. case 5: return [2];
  168. }
  169. });
  170. }); };
  171. exports.getDevices = getDevices;
  172. var getDeviceById = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  173. var id, device;
  174. return __generator(this, function (_a) {
  175. try {
  176. id = req.query.id;
  177. device = Controller_1.default.getDevice(id);
  178. if (!device) {
  179. res.json({
  180. error: 402,
  181. msg: 'device not found',
  182. });
  183. }
  184. res.json({
  185. error: 0,
  186. data: (0, formatDevice_1.formatDevice)(device),
  187. });
  188. }
  189. catch (err) {
  190. logger_1.logger.error("getDeviceById error: ".concat(err));
  191. res.json({
  192. error: 500,
  193. data: null,
  194. });
  195. }
  196. return [2];
  197. });
  198. }); };
  199. exports.getDeviceById = getDeviceById;
  200. var disableDevice = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  201. var _a, disabled, id, device, error, err_3;
  202. return __generator(this, function (_b) {
  203. switch (_b.label) {
  204. case 0:
  205. _b.trys.push([0, 4, , 5]);
  206. _a = req.body, disabled = _a.disabled, id = _a.id;
  207. device = Controller_1.default.getDevice(id);
  208. if (!device) {
  209. res.json({
  210. error: 402,
  211. msg: 'not such device',
  212. });
  213. }
  214. device.disabled = disabled;
  215. return [4, (0, modifyDeviceStatus_1.modifyDeviceStatus)(id, disabled)];
  216. case 1:
  217. error = _b.sent();
  218. if (device && disabled) {
  219. (0, removeEntityByDevice_1.default)(device);
  220. }
  221. if (!!disabled) return [3, 3];
  222. return [4, (0, syncDevice2Ha_1.default)({
  223. syncLovelace: false,
  224. sleepTime: 2000,
  225. })];
  226. case 2:
  227. _b.sent();
  228. _b.label = 3;
  229. case 3:
  230. if (error === 0) {
  231. res.json({
  232. error: 0,
  233. data: null,
  234. });
  235. eventBus_1.default.emit('sse');
  236. }
  237. else {
  238. res.json({
  239. error: 500,
  240. data: null,
  241. });
  242. }
  243. return [3, 5];
  244. case 4:
  245. err_3 = _b.sent();
  246. logger_1.logger.error("disableDevice error: ".concat(err_3));
  247. res.json({
  248. error: 500,
  249. data: null,
  250. });
  251. return [3, 5];
  252. case 5: return [2];
  253. }
  254. });
  255. }); };
  256. exports.disableDevice = disableDevice;
  257. var updateDeviceName = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  258. var _a, newName, id, device, error, err_4;
  259. return __generator(this, function (_b) {
  260. switch (_b.label) {
  261. case 0:
  262. _b.trys.push([0, 4, , 5]);
  263. _a = req.body, newName = _a.newName, id = _a.id;
  264. device = Controller_1.default.getDevice(id);
  265. if (!(device instanceof CloudDeviceController_1.default || device instanceof LanDeviceController_1.default)) return [3, 2];
  266. return [4, (0, ckApi_1.updateDeviceNameAPI)(id, newName)];
  267. case 1:
  268. error = (_b.sent()).error;
  269. if (error === 0) {
  270. res.json({
  271. error: 0,
  272. data: null,
  273. });
  274. device.deviceName = newName;
  275. eventBus_1.default.emit('sse');
  276. }
  277. else {
  278. logger_1.logger.warn("updateDeviceName error, deviceid: ".concat(id, ", error: ").concat(error));
  279. res.json({
  280. error: error,
  281. data: null,
  282. });
  283. }
  284. return [3, 3];
  285. case 2:
  286. res.json({
  287. error: 402,
  288. msg: 'not such device',
  289. });
  290. _b.label = 3;
  291. case 3: return [3, 5];
  292. case 4:
  293. err_4 = _b.sent();
  294. logger_1.logger.error("updateDeviceName error: ".concat(err_4));
  295. res.json({
  296. error: 500,
  297. data: null,
  298. });
  299. return [3, 5];
  300. case 5: return [2];
  301. }
  302. });
  303. }); };
  304. exports.updateDeviceName = updateDeviceName;
  305. var updateChannelName = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  306. var _a, tags, id, ck_channel_name, device, error, error, err_5;
  307. return __generator(this, function (_b) {
  308. switch (_b.label) {
  309. case 0:
  310. _b.trys.push([0, 5, , 6]);
  311. _a = req.body, tags = _a.tags, id = _a.id;
  312. ck_channel_name = tags;
  313. device = Controller_1.default.getDevice(id);
  314. if (!(device instanceof LanMultiChannelSwitchController_1.default ||
  315. device instanceof CloudMultiChannelSwitchController_1.default ||
  316. device instanceof CloudDualR3Controller_1.default ||
  317. device instanceof LanDualR3Controller_1.default ||
  318. device instanceof CloudZigbeeMultiSwitchController_1.default ||
  319. device instanceof LanSwitchController_1.default ||
  320. device instanceof CloudUIID130Controller_1.default)) return [3, 2];
  321. ck_channel_name = __assign(__assign({}, device.channelName), ck_channel_name);
  322. return [4, (0, ckApi_1.updateChannelNameAPI)(id, {
  323. ck_channel_name: ck_channel_name,
  324. })];
  325. case 1:
  326. error = (_b.sent()).error;
  327. if (error === 0) {
  328. res.json({
  329. error: 0,
  330. data: null,
  331. });
  332. device.channelName = ck_channel_name;
  333. eventBus_1.default.emit('sse');
  334. return [2];
  335. }
  336. else {
  337. res.json({
  338. error: error,
  339. data: null,
  340. });
  341. }
  342. _b.label = 2;
  343. case 2:
  344. if (!(device instanceof LanRFBridgeController_1.default || device instanceof CloudRFBridgeController_1.default)) return [3, 4];
  345. return [4, (0, ckApi_1.updateChannelNameAPI)(id, tags)];
  346. case 3:
  347. error = (_b.sent()).error;
  348. if (error === 0) {
  349. res.json({
  350. error: 0,
  351. data: null,
  352. });
  353. device.tags = tags;
  354. eventBus_1.default.emit('sse');
  355. return [2];
  356. }
  357. else {
  358. res.json({
  359. error: error,
  360. data: null,
  361. });
  362. }
  363. _b.label = 4;
  364. case 4:
  365. res.json({
  366. error: 500,
  367. data: null,
  368. });
  369. return [3, 6];
  370. case 5:
  371. err_5 = _b.sent();
  372. logger_1.logger.error("updateChannelName error: ".concat(err_5));
  373. res.json({
  374. error: 500,
  375. data: null,
  376. });
  377. return [3, 6];
  378. case 6: return [2];
  379. }
  380. });
  381. }); };
  382. exports.updateChannelName = updateChannelName;
  383. var proxy2ws = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  384. var _a, apikey, id, params, result, error, device, err_6;
  385. return __generator(this, function (_b) {
  386. switch (_b.label) {
  387. case 0:
  388. logger_1.logger.info('start proxy2ws()');
  389. _b.label = 1;
  390. case 1:
  391. _b.trys.push([1, 3, , 4]);
  392. _a = req.body, apikey = _a.apikey, id = _a.id, params = _a.params;
  393. logger_1.logger.verbose("proxy2ws params: ".concat(JSON.stringify(params)));
  394. return [4, coolkit_ws_1.default.updateThing({
  395. ownerApikey: apikey,
  396. deviceid: id,
  397. params: params,
  398. })];
  399. case 2:
  400. result = _b.sent();
  401. logger_1.logger.verbose("proxy2ws result: ".concat(JSON.stringify(result)));
  402. error = result.error;
  403. if (error === 0) {
  404. res.json({
  405. error: 0,
  406. data: result,
  407. });
  408. device = Controller_1.default.getDevice(id);
  409. if (device instanceof CloudDeviceController_1.default || device instanceof LanDeviceController_1.default) {
  410. device.params = (0, mergeDeviceParams_1.default)(device.params, params);
  411. device.online = true;
  412. eventBus_1.default.emit('sse');
  413. }
  414. if (device instanceof CloudSwitchController_1.default ||
  415. device instanceof LanSwitchController_1.default ||
  416. device instanceof CloudTandHModificationController_1.default ||
  417. device instanceof LanTandHModificationController_1.default) {
  418. device.updateState(device.params.switch);
  419. }
  420. else if (device instanceof CloudPowerDetectionSwitchController_1.default || device instanceof LanPowerDetectionSwitchController_1.default) {
  421. device.updateState({
  422. status: device.params.switch,
  423. });
  424. }
  425. else if (device instanceof CloudMultiChannelSwitchController_1.default ||
  426. device instanceof LanMultiChannelSwitchController_1.default ||
  427. device instanceof CloudDualR3Controller_1.default ||
  428. device instanceof LanDualR3Controller_1.default ||
  429. device instanceof CloudZigbeeMultiSwitchController_1.default ||
  430. device instanceof CloudUIID130Controller_1.default ||
  431. device instanceof CloudUIID182Controller_1.default) {
  432. device.updateState(device.params.switches);
  433. }
  434. else if (device instanceof CloudUIID34Controller_1.default) {
  435. device.updateState(device.params.switches);
  436. }
  437. else if (device instanceof CloudUIID44Controller_1.default) {
  438. device.updateState(device.params);
  439. }
  440. else if (device instanceof CloudZigbeeDoubleColorBulbController_1.default || device instanceof CloudZigbeeFiveColorBulbController_1.default) {
  441. device.updateState(device.params);
  442. }
  443. else if (device instanceof CloudUIID181Controller_1.default) {
  444. device.updateState(device.params.switch);
  445. }
  446. else if (device instanceof CloudUIID190Controller_1.default) {
  447. device.updateState(device.params.switches);
  448. }
  449. else if (device instanceof CloudUIID137Controller_1.default || device instanceof CloudUIID173Controller_1.default) {
  450. device.updateState(device.params);
  451. }
  452. }
  453. else {
  454. logger_1.logger.warn("call proxy2ws failed, error: ".concat(error));
  455. res.json({
  456. error: error,
  457. data: result,
  458. });
  459. }
  460. return [3, 4];
  461. case 3:
  462. err_6 = _b.sent();
  463. logger_1.logger.error('call proxy2ws() error', err_6);
  464. res.json({
  465. error: 500,
  466. data: null,
  467. });
  468. return [3, 4];
  469. case 4: return [2];
  470. }
  471. });
  472. }); };
  473. exports.proxy2ws = proxy2ws;
  474. var getOTAinfo = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  475. var list, _a, error, data, err_7;
  476. return __generator(this, function (_b) {
  477. switch (_b.label) {
  478. case 0:
  479. _b.trys.push([0, 2, , 3]);
  480. list = req.body.list;
  481. logger_1.logger.verbose("getOTAinfo list: ".concat(JSON.stringify(list)));
  482. return [4, (0, ckApi_1.getOTAinfoAPI)(list)];
  483. case 1:
  484. _a = _b.sent(), error = _a.error, data = _a.data;
  485. if (error === 0) {
  486. res.json({
  487. error: 0,
  488. data: data,
  489. });
  490. }
  491. else {
  492. res.json({
  493. error: error,
  494. data: null,
  495. });
  496. }
  497. return [3, 3];
  498. case 2:
  499. err_7 = _b.sent();
  500. res.json({
  501. error: 500,
  502. data: null,
  503. });
  504. return [3, 3];
  505. case 3: return [2];
  506. }
  507. });
  508. }); };
  509. exports.getOTAinfo = getOTAinfo;
  510. var upgradeDevice = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  511. var _a, apikey, id, params, result, error, err_8;
  512. return __generator(this, function (_b) {
  513. switch (_b.label) {
  514. case 0:
  515. _b.trys.push([0, 2, , 3]);
  516. _a = req.body, apikey = _a.apikey, id = _a.id, params = _a.params;
  517. return [4, coolkit_ws_1.default.upgradeThing({
  518. ownerApikey: apikey,
  519. deviceid: id,
  520. params: params,
  521. })];
  522. case 1:
  523. result = _b.sent();
  524. logger_1.logger.verbose("upgradeDevice result: ".concat(JSON.stringify(result)));
  525. error = result.error;
  526. if (error === 0) {
  527. res.json({
  528. error: 0,
  529. data: null,
  530. });
  531. }
  532. else {
  533. res.json({
  534. error: error,
  535. data: null,
  536. });
  537. }
  538. return [3, 3];
  539. case 2:
  540. err_8 = _b.sent();
  541. res.json({
  542. error: 500,
  543. data: null,
  544. });
  545. return [3, 3];
  546. case 3: return [2];
  547. }
  548. });
  549. }); };
  550. exports.upgradeDevice = upgradeDevice;
  551. var updateDiyDevice = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  552. var _a, type, id, params, device, result, reqParams, err_9;
  553. return __generator(this, function (_b) {
  554. switch (_b.label) {
  555. case 0:
  556. _a = req.body, type = _a.type, id = _a.id, params = _a.params;
  557. _b.label = 1;
  558. case 1:
  559. _b.trys.push([1, 13, , 14]);
  560. device = Controller_1.default.getDevice(id);
  561. if (!(device instanceof DiyDeviceController_1.default)) return [3, 12];
  562. result = void 0;
  563. reqParams = __assign({ deviceid: id, ip: device.ip, port: device.port }, params);
  564. logger_1.logger.verbose("updateDiyDevice reqParams: ".concat(JSON.stringify(reqParams)));
  565. if (!(type === 'switch')) return [3, 3];
  566. return [4, (0, diyDeviceApi_1.updateDiySwitchAPI)(reqParams)];
  567. case 2:
  568. result = _b.sent();
  569. return [3, 11];
  570. case 3:
  571. if (!(type === 'startup')) return [3, 5];
  572. return [4, (0, diyDeviceApi_1.updateDiyStartupAPI)(reqParams)];
  573. case 4:
  574. result = _b.sent();
  575. return [3, 11];
  576. case 5:
  577. if (!(type === 'pulse')) return [3, 7];
  578. return [4, (0, diyDeviceApi_1.updateDiyPulseAPI)(reqParams)];
  579. case 6:
  580. result = _b.sent();
  581. return [3, 11];
  582. case 7:
  583. if (!(type === 'sledOnline')) return [3, 9];
  584. return [4, (0, diyDeviceApi_1.updateDiySledOnlineAPI)(reqParams)];
  585. case 8:
  586. result = _b.sent();
  587. return [3, 11];
  588. case 9:
  589. if (!(type === 'deviceName')) return [3, 11];
  590. return [4, (0, updateDiyDeviceName_1.default)(id, params)];
  591. case 10:
  592. result = _b.sent();
  593. _b.label = 11;
  594. case 11:
  595. logger_1.logger.verbose("updateDiyDevice result: ".concat(JSON.stringify(result)));
  596. if (result && result.error === 0) {
  597. res.json({
  598. error: 0,
  599. data: null,
  600. });
  601. }
  602. else {
  603. res.json({
  604. error: result.error,
  605. data: null,
  606. });
  607. }
  608. _b.label = 12;
  609. case 12: return [3, 14];
  610. case 13:
  611. err_9 = _b.sent();
  612. res.json({
  613. error: 500,
  614. data: null,
  615. });
  616. return [3, 14];
  617. case 14: return [2];
  618. }
  619. });
  620. }); };
  621. exports.updateDiyDevice = updateDiyDevice;
  622. var updateLanDevice = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  623. var _a, id, params, device, result, _b, _c, err_10;
  624. return __generator(this, function (_d) {
  625. switch (_d.label) {
  626. case 0:
  627. _a = req.body, id = _a.id, params = _a.params;
  628. _d.label = 1;
  629. case 1:
  630. _d.trys.push([1, 19, , 20]);
  631. device = Controller_1.default.getDevice(id);
  632. if (!(device instanceof LanDeviceController_1.default)) return [3, 17];
  633. result = void 0;
  634. if (!(device instanceof LanSwitchController_1.default)) return [3, 3];
  635. return [4, device.setSwitch(params.switch)];
  636. case 2:
  637. result = _d.sent();
  638. _d.label = 3;
  639. case 3:
  640. if (!(device instanceof LanMultiChannelSwitchController_1.default || device instanceof LanDualR3Controller_1.default)) return [3, 5];
  641. return [4, device.setSwitch(params.switches)];
  642. case 4:
  643. result = _d.sent();
  644. _d.label = 5;
  645. case 5:
  646. if (!(device instanceof LanTandHModificationController_1.default)) return [3, 7];
  647. return [4, device.setSwitch(params.switch)];
  648. case 6:
  649. result = _d.sent();
  650. _d.label = 7;
  651. case 7:
  652. if (!(device instanceof LanPowerDetectionSwitchController_1.default)) return [3, 9];
  653. return [4, device.setSwitch(params.switch)];
  654. case 8:
  655. result = _d.sent();
  656. _d.label = 9;
  657. case 9:
  658. if (!(device instanceof LanRFBridgeController_1.default)) return [3, 11];
  659. return [4, device.transmitRfChl(params)];
  660. case 10:
  661. result = _d.sent();
  662. _d.label = 11;
  663. case 11:
  664. if (!(device instanceof LanUIID34Controller_1.default)) return [3, 16];
  665. _b = params.fan;
  666. if (!_b) return [3, 13];
  667. return [4, device.setFan(params)];
  668. case 12:
  669. _b = (result = _d.sent());
  670. _d.label = 13;
  671. case 13:
  672. _b;
  673. _c = params.light;
  674. if (!_c) return [3, 15];
  675. return [4, device.toggleLight(params)];
  676. case 14:
  677. _c = (result = _d.sent());
  678. _d.label = 15;
  679. case 15:
  680. _c;
  681. _d.label = 16;
  682. case 16:
  683. if (result === 0) {
  684. res.json({
  685. error: 0,
  686. data: null,
  687. });
  688. }
  689. else {
  690. res.json({
  691. error: 500,
  692. data: null,
  693. });
  694. }
  695. return [3, 18];
  696. case 17:
  697. res.json({
  698. error: 402,
  699. msg: 'not such device',
  700. data: null,
  701. });
  702. _d.label = 18;
  703. case 18: return [3, 20];
  704. case 19:
  705. err_10 = _d.sent();
  706. res.json({
  707. error: 500,
  708. data: null,
  709. });
  710. return [3, 20];
  711. case 20: return [2];
  712. }
  713. });
  714. }); };
  715. exports.updateLanDevice = updateLanDevice;
  716. var removeDiyDevice = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  717. var id, diyDevices, code, err_11;
  718. return __generator(this, function (_a) {
  719. switch (_a.label) {
  720. case 0:
  721. _a.trys.push([0, 2, , 3]);
  722. id = req.body.id;
  723. diyDevices = (0, dataUtil_1.getDataSync)('diy.json', []);
  724. return [4, (0, dataUtil_1.saveData)('diy.json', JSON.stringify(lodash_1.default.omit(diyDevices, [id])))];
  725. case 1:
  726. code = _a.sent();
  727. if (code) {
  728. res.json({
  729. error: 0,
  730. data: null,
  731. });
  732. }
  733. else {
  734. res.json({
  735. error: 500,
  736. data: null,
  737. });
  738. }
  739. return [3, 3];
  740. case 2:
  741. err_11 = _a.sent();
  742. res.json({
  743. error: 500,
  744. data: null,
  745. });
  746. return [3, 3];
  747. case 3: return [2];
  748. }
  749. });
  750. }); };
  751. exports.removeDiyDevice = removeDiyDevice;
  752. var changeUnit = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  753. var _a, id, unit, device, code, err_12;
  754. return __generator(this, function (_b) {
  755. switch (_b.label) {
  756. case 0:
  757. _b.trys.push([0, 4, , 5]);
  758. _a = req.body, id = _a.id, unit = _a.unit;
  759. device = Controller_1.default.getDevice(id);
  760. if (!(device instanceof CloudTandHModificationController_1.default || device instanceof LanTandHModificationController_1.default)) return [3, 2];
  761. return [4, (0, modifyDeviceStatus_1.changeDeviceUnit)(id, unit)];
  762. case 1:
  763. code = _b.sent();
  764. if (code === 0) {
  765. res.json({
  766. error: 0,
  767. data: null,
  768. });
  769. device.unit = unit;
  770. }
  771. else {
  772. res.json({
  773. error: 500,
  774. data: null,
  775. });
  776. }
  777. eventBus_1.default.emit('sse');
  778. return [3, 3];
  779. case 2:
  780. res.json({
  781. error: 402,
  782. data: null,
  783. msg: 'not such device',
  784. });
  785. _b.label = 3;
  786. case 3: return [3, 5];
  787. case 4:
  788. err_12 = _b.sent();
  789. res.json({
  790. error: 500,
  791. data: null,
  792. });
  793. return [3, 5];
  794. case 5: return [2];
  795. }
  796. });
  797. }); };
  798. exports.changeUnit = changeUnit;
  799. var setRate = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  800. var _a, id, rate, device, code, err_13;
  801. return __generator(this, function (_b) {
  802. switch (_b.label) {
  803. case 0:
  804. _b.trys.push([0, 4, , 5]);
  805. _a = req.body, id = _a.id, rate = _a.rate;
  806. device = Controller_1.default.getDevice(id);
  807. if (!(device instanceof CloudPowerDetectionSwitchController_1.default ||
  808. device instanceof CloudDualR3Controller_1.default ||
  809. device instanceof LanPowerDetectionSwitchController_1.default ||
  810. device instanceof LanDualR3Controller_1.default)) return [3, 2];
  811. return [4, (0, modifyDeviceStatus_1.setDeviceRate)(id, rate)];
  812. case 1:
  813. code = _b.sent();
  814. if (code === 0) {
  815. res.json({
  816. error: 0,
  817. data: null,
  818. });
  819. device.rate = rate;
  820. }
  821. else {
  822. res.json({
  823. error: 500,
  824. data: null,
  825. });
  826. }
  827. eventBus_1.default.emit('sse');
  828. return [3, 3];
  829. case 2:
  830. res.json({
  831. error: 402,
  832. data: null,
  833. msg: 'not such device',
  834. });
  835. _b.label = 3;
  836. case 3: return [3, 5];
  837. case 4:
  838. err_13 = _b.sent();
  839. res.json({
  840. error: 500,
  841. data: null,
  842. });
  843. return [3, 5];
  844. case 5: return [2];
  845. }
  846. });
  847. }); };
  848. exports.setRate = setRate;
  849. var updateElectricRate = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
  850. var _a, id, tags, resp, error_1;
  851. return __generator(this, function (_b) {
  852. switch (_b.label) {
  853. case 0:
  854. _b.trys.push([0, 2, , 3]);
  855. _a = req.body, id = _a.id, tags = _a.tags;
  856. return [4, (0, ckApi_1.updateDeviceTags)({ id: id, tags: tags })];
  857. case 1:
  858. resp = _b.sent();
  859. if (resp.error === 0) {
  860. res.json({
  861. error: 0,
  862. data: null
  863. });
  864. }
  865. else {
  866. res.json({
  867. error: 400,
  868. data: null
  869. });
  870. }
  871. return [3, 3];
  872. case 2:
  873. error_1 = _b.sent();
  874. res.json({
  875. error: 500,
  876. data: null
  877. });
  878. return [3, 3];
  879. case 3: return [2];
  880. }
  881. });
  882. }); };
  883. exports.updateElectricRate = updateElectricRate;