utils.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.getHaBrightness = exports.getCkBrightness = exports.getHaColorTemp = exports.getCkColorTemp = exports.getCkDeviceModelIdByUiid = exports.getHaDeviceUiid = void 0;
  18. var const_1 = require("./const");
  19. var WebSocket2Ha_1 = require("./WebSocket2Ha");
  20. var process_1 = __importDefault(require("process"));
  21. var logger_1 = require("../utils/logger");
  22. function getHaDeviceUiid(data) {
  23. var e_1, _a, e_2, _b, e_3, _c;
  24. try {
  25. var type = '';
  26. try {
  27. for (var _d = __values(data.entities), _e = _d.next(); !_e.done; _e = _d.next()) {
  28. var ent = _e.value;
  29. if ((0, WebSocket2Ha_1.getEntityTypeById)(ent.entityData.entity_id) === 'light') {
  30. type = 'light';
  31. break;
  32. }
  33. else if ((0, WebSocket2Ha_1.getEntityTypeById)(ent.entityData.entity_id) === 'switch') {
  34. type = 'switch';
  35. break;
  36. }
  37. }
  38. }
  39. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  40. finally {
  41. try {
  42. if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
  43. }
  44. finally { if (e_1) throw e_1.error; }
  45. }
  46. if (type === 'light') {
  47. var lightEntity = void 0;
  48. try {
  49. for (var _f = __values(data.entities), _g = _f.next(); !_g.done; _g = _f.next()) {
  50. var ent = _g.value;
  51. if ((0, WebSocket2Ha_1.getEntityTypeById)(ent.entityData.entity_id) === 'light') {
  52. lightEntity = ent;
  53. break;
  54. }
  55. }
  56. }
  57. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  58. finally {
  59. try {
  60. if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
  61. }
  62. finally { if (e_2) throw e_2.error; }
  63. }
  64. var supportedColorMode = lightEntity === null || lightEntity === void 0 ? void 0 : lightEntity.entityState.attributes.supported_color_modes;
  65. if (supportedColorMode.includes(const_1.HA_COLOR_MODE_COLOR_TEMP)
  66. && (supportedColorMode.includes(const_1.HA_COLOR_MODE_XY) || supportedColorMode.includes(const_1.HA_COLOR_MODE_HS))) {
  67. return const_1.CK_UIID_20008;
  68. }
  69. else if (supportedColorMode.includes(const_1.HA_COLOR_MODE_COLOR_TEMP)) {
  70. return const_1.CK_UIID_20007;
  71. }
  72. else if (supportedColorMode.includes(const_1.HA_COLOR_MODE_BRIGHTNESS)) {
  73. return const_1.CK_UIID_20006;
  74. }
  75. else if (supportedColorMode.length !== 0) {
  76. return const_1.CK_UIID_20005;
  77. }
  78. else {
  79. return -1;
  80. }
  81. }
  82. else if (type === 'switch') {
  83. var switchCnt = 0;
  84. try {
  85. for (var _h = __values(data.entities), _j = _h.next(); !_j.done; _j = _h.next()) {
  86. var ent = _j.value;
  87. if ((0, WebSocket2Ha_1.getEntityTypeById)(ent.entityData.entity_id) === 'switch') {
  88. switchCnt++;
  89. }
  90. }
  91. }
  92. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  93. finally {
  94. try {
  95. if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
  96. }
  97. finally { if (e_3) throw e_3.error; }
  98. }
  99. if (switchCnt === 1) {
  100. return const_1.CK_UIID_20001;
  101. }
  102. else if (switchCnt === 2) {
  103. return const_1.CK_UIID_20002;
  104. }
  105. else if (switchCnt === 3) {
  106. return const_1.CK_UIID_20003;
  107. }
  108. else {
  109. return const_1.CK_UIID_20004;
  110. }
  111. }
  112. else {
  113. logger_1.logger.info("getHaDeviceUiid(): unsupport ha device data: ".concat(JSON.stringify(data)));
  114. return -1;
  115. }
  116. }
  117. catch (err) {
  118. console.error('getHaDeviceUiid() error', err);
  119. console.error('ha device data:', JSON.stringify(data));
  120. return -1;
  121. }
  122. }
  123. exports.getHaDeviceUiid = getHaDeviceUiid;
  124. function isCkApiTest() {
  125. return process_1.default.env.CK_API_ENV === 'test';
  126. }
  127. function getCkDeviceModelIdByUiid(uiid) {
  128. if (uiid === const_1.CK_UIID_20001) {
  129. return isCkApiTest() ? '620a2e3a2ce380e4ddb648af' : '620c592e8d85576e68376d34';
  130. }
  131. else if (uiid === const_1.CK_UIID_20002) {
  132. return isCkApiTest() ? '620a2e4f2ce380e4ddb648b0' : '620c59478d85576e68376d35';
  133. }
  134. else if (uiid === const_1.CK_UIID_20003) {
  135. return isCkApiTest() ? '620a2e672ce380e4ddb648b1' : '620c59d58d85576e68376d36';
  136. }
  137. else if (uiid === const_1.CK_UIID_20004) {
  138. return isCkApiTest() ? '620a2e922ce380e4ddb648b2' : '620c59ea8d85576e68376d37';
  139. }
  140. else if (uiid === const_1.CK_UIID_20005) {
  141. return isCkApiTest() ? '620a2ea82ce380e4ddb648b3' : '620c59fd8d85576e68376d38';
  142. }
  143. else if (uiid === const_1.CK_UIID_20006) {
  144. return isCkApiTest() ? '620a2ed42ce380e4ddb648b4' : '620c5a148d85576e68376d39';
  145. }
  146. else if (uiid === const_1.CK_UIID_20007) {
  147. return isCkApiTest() ? '620a2ee72ce380e4ddb648b5' : '620c5a598d85576e68376d3a';
  148. }
  149. else if (uiid === const_1.CK_UIID_20008) {
  150. return isCkApiTest() ? '620a2efa2ce380e4ddb648b6' : '620c5a748d85576e68376d3b';
  151. }
  152. else {
  153. return 'n/a';
  154. }
  155. }
  156. exports.getCkDeviceModelIdByUiid = getCkDeviceModelIdByUiid;
  157. function getCkColorTemp(min, max, n) {
  158. var a1 = const_1.CK_COLOR_TEMP_MIN - const_1.CK_COLOR_TEMP_MAX;
  159. var b1 = max - min;
  160. var b2 = max - n;
  161. var x = Math.round(-(a1 * b2 / b1 - const_1.CK_COLOR_TEMP_MIN));
  162. return x;
  163. }
  164. exports.getCkColorTemp = getCkColorTemp;
  165. function getHaColorTemp(min, max, n) {
  166. var a1 = const_1.CK_COLOR_TEMP_MIN - const_1.CK_COLOR_TEMP_MAX;
  167. var b1 = max - min;
  168. var a2 = const_1.CK_COLOR_TEMP_MIN - n;
  169. var x = Math.round(-(a2 * b1 / a1 - max));
  170. return x;
  171. }
  172. exports.getHaColorTemp = getHaColorTemp;
  173. function getCkBrightness(haBr) {
  174. var result = Math.round(haBr / const_1.HA_BRIGHTNESS_MAX * 100);
  175. return result;
  176. }
  177. exports.getCkBrightness = getCkBrightness;
  178. function getHaBrightness(ckBr) {
  179. var result = Math.round(ckBr / 100 * const_1.HA_BRIGHTNESS_MAX);
  180. return result;
  181. }
  182. exports.getHaBrightness = getHaBrightness;