@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,70 @@
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.filter.js";
4
+ import "core-js/modules/esnext.iterator.for-each.js";
5
+ /* eslint-disable no-case-declarations */
6
+ import { useReducer, useCallback } from 'react';
7
+ import { cloneDeep } from 'lodash-es';
8
+ import { ECouldTimer, ERtcTimer } from './type';
9
+ import { scheduleLogger as ScheduleLogger } from '../../utils/ScheduleLogger';
10
+
11
+ // 普通定时器 Reducer
12
+
13
+ export const useTimerReducer = init => {
14
+ const timerReducer = useCallback((state, action) => {
15
+ ScheduleLogger.debug('TIMER_REDUCER RUN:', action);
16
+ switch (action.type) {
17
+ // 本地定时更新 Start
18
+ case ERtcTimer.ADD_RTC_TIMER:
19
+ // eslint-disable-next-line no-case-declarations
20
+ const _newTimers = cloneDeep(state.rtcTimerList);
21
+ const index = _newTimers.findIndex(i => {
22
+ var _action$payload;
23
+ return i.timerId === ((_action$payload = action.payload) === null || _action$payload === void 0 ? void 0 : _action$payload.timerId);
24
+ });
25
+ index > -1 && _newTimers.splice(index, 1);
26
+ // 添加
27
+ _newTimers.push(action.payload);
28
+ return _objectSpread(_objectSpread({}, state), {}, {
29
+ rtcTimerList: [..._newTimers]
30
+ });
31
+ case ERtcTimer.REMOVE_RTC_TIMER:
32
+ // eslint-disable-next-line no-case-declarations
33
+ const _data = action.payload;
34
+ // eslint-disable-next-line no-case-declarations
35
+ const rNewTimers = state.rtcTimerList.filter(item => item.timerId !== (_data === null || _data === void 0 ? void 0 : _data.timerId));
36
+ return _objectSpread(_objectSpread({}, state), {}, {
37
+ rtcTimerList: [...rNewTimers]
38
+ });
39
+ case ERtcTimer.UPDATE_RTC_TIMER:
40
+ // eslint-disable-next-line no-case-declarations
41
+ const data = action.payload;
42
+ // eslint-disable-next-line no-case-declarations
43
+ const newTimers = state.rtcTimerList;
44
+ newTimers.forEach((item, index) => {
45
+ if (item.timerId === (data === null || data === void 0 ? void 0 : data.timerId)) {
46
+ newTimers[index] = data;
47
+ }
48
+ });
49
+ return _objectSpread(_objectSpread({}, state), {}, {
50
+ rtcTimerList: [...newTimers]
51
+ });
52
+ case ERtcTimer.FLUSH_RTC_TIMER_LIST:
53
+ return _objectSpread(_objectSpread({}, state), {}, {
54
+ rtcTimerList: action.payload
55
+ });
56
+ // 本地定时器更新 End
57
+
58
+ // 云定时器 更新 Start
59
+ case ECouldTimer.FLUSH_CLOUD_TIMER_LIST:
60
+ return _objectSpread(_objectSpread({}, state), {}, {
61
+ cloudTimerList: action.payload
62
+ });
63
+ // 云定时器 更新 End
64
+
65
+ default:
66
+ return state;
67
+ }
68
+ }, []);
69
+ return useReducer(timerReducer, init);
70
+ };
@@ -0,0 +1,46 @@
1
+ import { Dispatch } from 'react';
2
+ import { TTimerData, TTimerDataAdd, TTimerDataUpdate } from '../../types/index';
3
+ export declare enum ERtcTimer {
4
+ ADD_RTC_TIMER = "ADD_RTC_TIMER",
5
+ REMOVE_RTC_TIMER = "REMOVE_RTC_TIMER",
6
+ UPDATE_RTC_TIMER = "UPDATE_RTC_TIMER",
7
+ CLOSE_ALL_RTC_TIMER = "CLOSE_ALL_RTC_TIMER",
8
+ FLUSH_RTC_TIMER_LIST = "FLUSH_RTC_TIMER_LIST"
9
+ }
10
+ export declare enum ETimer {
11
+ }
12
+ export declare enum ECouldTimer {
13
+ FLUSH_CLOUD_TIMER_LIST = "FLUSH_CLOUD_TIMER_LIST"
14
+ }
15
+ export interface IState {
16
+ rtcTimerList: TTimerData[];
17
+ cloudTimerList: TTimerData[];
18
+ }
19
+ export interface ILocalTimerReturn {
20
+ type: ERtcTimer;
21
+ payload: any;
22
+ }
23
+ export interface ICloudTimerReturn {
24
+ type: ECouldTimer;
25
+ payload: any;
26
+ }
27
+ export interface ITimerReturn {
28
+ type: ETimer;
29
+ payload: any;
30
+ }
31
+ export type TActions = {
32
+ addRtcTimer: (data: TTimerDataAdd) => ILocalTimerReturn;
33
+ removeRtcTimer: (timerId: string) => ILocalTimerReturn;
34
+ updateRtcTimer: (data: TTimerDataUpdate) => ILocalTimerReturn;
35
+ closeAllRtcTimer: (timerIdList: string[]) => ILocalTimerReturn;
36
+ flushRtcTimerList: (data: TTimerData[]) => ILocalTimerReturn;
37
+ flushCloudTimerList: (data: TTimerData[]) => ICloudTimerReturn;
38
+ };
39
+ export interface IContext {
40
+ state: IState;
41
+ dispatch: Dispatch<{
42
+ type: string;
43
+ payload?: ILocalTimerReturn | ICloudTimerReturn;
44
+ }>;
45
+ actions?: TActions;
46
+ }
@@ -0,0 +1,24 @@
1
+ // 本地定时器类型
2
+ export let ERtcTimer = /*#__PURE__*/function (ERtcTimer) {
3
+ ERtcTimer["ADD_RTC_TIMER"] = "ADD_RTC_TIMER";
4
+ ERtcTimer["REMOVE_RTC_TIMER"] = "REMOVE_RTC_TIMER";
5
+ ERtcTimer["UPDATE_RTC_TIMER"] = "UPDATE_RTC_TIMER";
6
+ ERtcTimer["CLOSE_ALL_RTC_TIMER"] = "CLOSE_ALL_RTC_TIMER";
7
+ ERtcTimer["FLUSH_RTC_TIMER_LIST"] = "FLUSH_RTC_TIMER_LIST";
8
+ return ERtcTimer;
9
+ }({});
10
+
11
+ // 通用定时类型
12
+ export let ETimer = /*#__PURE__*/function (ETimer) {
13
+ return ETimer;
14
+ }({});
15
+
16
+ // 云定时器类型
17
+ export let ECouldTimer = /*#__PURE__*/function (ECouldTimer) {
18
+ ECouldTimer["FLUSH_CLOUD_TIMER_LIST"] = "FLUSH_CLOUD_TIMER_LIST";
19
+ return ECouldTimer;
20
+ }({});
21
+
22
+ // 本地定时器返回
23
+
24
+ // 云定时器返回
@@ -0,0 +1,63 @@
1
+ # 复杂 dp 协议解析
2
+
3
+ ## dp 解析函数
4
+ - 名称:generateDpStrStep
5
+ - 描述: raw 型或 string 型 DP 解析步进函数
6
+
7
+ 请求参数
8
+
9
+ |参数 |数据类型 |说明| 是否必填|
10
+ |--|--|--|--|
11
+ |value| string| 16进制值| 是|
12
+
13
+ 返回参数
14
+
15
+ |参数 |数据类型 |说明|
16
+ |--|--|--|
17
+ result |Function| 步进函数|
18
+
19
+ ```ts
20
+ import { utils } from '@ray-js/panel-sdk';
21
+ const { generateDpStrStep } = utils;
22
+
23
+ generateDpStrStep(value);
24
+ // Example:
25
+ const step = generateDpStrStep('3264');
26
+ const val1 = step().value; // 50
27
+ const val2 = step(2).value; // 100
28
+
29
+ ```
30
+
31
+ ```ts
32
+ // 输出的函数
33
+ class XxxParser {
34
+ constructor() {
35
+ this.dpCode = 'xxx';
36
+ }
37
+ parser(str: string) {
38
+ // 这里是将 dp string 的数据转为对象对数据格式
39
+ // str 为16进制的字符串,输出为10进制的数据
40
+ }
41
+ formatter(data: {[key: string]: any }) {
42
+ // 这里是将 解析的dp数据进行 hex 化,用来下发给设备
43
+ // 最后输出为16进制字符串
44
+ }
45
+ }
46
+
47
+ const xxxParser = new XxxParser();
48
+
49
+ ```
50
+
51
+ ## dp 协议类型
52
+ - raw 类型
53
+ ```ts
54
+
55
+ ```
56
+ - string 类型
57
+
58
+ - value/number 类型
59
+
60
+ - enum 类型
61
+
62
+ - bool 类型
63
+
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,258 @@
1
+ import { cycleParser, getCycleParser } from '../cycle/index';
2
+ // Mock config module
3
+ jest.mock('../../config/dpCodes', () => ({
4
+ scheduleDpCodes: {
5
+ CYCLE_TIMING: 'cycle_timing'
6
+ }
7
+ }));
8
+ const DEFAULT_EMPTY_DATA = {
9
+ version: 0,
10
+ length: 16,
11
+ nodes: []
12
+ };
13
+ describe('CycleTimerParser', () => {
14
+ describe('parser', () => {
15
+ it('should parse a DP string with single node', () => {
16
+ // version=0, length=16
17
+ // powerChannel=0x03 (channel=1, onOff=true), weeks=0x7f (all days)
18
+ // startTime=0x0384 (900), endTime=0x04b0 (1200)
19
+ // startTimeDuration=0x003c (60), endTimeDuration=0x003c (60)
20
+ // color: hue=0x0078 (120), sat=0x64 (100), val=0x32 (50), bright=0x50 (80), temp=0x3c (60)
21
+ const dpValue = '0010037f038404b0003c003c00786432503c';
22
+
23
+ // 获取实际解析结果,用于调试
24
+ const actual = cycleParser.parser(dpValue);
25
+ console.log('Actual parsed result (single node):', JSON.stringify(actual, null, 2));
26
+ expect(cycleParser.parser(dpValue)).toEqual({
27
+ version: 0,
28
+ length: 16,
29
+ nodes: [{
30
+ onOff: true,
31
+ channel: 1,
32
+ loops: '1111111',
33
+ startTime: 900,
34
+ endTime: 1200,
35
+ startTimeDuration: 60,
36
+ endTimeDuration: 60,
37
+ index: 0,
38
+ color: {
39
+ brightness: 80,
40
+ hue: 120,
41
+ saturation: 100,
42
+ temperature: 60,
43
+ value: 50
44
+ }
45
+ }]
46
+ });
47
+ });
48
+ it('should parse a DP string with multiple nodes', () => {
49
+ // version=0, length=16
50
+ // Node 1: as above
51
+ // Node 2: powerChannel=0x05 (channel=2, onOff=true), weeks=0x01 (Sun)
52
+ // startTime=0x0514 (1300), endTime=0x05dc (1500)
53
+ // startTimeDuration=0x001e (30), endTimeDuration=0x001e (30)
54
+ // color: hue=0x0000 (0), sat=0x00 (0), val=0x64 (100), bright=0x64 (100), temp=0x64 (100)
55
+ const dpValue = '0010037f038404b0003c003c00786432503c0501051405dc001e001e000000646464';
56
+
57
+ // 获取实际解析结果,用于调试
58
+ const actual = cycleParser.parser(dpValue);
59
+ console.log('Actual parsed result (multiple nodes):', JSON.stringify(actual, null, 2));
60
+ expect(cycleParser.parser(dpValue)).toEqual({
61
+ version: 0,
62
+ length: 16,
63
+ nodes: [{
64
+ onOff: true,
65
+ channel: 1,
66
+ loops: '1111111',
67
+ startTime: 900,
68
+ endTime: 1200,
69
+ startTimeDuration: 60,
70
+ endTimeDuration: 60,
71
+ index: 0,
72
+ color: {
73
+ hue: 120,
74
+ saturation: 100,
75
+ value: 50,
76
+ brightness: 80,
77
+ temperature: 60
78
+ }
79
+ }, {
80
+ endTime: 1500,
81
+ endTimeDuration: 30,
82
+ index: 1,
83
+ channel: 2,
84
+ loops: '1000000',
85
+ onOff: true,
86
+ startTime: 1300,
87
+ startTimeDuration: 30,
88
+ color: {
89
+ hue: 0,
90
+ saturation: 0,
91
+ value: 100,
92
+ brightness: 100,
93
+ temperature: 100
94
+ }
95
+ }]
96
+ });
97
+ });
98
+ it('should return default data for empty DP string', () => {
99
+ expect(cycleParser.parser('')).toEqual(DEFAULT_EMPTY_DATA);
100
+ });
101
+ it('should return default data for DP string with incorrect length', () => {
102
+ // Valid header, but node data is incomplete
103
+ // Missing last byte
104
+ expect(cycleParser.parser('0010037f038404b0003c003c007864325')).toEqual(DEFAULT_EMPTY_DATA);
105
+ });
106
+ it('should handle power off and specific channel', () => {
107
+ // version=0, length=16
108
+ // Node 1: powerChannel=0x02 (channel=1, onOff=false), weeks=0x40 (Sat)
109
+ // startTime=0x0100 (256), endTime=0x0200 (512)
110
+ // startTimeDuration=0x0014 (20), endTimeDuration=0x001e (30)
111
+ // color: hue=0x0000, sat=0x00, val=0x00, bright=0x00, temp=0x00
112
+ const dpValue = '00100240010002000014001e000000000000';
113
+
114
+ // 获取实际解析结果,用于调试
115
+ const actual = cycleParser.parser(dpValue);
116
+ console.log('Actual parsed result:', JSON.stringify(actual, null, 2));
117
+ expect(cycleParser.parser(dpValue)).toEqual({
118
+ version: 0,
119
+ length: 16,
120
+ nodes: [{
121
+ onOff: false,
122
+ channel: 1,
123
+ loops: '0000001',
124
+ startTime: 256,
125
+ endTime: 512,
126
+ startTimeDuration: 20,
127
+ endTimeDuration: 30,
128
+ index: 0,
129
+ color: {
130
+ hue: 0,
131
+ saturation: 0,
132
+ value: 0,
133
+ brightness: 0,
134
+ temperature: 0
135
+ }
136
+ }]
137
+ });
138
+ });
139
+ });
140
+ describe('formatter', () => {
141
+ it('should format TCycleData into a DP string', () => {
142
+ // version=00, length=10
143
+ // Node 1: powerChannel=03 (channel=1, onOff=true -> 00000011), weeks=7f (1111111 -> 01111111)
144
+ // startTime=0384, endTime=04b0
145
+ // startTimeDuration=003c, endTimeDuration=003c
146
+ // color: hue=0078, sat=64, val=32, bright=50, temp=3c
147
+
148
+ expect(cycleParser.formatter({
149
+ version: 0,
150
+ length: 16,
151
+ // This is fixed in the class, but we set it for clarity
152
+ nodes: [{
153
+ onOff: true,
154
+ channel: 1,
155
+ loops: '1111111',
156
+ startTime: 900,
157
+ endTime: 1200,
158
+ startTimeDuration: 60,
159
+ endTimeDuration: 60,
160
+ index: 0,
161
+ color: {
162
+ hue: 120,
163
+ saturation: 100,
164
+ value: 50,
165
+ brightness: 80,
166
+ temperature: 60
167
+ }
168
+ }]
169
+ })).toBe('0010037f038404b0003c003c00786432503c');
170
+ });
171
+ it('should format TCycleData with multiple nodes', () => {
172
+ // Node1: 037f038404b0003c003c00786432503c
173
+ // Node2: powerChannel=05 (channel=2, onOff=true -> 00000101), weeks=01 (1000000 -> 00000001)
174
+ // startTime=0514, endTime=05dc
175
+ // startTimeDuration=001e, endTimeDuration=001e
176
+ // color: hue=0000, sat=00, val=64, bright=64, temp=64
177
+
178
+ expect(cycleParser.formatter({
179
+ version: 0,
180
+ length: 16,
181
+ nodes: [{
182
+ onOff: true,
183
+ channel: 1,
184
+ loops: '1111111',
185
+ startTime: 900,
186
+ endTime: 1200,
187
+ startTimeDuration: 60,
188
+ endTimeDuration: 60,
189
+ index: 0,
190
+ color: {
191
+ hue: 120,
192
+ saturation: 100,
193
+ value: 50,
194
+ brightness: 80,
195
+ temperature: 60
196
+ }
197
+ }, {
198
+ onOff: true,
199
+ channel: 2,
200
+ loops: '1000000',
201
+ // Sunday
202
+ startTime: 1300,
203
+ endTime: 1500,
204
+ startTimeDuration: 30,
205
+ endTimeDuration: 30,
206
+ index: 1,
207
+ color: {
208
+ hue: 0,
209
+ saturation: 0,
210
+ value: 100,
211
+ brightness: 100,
212
+ temperature: 100
213
+ }
214
+ }]
215
+ })).toBe('0010037f038404b0003c003c00786432503c0501051405dc001e001e000000646464');
216
+ });
217
+ it('should format TCycleData with empty nodes array', () => {
218
+ // version=01, length=10
219
+ expect(cycleParser.formatter({
220
+ version: 1,
221
+ length: 16,
222
+ nodes: []
223
+ })).toBe('0110');
224
+ });
225
+ it('should handle channel 0 (default to 1 in formatter)', () => {
226
+ // powerChannel=03 (channel=1, onOff=true), weeks=40 (0000001 -> 01000000)
227
+
228
+ expect(cycleParser.formatter({
229
+ version: 0,
230
+ length: 16,
231
+ nodes: [{
232
+ onOff: true,
233
+ channel: 0,
234
+ // will be treated as 1
235
+ loops: '0000001',
236
+ // Saturday
237
+ startTime: 100,
238
+ endTime: 200,
239
+ startTimeDuration: 10,
240
+ endTimeDuration: 10,
241
+ index: 0,
242
+ color: {
243
+ hue: 0,
244
+ saturation: 0,
245
+ value: 0,
246
+ brightness: 0,
247
+ temperature: 0
248
+ }
249
+ }]
250
+ })).toBe('00100340006400c8000a000a000000000000');
251
+ });
252
+ });
253
+ describe('getCycleParser', () => {
254
+ it('should return the cycleParser instance', () => {
255
+ expect(getCycleParser()).toBe(cycleParser);
256
+ });
257
+ });
258
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,211 @@
1
+ import { randomParser } from '../random/index';
2
+ const DEFAULT_EMPTY_DATA = {
3
+ version: 0,
4
+ length: 12,
5
+ nodes: []
6
+ };
7
+ describe('RandomTimerParser', () => {
8
+ describe('parser', () => {
9
+ it('should parse a valid DP string into TRandomData', () => {
10
+ // version=0, length=12 (0x0c)
11
+ // Node 1: powerChannel=0x00 (channel=0, onOff=false), weeks=0x7f (all days), startTime=0x0384 (900), endTime=0x04b0 (1200)
12
+ // color: hue=0x0078 (120), sat=0x64 (100), val=0x32 (50), bright=0x50 (80), temp=0x3c (60)
13
+
14
+ expect(randomParser.parser('000c007f038404b000786432503c')).toEqual({
15
+ version: 0,
16
+ length: 12,
17
+ nodes: [{
18
+ onOff: false,
19
+ channel: 0,
20
+ loops: '1111111',
21
+ startTime: 900,
22
+ endTime: 1200,
23
+ index: 0,
24
+ color: {
25
+ hue: 120,
26
+ saturation: 100,
27
+ value: 50,
28
+ brightness: 80,
29
+ temperature: 60
30
+ }
31
+ }]
32
+ });
33
+ });
34
+ it('should parse a DP string with multiple nodes', () => {
35
+ // version=0, length=12
36
+ // Node 1: as above
37
+ // Node 2: powerChannel=0x05 (channel=2, onOff=true), weeks=0x01 (Sun), startTime=0x0514 (1300), endTime=0x05dc (1500)
38
+ // color: hue=0x0000 (0), sat=0x00 (0), val=0x64 (100), bright=0x64 (100), temp=0x64 (100)
39
+
40
+ expect(randomParser.parser('000c037f038404b000786432503c0501051405dc000000646464')).toEqual({
41
+ version: 0,
42
+ length: 12,
43
+ nodes: [{
44
+ onOff: true,
45
+ channel: 1,
46
+ loops: '1111111',
47
+ startTime: 900,
48
+ endTime: 1200,
49
+ index: 0,
50
+ color: {
51
+ hue: 120,
52
+ saturation: 100,
53
+ value: 50,
54
+ brightness: 80,
55
+ temperature: 60
56
+ }
57
+ }, {
58
+ onOff: true,
59
+ channel: 2,
60
+ loops: '1000000',
61
+ startTime: 1300,
62
+ endTime: 1500,
63
+ index: 1,
64
+ color: {
65
+ hue: 0,
66
+ saturation: 0,
67
+ value: 100,
68
+ brightness: 100,
69
+ temperature: 100
70
+ }
71
+ }]
72
+ });
73
+ });
74
+ it('should return default data for empty DP string', () => {
75
+ expect(randomParser.parser('')).toEqual(DEFAULT_EMPTY_DATA);
76
+ });
77
+ it('should return default data for DP string with incorrect length', () => {
78
+ // Valid header, but node data is incomplete
79
+ // Missing last byte
80
+ expect(randomParser.parser('000c037f038404b00078643250')).toEqual(DEFAULT_EMPTY_DATA);
81
+ });
82
+ it('should handle power off and specific channel', () => {
83
+ // version=0, length=12
84
+ // Node 1: powerChannel=0x02 (channel=1, onOff=false), weeks=0x40 (Sat), startTime=0x0100 (256), endTime=0x0200 (512)
85
+ // color: hue=0x0000, sat=0x00, val=0x00, bright=0x00, temp=0x00
86
+
87
+ expect(randomParser.parser('000c024001000200000000000000')).toEqual({
88
+ version: 0,
89
+ length: 12,
90
+ nodes: [{
91
+ onOff: false,
92
+ channel: 1,
93
+ loops: '0000001',
94
+ startTime: 256,
95
+ endTime: 512,
96
+ index: 0,
97
+ color: {
98
+ hue: 0,
99
+ saturation: 0,
100
+ value: 0,
101
+ brightness: 0,
102
+ temperature: 0
103
+ }
104
+ }]
105
+ });
106
+ });
107
+ });
108
+ describe('formatter', () => {
109
+ it('should format TRandomData into a DP string', () => {
110
+ // version=00, length=0c
111
+ // Node 1: powerChannel=03 (channel=1, onOff=true -> 00000011), weeks=7f (1111111 -> 01111111)
112
+ // startTime=0384, endTime=04b0
113
+ // color: hue=0078, sat=64, val=32, bright=50, temp=3c
114
+
115
+ expect(randomParser.formatter({
116
+ version: 0,
117
+ length: 12,
118
+ // This is fixed in the class, but we set it for clarity
119
+ nodes: [{
120
+ onOff: true,
121
+ channel: 1,
122
+ loops: '1111111',
123
+ startTime: 900,
124
+ endTime: 1200,
125
+ index: 0,
126
+ color: {
127
+ hue: 120,
128
+ saturation: 100,
129
+ value: 50,
130
+ brightness: 80,
131
+ temperature: 60
132
+ }
133
+ }]
134
+ })).toBe('000c037f038404b000786432503c');
135
+ });
136
+ it('should format TRandomData with multiple nodes', () => {
137
+ // Node1: 037f038404b000786432503c
138
+ // Node2: powerChannel=05 (channel=2, onOff=true -> 00000101), weeks=01 (1000000 -> 00000001)
139
+ // startTime=0514, endTime=05dc
140
+ // color: hue=0000, sat=00, val=64, bright=64, temp=64
141
+
142
+ expect(randomParser.formatter({
143
+ version: 0,
144
+ length: 12,
145
+ nodes: [{
146
+ onOff: true,
147
+ channel: 1,
148
+ loops: '1111111',
149
+ startTime: 900,
150
+ endTime: 1200,
151
+ index: 0,
152
+ color: {
153
+ hue: 120,
154
+ saturation: 100,
155
+ value: 50,
156
+ brightness: 80,
157
+ temperature: 60
158
+ }
159
+ }, {
160
+ onOff: true,
161
+ channel: 2,
162
+ loops: '1000000',
163
+ // Sunday
164
+ startTime: 1300,
165
+ endTime: 1500,
166
+ index: 1,
167
+ color: {
168
+ hue: 0,
169
+ saturation: 0,
170
+ value: 100,
171
+ brightness: 100,
172
+ temperature: 100
173
+ }
174
+ }]
175
+ })).toBe('000c037f038404b000786432503c0501051405dc000000646464');
176
+ });
177
+ it('should format TRandomData with empty nodes array', () => {
178
+ // version=01, length=0c
179
+ expect(randomParser.formatter({
180
+ version: 1,
181
+ length: 12,
182
+ nodes: []
183
+ })).toBe('010c');
184
+ });
185
+ it('should handle channel 0 (default to 1 in formatter)', () => {
186
+ // powerChannel=03 (channel=1, onOff=true), weeks=40 (0000001 -> 01000000)
187
+
188
+ expect(randomParser.formatter({
189
+ version: 0,
190
+ length: 12,
191
+ nodes: [{
192
+ onOff: true,
193
+ channel: 0,
194
+ // will be treated as 1
195
+ loops: '0000001',
196
+ // Saturday
197
+ startTime: 100,
198
+ endTime: 200,
199
+ index: 0,
200
+ color: {
201
+ hue: 0,
202
+ saturation: 0,
203
+ value: 0,
204
+ brightness: 0,
205
+ temperature: 0
206
+ }
207
+ }]
208
+ })).toBe('000c0340006400c8000000000000');
209
+ });
210
+ });
211
+ });
@@ -0,0 +1 @@
1
+ export {};