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

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.
Files changed (246) hide show
  1. package/README.md +565 -0
  2. package/lib/config/dpCodes.d.ts +27 -0
  3. package/lib/config/dpCodes.js +30 -0
  4. package/lib/config/index.d.ts +2 -0
  5. package/lib/config/index.js +2 -0
  6. package/lib/config/rhythms/defaultValue.d.ts +18 -0
  7. package/lib/config/rhythms/defaultValue.js +72 -0
  8. package/lib/config/rhythms/index.d.ts +10 -0
  9. package/lib/config/rhythms/index.js +35 -0
  10. package/lib/conflict/ConflictResolver.d.ts +2 -0
  11. package/lib/conflict/ConflictResolver.js +284 -0
  12. package/lib/conflict/__test__/ConflictResolver.test.d.ts +1 -0
  13. package/lib/conflict/__test__/ConflictResolver.test.js +183 -0
  14. package/lib/conflict/__test__/index.test.d.ts +1 -0
  15. package/lib/conflict/__test__/index.test.js +385 -0
  16. package/lib/conflict/__test__/scheduleDataManager.test.d.ts +1 -0
  17. package/lib/conflict/__test__/scheduleDataManager.test.js +170 -0
  18. package/lib/conflict/__test__/transform.test.d.ts +1 -0
  19. package/lib/conflict/__test__/transform.test.js +454 -0
  20. package/lib/conflict/__test__/updateDeviceDp.d.ts +1 -0
  21. package/lib/conflict/__test__/updateDeviceDp.js +114 -0
  22. package/lib/conflict/index.d.ts +42 -0
  23. package/lib/conflict/index.js +103 -0
  24. package/lib/conflict/scheduleDataManager.d.ts +12 -0
  25. package/lib/conflict/scheduleDataManager.js +79 -0
  26. package/lib/conflict/transform.d.ts +71 -0
  27. package/lib/conflict/transform.js +251 -0
  28. package/lib/conflict/type.d.ts +55 -0
  29. package/lib/conflict/type.js +17 -0
  30. package/lib/conflict/updateDeviceDp.d.ts +1 -0
  31. package/lib/conflict/updateDeviceDp.js +41 -0
  32. package/lib/constant/index.d.ts +5 -0
  33. package/lib/constant/index.js +5 -0
  34. package/lib/context/index.d.ts +3 -0
  35. package/lib/context/index.js +3 -0
  36. package/lib/context/rhythms/actions.d.ts +20 -0
  37. package/lib/context/rhythms/actions.js +52 -0
  38. package/lib/context/rhythms/index.d.ts +5 -0
  39. package/lib/context/rhythms/index.js +51 -0
  40. package/lib/context/rhythms/reducer.d.ts +24 -0
  41. package/lib/context/rhythms/reducer.js +38 -0
  42. package/lib/context/schedule/actions.d.ts +7 -0
  43. package/lib/context/schedule/actions.js +51 -0
  44. package/lib/context/schedule/context.d.ts +6 -0
  45. package/lib/context/schedule/context.js +52 -0
  46. package/lib/context/schedule/index.d.ts +1 -0
  47. package/lib/context/schedule/index.js +1 -0
  48. package/lib/context/schedule/reducer.d.ts +16 -0
  49. package/lib/context/schedule/reducer.js +104 -0
  50. package/lib/context/schedule/type.d.ts +41 -0
  51. package/lib/context/schedule/type.js +11 -0
  52. package/lib/context/timer/actions.d.ts +8 -0
  53. package/lib/context/timer/actions.js +52 -0
  54. package/lib/context/timer/context.d.ts +10 -0
  55. package/lib/context/timer/context.js +53 -0
  56. package/lib/context/timer/index.d.ts +1 -0
  57. package/lib/context/timer/index.js +1 -0
  58. package/lib/context/timer/reducer.d.ts +15 -0
  59. package/lib/context/timer/reducer.js +70 -0
  60. package/lib/context/timer/type.d.ts +46 -0
  61. package/lib/context/timer/type.js +24 -0
  62. package/lib/dpParser/README.md +63 -0
  63. package/lib/dpParser/__test__/cycle.test.d.ts +1 -0
  64. package/lib/dpParser/__test__/cycle.test.js +258 -0
  65. package/lib/dpParser/__test__/random.test.d.ts +1 -0
  66. package/lib/dpParser/__test__/random.test.js +211 -0
  67. package/lib/dpParser/__test__/rhythms.test.d.ts +1 -0
  68. package/lib/dpParser/__test__/rhythms.test.js +71 -0
  69. package/lib/dpParser/__test__/rtcTimer.test.d.ts +1 -0
  70. package/lib/dpParser/__test__/rtcTimer.test.js +235 -0
  71. package/lib/dpParser/__test__/sleep.test.d.ts +1 -0
  72. package/lib/dpParser/__test__/sleep.test.js +71 -0
  73. package/lib/dpParser/__test__/sleepCommon.test.d.ts +1 -0
  74. package/lib/dpParser/__test__/sleepCommon.test.js +197 -0
  75. package/lib/dpParser/__test__/timerReport.test.d.ts +1 -0
  76. package/lib/dpParser/__test__/timerReport.test.js +100 -0
  77. package/lib/dpParser/__test__/wakeup.test.d.ts +1 -0
  78. package/lib/dpParser/__test__/wakeup.test.js +69 -0
  79. package/lib/dpParser/__test__/wakeupCommon.test.d.ts +1 -0
  80. package/lib/dpParser/__test__/wakeupCommon.test.js +131 -0
  81. package/lib/dpParser/cycle/index.d.ts +15 -0
  82. package/lib/dpParser/cycle/index.js +134 -0
  83. package/lib/dpParser/index.d.ts +23 -0
  84. package/lib/dpParser/index.js +43 -0
  85. package/lib/dpParser/random/index.d.ts +15 -0
  86. package/lib/dpParser/random/index.js +127 -0
  87. package/lib/dpParser/rhythms.d.ts +41 -0
  88. package/lib/dpParser/rhythms.js +116 -0
  89. package/lib/dpParser/rtcTimer.d.ts +17 -0
  90. package/lib/dpParser/rtcTimer.js +300 -0
  91. package/lib/dpParser/sleep/index.d.ts +9 -0
  92. package/lib/dpParser/sleep/index.js +29 -0
  93. package/lib/dpParser/sleep/sleepCommon.d.ts +11 -0
  94. package/lib/dpParser/sleep/sleepCommon.js +117 -0
  95. package/lib/dpParser/sleep/sleepSigmesh.d.ts +0 -0
  96. package/lib/dpParser/sleep/sleepSigmesh.js +101 -0
  97. package/lib/dpParser/timerReport.d.ts +11 -0
  98. package/lib/dpParser/timerReport.js +43 -0
  99. package/lib/dpParser/utils.d.ts +27 -0
  100. package/lib/dpParser/utils.js +53 -0
  101. package/lib/dpParser/wakeup/index.d.ts +10 -0
  102. package/lib/dpParser/wakeup/index.js +34 -0
  103. package/lib/dpParser/wakeup/wakeupCommon.d.ts +11 -0
  104. package/lib/dpParser/wakeup/wakeupCommon.js +106 -0
  105. package/lib/dpParser/wakeup/wakeupSigmesh.d.ts +15 -0
  106. package/lib/dpParser/wakeup/wakeupSigmesh.js +86 -0
  107. package/lib/hooks/__test__/useCommon.test.d.ts +1 -0
  108. package/lib/hooks/__test__/useCommon.test.js +72 -0
  109. package/lib/hooks/__test__/useCountdownDp.test.d.ts +1 -0
  110. package/lib/hooks/__test__/useCountdownDp.test.js +82 -0
  111. package/lib/hooks/__test__/useCycleDp.test.d.ts +1 -0
  112. package/lib/hooks/__test__/useCycleDp.test.js +92 -0
  113. package/lib/hooks/__test__/useRandomDp.test.d.ts +1 -0
  114. package/lib/hooks/__test__/useRandomDp.test.js +88 -0
  115. package/lib/hooks/__test__/useRhythmsDp.test.d.ts +1 -0
  116. package/lib/hooks/__test__/useRhythmsDp.test.js +81 -0
  117. package/lib/hooks/__test__/useSleepDp.test.d.ts +1 -0
  118. package/lib/hooks/__test__/useSleepDp.test.js +127 -0
  119. package/lib/hooks/__test__/useTimeFlush.test.d.ts +1 -0
  120. package/lib/hooks/__test__/useTimeFlush.test.js +109 -0
  121. package/lib/hooks/__test__/useTimerDp.test.d.ts +1 -0
  122. package/lib/hooks/__test__/useTimerDp.test.js +79 -0
  123. package/lib/hooks/__test__/useTimerOperate.test.d.ts +1 -0
  124. package/lib/hooks/__test__/useTimerOperate.test.js +286 -0
  125. package/lib/hooks/__test__/useTimerOperateCloud.test.d.ts +1 -0
  126. package/lib/hooks/__test__/useTimerOperateCloud.test.js +190 -0
  127. package/lib/hooks/__test__/useTimerOperateLocal.test.d.ts +1 -0
  128. package/lib/hooks/__test__/useTimerOperateLocal.test.js +216 -0
  129. package/lib/hooks/__test__/useTimerReportDp.test.d.ts +1 -0
  130. package/lib/hooks/__test__/useTimerReportDp.test.js +93 -0
  131. package/lib/hooks/__test__/useTimerSupport.test.d.ts +1 -0
  132. package/lib/hooks/__test__/useTimerSupport.test.js +110 -0
  133. package/lib/hooks/__test__/useWakeUpDp.test.d.ts +1 -0
  134. package/lib/hooks/__test__/useWakeUpDp.test.js +129 -0
  135. package/lib/hooks/useBaseLightDp.d.ts +21 -0
  136. package/lib/hooks/useBaseLightDp.js +165 -0
  137. package/lib/hooks/useCommon.d.ts +29 -0
  138. package/lib/hooks/useCommon.js +59 -0
  139. package/lib/hooks/useCommonSupport.d.ts +25 -0
  140. package/lib/hooks/useCommonSupport.js +241 -0
  141. package/lib/hooks/useCountdownDp.d.ts +13 -0
  142. package/lib/hooks/useCountdownDp.js +28 -0
  143. package/lib/hooks/useCountdownSupport.d.ts +9 -0
  144. package/lib/hooks/useCountdownSupport.js +13 -0
  145. package/lib/hooks/useCycleDp.d.ts +7 -0
  146. package/lib/hooks/useCycleDp.js +21 -0
  147. package/lib/hooks/useCycleSupport.d.ts +12 -0
  148. package/lib/hooks/useCycleSupport.js +16 -0
  149. package/lib/hooks/useRandomDp.d.ts +7 -0
  150. package/lib/hooks/useRandomDp.js +21 -0
  151. package/lib/hooks/useRandomSupport.d.ts +12 -0
  152. package/lib/hooks/useRandomSupport.js +16 -0
  153. package/lib/hooks/useRhythmsDp.d.ts +7 -0
  154. package/lib/hooks/useRhythmsDp.js +23 -0
  155. package/lib/hooks/useSleepDp.d.ts +6 -0
  156. package/lib/hooks/useSleepDp.js +21 -0
  157. package/lib/hooks/useSleepSupport.d.ts +8 -0
  158. package/lib/hooks/useSleepSupport.js +13 -0
  159. package/lib/hooks/useTimeFlush.d.ts +8 -0
  160. package/lib/hooks/useTimeFlush.js +31 -0
  161. package/lib/hooks/useTimerDp.d.ts +5 -0
  162. package/lib/hooks/useTimerDp.js +25 -0
  163. package/lib/hooks/useTimerFlushList.d.ts +11 -0
  164. package/lib/hooks/useTimerFlushList.js +53 -0
  165. package/lib/hooks/useTimerOperate.d.ts +68 -0
  166. package/lib/hooks/useTimerOperate.js +403 -0
  167. package/lib/hooks/useTimerOperateCloud.d.ts +31 -0
  168. package/lib/hooks/useTimerOperateCloud.js +233 -0
  169. package/lib/hooks/useTimerOperateLocal.d.ts +40 -0
  170. package/lib/hooks/useTimerOperateLocal.js +310 -0
  171. package/lib/hooks/useTimerReportDp.d.ts +15 -0
  172. package/lib/hooks/useTimerReportDp.js +48 -0
  173. package/lib/hooks/useTimerSupport.d.ts +13 -0
  174. package/lib/hooks/useTimerSupport.js +38 -0
  175. package/lib/hooks/useWakeUpDp.d.ts +6 -0
  176. package/lib/hooks/useWakeUpDp.js +21 -0
  177. package/lib/hooks/useWakeUpSupport.d.ts +8 -0
  178. package/lib/hooks/useWakeUpSupport.js +13 -0
  179. package/lib/hooks/useWakeupDp.d.ts +6 -0
  180. package/lib/hooks/useWakeupDp.js +21 -0
  181. package/lib/hooks/useWakeupSupport.d.ts +8 -0
  182. package/lib/hooks/useWakeupSupport.js +13 -0
  183. package/lib/i18n/index.d.ts +142 -0
  184. package/lib/i18n/index.js +7 -0
  185. package/lib/i18n/rhythms.d.ts +73 -0
  186. package/lib/i18n/rhythms.js +72 -0
  187. package/lib/i18n/strings.d.ts +73 -0
  188. package/lib/i18n/strings.js +6 -0
  189. package/lib/index.d.ts +51 -0
  190. package/lib/index.js +64 -0
  191. package/lib/types/countdown.d.ts +3 -0
  192. package/lib/types/countdown.js +1 -0
  193. package/lib/types/cycle.d.ts +22 -0
  194. package/lib/types/cycle.js +1 -0
  195. package/lib/types/global.d.ts +9 -0
  196. package/lib/types/index.d.ts +331 -0
  197. package/lib/types/index.js +76 -0
  198. package/lib/types/random.d.ts +20 -0
  199. package/lib/types/random.js +1 -0
  200. package/lib/types/rhythms.d.ts +79 -0
  201. package/lib/types/rhythms.js +15 -0
  202. package/lib/types/sleep.d.ts +36 -0
  203. package/lib/types/sleep.js +1 -0
  204. package/lib/types/timer.d.ts +107 -0
  205. package/lib/types/timer.js +1 -0
  206. package/lib/types/wakeup.d.ts +38 -0
  207. package/lib/types/wakeup.js +1 -0
  208. package/lib/utils/ScheduleCache.d.ts +19 -0
  209. package/lib/utils/ScheduleCache.js +39 -0
  210. package/lib/utils/ScheduleCloudProperty.d.ts +75 -0
  211. package/lib/utils/ScheduleCloudProperty.js +220 -0
  212. package/lib/utils/ScheduleDataSync.d.ts +82 -0
  213. package/lib/utils/ScheduleDataSync.js +127 -0
  214. package/lib/utils/ScheduleEmit.d.ts +67 -0
  215. package/lib/utils/ScheduleEmit.js +61 -0
  216. package/lib/utils/ScheduleLogger.d.ts +19 -0
  217. package/lib/utils/ScheduleLogger.js +71 -0
  218. package/lib/utils/ScheduleSupport.d.ts +131 -0
  219. package/lib/utils/ScheduleSupport.js +373 -0
  220. package/lib/utils/ScheduleUtils.d.ts +12 -0
  221. package/lib/utils/ScheduleUtils.js +51 -0
  222. package/lib/utils/__test__/ScheduleCloudProperty.test.d.ts +1 -0
  223. package/lib/utils/__test__/ScheduleCloudProperty.test.js +320 -0
  224. package/lib/utils/__test__/ScheduleSupport.test.d.ts +1 -0
  225. package/lib/utils/__test__/ScheduleSupport.test.js +211 -0
  226. package/lib/utils/__test__/ScheduleUtils.test.d.ts +1 -0
  227. package/lib/utils/__test__/ScheduleUtils.test.js +119 -0
  228. package/lib/utils/__test__/common.test.d.ts +1 -0
  229. package/lib/utils/__test__/common.test.js +147 -0
  230. package/lib/utils/__test__/storage.test.d.ts +1 -0
  231. package/lib/utils/__test__/storage.test.js +98 -0
  232. package/lib/utils/__test__/time.test.d.ts +1 -0
  233. package/lib/utils/__test__/time.test.js +146 -0
  234. package/lib/utils/common.d.ts +23 -0
  235. package/lib/utils/common.js +109 -0
  236. package/lib/utils/dpState.d.ts +5 -0
  237. package/lib/utils/dpState.js +27 -0
  238. package/lib/utils/getCachedSystemInfo.d.ts +39 -0
  239. package/lib/utils/getCachedSystemInfo.js +20 -0
  240. package/lib/utils/isInIDE.d.ts +1 -0
  241. package/lib/utils/isInIDE.js +5 -0
  242. package/lib/utils/storage.d.ts +5 -0
  243. package/lib/utils/storage.js +32 -0
  244. package/lib/utils/time.d.ts +40 -0
  245. package/lib/utils/time.js +84 -0
  246. package/package.json +78 -0
@@ -0,0 +1,68 @@
1
+ import { TTimerDataAdd, TTimerDataUpdate, TRtcTimer } from '../types';
2
+ /**
3
+ * 添加定时
4
+ */
5
+ export declare const useTimerAdd: () => {
6
+ isReady: boolean;
7
+ isLoading: boolean;
8
+ addTimer: (timer: TTimerDataAdd) => Promise<unknown>;
9
+ } | {
10
+ isReady: boolean;
11
+ isLoading: boolean;
12
+ addTimer: (timer: TTimerDataAdd, category: string) => Promise<TTimerDataUpdate>;
13
+ };
14
+ export declare const useTimerAddWithFlush: () => {
15
+ isLoading: boolean;
16
+ isReady: boolean;
17
+ addTimerWithFlush: (timer: TTimerDataAdd, category?: string) => Promise<TRtcTimer>;
18
+ };
19
+ /**
20
+ * 更新定时
21
+ */
22
+ export declare const useTimerUpdate: () => {
23
+ isLoading: boolean;
24
+ isReady: boolean;
25
+ updateTimer: (timer: TTimerDataUpdate, oldTimerData: TTimerDataUpdate) => Promise<boolean>;
26
+ };
27
+ export declare const useTimerUpdateWithFlush: () => {
28
+ isLoading: boolean;
29
+ updateTimerWithFlush: (data: TTimerDataUpdate, oldTimerData: TTimerDataUpdate, cloudTimerCategory: string) => Promise<boolean>;
30
+ };
31
+ /**
32
+ * 删除定时
33
+ */
34
+ export declare const useTimerRemove: () => {
35
+ isLoading: boolean;
36
+ isReady: boolean;
37
+ removeTimer: (timerId: string) => Promise<boolean>;
38
+ };
39
+ export declare const useTimerRemoveWithFlush: () => {
40
+ isLoading: boolean;
41
+ removeTimerWithFlush: (timerId: string, category: string) => Promise<boolean>;
42
+ };
43
+ /**
44
+ * 获取定时列表
45
+ */
46
+ export declare const useTimerList: () => {
47
+ isReady: boolean;
48
+ isLoading: boolean;
49
+ getTimerList: (category?: string | undefined) => Promise<{
50
+ timers: TTimerDataUpdate[];
51
+ }>;
52
+ };
53
+ /**
54
+ *更新定时状态, 开或关
55
+ */
56
+ export declare const useTimerUpdateStatus: () => {
57
+ isSupportLocalTimer: true;
58
+ isLoading: boolean;
59
+ updateTimerStatus: (timerId: string, status: boolean) => Promise<unknown>;
60
+ } | {
61
+ isSupportLocalTimer: boolean;
62
+ isLoading: boolean;
63
+ updateTimerStatus: (timerId: string, status: boolean) => Promise<boolean>;
64
+ };
65
+ export declare const useTimerUpdateStatusWithFlush: () => {
66
+ isLoading: boolean;
67
+ updateStatusTimerWithFlush: (timerId: string, status: boolean, category: string) => Promise<boolean>;
68
+ };
@@ -0,0 +1,403 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import "core-js/modules/esnext.iterator.constructor.js";
3
+ import "core-js/modules/esnext.iterator.find.js";
4
+ import "core-js/modules/esnext.iterator.map.js";
5
+ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
6
+ import { useState } from 'react';
7
+ import { useSupportLocalTimer } from './useTimerSupport';
8
+ import { useCloudTimerAdd, useCloudTimerList, useCloudTimerRemove, useCloudTimerUpdate, useCloudTimerUpdateStatus } from './useTimerOperateCloud';
9
+ import { useLocalTimerAdd, useLocalTimerList, useLocalTimerRemove, useLocalTimerUpdate, useLocalTimerUpdateStatus } from './useTimerOperateLocal';
10
+ import { useTimerDp } from './useTimerDp';
11
+ import { getTimerState } from '../context/timer';
12
+ import { findMinMissingNum } from '../utils/common';
13
+ import { useTimerListFlush } from './useTimeFlush';
14
+ import { scheduleLogger } from '../utils/ScheduleLogger';
15
+ import { CLOUD_TIMER_CATEGORY } from '../constant';
16
+ import { useSupport } from './useCommonSupport';
17
+
18
+ /**
19
+ * 添加定时
20
+ */
21
+ export const useTimerAdd = () => {
22
+ const support = useSupport();
23
+ const {
24
+ isSupport: isSupportLocalTimer,
25
+ isReady
26
+ } = useSupportLocalTimer();
27
+ const {
28
+ isLoading: isLocalLoading,
29
+ addTimer: addLocalTimer
30
+ } = useLocalTimerAdd(isSupportLocalTimer);
31
+ const {
32
+ isLoading: isCloudLoading,
33
+ addTimer: addCloudTimer
34
+ } = useCloudTimerAdd(isSupportLocalTimer);
35
+ const {
36
+ updateDp
37
+ } = useTimerDp();
38
+ const isGroup = support.isGroupDevice();
39
+ if (isSupportLocalTimer && !isGroup) {
40
+ return {
41
+ isReady,
42
+ isLoading: isLocalLoading,
43
+ addTimer: timer => {
44
+ return new Promise((resolve, reject) => {
45
+ addLocalTimer(timer).then(() => {
46
+ const dpTimer = {
47
+ status: true,
48
+ time: timer.time,
49
+ loops: timer.loops,
50
+ dps: timer.dps
51
+ };
52
+ // 如果未提供 timerId 则生成一个
53
+ if (!dpTimer.timerId) {
54
+ const {
55
+ rtcTimerList = []
56
+ } = getTimerState();
57
+ const minTimerId = findMinMissingNum(rtcTimerList.map(i => i.timerId));
58
+ dpTimer.timerId = `${minTimerId}`;
59
+ }
60
+ updateDp(dpTimer);
61
+ resolve(dpTimer);
62
+ }).catch(err => {
63
+ reject(err);
64
+ });
65
+ });
66
+ }
67
+ };
68
+ }
69
+ return {
70
+ isReady,
71
+ isLoading: isCloudLoading,
72
+ addTimer: (timer, category) => {
73
+ return addCloudTimer(timer, category);
74
+ }
75
+ };
76
+ };
77
+
78
+ // 添加定时并刷新定时列表
79
+ export const useTimerAddWithFlush = () => {
80
+ const [isLoading, setIsLoading] = useState(false);
81
+ const {
82
+ getTimerList
83
+ } = useTimerList();
84
+ const {
85
+ flushTimerList,
86
+ isReady: isListFlushReady
87
+ } = useTimerListFlush();
88
+ const {
89
+ addTimer,
90
+ isReady: isAddReady
91
+ } = useTimerAdd();
92
+ return {
93
+ isReady: isAddReady && isListFlushReady,
94
+ isLoading,
95
+ addTimerWithFlush: function (timer) {
96
+ let category = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : CLOUD_TIMER_CATEGORY;
97
+ return new Promise((resolve, reject) => {
98
+ setIsLoading(true);
99
+ let _newTimer = null;
100
+ addTimer(timer, category).then(newTimer => {
101
+ _newTimer = newTimer;
102
+ return getTimerList(category);
103
+ }).then(res => {
104
+ res.timers && flushTimerList(res.timers);
105
+ setIsLoading(false);
106
+ resolve(_newTimer);
107
+ }).catch(err => {
108
+ setIsLoading(false);
109
+ reject(err);
110
+ });
111
+ });
112
+ }
113
+ };
114
+ };
115
+
116
+ /**
117
+ * 更新定时
118
+ */
119
+ export const useTimerUpdate = () => {
120
+ const {
121
+ isSupport: isSupportLocalTimer,
122
+ isReady
123
+ } = useSupportLocalTimer();
124
+ const {
125
+ isLoading: isLocalLoading,
126
+ updateTimer: updateLocalTimer
127
+ } = useLocalTimerUpdate(isSupportLocalTimer);
128
+ const {
129
+ isLoading: isCloudLoading,
130
+ updateTimer: updateCloudTimer
131
+ } = useCloudTimerUpdate(isSupportLocalTimer);
132
+ const {
133
+ updateDp
134
+ } = useTimerDp();
135
+ const support = useSupport();
136
+ const isGroup = support.isGroupDevice();
137
+ if (isSupportLocalTimer && !isGroup) {
138
+ return {
139
+ isReady,
140
+ isLoading: isLocalLoading,
141
+ updateTimer: timer => {
142
+ return new Promise((resolve, reject) => {
143
+ updateLocalTimer(timer).then(() => {
144
+ const timerData = _objectSpread(_objectSpread({}, timer), {}, {
145
+ time: timer.time
146
+ });
147
+ updateDp(timerData);
148
+ resolve(true);
149
+ }).catch(err => {
150
+ reject(err);
151
+ });
152
+ });
153
+ }
154
+ };
155
+ }
156
+ return {
157
+ isReady,
158
+ isLoading: isCloudLoading,
159
+ updateTimer: updateCloudTimer
160
+ };
161
+ };
162
+
163
+ // 添加定时并刷新定时列表
164
+ export const useTimerUpdateWithFlush = () => {
165
+ const [isLoading, setIsLoading] = useState(false);
166
+ const {
167
+ getTimerList
168
+ } = useTimerList();
169
+ const {
170
+ flushTimerList
171
+ } = useTimerListFlush();
172
+ const {
173
+ updateTimer
174
+ } = useTimerUpdate();
175
+ return {
176
+ isLoading,
177
+ updateTimerWithFlush: (timer, oldTimerData, cloudTimerCategory) => {
178
+ return new Promise((resolve, reject) => {
179
+ setIsLoading(true);
180
+ updateTimer(timer, oldTimerData).then(() => {
181
+ return getTimerList(cloudTimerCategory);
182
+ }).then(res => {
183
+ flushTimerList(res.timers);
184
+ setIsLoading(false);
185
+ resolve(true);
186
+ }).catch(err => {
187
+ setIsLoading(false);
188
+ reject(err);
189
+ });
190
+ });
191
+ }
192
+ };
193
+ };
194
+
195
+ /**
196
+ * 删除定时
197
+ */
198
+ export const useTimerRemove = () => {
199
+ const {
200
+ isSupport: isSupportLocalTimer,
201
+ isReady
202
+ } = useSupportLocalTimer();
203
+ const {
204
+ isLoading: isLocalLoading,
205
+ removeTimer: removeLocalTimer
206
+ } = useLocalTimerRemove(isSupportLocalTimer);
207
+ const {
208
+ isLoading: isCloudLoading,
209
+ removeTimer: removeCloudTimer
210
+ } = useCloudTimerRemove(isSupportLocalTimer);
211
+ const {
212
+ updateDp
213
+ } = useTimerDp();
214
+ const support = useSupport();
215
+ const isGroup = support.isGroupDevice();
216
+ if (isSupportLocalTimer && !isGroup) {
217
+ return {
218
+ isReady,
219
+ isLoading: isLocalLoading,
220
+ removeTimer: timerId => {
221
+ return new Promise((resolve, reject) => {
222
+ scheduleLogger.debug('removeLocalTimer', timerId);
223
+ removeLocalTimer(timerId).then(res => {
224
+ const {
225
+ rtcTimerList
226
+ } = getTimerState();
227
+ const currentTimer = rtcTimerList.find(i => i.timerId === timerId);
228
+ const dpData = {
229
+ timerId: currentTimer.timerId,
230
+ dps: _objectSpread(_objectSpread({}, currentTimer.dps), {}, {
231
+ power: false
232
+ }),
233
+ loops: '',
234
+ // 删除时不需要
235
+ time: currentTimer.time,
236
+ status: false
237
+ };
238
+ updateDp(dpData);
239
+ resolve(res);
240
+ }).catch(err => {
241
+ reject(err);
242
+ });
243
+ });
244
+ }
245
+ };
246
+ }
247
+ return {
248
+ isReady,
249
+ isLoading: isCloudLoading,
250
+ removeTimer: removeCloudTimer
251
+ };
252
+ };
253
+
254
+ // 删除定时并重新获取定时列表
255
+ export const useTimerRemoveWithFlush = () => {
256
+ const [isLoading, setIsLoading] = useState(false);
257
+ const {
258
+ removeTimer
259
+ } = useTimerRemove();
260
+ const {
261
+ flushTimerList
262
+ } = useTimerListFlush();
263
+ const {
264
+ getTimerList
265
+ } = useTimerList();
266
+ return {
267
+ isLoading,
268
+ removeTimerWithFlush: (timerId, category) => {
269
+ return new Promise((resolve, reject) => {
270
+ scheduleLogger.debug('removeTimerWithFlush', timerId);
271
+ setIsLoading(true);
272
+ removeTimer(timerId).then(() => {
273
+ return getTimerList(category);
274
+ }).then(result => {
275
+ flushTimerList(result.timers);
276
+ setIsLoading(false);
277
+ resolve(true);
278
+ }).catch(error => {
279
+ setIsLoading(false);
280
+ reject(error);
281
+ });
282
+ });
283
+ }
284
+ };
285
+ };
286
+
287
+ /**
288
+ * 获取定时列表
289
+ */
290
+ export const useTimerList = () => {
291
+ const {
292
+ isSupport: isSupportLocalTimer,
293
+ isReady
294
+ } = useSupportLocalTimer();
295
+ const {
296
+ isLoading: isLocalLoading,
297
+ getTimerList: getLocalTimerList
298
+ } = useLocalTimerList(isSupportLocalTimer);
299
+ const {
300
+ isLoading: isCloudLoading,
301
+ getTimerList: getCloudTimerList
302
+ } = useCloudTimerList(isSupportLocalTimer);
303
+ const support = useSupport();
304
+ const isGroup = support.isGroupDevice();
305
+ if (isSupportLocalTimer && !isGroup) {
306
+ return {
307
+ isReady,
308
+ isLoading: isLocalLoading,
309
+ getTimerList: getLocalTimerList
310
+ };
311
+ }
312
+ return {
313
+ isReady,
314
+ isLoading: isCloudLoading,
315
+ getTimerList: getCloudTimerList
316
+ };
317
+ };
318
+
319
+ /**
320
+ *更新定时状态, 开或关
321
+ */
322
+ export const useTimerUpdateStatus = () => {
323
+ const {
324
+ isSupport: isSupportLocalTimer
325
+ } = useSupportLocalTimer();
326
+ const {
327
+ isLoading: isLocalLoading,
328
+ updateTimerStatus: updateLocalTimerStatus
329
+ } = useLocalTimerUpdateStatus(isSupportLocalTimer);
330
+ const {
331
+ isLoading: isCloudLoading,
332
+ updateTimerStatus: updateCloudTimerStatus
333
+ } = useCloudTimerUpdateStatus(isSupportLocalTimer);
334
+ const {
335
+ updateDp
336
+ } = useTimerDp();
337
+ const support = useSupport();
338
+ const isGroup = support.isGroupDevice();
339
+ if (isSupportLocalTimer && !isGroup) {
340
+ return {
341
+ isSupportLocalTimer,
342
+ isLoading: isLocalLoading,
343
+ updateTimerStatus: (timerId, status) => {
344
+ return new Promise((resolve, reject) => {
345
+ updateLocalTimerStatus(timerId, status).then(res => {
346
+ const {
347
+ rtcTimerList = []
348
+ } = getTimerState();
349
+ const currentTimer = rtcTimerList.find(item => item.timerId === timerId);
350
+ const dpTimer = {
351
+ timerId: `${currentTimer.timerId}`,
352
+ status,
353
+ time: currentTimer.time,
354
+ loops: currentTimer.loops,
355
+ dps: currentTimer.dps
356
+ };
357
+ updateDp(dpTimer);
358
+ resolve(res);
359
+ }).catch(err => {
360
+ reject(err);
361
+ });
362
+ });
363
+ }
364
+ };
365
+ }
366
+ return {
367
+ isSupportLocalTimer,
368
+ isLoading: isCloudLoading,
369
+ updateTimerStatus: updateCloudTimerStatus
370
+ };
371
+ };
372
+
373
+ // 更新定时状态并重新获取定时列表
374
+ export const useTimerUpdateStatusWithFlush = () => {
375
+ const [isLoading, setIsLoading] = useState(false);
376
+ const {
377
+ flushTimerList
378
+ } = useTimerListFlush();
379
+ const {
380
+ getTimerList
381
+ } = useTimerList();
382
+ const {
383
+ updateTimerStatus
384
+ } = useTimerUpdateStatus();
385
+ return {
386
+ isLoading,
387
+ updateStatusTimerWithFlush: (timerId, status, category) => {
388
+ return new Promise((resolve, reject) => {
389
+ setIsLoading(true);
390
+ updateTimerStatus(timerId, status).then(() => {
391
+ return getTimerList(category);
392
+ }).then(result => {
393
+ flushTimerList(result.timers);
394
+ setIsLoading(false);
395
+ resolve(true);
396
+ }).catch(error => {
397
+ setIsLoading(false);
398
+ reject(error);
399
+ });
400
+ });
401
+ }
402
+ };
403
+ };
@@ -0,0 +1,31 @@
1
+ import { TTimerDataUpdate, TTimerData, TTimerDataAdd } from '../types';
2
+ export declare const useCloudTimerAdd: (isSupportLocalTimer: boolean) => {
3
+ isLoading: boolean;
4
+ isSupportLocalTimer: boolean;
5
+ addTimer: (timer: TTimerDataAdd, category: string) => Promise<TTimerData>;
6
+ };
7
+ export declare const useCloudTimerUpdate: (isSupportLocalTimer: boolean) => {
8
+ isLoading: boolean;
9
+ isSupportLocalTimer: boolean;
10
+ updateTimer: (timer: TTimerDataUpdate) => Promise<boolean>;
11
+ };
12
+ export declare const useCloudTimerRemove: (isSupportLocalTimer: boolean) => {
13
+ isLoading: boolean;
14
+ isSupportLocalTimer: boolean;
15
+ removeTimer: (timerId: string) => Promise<boolean>;
16
+ };
17
+ export declare const useCloudTimerList: (isSupportLocalTimer: boolean) => {
18
+ isLoading: boolean;
19
+ isSupportLocalTimer: boolean;
20
+ getTimerList: (category?: string) => Promise<{
21
+ timers: TTimerData[];
22
+ }>;
23
+ };
24
+ /**
25
+ * 更新定时开关状态
26
+ */
27
+ export declare const useCloudTimerUpdateStatus: (isSupportLocalTimer: boolean) => {
28
+ isLoading: boolean;
29
+ isSupportLocalTimer: boolean;
30
+ updateTimerStatus: (timerId: string, status: boolean) => Promise<boolean>;
31
+ };