CloudUIID173Controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  29. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  30. return new (P || (P = Promise))(function (resolve, reject) {
  31. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  32. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  33. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  34. step((generator = generator.apply(thisArg, _arguments || [])).next());
  35. });
  36. };
  37. var __generator = (this && this.__generator) || function (thisArg, body) {
  38. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  39. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  40. function verb(n) { return function (v) { return step([n, v]); }; }
  41. function step(op) {
  42. if (f) throw new TypeError("Generator is already executing.");
  43. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  44. 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;
  45. if (y = 0, t) op = [op[0] & 2, t.value];
  46. switch (op[0]) {
  47. case 0: case 1: t = op; break;
  48. case 4: _.label++; return { value: op[1], done: false };
  49. case 5: _.label++; y = op[1]; op = [0]; continue;
  50. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  51. default:
  52. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  53. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  54. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  55. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  56. if (t[2]) _.ops.pop();
  57. _.trys.pop(); continue;
  58. }
  59. op = body.call(thisArg, _);
  60. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  61. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  62. }
  63. };
  64. var __read = (this && this.__read) || function (o, n) {
  65. var m = typeof Symbol === "function" && o[Symbol.iterator];
  66. if (!m) return o;
  67. var i = m.call(o), r, ar = [], e;
  68. try {
  69. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  70. }
  71. catch (error) { e = { error: error }; }
  72. finally {
  73. try {
  74. if (r && !r.done && (m = i["return"])) m.call(i);
  75. }
  76. finally { if (e) throw e.error; }
  77. }
  78. return ar;
  79. };
  80. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  81. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  82. if (ar || !(i in from)) {
  83. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  84. ar[i] = from[i];
  85. }
  86. }
  87. return to.concat(ar || Array.prototype.slice.call(from));
  88. };
  89. var __values = (this && this.__values) || function(o) {
  90. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  91. if (m) return m.call(o);
  92. if (o && typeof o.length === "number") return {
  93. next: function () {
  94. if (o && i >= o.length) o = void 0;
  95. return { value: o && o[i++], done: !o };
  96. }
  97. };
  98. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  99. };
  100. var __importDefault = (this && this.__importDefault) || function (mod) {
  101. return (mod && mod.__esModule) ? mod : { "default": mod };
  102. };
  103. Object.defineProperty(exports, "__esModule", { value: true });
  104. var CloudDeviceController_1 = __importDefault(require("./CloudDeviceController"));
  105. var light_1 = require("../config/light");
  106. var coolkit_ws_1 = __importDefault(require("coolkit-ws"));
  107. var mergeDeviceParams_1 = __importDefault(require("../utils/mergeDeviceParams"));
  108. var restApi_1 = require("../apis/restApi");
  109. var CloudUIID173Controller = (function (_super) {
  110. __extends(CloudUIID173Controller, _super);
  111. function CloudUIID173Controller(params) {
  112. var _this = _super.call(this, params) || this;
  113. _this.uiid = 173;
  114. _this.effectList = __spreadArray([], __read(light_1.uiid173EffectList.keys()), false);
  115. _this.entityId = "light.".concat(params.deviceId);
  116. _this.params = params.params;
  117. _this.uiid = params.extra.uiid;
  118. return _this;
  119. }
  120. return CloudUIID173Controller;
  121. }(CloudDeviceController_1.default));
  122. CloudUIID173Controller.prototype.parseHaData2Ck = function (params) {
  123. console.log("parseHaData2Ck ~ 173 接收HA的参数", JSON.stringify(params));
  124. var state = params.state, brightness = params.brightness, brightness_pct = params.brightness_pct, effect = params.effect, color_temp_kelvin = params.color_temp_kelvin, rgb_color = params.rgb_color, color_temp = params.color_temp;
  125. var res = { switch: 'on' };
  126. if (state === 'off') {
  127. return {
  128. switch: 'off',
  129. };
  130. }
  131. brightness === 0 && (brightness = 1);
  132. brightness_pct === 0 && (brightness_pct = 1);
  133. if (!brightness && !brightness_pct && !color_temp_kelvin && !effect && !rgb_color && !color_temp) {
  134. return {
  135. switch: 'on',
  136. mode: this.params.mode,
  137. colorTemp: this.params.colorTemp,
  138. colorR: this.params.colorR,
  139. colorG: this.params.colorG,
  140. bright: this.params.bright
  141. };
  142. }
  143. if (effect) {
  144. var mode = light_1.uiid173EffectList.get(effect);
  145. res = __assign(__assign({}, res), { mode: mode });
  146. }
  147. if (brightness_pct) {
  148. res = __assign(__assign({}, res), { mode: [1, 2, 3].includes(this.params.mode) ? this.params.mode : 1, bright: brightness_pct });
  149. if (res.mode === 1) {
  150. res.colorR = this.params.colorR;
  151. res.colorG = this.params.colorG;
  152. res.colorB = this.params.colorB;
  153. }
  154. if (res.mode === 2) {
  155. res.colorTemp = this.params.colorTemp;
  156. }
  157. }
  158. if (brightness) {
  159. var br = ((brightness / 2.55) >> 0 === 0) ? 1 : (brightness / 2.55) >> 0;
  160. res = __assign(__assign({}, res), { mode: [1, 2, 3].includes(this.params.mode) ? this.params.mode : 1, bright: br });
  161. if (res.mode === 1) {
  162. res.colorR = this.params.colorR;
  163. res.colorG = this.params.colorG;
  164. res.colorB = this.params.colorB;
  165. }
  166. if (res.mode === 2) {
  167. res.colorTemp = this.params.colorTemp;
  168. }
  169. }
  170. if (rgb_color) {
  171. res = __assign(__assign({}, res), { mode: 1, colorR: rgb_color[0], colorG: rgb_color[1], colorB: rgb_color[2] });
  172. if (!res.bright) {
  173. res.bright = this.params.bright;
  174. }
  175. }
  176. if (color_temp_kelvin) {
  177. res = __assign(__assign({}, res), { mode: 2, colorTemp: Math.round((6700 - color_temp_kelvin) / 41) });
  178. }
  179. if (color_temp) {
  180. res = __assign(__assign({}, res), { mode: 2, colorTemp: color_temp });
  181. }
  182. return res;
  183. };
  184. CloudUIID173Controller.prototype.updateLight = function (params) {
  185. return __awaiter(this, void 0, void 0, function () {
  186. var res;
  187. return __generator(this, function (_a) {
  188. switch (_a.label) {
  189. case 0: return [4, coolkit_ws_1.default.updateThing({
  190. ownerApikey: this.apikey,
  191. deviceid: this.deviceId,
  192. params: params
  193. })];
  194. case 1:
  195. res = _a.sent();
  196. if (res.error === 0) {
  197. this.params = (0, mergeDeviceParams_1.default)(this.params, params);
  198. this.updateState(params);
  199. }
  200. return [2];
  201. }
  202. });
  203. });
  204. };
  205. CloudUIID173Controller.prototype.updateState = function (params) {
  206. return __awaiter(this, void 0, void 0, function () {
  207. var _a, status, mode, colorTemp, colorB, colorG, colorR, bright, state, getEffect, ckColorTemp, attributes;
  208. return __generator(this, function (_b) {
  209. if (this.disabled) {
  210. return [2];
  211. }
  212. _a = params.switch, status = _a === void 0 ? 'on' : _a, mode = params.mode, colorTemp = params.colorTemp, colorB = params.colorB, colorG = params.colorG, colorR = params.colorR, bright = params.bright;
  213. state = status;
  214. if (!this.online) {
  215. state = 'unavailable';
  216. }
  217. getEffect = function (mode) {
  218. var e_1, _a;
  219. var keys = light_1.uiid173EffectList.keys();
  220. try {
  221. for (var keys_1 = __values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) {
  222. var key = keys_1_1.value;
  223. if (light_1.uiid173EffectList.get(key) === mode) {
  224. return key;
  225. }
  226. }
  227. }
  228. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  229. finally {
  230. try {
  231. if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
  232. }
  233. finally { if (e_1) throw e_1.error; }
  234. }
  235. return '';
  236. };
  237. mode = mode || this.params.mode;
  238. ckColorTemp = (colorTemp || this.params.colorTemp);
  239. attributes = {
  240. restored: false,
  241. supported_features: 4,
  242. friendly_name: this.deviceName,
  243. supported_color_modes: ['color_temp', 'rgb'],
  244. color_mode: mode === 2 ? 'color_temp' : mode === 1 ? 'rgb' : '',
  245. state: state,
  246. min_mireds: 1,
  247. max_mireds: 100,
  248. min_color_temp_kelvin: 2600,
  249. max_color_temp_kelvin: 6700,
  250. color_temp_kelvin: 6700 - ckColorTemp * 41,
  251. color_temp: ckColorTemp === 0 ? 1 : ckColorTemp,
  252. effect: getEffect(mode),
  253. effect_list: this.effectList,
  254. brightness: bright ? (bright * 2.55) >> 0 : (this.params.bright * 2.55) >> 0,
  255. rgb_color: [
  256. colorR ? colorR : this.params.colorR,
  257. colorG ? colorG : this.params.colorG,
  258. colorB ? colorB : this.params.colorB
  259. ],
  260. };
  261. (0, restApi_1.updateStates)(this.entityId, {
  262. entity_id: this.entityId,
  263. state: state,
  264. attributes: attributes
  265. });
  266. return [2];
  267. });
  268. });
  269. };
  270. exports.default = CloudUIID173Controller;