@ray-js/lamp-schedule-core 1.0.0-beta-1 → 1.0.0-beta-3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -51,220 +51,230 @@ export class RtcTimerFormatter {
51
51
  ScheduleLogger.warn('RtcTimerFormatter parser,value is empty, return defaultValue', this.defaultValue);
52
52
  return this.defaultValue;
53
53
  }
54
- const {
55
- length
56
- } = val;
57
- const mixLightLen = 26; // 混光长度
58
- const colourLightLen = 22; // 彩光长度
59
- const brightLightLen = 18; // 白光长度
60
- const powerLen = 14; // 纯开关长度
54
+ try {
55
+ const {
56
+ length
57
+ } = val;
58
+ const mixLightLen = 26; // 混光长度
59
+ const colourLightLen = 22; // 彩光长度
60
+ const brightLightLen = 18; // 白光长度
61
+ const powerLen = 14; // 纯开关长度
61
62
 
62
- // 删除定时
63
+ // 删除定时
63
64
 
64
- if (length !== 4 && length !== powerLen && length !== brightLightLen && length !== colourLightLen && length !== mixLightLen) {
65
- // 数据位数不定, 可能是14位或18位22位26位, 删除定时指令为4位
66
- ScheduleLogger.warn('RtcTimerFormatter parser fail:', RTC_TIMER, '=>', val);
67
- return this.defaultValue;
68
- }
69
- const step = generateDpStrStep(val);
70
- if (length === powerLen || length === brightLightLen || length === colourLightLen || length === mixLightLen) {
71
- const id = step(2); // // 定时任务ID, ABBBBBBB:A:定时任务开关,0-关闭,1-开启;BBBBBBB:定时ID:1-10,最多支持10条
72
- const idBinaryStr = numToBinStrPad8(id.value); // 转为二进制的八位 => ABBBBBBB
73
- const status = idBinaryStr.slice(0, 1) === '1'; // 0-关闭,1-开启
74
- const timerId = parseInt(idBinaryStr.slice(1), 2); // 定时ID:1-10
65
+ if (length !== 4 && length !== powerLen && length !== brightLightLen && length !== colourLightLen && length !== mixLightLen) {
66
+ // 数据位数不定, 可能是14位或18位22位26位, 删除定时指令为4位
67
+ ScheduleLogger.warn('RtcTimerFormatter parser fail:', RTC_TIMER, '=>', val);
68
+ return this.defaultValue;
69
+ }
70
+ const step = generateDpStrStep(val);
71
+ if (length === powerLen || length === brightLightLen || length === colourLightLen || length === mixLightLen) {
72
+ const id = step(2); // // 定时任务ID, ABBBBBBB:A:定时任务开关,0-关闭,1-开启;BBBBBBB:定时ID:1-10,最多支持10条
73
+ const idBinaryStr = numToBinStrPad8(id.value); // 转为二进制的八位 => ABBBBBBB
74
+ const status = idBinaryStr.slice(0, 1) === '1'; // 0-关闭,1-开启
75
+ const timerId = parseInt(idBinaryStr.slice(1), 2); // 定时ID:1-10
75
76
 
76
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
77
- step(2).value; // 定时类型, 普通定时选择:0x01
78
- const repeatInt = step(2).value; // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时 bit0 => 周日
79
- const loops = padEnd(repeatInt.toString(2).split('').reverse().join(''), 7, '0');
77
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
78
+ step(2).value; // 定时类型, 普通定时选择:0x01
79
+ const repeatInt = step(2).value; // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时 bit0 => 周日
80
+ const loops = padEnd(repeatInt.toString(2).split('').reverse().join(''), 7, '0');
80
81
 
81
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
82
- step(2).value; // 时间类型 => 0x00:精确时间 0x01:日出前 0x02:日出后 0x03:日落前 0x04:日落后 0x05:倒计时
83
- const time = step(4).value; // 时间 => 一天为 24H*60=1440
82
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
83
+ step(2).value; // 时间类型 => 0x00:精确时间 0x01:日出前 0x02:日出后 0x03:日落前 0x04:日落后 0x05:倒计时
84
+ const time = step(4).value; // 时间 => 一天为 24H*60=1440
84
85
 
85
- /**
86
- * A:设备动作
87
- * A=0x00,关灯,
88
- A=0x01,开灯,
89
- A=0x02,白光,
90
- A=0x03,彩光,
91
- A=0x04,混光,
92
- A=0x05,情景,
93
- A=0x06,节能,
94
- A=0x07,太阳能
95
- */
96
- step(2).value;
97
- let dps = {}; // 设备动作
98
- // 白光模式 dp
99
- if (length === brightLightLen) {
100
- const bright_value = step(2).value;
101
- const temp_value = step(2).value;
102
- dps = {
103
- [EDpCodes.bright_value]: bright_value,
104
- [EDpCodes.temp_value]: temp_value
105
- };
106
- } else if (length === colourLightLen) {
107
- // 彩光模式dp
108
- const hue = step(4).value;
109
- const saturation = step(2).value;
110
- const _value = step(2).value;
111
- dps = {
112
- [EDpCodes.colour_data]: {
113
- hue,
114
- saturation,
115
- value: _value
116
- }
117
- };
118
- } else if (length === mixLightLen) {
119
- // 混光模式
120
- const hue = step(4).value;
121
- const saturation = step(2).value;
122
- const _value = step(2).value;
123
- const bright_value = step(2).value;
124
- const temp_value = step(2).value;
125
- dps = {
126
- [EDpCodes.colour_data]: {
127
- hue,
128
- saturation,
129
- value: _value
130
- },
131
- [EDpCodes.bright_value]: bright_value,
132
- [EDpCodes.temp_value]: temp_value
133
- };
134
- } else {
135
- // 开关灯模式
136
- // 开灯
137
- // 01
138
- const startDpDataInt = step(2).value;
139
- const startDpDataHex = this.to16(startDpDataInt, 2);
140
- const switch_led = startDpDataHex.slice(0, 2) === '01';
141
- dps = {
142
- [EDpCodes.switch_led]: switch_led
86
+ /**
87
+ * A:设备动作
88
+ * A=0x00,关灯,
89
+ A=0x01,开灯,
90
+ A=0x02,白光,
91
+ A=0x03,彩光,
92
+ A=0x04,混光,
93
+ A=0x05,情景,
94
+ A=0x06,节能,
95
+ A=0x07,太阳能
96
+ */
97
+ step(2).value;
98
+ let dps = {}; // 设备动作
99
+ // 白光模式 dp
100
+ if (length === brightLightLen) {
101
+ const bright_value = step(2).value;
102
+ const temp_value = step(2).value;
103
+ dps = {
104
+ [EDpCodes.bright_value]: bright_value,
105
+ [EDpCodes.temp_value]: temp_value
106
+ };
107
+ } else if (length === colourLightLen) {
108
+ // 彩光模式dp
109
+ const hue = step(4).value;
110
+ const saturation = step(2).value;
111
+ const _value = step(2).value;
112
+ dps = {
113
+ [EDpCodes.colour_data]: {
114
+ hue,
115
+ saturation,
116
+ value: _value
117
+ }
118
+ };
119
+ } else if (length === mixLightLen) {
120
+ // 混光模式
121
+ const hue = step(4).value;
122
+ const saturation = step(2).value;
123
+ const _value = step(2).value;
124
+ const bright_value = step(2).value;
125
+ const temp_value = step(2).value;
126
+ dps = {
127
+ [EDpCodes.colour_data]: {
128
+ hue,
129
+ saturation,
130
+ value: _value
131
+ },
132
+ [EDpCodes.bright_value]: bright_value,
133
+ [EDpCodes.temp_value]: temp_value
134
+ };
135
+ } else {
136
+ // 开关灯模式
137
+ // 开灯
138
+ // 01
139
+ const startDpDataInt = step(2).value;
140
+ const startDpDataHex = this.to16(startDpDataInt, 2);
141
+ const switch_led = startDpDataHex.slice(0, 2) === '01';
142
+ dps = {
143
+ [EDpCodes.switch_led]: switch_led
144
+ };
145
+ }
146
+ return {
147
+ timerId: `${timerId}`,
148
+ status,
149
+ loops,
150
+ time: minuteToTime(time),
151
+ dps
143
152
  };
144
153
  }
154
+
155
+ // 删除时 dp 数据
156
+ const id = step(2).value;
157
+ const idBinaryStr = numToBinStrPad8(id);
158
+ const status = idBinaryStr.slice(0, 1) === '1';
159
+ const timerId = parseInt(idBinaryStr.slice(1), 2);
145
160
  return {
146
161
  timerId: `${timerId}`,
147
- status,
148
- loops,
149
- time: minuteToTime(time),
150
- dps
162
+ status
151
163
  };
164
+ } catch (err) {
165
+ ScheduleLogger.error('RtcTimerFormatter parser fail:', RTC_TIMER, '=>', val, err);
166
+ return this.defaultValue;
152
167
  }
153
-
154
- // 删除时 dp 数据
155
- const id = step(2).value;
156
- const idBinaryStr = numToBinStrPad8(id);
157
- const status = idBinaryStr.slice(0, 1) === '1';
158
- const timerId = parseInt(idBinaryStr.slice(1), 2);
159
- return {
160
- timerId: `${timerId}`,
161
- status
162
- };
163
168
  }
164
169
  to16(value, length) {
165
170
  return numToHexString(value, length);
166
171
  }
167
172
  formatter(data) {
168
- if (typeof data === 'string') {
169
- return data;
170
- }
171
- // 自定义格式转为16进制
172
- const {
173
- timerId,
174
- // 定时任务ID
175
- status,
176
- // type, // 定时类型,普通定时选择:0x01
177
- loops,
178
- // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时,0 => 周日
179
- // startTimeType, // 定时任务开始时间类型,0x00 => 固定时间
180
- time,
181
- dps
182
- } = data;
183
- ScheduleLogger.debug('rtcTimer formatter data:', data);
184
- if (loops) {
185
- const _dps = {};
186
- Object.keys(dps).forEach(key => {
187
- // eslint-disable-next-line no-self-compare
188
- if (+key === +key) {
189
- const dpCode = getDpCodeByDpId(key);
190
- dpCode && (_dps[dpCode] = dps[key]);
173
+ try {
174
+ if (typeof data === 'string') {
175
+ return data;
176
+ }
177
+ // 自定义格式转为16进制
178
+ const {
179
+ timerId,
180
+ // 定时任务ID
181
+ status,
182
+ // type, // 定时类型,普通定时选择:0x01
183
+ loops,
184
+ // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时,0 => 周日
185
+ // startTimeType, // 定时任务开始时间类型,0x00 => 固定时间
186
+ time,
187
+ dps
188
+ } = data;
189
+ ScheduleLogger.debug('rtcTimer formatter data:', data);
190
+ if (loops) {
191
+ const _dps = {};
192
+ Object.keys(dps).forEach(key => {
193
+ // eslint-disable-next-line no-self-compare
194
+ if (+key === +key) {
195
+ const dpCode = getDpCodeByDpId(key);
196
+ dpCode && (_dps[dpCode] = dps[key]);
197
+ } else {
198
+ _dps[key] = dps[key];
199
+ }
200
+ });
201
+ const statusStr = status ? '1' : '0';
202
+ const timerIdStr = padStart(Number(timerId).toString(2), 7, '0');
203
+ const idStr = this.to16(parseInt(`${statusStr}${timerIdStr}`, 2), 2);
204
+ const typeStr = this.to16('01', 2);
205
+ const repeatStr = this.to16(parseInt(loops.split('').reverse().join(''), 2), 2);
206
+ const startTimeTypeStr = this.to16('00', 2);
207
+ let timeNum = +time;
208
+ if (isNaN(time) && typeof time === 'string') {
209
+ const [min, second] = time === null || time === void 0 ? void 0 : time.split(':');
210
+ timeNum = +min * 60 + Number(second);
211
+ }
212
+ const startTimeStr = this.to16(timeNum, 4);
213
+ let dpDataStr;
214
+ /**
215
+ * ABCDEFG 7字节; A:设备动作;BCDEFG:动作参数;
216
+ * A=0x00,关灯,BCDEFG无效;
217
+ * A=0x01,开灯,BCDEFG无效;
218
+ * A=0x02,白光,B:亮度,1-100,C:色温,0-100,DEFG无效;
219
+ * A=0x03,彩光,BC:色度,0-360,D:饱和度,0-100,E:彩光亮度,0-100,FG无效;
220
+ * A=0x04,混光,RGBCW全亮,BC:色度,0-360,D:饱和度,0-100,E:彩光亮度,1-100,F:白光亮度,1-100,G:色温,0-100;
221
+ * A=0x05,情景,B:场景ID,0-15,CDEFG无效;
222
+ * A=0x06,节能,B:场景ID,0-15,C:节能开关,0-关,1-开,DEFG无效;
223
+ * A=0x07,太阳能,BC:色度,0-360,D:饱和度,0-100,E:彩光亮度,1-100,F:白光亮度,1-100,G:色温,0-100;
224
+ * A=0x08,执行定时前状态,BCDEFG无效;
225
+ */
226
+ if (_dps[EDpCodes.switch_led] === false) {
227
+ dpDataStr = '00';
228
+ } else if (Object.keys(_dps).includes(EDpCodes.colour_data) && Object.keys(_dps).includes(EDpCodes.temp_value)) {
229
+ const bright_value = _dps[EDpCodes.bright_value];
230
+ const temp_value = _dps[EDpCodes.temp_value];
231
+ const {
232
+ hue,
233
+ saturation,
234
+ value
235
+ } = _dps[EDpCodes.colour_data];
236
+ const hueStr = this.to16(hue, 4);
237
+ const saturationStr = this.to16(saturation, 2);
238
+ const valueStr = this.to16(value, 2);
239
+ const brightStr = this.to16(bright_value, 2);
240
+ const tempStr = this.to16(temp_value, 2);
241
+ dpDataStr = `04${hueStr}${saturationStr}${valueStr}${brightStr}${tempStr}`;
242
+ } else if (Object.keys(_dps).includes(EDpCodes.colour_data)) {
243
+ const {
244
+ hue = 0,
245
+ saturation = 100,
246
+ value = 100
247
+ } = _dps[EDpCodes.colour_data];
248
+ const hueStr = this.to16(hue, 4);
249
+ const saturationStr = this.to16(saturation, 2);
250
+ const valueStr = this.to16(value, 2);
251
+ dpDataStr = `03${hueStr}${saturationStr}${valueStr}`;
252
+ } else if (Object.keys(_dps).includes(EDpCodes.bright_value)) {
253
+ var _dps$EDpCodes$bright_, _dps$EDpCodes$temp_va;
254
+ const bright_value = (_dps$EDpCodes$bright_ = _dps[EDpCodes.bright_value]) !== null && _dps$EDpCodes$bright_ !== void 0 ? _dps$EDpCodes$bright_ : 100;
255
+ const temp_value = (_dps$EDpCodes$temp_va = _dps[EDpCodes.temp_value]) !== null && _dps$EDpCodes$temp_va !== void 0 ? _dps$EDpCodes$temp_va : 0;
256
+ const brightStr = this.to16(bright_value, 2);
257
+ const temperatureStr = this.to16(temp_value, 2);
258
+ dpDataStr = `02${brightStr}${temperatureStr}`;
191
259
  } else {
192
- _dps[key] = dps[key];
260
+ const powerStr = _dps[EDpCodes.switch_led] ? '01' : '00';
261
+ dpDataStr = `${powerStr}`;
193
262
  }
194
- });
263
+ const _res = `${idStr}${typeStr}${repeatStr}${startTimeTypeStr}${startTimeStr}${dpDataStr}`;
264
+ ScheduleLogger.debug('rtcTimer formatter dpStr1:', dpDataStr);
265
+ ScheduleLogger.debug('rtcTimer formatter dpStr2:', _res);
266
+ return _res;
267
+ }
195
268
  const statusStr = status ? '1' : '0';
196
269
  const timerIdStr = padStart(Number(timerId).toString(2), 7, '0');
197
270
  const idStr = this.to16(parseInt(`${statusStr}${timerIdStr}`, 2), 2);
198
- const typeStr = this.to16('01', 2);
199
- const repeatStr = this.to16(parseInt(loops.split('').reverse().join(''), 2), 2);
200
- const startTimeTypeStr = this.to16('00', 2);
201
- let timeNum = +time;
202
- if (isNaN(time) && typeof time === 'string') {
203
- const [min, second] = time === null || time === void 0 ? void 0 : time.split(':');
204
- timeNum = +min * 60 + Number(second);
205
- }
206
- const startTimeStr = this.to16(timeNum, 4);
207
- let dpDataStr;
208
- /**
209
- * ABCDEFG 7字节; A:设备动作;BCDEFG:动作参数;
210
- * A=0x00,关灯,BCDEFG无效;
211
- * A=0x01,开灯,BCDEFG无效;
212
- * A=0x02,白光,B:亮度,1-100,C:色温,0-100,DEFG无效;
213
- * A=0x03,彩光,BC:色度,0-360,D:饱和度,0-100,E:彩光亮度,0-100,FG无效;
214
- * A=0x04,混光,RGBCW全亮,BC:色度,0-360,D:饱和度,0-100,E:彩光亮度,1-100,F:白光亮度,1-100,G:色温,0-100;
215
- * A=0x05,情景,B:场景ID,0-15,CDEFG无效;
216
- * A=0x06,节能,B:场景ID,0-15,C:节能开关,0-关,1-开,DEFG无效;
217
- * A=0x07,太阳能,BC:色度,0-360,D:饱和度,0-100,E:彩光亮度,1-100,F:白光亮度,1-100,G:色温,0-100;
218
- * A=0x08,执行定时前状态,BCDEFG无效;
219
- */
220
- if (_dps[EDpCodes.switch_led] === false) {
221
- dpDataStr = '00';
222
- } else if (Object.keys(_dps).includes(EDpCodes.colour_data) && Object.keys(_dps).includes(EDpCodes.temp_value)) {
223
- const bright_value = _dps[EDpCodes.bright_value];
224
- const temp_value = _dps[EDpCodes.temp_value];
225
- const {
226
- hue,
227
- saturation,
228
- value
229
- } = _dps[EDpCodes.colour_data];
230
- const hueStr = this.to16(hue, 4);
231
- const saturationStr = this.to16(saturation, 2);
232
- const valueStr = this.to16(value, 2);
233
- const brightStr = this.to16(bright_value, 2);
234
- const tempStr = this.to16(temp_value, 2);
235
- dpDataStr = `04${hueStr}${saturationStr}${valueStr}${brightStr}${tempStr}`;
236
- } else if (Object.keys(_dps).includes(EDpCodes.colour_data)) {
237
- const {
238
- hue = 0,
239
- saturation = 100,
240
- value = 100
241
- } = _dps[EDpCodes.colour_data];
242
- const hueStr = this.to16(hue, 4);
243
- const saturationStr = this.to16(saturation, 2);
244
- const valueStr = this.to16(value, 2);
245
- dpDataStr = `03${hueStr}${saturationStr}${valueStr}`;
246
- } else if (Object.keys(_dps).includes(EDpCodes.bright_value)) {
247
- var _dps$EDpCodes$bright_, _dps$EDpCodes$temp_va;
248
- const bright_value = (_dps$EDpCodes$bright_ = _dps[EDpCodes.bright_value]) !== null && _dps$EDpCodes$bright_ !== void 0 ? _dps$EDpCodes$bright_ : 100;
249
- const temp_value = (_dps$EDpCodes$temp_va = _dps[EDpCodes.temp_value]) !== null && _dps$EDpCodes$temp_va !== void 0 ? _dps$EDpCodes$temp_va : 0;
250
- const brightStr = this.to16(bright_value, 2);
251
- const temperatureStr = this.to16(temp_value, 2);
252
- dpDataStr = `02${brightStr}${temperatureStr}`;
253
- } else {
254
- const powerStr = _dps[EDpCodes.switch_led] ? '01' : '00';
255
- dpDataStr = `${powerStr}`;
256
- }
257
- const _res = `${idStr}${typeStr}${repeatStr}${startTimeTypeStr}${startTimeStr}${dpDataStr}`;
258
- ScheduleLogger.debug('rtcTimer formatter dpStr1:', dpDataStr);
259
- ScheduleLogger.debug('rtcTimer formatter dpStr2:', _res);
271
+ const _res = `${idStr}00`;
272
+ ScheduleLogger.debug('rtcTimer remove formatter2 data:', _res);
260
273
  return _res;
274
+ } catch (error) {
275
+ ScheduleLogger.error('rtcTimer formatter error', error);
276
+ return '';
261
277
  }
262
- const statusStr = status ? '1' : '0';
263
- const timerIdStr = padStart(Number(timerId).toString(2), 7, '0');
264
- const idStr = this.to16(parseInt(`${statusStr}${timerIdStr}`, 2), 2);
265
- const _res = `${idStr}00`;
266
- ScheduleLogger.debug('rtcTimer remove formatter2 data:', _res);
267
- return _res;
268
278
  }
269
279
  }
270
280
  export const rtcTimerParser = new RtcTimerFormatter();
@@ -17,101 +17,98 @@ export class Sleep {
17
17
  nodes: []
18
18
  };
19
19
  parser(dpStr) {
20
- scheduleLogger.debug('Sleep parser dpStr: ', dpStr);
21
- if (!dpStr) {
22
- return this.defaultValue;
23
- }
24
- const dpLength = dpStr.length;
25
- if (!dpLength || (dpLength - 4) % 22 !== 0) {
26
- scheduleLogger.error('数据有问题,无法解析');
27
- return this.defaultValue;
28
- }
29
- const step = generateDpStrStep(dpStr);
30
- const version = step(2).value;
31
- let length = step(2).value;
32
- length = Math.min(length, 4);
33
- const nodes = [];
34
- // 解析循环节点
35
- for (let i = 0; i < length; i++) {
36
- const onOff = !!step(2).value;
37
- const repeatInt = step(2).value; // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时 bit0 => 周日
38
- const loops = padEnd(repeatInt.toString(2).split('').reverse().join(''), 7, '0');
39
- const stepNum = step(2).value;
40
- const hour = step(2).value;
41
- const minute = step(2).value;
20
+ try {
21
+ scheduleLogger.debug('Sleep parser dpStr: ', dpStr);
22
+ if (!dpStr) {
23
+ return this.defaultValue;
24
+ }
25
+ const dpLength = dpStr.length;
26
+ if (!dpLength || (dpLength - 4) % 22 !== 0) {
27
+ scheduleLogger.error('数据有问题,无法解析');
28
+ return this.defaultValue;
29
+ }
30
+ const step = generateDpStrStep(dpStr);
31
+ const version = step(2).value;
32
+ let length = step(2).value;
33
+ length = Math.min(length, 4);
34
+ const nodes = [];
35
+ // 解析循环节点
36
+ for (let i = 0; i < length; i++) {
37
+ const onOff = !!step(2).value;
38
+ const repeatInt = step(2).value; // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时 bit0 => 周日
39
+ const loops = padEnd(repeatInt.toString(2).split('').reverse().join(''), 7, '0');
40
+ const stepNum = step(2).value;
41
+ const hour = step(2).value;
42
+ const minute = step(2).value;
42
43
 
43
- // 解析色相值(组合百位和十个位)
44
- const hundreds = step(2).value;
45
- const tensOnes = step(2).value;
46
- const saturation = step(2).value;
47
- const val = step(2).value;
48
- const brightness = step(2).value;
49
- const temperatureStep = step(2);
50
- const temperature = temperatureStep.value;
51
- nodes.push({
52
- onOff,
53
- loops,
54
- step: stepNum,
55
- hour,
56
- minute,
57
- hue: Math.round(hundreds * 100 + tensOnes),
58
- // 0-360
59
- saturation,
60
- value: val,
61
- brightness,
62
- temperature,
63
- index: i
64
- });
65
- if (temperatureStep.done) {
66
- break;
44
+ // 解析色相值(组合百位和十个位)
45
+ const hundreds = step(2).value;
46
+ const tensOnes = step(2).value;
47
+ const saturation = step(2).value;
48
+ const val = step(2).value;
49
+ const brightness = step(2).value;
50
+ const temperatureStep = step(2);
51
+ const temperature = temperatureStep.value;
52
+ nodes.push({
53
+ onOff,
54
+ loops,
55
+ step: stepNum,
56
+ hour,
57
+ minute,
58
+ hue: Math.round(hundreds * 100 + tensOnes),
59
+ // 0-360
60
+ saturation,
61
+ value: val,
62
+ brightness,
63
+ temperature,
64
+ index: i
65
+ });
66
+ if (temperatureStep.done) {
67
+ break;
68
+ }
67
69
  }
70
+ return {
71
+ version,
72
+ length,
73
+ nodes
74
+ };
75
+ } catch (error) {
76
+ scheduleLogger.error('Sleep parser error', error);
77
+ return this.defaultValue;
68
78
  }
69
- return {
70
- version,
71
- length,
72
- nodes
73
- };
74
79
  }
75
80
  formatter(data) {
76
- var _data$nodes, _data$nodes2;
77
- const result = [numToHexString(data.version), numToHexString((_data$nodes = data.nodes) === null || _data$nodes === void 0 ? void 0 : _data$nodes.length)];
78
- scheduleLogger.debug('sleep formatter data', data);
79
- data === null || data === void 0 || (_data$nodes2 = data.nodes) === null || _data$nodes2 === void 0 || _data$nodes2.forEach(node => {
80
- // 拆分色相值为百位和十个位
81
- const hueHundreds = Math.floor(node.hue / 100);
82
- const hueTensOnes = node.hue % 100;
83
- const {
84
- saturation,
85
- value,
86
- brightness,
87
- temperature
88
- } = node;
89
- let saturation1 = node.saturation;
90
- let value1 = node.value;
91
- let brightness1 = node.brightness;
92
- let temperature1 = node.temperature;
93
- if (saturation === 0 && value === 0 && brightness === 0 && temperature === 0) {
94
- saturation1 = defaultColor.saturation;
95
- value1 = defaultColor.value;
96
- brightness1 = defaultColor.brightness;
97
- temperature1 = defaultColor.temperature;
98
- }
99
- result.push(numToHexString(node.onOff ? 1 : 0), numToHexString(parseInt(node.loops.split('').reverse().join(''), 2), 2), numToHexString(node.step), numToHexString(node.hour), numToHexString(node.minute), numToHexString(hueHundreds), numToHexString(hueTensOnes), numToHexString(saturation1), numToHexString(value1), numToHexString(brightness1), numToHexString(temperature1));
100
- });
101
- return result.join('');
81
+ try {
82
+ var _data$nodes, _data$nodes2;
83
+ const result = [numToHexString(data.version), numToHexString((_data$nodes = data.nodes) === null || _data$nodes === void 0 ? void 0 : _data$nodes.length)];
84
+ scheduleLogger.debug('sleep formatter data', data);
85
+ data === null || data === void 0 || (_data$nodes2 = data.nodes) === null || _data$nodes2 === void 0 || _data$nodes2.forEach(node => {
86
+ // 拆分色相值为百位和十个位
87
+ const hueHundreds = Math.floor(node.hue / 100);
88
+ const hueTensOnes = node.hue % 100;
89
+ const {
90
+ saturation,
91
+ value,
92
+ brightness,
93
+ temperature
94
+ } = node;
95
+ let saturation1 = node.saturation;
96
+ let value1 = node.value;
97
+ let brightness1 = node.brightness;
98
+ let temperature1 = node.temperature;
99
+ if (saturation === 0 && value === 0 && brightness === 0 && temperature === 0) {
100
+ saturation1 = defaultColor.saturation;
101
+ value1 = defaultColor.value;
102
+ brightness1 = defaultColor.brightness;
103
+ temperature1 = defaultColor.temperature;
104
+ }
105
+ result.push(numToHexString(node.onOff ? 1 : 0), numToHexString(parseInt(node.loops.split('').reverse().join(''), 2), 2), numToHexString(node.step), numToHexString(node.hour), numToHexString(node.minute), numToHexString(hueHundreds), numToHexString(hueTensOnes), numToHexString(saturation1), numToHexString(value1), numToHexString(brightness1), numToHexString(temperature1));
106
+ });
107
+ return result.join('');
108
+ } catch (error) {
109
+ scheduleLogger.error('sleep formatter error', error);
110
+ return '';
111
+ }
102
112
  }
103
113
  }
104
- export const sleepParser = new Sleep();
105
-
106
- // if (isInIDEFun()) {
107
- // // 使用示例
108
- // const dpStr = '00 01 01 42 01 0b 15 00 00 00 00 00'.replaceAll(' ', '');
109
- // const result = sleepParser.parser(dpStr);
110
- // const formatted = sleepParser.formatter(result);
111
- // console.log(result, 'resultresult');
112
- // console.log(formatted, 'formattedformatted');
113
- // console.log(dpStr, 'dpStrdpStr');
114
- // if (formatted !== dpStr) {
115
- // throw new Error('sleep dp format error');
116
- // }
117
- // }
114
+ export const sleepParser = new Sleep();