lanDeviceApi.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.setFanAPI = exports.toggleLanLightAPI = exports.transmitRfChlAPI = exports.updateLanLight = exports.getLanDeviceParams = exports.setSwitches = exports.setSwitch = void 0;
  43. var axios_1 = __importDefault(require("axios"));
  44. var coolkit_ws_1 = __importDefault(require("coolkit-ws"));
  45. var lanControlAuthenticationUtils_1 = __importDefault(require("../utils/lanControlAuthenticationUtils"));
  46. var logger_1 = require("../utils/logger");
  47. var setSwitch = function (params) { return __awaiter(void 0, void 0, void 0, function () {
  48. var ip, port, deviceid, devicekey, data, selfApikey, iv, reqData, res;
  49. return __generator(this, function (_a) {
  50. switch (_a.label) {
  51. case 0:
  52. ip = params.ip, port = params.port, deviceid = params.deviceid, devicekey = params.devicekey, data = params.data, selfApikey = params.selfApikey;
  53. iv = "abcdef".concat(Date.now(), "abcdef").slice(0, 16);
  54. reqData = {
  55. iv: lanControlAuthenticationUtils_1.default.encryptionBase64(iv),
  56. deviceid: deviceid,
  57. selfApikey: selfApikey,
  58. encrypt: true,
  59. sequence: "".concat(Date.now()),
  60. data: lanControlAuthenticationUtils_1.default.encryptionData({
  61. iv: iv,
  62. data: data,
  63. key: devicekey,
  64. }),
  65. };
  66. res = axios_1.default.post("http://".concat(ip, ":").concat(port, "/zeroconf/switch"), reqData);
  67. res.catch(function (e) { return __awaiter(void 0, void 0, void 0, function () {
  68. return __generator(this, function (_a) {
  69. switch (_a.label) {
  70. case 0:
  71. logger_1.logger.warn("Update LAN single channel switch device error, ".concat(JSON.stringify(reqData)));
  72. return [4, coolkit_ws_1.default.updateThing({
  73. deviceid: deviceid,
  74. ownerApikey: selfApikey,
  75. params: JSON.parse(data),
  76. })];
  77. case 1: return [2, _a.sent()];
  78. }
  79. });
  80. }); });
  81. return [4, res];
  82. case 1: return [2, _a.sent()];
  83. }
  84. });
  85. }); };
  86. exports.setSwitch = setSwitch;
  87. var setSwitches = function (params) { return __awaiter(void 0, void 0, void 0, function () {
  88. var ip, port, deviceid, devicekey, data, selfApikey, iv, reqData, res;
  89. return __generator(this, function (_a) {
  90. switch (_a.label) {
  91. case 0:
  92. ip = params.ip, port = params.port, deviceid = params.deviceid, devicekey = params.devicekey, data = params.data, selfApikey = params.selfApikey;
  93. iv = "abcdef".concat(Date.now(), "abcdef").slice(0, 16);
  94. reqData = {
  95. iv: lanControlAuthenticationUtils_1.default.encryptionBase64(iv),
  96. deviceid: deviceid,
  97. selfApikey: selfApikey,
  98. encrypt: true,
  99. sequence: "".concat(Date.now()),
  100. data: lanControlAuthenticationUtils_1.default.encryptionData({
  101. iv: iv,
  102. data: data,
  103. key: devicekey,
  104. }),
  105. };
  106. res = axios_1.default.post("http://".concat(ip, ":").concat(port, "/zeroconf/switches"), reqData);
  107. res.catch(function (e) { return __awaiter(void 0, void 0, void 0, function () {
  108. return __generator(this, function (_a) {
  109. switch (_a.label) {
  110. case 0:
  111. logger_1.logger.warn("Update LAN multi-switch device error, ".concat(JSON.stringify(reqData)));
  112. return [4, coolkit_ws_1.default.updateThing({
  113. deviceid: deviceid,
  114. ownerApikey: selfApikey,
  115. params: JSON.parse(data),
  116. })];
  117. case 1: return [2, _a.sent()];
  118. }
  119. });
  120. }); });
  121. return [4, res];
  122. case 1: return [2, _a.sent()];
  123. }
  124. });
  125. }); };
  126. exports.setSwitches = setSwitches;
  127. var transmitRfChlAPI = function (params) { return __awaiter(void 0, void 0, void 0, function () {
  128. var ip, port, deviceid, devicekey, data, selfApikey, iv, reqData, res;
  129. return __generator(this, function (_a) {
  130. switch (_a.label) {
  131. case 0:
  132. ip = params.ip, port = params.port, deviceid = params.deviceid, devicekey = params.devicekey, data = params.data, selfApikey = params.selfApikey;
  133. iv = "abcdef".concat(Date.now(), "abcdef").slice(0, 16);
  134. reqData = {
  135. iv: lanControlAuthenticationUtils_1.default.encryptionBase64(iv),
  136. deviceid: deviceid,
  137. selfApikey: selfApikey,
  138. encrypt: true,
  139. sequence: "".concat(Date.now()),
  140. data: lanControlAuthenticationUtils_1.default.encryptionData({
  141. iv: iv,
  142. data: data,
  143. key: devicekey,
  144. }),
  145. };
  146. res = axios_1.default.post("http://".concat(ip, ":").concat(port, "/zeroconf/transmit"), reqData);
  147. res.catch(function (e) { return __awaiter(void 0, void 0, void 0, function () {
  148. return __generator(this, function (_a) {
  149. switch (_a.label) {
  150. case 0:
  151. logger_1.logger.warn("Update LAN RF-Bridge error, ".concat(JSON.stringify(reqData)));
  152. return [4, coolkit_ws_1.default.updateThing({
  153. deviceid: deviceid,
  154. ownerApikey: selfApikey,
  155. params: JSON.parse(data),
  156. })];
  157. case 1: return [2, _a.sent()];
  158. }
  159. });
  160. }); });
  161. return [4, res];
  162. case 1: return [2, _a.sent()];
  163. }
  164. });
  165. }); };
  166. exports.transmitRfChlAPI = transmitRfChlAPI;
  167. var getLanDeviceParams = function (params) { return __awaiter(void 0, void 0, void 0, function () {
  168. var ip, port, deviceid, devicekey, selfApikey, iv, reqData, res;
  169. return __generator(this, function (_a) {
  170. switch (_a.label) {
  171. case 0:
  172. ip = params.ip, port = params.port, deviceid = params.deviceid, devicekey = params.devicekey, selfApikey = params.selfApikey;
  173. iv = "abcdef".concat(Date.now(), "abcdef").slice(0, 16);
  174. reqData = {
  175. iv: lanControlAuthenticationUtils_1.default.encryptionBase64(iv),
  176. deviceid: deviceid,
  177. selfApikey: selfApikey,
  178. encrypt: true,
  179. sequence: "".concat(Date.now()),
  180. data: lanControlAuthenticationUtils_1.default.encryptionData({
  181. iv: iv,
  182. data: JSON.stringify({}),
  183. key: devicekey,
  184. }),
  185. };
  186. res = axios_1.default.post("http://".concat(ip, ":").concat(port, "/zeroconf/info"), reqData);
  187. res.catch(function (e) { return __awaiter(void 0, void 0, void 0, function () {
  188. return __generator(this, function (_a) {
  189. logger_1.logger.warn("Get LAN device params failed, deviceid ".concat(deviceid));
  190. return [2];
  191. });
  192. }); });
  193. return [4, res];
  194. case 1: return [2, _a.sent()];
  195. }
  196. });
  197. }); };
  198. exports.getLanDeviceParams = getLanDeviceParams;
  199. var updateLanLight = function (params) { return __awaiter(void 0, void 0, void 0, function () {
  200. var ip, port, deviceid, devicekey, data, selfApikey, iv, reqData, res;
  201. return __generator(this, function (_a) {
  202. switch (_a.label) {
  203. case 0:
  204. ip = params.ip, port = params.port, deviceid = params.deviceid, devicekey = params.devicekey, data = params.data, selfApikey = params.selfApikey;
  205. iv = "abcdef".concat(Date.now(), "abcdef").slice(0, 16);
  206. reqData = {
  207. iv: lanControlAuthenticationUtils_1.default.encryptionBase64(iv),
  208. deviceid: deviceid,
  209. selfApikey: selfApikey,
  210. encrypt: true,
  211. sequence: "".concat(Date.now()),
  212. data: lanControlAuthenticationUtils_1.default.encryptionData({
  213. iv: iv,
  214. data: data,
  215. key: devicekey,
  216. }),
  217. };
  218. res = axios_1.default.post("http://".concat(ip, ":").concat(port, "/zeroconf/dimmable"), reqData);
  219. res.catch(function (e) { return __awaiter(void 0, void 0, void 0, function () {
  220. return __generator(this, function (_a) {
  221. switch (_a.label) {
  222. case 0:
  223. logger_1.logger.warn("Update LAN Lamp device error, ".concat(JSON.stringify(reqData)));
  224. return [4, coolkit_ws_1.default.updateThing({
  225. deviceid: deviceid,
  226. ownerApikey: selfApikey,
  227. params: JSON.parse(data),
  228. })];
  229. case 1: return [2, _a.sent()];
  230. }
  231. });
  232. }); });
  233. return [4, res];
  234. case 1: return [2, _a.sent()];
  235. }
  236. });
  237. }); };
  238. exports.updateLanLight = updateLanLight;
  239. var toggleLanLightAPI = function (params) { return __awaiter(void 0, void 0, void 0, function () {
  240. var ip, port, deviceid, devicekey, data, selfApikey, iv, reqData, res;
  241. return __generator(this, function (_a) {
  242. switch (_a.label) {
  243. case 0:
  244. ip = params.ip, port = params.port, deviceid = params.deviceid, devicekey = params.devicekey, data = params.data, selfApikey = params.selfApikey;
  245. iv = "abcdef".concat(Date.now(), "abcdef").slice(0, 16);
  246. reqData = {
  247. iv: lanControlAuthenticationUtils_1.default.encryptionBase64(iv),
  248. deviceid: deviceid,
  249. selfApikey: selfApikey,
  250. encrypt: true,
  251. sequence: "".concat(Date.now()),
  252. data: lanControlAuthenticationUtils_1.default.encryptionData({
  253. iv: iv,
  254. data: data,
  255. key: devicekey,
  256. }),
  257. };
  258. res = axios_1.default.post("http://".concat(ip, ":").concat(port, "/zeroconf/light"), reqData);
  259. res.catch(function (e) { return __awaiter(void 0, void 0, void 0, function () {
  260. return __generator(this, function (_a) {
  261. switch (_a.label) {
  262. case 0:
  263. logger_1.logger.warn("Update LAN Lamp device error, ".concat(JSON.stringify(reqData)));
  264. return [4, coolkit_ws_1.default.updateThing({
  265. deviceid: deviceid,
  266. ownerApikey: selfApikey,
  267. params: JSON.parse(data),
  268. })];
  269. case 1: return [2, _a.sent()];
  270. }
  271. });
  272. }); });
  273. return [4, res];
  274. case 1: return [2, _a.sent()];
  275. }
  276. });
  277. }); };
  278. exports.toggleLanLightAPI = toggleLanLightAPI;
  279. var setFanAPI = function (params) { return __awaiter(void 0, void 0, void 0, function () {
  280. var ip, port, deviceid, devicekey, data, selfApikey, iv, reqData, res;
  281. return __generator(this, function (_a) {
  282. switch (_a.label) {
  283. case 0:
  284. ip = params.ip, port = params.port, deviceid = params.deviceid, devicekey = params.devicekey, data = params.data, selfApikey = params.selfApikey;
  285. iv = "abcdef".concat(Date.now(), "abcdef").slice(0, 16);
  286. reqData = {
  287. iv: lanControlAuthenticationUtils_1.default.encryptionBase64(iv),
  288. deviceid: deviceid,
  289. selfApikey: selfApikey,
  290. encrypt: true,
  291. sequence: "".concat(Date.now()),
  292. data: lanControlAuthenticationUtils_1.default.encryptionData({
  293. iv: iv,
  294. data: data,
  295. key: devicekey,
  296. }),
  297. };
  298. res = axios_1.default.post("http://".concat(ip, ":").concat(port, "/zeroconf/fan"), reqData);
  299. res.catch(function (e) { return __awaiter(void 0, void 0, void 0, function () {
  300. return __generator(this, function (_a) {
  301. switch (_a.label) {
  302. case 0:
  303. logger_1.logger.warn("Update LAN Lamp device error, ".concat(JSON.stringify(reqData)));
  304. return [4, coolkit_ws_1.default.updateThing({
  305. deviceid: deviceid,
  306. ownerApikey: selfApikey,
  307. params: JSON.parse(data),
  308. })];
  309. case 1: return [2, _a.sent()];
  310. }
  311. });
  312. }); });
  313. return [4, res];
  314. case 1: return [2, _a.sent()];
  315. }
  316. });
  317. }); };
  318. exports.setFanAPI = setFanAPI;