@ray-js/lamp-schedule-core 1.0.4-beta-5 → 1.0.4-beta-7

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.
@@ -176,8 +176,9 @@ describe('transform', () => {
176
176
  expect(result.data).toEqual({
177
177
  status: true,
178
178
  weeks: [1, 0, 0, 0, 0, 0, 0],
179
- startTime: '22:10',
180
- endTime: '22:30'
179
+ startTime: '22:20',
180
+ // 开始入睡时间 = hour:minute
181
+ endTime: '22:30' // 淡出至关灯时间 = hour:minute + step*5分钟 = 22:20 + 10 = 22:30
181
182
  });
182
183
  expect(result.id).toMatch(/^sleep_/);
183
184
  });
@@ -31,6 +31,9 @@ export declare const wakeUpDataToScheduleList: <T extends TWakeUpNode>(data: T[]
31
31
  * 入睡数据转换成适合冲突判断的日程对象
32
32
  * @param data TSleepNode
33
33
  * @returns Schedule
34
+ * 入睡任务时间段为开始入睡到淡出至关灯的时间段
35
+ * startTime = hour:minute(开始入睡时间)
36
+ * endTime = hour:minute + step*5分钟(淡出至关灯的时间)
34
37
  */
35
38
  export declare const sleepNodeToSchedule: <T extends TSleepNode>(data: T) => Schedule;
36
39
  /**
@@ -86,10 +86,13 @@ export const wakeUpDataToScheduleList = data => {
86
86
  * 入睡数据转换成适合冲突判断的日程对象
87
87
  * @param data TSleepNode
88
88
  * @returns Schedule
89
+ * 入睡任务时间段为开始入睡到淡出至关灯的时间段
90
+ * startTime = hour:minute(开始入睡时间)
91
+ * endTime = hour:minute + step*5分钟(淡出至关灯的时间)
89
92
  */
90
93
  export const sleepNodeToSchedule = data => {
91
- const startTime = getPreOffsetTimeByHourMins(data.hour, data.minute, data.step * 5);
92
- const endTime = data.time || getTimeFormatStrByHourMins(data.hour, data.minute);
94
+ const startTime = getTimeFormatStrByHourMins(data.hour, data.minute);
95
+ const endTime = getBackwardOffsetTimeByHourMins(data.hour, data.minute, (data.step || 0) * 5);
93
96
  const _data = {
94
97
  status: data.onOff,
95
98
  weeks: data.loops.split('').map(item => Number(item)),
@@ -22,9 +22,15 @@ export declare const useFetchDpDataByMesh: (dpCodes: string[]) => {
22
22
  /**
23
23
  * 主动拉取 dp 数据, 不限制协议
24
24
  * @param dpCodes dpCode 数组
25
+ * @param options 拉取配置
26
+ * @param options.initFetch 是否初始化拉取
27
+ * @param options.offsetTime 延迟时间 ms
25
28
  * @returns refresh 刷新 dp 数据
26
29
  */
27
- export declare const useFetchDpData: (dpCodes: string[]) => {
30
+ export declare const useFetchDpData: (dpCodes: string[], options?: {
31
+ initFetch: boolean;
32
+ offsetTime: number;
33
+ }) => {
28
34
  refresh: () => void;
29
35
  };
30
36
  export {};
@@ -2,7 +2,7 @@ import "core-js/modules/esnext.iterator.constructor.js";
2
2
  import "core-js/modules/esnext.iterator.filter.js";
3
3
  import "core-js/modules/esnext.iterator.map.js";
4
4
  /* eslint-disable no-console */
5
- import { useState, useEffect, useCallback, useRef } from 'react';
5
+ import { useState, useEffect, useCallback, useRef, useMemo } from 'react';
6
6
  import { isEqual } from 'lodash-es';
7
7
  import { queryDps } from '@ray-js/ray';
8
8
  import { scheduleLogger } from '../utils/ScheduleLogger';
@@ -212,17 +212,44 @@ export const useFetchDpDataByMesh = dpCodes => {
212
212
  /**
213
213
  * 主动拉取 dp 数据, 不限制协议
214
214
  * @param dpCodes dpCode 数组
215
+ * @param options 拉取配置
216
+ * @param options.initFetch 是否初始化拉取
217
+ * @param options.offsetTime 延迟时间 ms
215
218
  * @returns refresh 刷新 dp 数据
216
219
  */
217
- export const useFetchDpData = dpCodes => {
218
- const getDpDataFn = () => {
219
- scheduleLogger.debug('getDpDataFn get:', dpCodes);
220
- fetchDpData(dpCodes);
220
+ export const useFetchDpData = function (dpCodes) {
221
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
222
+ initFetch: true,
223
+ offsetTime: 0
221
224
  };
225
+ // 使用 useMemo 稳定 dpCodes 的字符串表示,避免因数组引用变化导致重复执行
226
+ // 注意:这里依赖 dpCodes 数组本身,但由于调用方已使用 useMemo 稳定引用,所以是安全的
227
+ const dpCodesKey = useMemo(() => dpCodes.join(','), [dpCodes]);
228
+ const getDpDataFn = useCallback(() => {
229
+ scheduleLogger.debug('useFetchDpData getDpDataFn get1:', dpCodes);
230
+ fetchDpData(dpCodes);
231
+ }, [dpCodes]);
232
+
233
+ // 使用 useRef 跟踪上一次的 dpCodesKey,避免重复执行
234
+ const prevDpCodesKeyRef = useRef(null);
222
235
  useEffect(() => {
223
- scheduleLogger.debug('useFetchDpData get:', dpCodes);
224
- getDpDataFn();
225
- }, [dpCodes.join(',')]);
236
+ if (!options.initFetch) {
237
+ return () => null;
238
+ }
239
+ // 只有当 dpCodesKey 真正变化时才执行
240
+ if (prevDpCodesKeyRef.current === dpCodesKey) {
241
+ scheduleLogger.debug('useFetchDpData useEffect: dpCodesKey unchanged', dpCodesKey);
242
+ return () => null;
243
+ }
244
+ prevDpCodesKeyRef.current = dpCodesKey;
245
+ const timer = setTimeout(() => {
246
+ scheduleLogger.debug('useFetchDpData useEffect get:', dpCodes);
247
+ getDpDataFn();
248
+ }, options.offsetTime || 0);
249
+ return () => {
250
+ clearTimeout(timer);
251
+ };
252
+ }, [dpCodesKey, getDpDataFn, dpCodes, options.initFetch, options.offsetTime]);
226
253
  return {
227
254
  refresh: getDpDataFn
228
255
  };
@@ -243,7 +243,13 @@ export const isInitFn = () => {
243
243
  };
244
244
  export const useInit = () => {
245
245
  const [isInit, setIsInit] = useState(_isInit);
246
+ const initCheckedRef = useRef(false);
246
247
  useEffect(() => {
248
+ // 如果初始化已经完成,立即更新状态(只检查一次)
249
+ if (!initCheckedRef.current && _isInit) {
250
+ initCheckedRef.current = true;
251
+ setIsInit(true);
252
+ }
247
253
  const callback = () => {
248
254
  scheduleLogger.info('useInit: initDone');
249
255
  setIsInit(true);
@@ -253,7 +259,7 @@ export const useInit = () => {
253
259
  emitter.off('initDone', callback);
254
260
  };
255
261
  }, []);
256
- return isInit || _isInit;
262
+ return isInit;
257
263
  };
258
264
 
259
265
  /**
@@ -6,8 +6,14 @@ export declare function useCountdownDp(dpCode?: "countdown"): {
6
6
  /**
7
7
  * 主动拉取倒计时dp数据
8
8
  * @param countdownCode 倒计时dpCode
9
+ * @param options 拉取配置
10
+ * @param options.initFetch 是否初始化拉取
11
+ * @param options.offsetTime 延迟时间 ms
9
12
  * @returns refresh 函数 刷新dp数据
10
13
  */
11
- export declare const useCountdownDpPull: (countdownCode?: "countdown") => {
14
+ export declare const useCountdownDpPull: (countdownCode?: "countdown", options?: {
15
+ initFetch: boolean;
16
+ offsetTime: number;
17
+ }) => {
12
18
  refresh: () => void;
13
19
  };
@@ -1,3 +1,4 @@
1
+ import { useMemo } from 'react';
1
2
  import { useBaseLightDp, useFetchDpData } from './useBaseLightDp';
2
3
  import { scheduleDpCodes } from '../config/dpCodes';
3
4
  export function useCountdownDp() {
@@ -15,13 +16,19 @@ export function useCountdownDp() {
15
16
  /**
16
17
  * 主动拉取倒计时dp数据
17
18
  * @param countdownCode 倒计时dpCode
19
+ * @param options 拉取配置
20
+ * @param options.initFetch 是否初始化拉取
21
+ * @param options.offsetTime 延迟时间 ms
18
22
  * @returns refresh 函数 刷新dp数据
19
23
  */
20
24
  export const useCountdownDpPull = function () {
21
25
  let countdownCode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : scheduleDpCodes.COUNTDOWN;
26
+ let options = arguments.length > 1 ? arguments[1] : undefined;
27
+ // 使用 useMemo 稳定数组引用,避免每次渲染都创建新数组导致重复执行
28
+ const dpCodes = useMemo(() => [countdownCode], [countdownCode]);
22
29
  const {
23
30
  refresh
24
- } = useFetchDpData([countdownCode]);
31
+ } = useFetchDpData(dpCodes, options);
25
32
  return {
26
33
  refresh
27
34
  };
@@ -1,4 +1,4 @@
1
- import { useMemo } from 'react';
1
+ import { useCallback, useMemo } from 'react';
2
2
  import { useBaseLightDp } from './useBaseLightDp';
3
3
  import { scheduleDpCodes } from '../config/dpCodes';
4
4
  import { getCycleParser } from '../dpParser/cycle';
@@ -9,13 +9,17 @@ export function useCycleDp() {
9
9
  updateDp
10
10
  } = useBaseLightDp(dpCode);
11
11
  const cycleParser = getCycleParser();
12
+ const _updateDp = useCallback(dpData => {
13
+ updateDp(cycleParser.formatter(dpData));
14
+ }, [cycleParser]);
12
15
  const _dpValue = useMemo(() => {
16
+ if (dpValue === null || dpValue === undefined) {
17
+ return cycleParser.defaultValue;
18
+ }
13
19
  return cycleParser.parser(dpValue);
14
- }, [dpValue]);
20
+ }, [dpValue, cycleParser]);
15
21
  return {
16
22
  dpValue: _dpValue,
17
- updateDp: dpData => {
18
- updateDp(cycleParser.formatter(dpData));
19
- }
23
+ updateDp: _updateDp
20
24
  };
21
25
  }
@@ -1,4 +1,4 @@
1
- import { useMemo } from 'react';
1
+ import { useCallback, useMemo } from 'react';
2
2
  import { useBaseLightDp } from './useBaseLightDp';
3
3
  import { scheduleDpCodes } from '../config/dpCodes';
4
4
  import { getRandomParser } from '../dpParser/random';
@@ -9,13 +9,17 @@ export function useRandomDp() {
9
9
  updateDp
10
10
  } = useBaseLightDp(dpCode);
11
11
  const randomParser = getRandomParser();
12
+ const _updateDp = useCallback(dpData => {
13
+ updateDp(randomParser.formatter(dpData));
14
+ }, [randomParser]);
12
15
  const _dpValue = useMemo(() => {
16
+ if (dpValue === null || dpValue === undefined) {
17
+ return randomParser.defaultValue;
18
+ }
13
19
  return randomParser.parser(dpValue);
14
- }, [dpValue]);
20
+ }, [dpValue, randomParser]);
15
21
  return {
16
22
  dpValue: _dpValue,
17
- updateDp: dpData => {
18
- updateDp(randomParser.formatter(dpData));
19
- }
23
+ updateDp: _updateDp
20
24
  };
21
25
  }
@@ -8,8 +8,14 @@ export declare function useTimerReportDp(dpCode?: "timer_report"): {
8
8
  * 主动拉取定时 timer_report dp 数据
9
9
  * 由于本地定时数据不会主动上报,所以需要主动拉取用来同步定时数据
10
10
  * @param timerReport 定时上报 dpCode
11
+ * @param options 拉取配置
12
+ * @param options.initFetch 是否初始化拉取
13
+ * @param options.offsetTime 延迟时间 ms
11
14
  * @returns refresh 刷新 dp 数据
12
15
  */
13
- export declare const useTimerReportDpPull: (timerReport?: "timer_report") => {
16
+ export declare const useTimerReportDpPull: (timerReport?: "timer_report", options?: {
17
+ initFetch: boolean;
18
+ offsetTime: number;
19
+ }) => {
14
20
  refresh: () => void;
15
21
  };
@@ -34,14 +34,20 @@ export function useTimerReportDp() {
34
34
  * 主动拉取定时 timer_report dp 数据
35
35
  * 由于本地定时数据不会主动上报,所以需要主动拉取用来同步定时数据
36
36
  * @param timerReport 定时上报 dpCode
37
+ * @param options 拉取配置
38
+ * @param options.initFetch 是否初始化拉取
39
+ * @param options.offsetTime 延迟时间 ms
37
40
  * @returns refresh 刷新 dp 数据
38
41
  */
39
42
  export const useTimerReportDpPull = function () {
40
43
  let timerReport = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : scheduleDpCodes.TIMER_REPORT;
44
+ let options = arguments.length > 1 ? arguments[1] : undefined;
41
45
  ScheduleLogger.debug('useTimerReportDpPull 执行');
46
+ // 使用 useMemo 稳定数组引用,避免每次渲染都创建新数组导致重复执行
47
+ const dpCodes = useMemo(() => [timerReport], [timerReport]);
42
48
  const {
43
49
  refresh
44
- } = useFetchDpData([timerReport]);
50
+ } = useFetchDpData(dpCodes, options);
45
51
  return {
46
52
  refresh
47
53
  };
@@ -96,12 +96,12 @@ export class Support {
96
96
  getGroupInfo({
97
97
  groupId,
98
98
  success: groupRes => {
99
- this.groupDevInfo = groupRes;
99
+ this.groupDevInfo = groupRes || {};
100
100
  ScheduleLogger.debug('getGroupInfo groupId: ', groupId);
101
101
  ScheduleLogger.debug('getGroupInfo success1: ', groupRes);
102
102
  const {
103
103
  deviceList = []
104
- } = groupRes;
104
+ } = groupRes || {};
105
105
  const firstDevice = deviceList === null || deviceList === void 0 ? void 0 : deviceList[0];
106
106
  getDeviceInfo({
107
107
  deviceId: firstDevice === null || firstDevice === void 0 ? void 0 : firstDevice.devId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/lamp-schedule-core",
3
- "version": "1.0.4-beta-5",
3
+ "version": "1.0.4-beta-7",
4
4
  "description": "照明计划模块核心能力",
5
5
  "main": "./lib/index.js",
6
6
  "files": [