@ray-js/robot-data-stream 0.0.14 → 0.0.15-beta.10

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 (157) hide show
  1. package/lib/api/sweeperP2p.js +1 -1
  2. package/lib/mqtt/createCommonOptions.d.ts +4 -2
  3. package/lib/mqtt/createCommonOptions.js +10 -5
  4. package/lib/mqtt/hooks/useStructuredMessage.d.ts +17 -0
  5. package/lib/mqtt/hooks/useStructuredMessage.js +206 -0
  6. package/lib/mqtt/mqttProvider.d.ts +28 -1
  7. package/lib/mqtt/mqttProvider.js +72 -9
  8. package/lib/mqtt/promise.d.ts +57 -4
  9. package/lib/mqtt/promise.js +115 -36
  10. package/lib/mqtt/type/fun.d.ts +233 -0
  11. package/lib/mqtt/type/fun.js +145 -0
  12. package/lib/mqtt/type/index.d.ts +0 -7
  13. package/lib/mqtt/type/index.js +0 -1
  14. package/lib/mqtt/type/protocols/base.d.ts +21 -0
  15. package/lib/mqtt/type/protocols/base.js +32 -0
  16. package/lib/mqtt/type/protocols/carpetCleanProtocol.d.ts +14 -0
  17. package/lib/mqtt/type/protocols/carpetCleanProtocol.js +14 -0
  18. package/lib/mqtt/type/protocols/carpetProtocol.d.ts +102 -0
  19. package/lib/mqtt/type/protocols/carpetProtocol.js +202 -0
  20. package/lib/mqtt/type/protocols/devInfoProtocol.d.ts +11 -0
  21. package/lib/mqtt/type/protocols/devInfoProtocol.js +10 -0
  22. package/lib/mqtt/type/protocols/deviceModelProtocol.d.ts +31 -0
  23. package/lib/mqtt/type/protocols/deviceModelProtocol.js +32 -0
  24. package/lib/mqtt/type/protocols/furnitureModelProtocol.d.ts +27 -0
  25. package/lib/mqtt/type/protocols/furnitureModelProtocol.js +27 -0
  26. package/lib/mqtt/type/protocols/historyMapProtocol.d.ts +39 -0
  27. package/lib/mqtt/type/protocols/historyMapProtocol.js +37 -0
  28. package/lib/mqtt/type/protocols/index.d.ts +20 -0
  29. package/lib/mqtt/type/protocols/index.js +20 -0
  30. package/lib/mqtt/type/protocols/partDivisionProtocol.d.ts +20 -0
  31. package/lib/mqtt/type/protocols/partDivisionProtocol.js +12 -0
  32. package/lib/mqtt/type/protocols/partMergeProtocol.d.ts +13 -0
  33. package/lib/mqtt/type/protocols/partMergeProtocol.js +12 -0
  34. package/lib/mqtt/type/protocols/passwordProtocol.d.ts +24 -0
  35. package/lib/mqtt/type/protocols/passwordProtocol.js +25 -0
  36. package/lib/mqtt/type/protocols/quietHoursProtocol.d.ts +29 -0
  37. package/lib/mqtt/type/protocols/quietHoursProtocol.js +35 -0
  38. package/lib/mqtt/type/protocols/resetMapProtocol.d.ts +4 -0
  39. package/lib/mqtt/type/protocols/resetMapProtocol.js +1 -0
  40. package/lib/mqtt/type/protocols/roomPropertyProtocol.d.ts +42 -0
  41. package/lib/mqtt/type/protocols/roomPropertyProtocol.js +113 -0
  42. package/lib/mqtt/type/protocols/scheduleProtocol.d.ts +62 -0
  43. package/lib/mqtt/type/protocols/scheduleProtocol.js +100 -0
  44. package/lib/mqtt/type/protocols/selectRoomCleanProtocol.d.ts +27 -0
  45. package/lib/mqtt/type/protocols/selectRoomCleanProtocol.js +40 -0
  46. package/lib/mqtt/type/protocols/spotCleanProtocol.d.ts +38 -0
  47. package/lib/mqtt/type/protocols/spotCleanProtocol.js +72 -0
  48. package/lib/mqtt/type/protocols/virtualAreaProtocol.d.ts +32 -0
  49. package/lib/mqtt/type/protocols/virtualAreaProtocol.js +36 -0
  50. package/lib/mqtt/type/protocols/virtualWallProtocol.d.ts +27 -0
  51. package/lib/mqtt/type/protocols/virtualWallProtocol.js +25 -0
  52. package/lib/mqtt/type/protocols/voiceProtocol.d.ts +21 -0
  53. package/lib/mqtt/type/protocols/voiceProtocol.js +23 -0
  54. package/lib/mqtt/type/protocols/wifiMapProtocol.d.ts +16 -0
  55. package/lib/mqtt/type/protocols/wifiMapProtocol.js +14 -0
  56. package/lib/mqtt/type/protocols/zoneCleanProtocol.d.ts +47 -0
  57. package/lib/mqtt/type/protocols/zoneCleanProtocol.js +49 -0
  58. package/lib/mqtt/useCarpet.d.ts +7 -67
  59. package/lib/mqtt/useCarpet.js +101 -181
  60. package/lib/mqtt/useCarpetClean.d.ts +2 -11
  61. package/lib/mqtt/useCarpetClean.js +26 -50
  62. package/lib/mqtt/useDevInfo.d.ts +8 -6
  63. package/lib/mqtt/useDevInfo.js +44 -36
  64. package/lib/mqtt/useDeviceModel.d.ts +6 -19
  65. package/lib/mqtt/useDeviceModel.js +31 -42
  66. package/lib/mqtt/useFurnitureModel.d.ts +6 -18
  67. package/lib/mqtt/useFurnitureModel.js +31 -43
  68. package/lib/mqtt/useHistoryMap.d.ts +2 -18
  69. package/lib/mqtt/useHistoryMap.js +102 -119
  70. package/lib/mqtt/usePartDivision.d.ts +2 -9
  71. package/lib/mqtt/usePartDivision.js +48 -57
  72. package/lib/mqtt/usePartMerge.d.ts +2 -8
  73. package/lib/mqtt/usePartMerge.js +34 -49
  74. package/lib/mqtt/usePassword.d.ts +6 -13
  75. package/lib/mqtt/usePassword.js +84 -74
  76. package/lib/mqtt/useQuiteHours.d.ts +4 -37
  77. package/lib/mqtt/useQuiteHours.js +66 -100
  78. package/lib/mqtt/useResetMap.d.ts +2 -8
  79. package/lib/mqtt/useResetMap.js +27 -34
  80. package/lib/mqtt/useRoomProperty.d.ts +8 -9
  81. package/lib/mqtt/useRoomProperty.js +67 -65
  82. package/lib/mqtt/useSchedule.d.ts +3 -23
  83. package/lib/mqtt/useSchedule.js +72 -101
  84. package/lib/mqtt/useSelectRoomClean.d.ts +3 -24
  85. package/lib/mqtt/useSelectRoomClean.js +107 -133
  86. package/lib/mqtt/useSpotClean.d.ts +5 -15
  87. package/lib/mqtt/useSpotClean.js +68 -121
  88. package/lib/mqtt/useVirtualArea.d.ts +6 -17
  89. package/lib/mqtt/useVirtualArea.js +109 -119
  90. package/lib/mqtt/useVirtualWall.d.ts +2 -18
  91. package/lib/mqtt/useVirtualWall.js +69 -90
  92. package/lib/mqtt/useVoice.d.ts +2 -16
  93. package/lib/mqtt/useVoice.js +48 -68
  94. package/lib/mqtt/useWifiMap.d.ts +6 -5
  95. package/lib/mqtt/useWifiMap.js +43 -42
  96. package/lib/mqtt/useZoneClean.d.ts +2 -21
  97. package/lib/mqtt/useZoneClean.js +125 -137
  98. package/lib/myLib/zod/mini/index.d.cts +1 -0
  99. package/lib/myLib/zod/mini/index.d.ts +1 -0
  100. package/lib/myLib/zod/mini/index.js +1 -0
  101. package/lib/myLib/zod/mini/package.json +6 -0
  102. package/lib/myLib/zod/src/mini/index.d.ts +1 -0
  103. package/lib/myLib/zod/src/mini/index.js +1 -0
  104. package/lib/myLib/zod/src/v4/core/api.d.ts +306 -0
  105. package/lib/myLib/zod/src/v4/core/api.js +1256 -0
  106. package/lib/myLib/zod/src/v4/core/checks.d.ts +278 -0
  107. package/lib/myLib/zod/src/v4/core/checks.js +816 -0
  108. package/lib/myLib/zod/src/v4/core/config.d.ts +9 -0
  109. package/lib/myLib/zod/src/v4/core/config.js +5 -0
  110. package/lib/myLib/zod/src/v4/core/core.d.ts +70 -0
  111. package/lib/myLib/zod/src/v4/core/core.js +95 -0
  112. package/lib/myLib/zod/src/v4/core/doc.d.ts +14 -0
  113. package/lib/myLib/zod/src/v4/core/doc.js +42 -0
  114. package/lib/myLib/zod/src/v4/core/errors.d.ts +220 -0
  115. package/lib/myLib/zod/src/v4/core/errors.js +232 -0
  116. package/lib/myLib/zod/src/v4/core/index.d.ts +15 -0
  117. package/lib/myLib/zod/src/v4/core/index.js +15 -0
  118. package/lib/myLib/zod/src/v4/core/json-schema-generator.d.ts +65 -0
  119. package/lib/myLib/zod/src/v4/core/json-schema-generator.js +120 -0
  120. package/lib/myLib/zod/src/v4/core/json-schema-processors.d.ts +49 -0
  121. package/lib/myLib/zod/src/v4/core/json-schema-processors.js +593 -0
  122. package/lib/myLib/zod/src/v4/core/json-schema.d.ts +88 -0
  123. package/lib/myLib/zod/src/v4/core/json-schema.js +1 -0
  124. package/lib/myLib/zod/src/v4/core/parse.d.ts +49 -0
  125. package/lib/myLib/zod/src/v4/core/parse.js +147 -0
  126. package/lib/myLib/zod/src/v4/core/regexes.d.ts +78 -0
  127. package/lib/myLib/zod/src/v4/core/regexes.js +146 -0
  128. package/lib/myLib/zod/src/v4/core/registries.d.ts +35 -0
  129. package/lib/myLib/zod/src/v4/core/registries.js +51 -0
  130. package/lib/myLib/zod/src/v4/core/schemas.d.ts +1136 -0
  131. package/lib/myLib/zod/src/v4/core/schemas.js +2745 -0
  132. package/lib/myLib/zod/src/v4/core/standard-schema.d.ts +126 -0
  133. package/lib/myLib/zod/src/v4/core/standard-schema.js +1 -0
  134. package/lib/myLib/zod/src/v4/core/to-json-schema.d.ts +114 -0
  135. package/lib/myLib/zod/src/v4/core/to-json-schema.js +488 -0
  136. package/lib/myLib/zod/src/v4/core/util.d.ts +199 -0
  137. package/lib/myLib/zod/src/v4/core/util.js +656 -0
  138. package/lib/myLib/zod/src/v4/core/versions.d.ts +5 -0
  139. package/lib/myLib/zod/src/v4/core/versions.js +5 -0
  140. package/lib/myLib/zod/src/v4/core/zsf.d.ts +91 -0
  141. package/lib/myLib/zod/src/v4/core/zsf.js +1 -0
  142. package/lib/myLib/zod/src/v4/mini/checks.d.ts +1 -0
  143. package/lib/myLib/zod/src/v4/mini/checks.js +1 -0
  144. package/lib/myLib/zod/src/v4/mini/coerce.d.ts +7 -0
  145. package/lib/myLib/zod/src/v4/mini/coerce.js +27 -0
  146. package/lib/myLib/zod/src/v4/mini/external.d.ts +11 -0
  147. package/lib/myLib/zod/src/v4/mini/external.js +16 -0
  148. package/lib/myLib/zod/src/v4/mini/index.d.ts +530 -0
  149. package/lib/myLib/zod/src/v4/mini/index.js +30 -0
  150. package/lib/myLib/zod/src/v4/mini/iso.d.ts +22 -0
  151. package/lib/myLib/zod/src/v4/mini/iso.js +46 -0
  152. package/lib/myLib/zod/src/v4/mini/parse.d.ts +1 -0
  153. package/lib/myLib/zod/src/v4/mini/parse.js +1 -0
  154. package/lib/myLib/zod/src/v4/mini/schemas.js +1244 -0
  155. package/lib/utils/index.d.ts +1 -0
  156. package/lib/utils/index.js +2 -1
  157. package/package.json +5 -3
@@ -1,60 +1,53 @@
1
1
  // 重置地图
2
2
  import { useContext } from 'react';
3
- import { createSetCommonParams } from './createCommonOptions';
4
- import { normalResolve } from './promise';
5
3
  import { ResetCurrMapEnum } from './type';
6
4
  import { SingletonContext } from './mqttProvider';
7
5
  import { encodeResetMap0x42 } from '@ray-js/robot-protocol';
8
-
9
- // 重置地图响应类型
10
-
11
- // 设置重置地图函数类型定义
12
-
6
+ import { useStructuredMessage, handleMqttError } from './hooks/useStructuredMessage';
13
7
  /**
14
8
  * 自定义 Hook,用于重置地图
15
- * @param devId 设备ID
16
9
  * @returns 包含 setResetMap 函数的对象
17
10
  */
18
- export const useResetMap = devId => {
11
+ export const useResetMap = () => {
19
12
  const {
20
13
  useMqtt,
21
14
  devices,
22
15
  commandVersion
23
16
  } = useContext(SingletonContext);
17
+ const {
18
+ sendStructuredMessage
19
+ } = useStructuredMessage();
24
20
  /**
25
21
  * 重置地图
22
+ * @param data 可选参数(目前为空对象)
26
23
  * @returns Promise<ResetMapResponse> 响应结果
27
24
  */
28
25
 
29
26
  return {
30
27
  setResetMap: () => {
31
- try {
32
- if (useMqtt) {
33
- const params = createSetCommonParams({
34
- deviceId: devId,
35
- reqType: ResetCurrMapEnum.set
28
+ return new Promise((resolve, reject) => {
29
+ try {
30
+ if (useMqtt) {
31
+ sendStructuredMessage(ResetCurrMapEnum.set, {}, resolve, error => reject(handleMqttError(error, 'Failed to reset map')), ResetCurrMapEnum.query // 响应类型是 query,不是 set
32
+ );
33
+ return;
34
+ }
35
+
36
+ // 非 MQTT 模式:使用命令传输
37
+ devices.common.model.actions.command_trans.set(encodeResetMap0x42({
38
+ version: commandVersion
39
+ }));
40
+ resolve({
41
+ success: true,
42
+ errCode: 0,
43
+ reqType: ResetCurrMapEnum.query,
44
+ version: commandVersion,
45
+ taskId: `${Date.now()}`
36
46
  });
37
- ty.device.sendMqttMessage(params);
38
- const {
39
- taskId
40
- } = params.message;
41
- return normalResolve(ResetCurrMapEnum.query, taskId);
47
+ } catch (error) {
48
+ reject(handleMqttError(error, 'Failed to reset map'));
42
49
  }
43
-
44
- // 非 MQTT 模式:使用命令传输
45
- devices.common.model.actions.command_trans.set(encodeResetMap0x42({
46
- version: commandVersion
47
- }));
48
- return Promise.resolve({
49
- success: true,
50
- errCode: 0,
51
- reqType: ResetCurrMapEnum.query,
52
- version: commandVersion,
53
- taskId: `${Date.now()}`
54
- });
55
- } catch (error) {
56
- throw error instanceof Error ? error : new Error('Failed to reset map');
57
- }
50
+ });
58
51
  }
59
52
  };
60
53
  };
@@ -1,11 +1,10 @@
1
- import { BaseResponse, RoomPreference } from './type';
2
- type Response = BaseResponse & RoomPreference & {
3
- num: number;
4
- mapId: number;
5
- };
6
- type TSetRoomProperty = (message: Pick<Response, 'num' | 'suctions' | 'cisterns' | 'floorTypes' | 'names' | 'orders' | 'yMops' | 'sweepMopModes' | 'cleanCounts' | 'ids'>) => Promise<RoomPreference>;
7
- export declare const useRoomProperty: (devId: string) => {
8
- requestRoomProperty: () => Promise<Response>;
1
+ import { type TRequestRoomProperty, type TSetRoomProperty } from './type/protocols/roomPropertyProtocol';
2
+ /**
3
+ * 自定义 Hook,用于房间属性管理
4
+ * @param devId 设备ID(可选,如果不提供则从 MqttProvider Context 获取)
5
+ * @returns 包含 requestRoomProperty 和 setRoomProperty 函数的对象
6
+ */
7
+ export declare const useRoomProperty: () => {
8
+ requestRoomProperty: TRequestRoomProperty;
9
9
  setRoomProperty: TSetRoomProperty;
10
10
  };
11
- export {};
@@ -1,80 +1,82 @@
1
- import { isArray } from 'lodash-es';
2
- import { createSetCommonParams } from './createCommonOptions';
3
- import { normalResolve } from './promise';
4
- import { RoomPropertyEnum } from './type';
5
1
  import { useContext } from 'react';
2
+ import { handleMqttError, useStructuredMessage } from './hooks/useStructuredMessage';
6
3
  import { SingletonContext } from './mqttProvider';
7
- export const useRoomProperty = devId => {
4
+ import { RoomPropertyEnum } from './type';
5
+ import { validator } from './type/fun';
6
+ /**
7
+ * 自定义 Hook,用于房间属性管理
8
+ * @param devId 设备ID(可选,如果不提供则从 MqttProvider Context 获取)
9
+ * @returns 包含 requestRoomProperty 和 setRoomProperty 函数的对象
10
+ */
11
+ export const useRoomProperty = () => {
8
12
  const {
9
13
  useMqtt
10
14
  } = useContext(SingletonContext);
15
+ const {
16
+ sendStructuredMessage
17
+ } = useStructuredMessage();
11
18
 
12
- // 请求房间属性
19
+ /**
20
+ * 请求房间属性
21
+ * @param data 可选参数(目前为空对象)
22
+ * @returns Promise<RoomPropertyResponse> 响应结果
23
+ */
13
24
 
14
- // 设置房间属性
25
+ /**
26
+ * 设置房间属性
27
+ * @param data 包含房间属性设置参数的对象
28
+ * @returns Promise<RoomPropertyResponse> 响应结果
29
+ */
15
30
 
16
31
  return {
17
32
  requestRoomProperty: () => {
18
- if (!useMqtt) return Promise.reject(new Error('useMqtt is not used'));
19
- const params = createSetCommonParams({
20
- deviceId: devId,
21
- reqType: RoomPropertyEnum.query
33
+ return new Promise((resolve, reject) => {
34
+ try {
35
+ if (!useMqtt) {
36
+ reject(new Error('useMqtt is not used'));
37
+ return;
38
+ }
39
+ sendStructuredMessage(RoomPropertyEnum.query, {}, resolve, error => reject(handleMqttError(error, 'Failed to request room property')));
40
+ } catch (error) {
41
+ reject(handleMqttError(error, 'Failed to request room property'));
42
+ }
22
43
  });
23
- ty.device.sendMqttMessage(params);
24
- return normalResolve(RoomPropertyEnum.query, params.message.taskId);
25
44
  },
26
- setRoomProperty: message => {
27
- // 参数验证
28
- const {
29
- ids
30
- } = message;
31
- if (!isArray(ids) || isArray(ids) && ids.length === 0) {
32
- throw new Error('ids is required and must be a non-empty array');
33
- }
34
- const len = ids.length;
35
- const {
36
- suctions = new Array(len).fill('closed'),
37
- cisterns = new Array(len).fill('closed'),
38
- cleanCounts = new Array(len).fill(1),
39
- yMops = new Array(len).fill(-1),
40
- sweepMopModes = new Array(len).fill('only_sweep'),
41
- names = new Array(len).fill('')
42
- } = message;
43
- if (suctions && !isArray(suctions)) {
44
- throw new Error('suctions is illegal');
45
- }
46
- if (cisterns && !isArray(cisterns)) {
47
- throw new Error('cisterns is illegal');
48
- }
49
- if (cleanCounts && !isArray(cleanCounts)) {
50
- throw new Error('cleanCounts is illegal');
51
- }
52
- if (yMops && !isArray(yMops)) {
53
- throw new Error('yMops is illegal');
54
- }
55
- if (sweepMopModes && !isArray(sweepMopModes)) {
56
- throw new Error('sweepMopModes is illegal');
57
- }
58
- if (names && !isArray(names)) {
59
- throw new Error('names is illegal');
60
- }
61
- if (len !== suctions.length || len !== cisterns.length || len !== cleanCounts.length || len !== yMops.length || len !== sweepMopModes.length || len !== names.length) {
62
- throw new Error('The length of the parameters is inconsistent');
63
- }
64
- try {
65
- const params = createSetCommonParams({
66
- deviceId: devId,
67
- reqType: RoomPropertyEnum.set,
68
- message
69
- });
70
- ty.device.sendMqttMessage(params);
71
- const {
72
- taskId
73
- } = params.message;
74
- return normalResolve(RoomPropertyEnum.query, taskId);
75
- } catch (error) {
76
- throw error instanceof Error ? error : new Error('Failed to set room property');
77
- }
45
+ setRoomProperty: data => {
46
+ return new Promise((resolve, reject) => {
47
+ try {
48
+ var _data$num, _data$suctions, _data$cisterns, _data$cleanCounts, _data$yMops, _data$sweepMopModes, _data$names;
49
+ if (!useMqtt) {
50
+ reject(new Error('useMqtt is not used'));
51
+ return;
52
+ }
53
+
54
+ // 构建发送数据,为可选字段设置默认值
55
+ const {
56
+ ids
57
+ } = data;
58
+ const len = ids.length;
59
+ const sendData = {
60
+ num: (_data$num = data.num) !== null && _data$num !== void 0 ? _data$num : len,
61
+ ids,
62
+ suctions: (_data$suctions = data.suctions) !== null && _data$suctions !== void 0 ? _data$suctions : new Array(len).fill('closed'),
63
+ cisterns: (_data$cisterns = data.cisterns) !== null && _data$cisterns !== void 0 ? _data$cisterns : new Array(len).fill('closed'),
64
+ cleanCounts: (_data$cleanCounts = data.cleanCounts) !== null && _data$cleanCounts !== void 0 ? _data$cleanCounts : new Array(len).fill(1),
65
+ yMops: (_data$yMops = data.yMops) !== null && _data$yMops !== void 0 ? _data$yMops : new Array(len).fill(-1),
66
+ sweepMopModes: (_data$sweepMopModes = data.sweepMopModes) !== null && _data$sweepMopModes !== void 0 ? _data$sweepMopModes : new Array(len).fill('only_sweep'),
67
+ names: (_data$names = data.names) !== null && _data$names !== void 0 ? _data$names : new Array(len).fill(''),
68
+ floorTypes: data.floorTypes,
69
+ orders: data.orders
70
+ };
71
+
72
+ // 使用 TypeBox Schema 进行参数验证(包含自定义验证逻辑)
73
+ const validatedData = validator.validate('set_room_property', sendData);
74
+ sendStructuredMessage(RoomPropertyEnum.set, validatedData, resolve, error => reject(handleMqttError(error, 'Failed to set room property')), RoomPropertyEnum.query // 响应类型是 query,不是 set
75
+ );
76
+ } catch (error) {
77
+ reject(handleMqttError(error, 'Failed to set room property'));
78
+ }
79
+ });
78
80
  }
79
81
  };
80
82
  };
@@ -1,30 +1,10 @@
1
- import { ScheduleEnum, RoomPreference, BaseResponse } from './type';
2
- type ScheduleItem = Pick<RoomPreference, 'suctions' | 'cisterns' | 'cleanCounts' | 'yMops' | 'sweepMopModes' | 'ids'> & {
3
- active: number;
4
- cycle: number[];
5
- time: number[];
6
- };
7
- interface Schedule {
8
- list: Array<ScheduleItem>;
9
- num: number;
10
- version?: string;
11
- }
12
- interface ScheduleResponse extends BaseResponse {
13
- reqType: ScheduleEnum.query;
14
- list: Array<ScheduleItem>;
15
- num: number;
16
- }
17
- type TRequestSchedule = (message?: {
18
- version?: string;
19
- }) => Promise<ScheduleResponse>;
20
- type TSetSchedule = (message: Schedule) => Promise<ScheduleResponse>;
1
+ import { type TRequestSchedule, type TSetSchedule } from './type/protocols';
21
2
  /**
22
3
  * 自定义 Hook,用于定时任务
23
- * @param devId 设备ID
4
+ * @param devId 设备ID(可选,如果不提供则从 MqttProvider Context 获取)
24
5
  * @returns 包含 requestSchedule 和 setSchedule 函数的对象
25
6
  */
26
- export declare const useSchedule: (devId: string) => {
7
+ export declare const useSchedule: () => {
27
8
  requestSchedule: TRequestSchedule;
28
9
  setSchedule: TSetSchedule;
29
10
  };
30
- export {};
@@ -1,34 +1,26 @@
1
1
  import "core-js/modules/esnext.iterator.map.js";
2
2
  // 定时
3
- import { useContext } from 'react';
4
- import { createSetCommonParams } from './createCommonOptions';
5
- import { normalResolve } from './promise';
6
- import { ScheduleEnum } from './type';
7
- import { SingletonContext } from './mqttProvider';
8
3
  import { encodeDeviceTimer0x30 } from '@ray-js/robot-protocol';
4
+ import { useContext } from 'react';
9
5
  import { CISTERN_MAP, CLEAN_MODE_MAP, SUCTION_MAP } from '../constant';
10
-
11
- // 定时任务项
12
-
13
- // 定时任务数据
14
-
15
- // 定时任务响应类型
16
-
17
- // 请求定时数据函数类型定义
18
-
19
- // 设置定时数据函数类型定义
20
-
6
+ import { handleMqttError, useStructuredMessage } from './hooks/useStructuredMessage';
7
+ import { SingletonContext } from './mqttProvider';
8
+ import { ScheduleEnum } from './type';
9
+ import { validator } from './type/fun';
21
10
  /**
22
11
  * 自定义 Hook,用于定时任务
23
- * @param devId 设备ID
12
+ * @param devId 设备ID(可选,如果不提供则从 MqttProvider Context 获取)
24
13
  * @returns 包含 requestSchedule 和 setSchedule 函数的对象
25
14
  */
26
- export const useSchedule = devId => {
15
+ export const useSchedule = () => {
27
16
  const {
28
17
  useMqtt,
29
18
  devices,
30
19
  commandVersion
31
20
  } = useContext(SingletonContext);
21
+ const {
22
+ sendStructuredMessage
23
+ } = useStructuredMessage();
32
24
  /**
33
25
  * 请求定时数据
34
26
  * @param message 可选参数,包含版本号
@@ -46,93 +38,72 @@ export const useSchedule = devId => {
46
38
  let message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
47
39
  version: '1.0.0'
48
40
  };
49
- if (!useMqtt) {
50
- return Promise.reject(new Error('useMqtt is not used'));
51
- }
52
- try {
53
- const params = createSetCommonParams({
54
- deviceId: devId,
55
- reqType: ScheduleEnum.query,
56
- message
57
- });
58
- ty.device.sendMqttMessage(params);
59
- const {
60
- taskId
61
- } = params.message;
62
- return normalResolve(ScheduleEnum.query, taskId);
63
- } catch (error) {
64
- return Promise.reject(error instanceof Error ? error : new Error('Failed to request schedule'));
65
- }
41
+ return new Promise((resolve, reject) => {
42
+ try {
43
+ // 使用 TypeBox Schema 进行参数验证
44
+ const validatedParams = validator.validate('request_schedule', message);
45
+ sendStructuredMessage(ScheduleEnum.query, validatedParams, resolve, error => reject(handleMqttError(error, 'Failed to request schedule')));
46
+ } catch (error) {
47
+ reject(handleMqttError(error, 'Failed to request schedule'));
48
+ }
49
+ });
66
50
  },
67
51
  setSchedule: message => {
68
- // 参数验证
69
- if (!message || typeof message.num !== 'number') {
70
- throw new Error('message.num is required and must be a number');
71
- }
72
- if (!Array.isArray(message.list)) {
73
- throw new Error('message.list is required and must be an array');
74
- }
75
- if (message.num !== message.list.length) {
76
- throw new Error('message.num must equal message.list.length');
77
- }
78
- try {
79
- if (useMqtt) {
80
- const params = createSetCommonParams({
81
- deviceId: devId,
82
- reqType: ScheduleEnum.set,
83
- message
52
+ return new Promise((resolve, reject) => {
53
+ try {
54
+ // 使用 TypeBox Schema 进行参数验证
55
+ const validatedData = validator.validate('set_schedule', message);
56
+ if (useMqtt) {
57
+ sendStructuredMessage(ScheduleEnum.set, validatedData, resolve, error => reject(handleMqttError(error, 'Failed to set schedule')), ScheduleEnum.query // 响应类型是 query,不是 set
58
+ );
59
+ return;
60
+ }
61
+
62
+ // 非 MQTT 模式:使用命令传输
63
+ const command = encodeDeviceTimer0x30({
64
+ version: commandVersion,
65
+ number: validatedData.num,
66
+ list: validatedData.list.map(_ref => {
67
+ let {
68
+ active,
69
+ suctions,
70
+ sweepMopModes,
71
+ ids,
72
+ cleanCounts,
73
+ cycle,
74
+ cisterns,
75
+ time
76
+ } = _ref;
77
+ return {
78
+ effectiveness: active,
79
+ cleanMode: CLEAN_MODE_MAP[(sweepMopModes === null || sweepMopModes === void 0 ? void 0 : sweepMopModes[0]) || ''],
80
+ fanLevel: SUCTION_MAP[(suctions === null || suctions === void 0 ? void 0 : suctions[0]) || ''],
81
+ waterLevel: CISTERN_MAP[(cisterns === null || cisterns === void 0 ? void 0 : cisterns[0]) || ''],
82
+ time: {
83
+ hour: time[0],
84
+ minute: time[1]
85
+ },
86
+ week: cycle,
87
+ sweepCount: (cleanCounts === null || cleanCounts === void 0 ? void 0 : cleanCounts[0]) || 1,
88
+ roomIds: ids || [],
89
+ roomNum: (ids === null || ids === void 0 ? void 0 : ids.length) || 0
90
+ };
91
+ })
84
92
  });
85
- ty.device.sendMqttMessage(params);
86
- const {
87
- taskId
88
- } = params.message;
89
- return normalResolve(ScheduleEnum.query, taskId);
93
+ devices.common.model.actions.device_timer.set(command);
94
+ resolve({
95
+ success: true,
96
+ errCode: 0,
97
+ reqType: ScheduleEnum.query,
98
+ version: commandVersion,
99
+ taskId: `${Date.now()}`,
100
+ list: validatedData.list,
101
+ num: validatedData.num
102
+ });
103
+ } catch (error) {
104
+ reject(handleMqttError(error, 'Failed to set schedule'));
90
105
  }
91
-
92
- // 非 MQTT 模式:使用命令传输
93
- const command = encodeDeviceTimer0x30({
94
- version: commandVersion,
95
- number: message.num,
96
- list: message.list.map(_ref => {
97
- let {
98
- active,
99
- suctions,
100
- sweepMopModes,
101
- ids,
102
- cleanCounts,
103
- cycle,
104
- cisterns,
105
- time
106
- } = _ref;
107
- return {
108
- effectiveness: active,
109
- cleanMode: CLEAN_MODE_MAP[sweepMopModes[0]],
110
- fanLevel: SUCTION_MAP[suctions[0]],
111
- waterLevel: CISTERN_MAP[cisterns[0]],
112
- time: {
113
- hour: time[0],
114
- minute: time[1]
115
- },
116
- week: cycle,
117
- sweepCount: cleanCounts[0],
118
- roomIds: ids,
119
- roomNum: ids.length
120
- };
121
- })
122
- });
123
- devices.common.model.actions.device_timer.set(command);
124
- return Promise.resolve({
125
- success: true,
126
- errCode: 0,
127
- reqType: ScheduleEnum.query,
128
- version: commandVersion,
129
- taskId: `${Date.now()}`,
130
- list: message.list,
131
- num: message.num
132
- });
133
- } catch (error) {
134
- throw error instanceof Error ? error : new Error('Failed to set schedule');
135
- }
106
+ });
136
107
  }
137
108
  };
138
109
  };
@@ -1,30 +1,9 @@
1
- import { BaseResponse, RoomCleanSetEnum } from './type';
2
- interface RoomInfo {
3
- roomId: number;
4
- suction?: string;
5
- cistern: string;
6
- cleanTimes?: number;
7
- yMop?: number;
8
- sweepMopMode?: string;
9
- }
10
- interface RoomCleanResponse extends BaseResponse {
11
- reqType: RoomCleanSetEnum.query;
12
- ids: number[];
13
- suctions?: string[];
14
- cisterns?: string[];
15
- cleanCounts?: number[];
16
- yMops?: number[];
17
- sweepMopModes?: string[];
18
- }
19
- type TSetRoomClean = (rooms: RoomInfo[]) => Promise<RoomCleanResponse>;
20
- type TRequestSelectRoomClean = () => Promise<RoomCleanResponse>;
1
+ import { type TRequestSelectRoomClean, type TSetSelectRoomClean } from './type/protocols';
21
2
  /**
22
3
  * 自定义 Hook,用于选区清扫
23
- * @param devId 设备ID
24
4
  * @returns 包含 requestSelectRoomClean 和 setRoomClean 函数的对象
25
5
  */
26
- export declare const useSelectRoomClean: (devId: string) => {
6
+ export declare const useSelectRoomClean: () => {
27
7
  requestSelectRoomClean: TRequestSelectRoomClean;
28
- setRoomClean: TSetRoomClean;
8
+ setRoomClean: TSetSelectRoomClean;
29
9
  };
30
- export {};