@ray-js/robot-data-stream 0.0.14 → 0.0.15-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 (156) hide show
  1. package/lib/mqtt/createCommonOptions.d.ts +4 -2
  2. package/lib/mqtt/createCommonOptions.js +10 -5
  3. package/lib/mqtt/hooks/useStructuredMessage.d.ts +14 -0
  4. package/lib/mqtt/hooks/useStructuredMessage.js +131 -0
  5. package/lib/mqtt/mqttProvider.d.ts +19 -1
  6. package/lib/mqtt/mqttProvider.js +69 -8
  7. package/lib/mqtt/promise.d.ts +57 -4
  8. package/lib/mqtt/promise.js +115 -36
  9. package/lib/mqtt/type/fun.d.ts +49 -0
  10. package/lib/mqtt/type/fun.js +145 -0
  11. package/lib/mqtt/type/index.d.ts +0 -7
  12. package/lib/mqtt/type/index.js +0 -1
  13. package/lib/mqtt/type/protocols/base.d.ts +5 -0
  14. package/lib/mqtt/type/protocols/base.js +32 -0
  15. package/lib/mqtt/type/protocols/carpetCleanProtocol.d.ts +8 -0
  16. package/lib/mqtt/type/protocols/carpetCleanProtocol.js +16 -0
  17. package/lib/mqtt/type/protocols/carpetProtocol.d.ts +41 -0
  18. package/lib/mqtt/type/protocols/carpetProtocol.js +202 -0
  19. package/lib/mqtt/type/protocols/devInfoProtocol.d.ts +5 -0
  20. package/lib/mqtt/type/protocols/devInfoProtocol.js +12 -0
  21. package/lib/mqtt/type/protocols/deviceModelProtocol.d.ts +8 -0
  22. package/lib/mqtt/type/protocols/deviceModelProtocol.js +34 -0
  23. package/lib/mqtt/type/protocols/furnitureModelProtocol.d.ts +10 -0
  24. package/lib/mqtt/type/protocols/furnitureModelProtocol.js +35 -0
  25. package/lib/mqtt/type/protocols/historyMapProtocol.d.ts +17 -0
  26. package/lib/mqtt/type/protocols/historyMapProtocol.js +37 -0
  27. package/lib/mqtt/type/protocols/index.d.ts +20 -0
  28. package/lib/mqtt/type/protocols/index.js +20 -0
  29. package/lib/mqtt/type/protocols/partDivisionProtocol.d.ts +8 -0
  30. package/lib/mqtt/type/protocols/partDivisionProtocol.js +12 -0
  31. package/lib/mqtt/type/protocols/partMergeProtocol.d.ts +7 -0
  32. package/lib/mqtt/type/protocols/partMergeProtocol.js +12 -0
  33. package/lib/mqtt/type/protocols/passwordProtocol.d.ts +11 -0
  34. package/lib/mqtt/type/protocols/passwordProtocol.js +25 -0
  35. package/lib/mqtt/type/protocols/quietHoursProtocol.d.ts +8 -0
  36. package/lib/mqtt/type/protocols/quietHoursProtocol.js +35 -0
  37. package/lib/mqtt/type/protocols/resetMapProtocol.d.ts +4 -0
  38. package/lib/mqtt/type/protocols/resetMapProtocol.js +1 -0
  39. package/lib/mqtt/type/protocols/roomPropertyProtocol.d.ts +14 -0
  40. package/lib/mqtt/type/protocols/roomPropertyProtocol.js +113 -0
  41. package/lib/mqtt/type/protocols/scheduleProtocol.d.ts +18 -0
  42. package/lib/mqtt/type/protocols/scheduleProtocol.js +100 -0
  43. package/lib/mqtt/type/protocols/selectRoomCleanProtocol.d.ts +8 -0
  44. package/lib/mqtt/type/protocols/selectRoomCleanProtocol.js +40 -0
  45. package/lib/mqtt/type/protocols/spotCleanProtocol.d.ts +9 -0
  46. package/lib/mqtt/type/protocols/spotCleanProtocol.js +72 -0
  47. package/lib/mqtt/type/protocols/virtualAreaProtocol.d.ts +8 -0
  48. package/lib/mqtt/type/protocols/virtualAreaProtocol.js +36 -0
  49. package/lib/mqtt/type/protocols/virtualWallProtocol.d.ts +8 -0
  50. package/lib/mqtt/type/protocols/virtualWallProtocol.js +25 -0
  51. package/lib/mqtt/type/protocols/voiceProtocol.d.ts +8 -0
  52. package/lib/mqtt/type/protocols/voiceProtocol.js +23 -0
  53. package/lib/mqtt/type/protocols/wifiMapProtocol.d.ts +8 -0
  54. package/lib/mqtt/type/protocols/wifiMapProtocol.js +14 -0
  55. package/lib/mqtt/type/protocols/zoneCleanProtocol.d.ts +8 -0
  56. package/lib/mqtt/type/protocols/zoneCleanProtocol.js +49 -0
  57. package/lib/mqtt/useCarpet.d.ts +7 -67
  58. package/lib/mqtt/useCarpet.js +101 -181
  59. package/lib/mqtt/useCarpetClean.d.ts +2 -11
  60. package/lib/mqtt/useCarpetClean.js +24 -51
  61. package/lib/mqtt/useDevInfo.d.ts +8 -6
  62. package/lib/mqtt/useDevInfo.js +44 -36
  63. package/lib/mqtt/useDeviceModel.d.ts +6 -19
  64. package/lib/mqtt/useDeviceModel.js +29 -42
  65. package/lib/mqtt/useFurnitureModel.d.ts +6 -18
  66. package/lib/mqtt/useFurnitureModel.js +29 -43
  67. package/lib/mqtt/useHistoryMap.d.ts +2 -18
  68. package/lib/mqtt/useHistoryMap.js +102 -119
  69. package/lib/mqtt/usePartDivision.d.ts +2 -9
  70. package/lib/mqtt/usePartDivision.js +48 -57
  71. package/lib/mqtt/usePartMerge.d.ts +2 -8
  72. package/lib/mqtt/usePartMerge.js +34 -49
  73. package/lib/mqtt/usePassword.d.ts +6 -13
  74. package/lib/mqtt/usePassword.js +84 -74
  75. package/lib/mqtt/useQuiteHours.d.ts +4 -37
  76. package/lib/mqtt/useQuiteHours.js +66 -100
  77. package/lib/mqtt/useResetMap.d.ts +2 -8
  78. package/lib/mqtt/useResetMap.js +27 -34
  79. package/lib/mqtt/useRoomProperty.d.ts +8 -9
  80. package/lib/mqtt/useRoomProperty.js +67 -65
  81. package/lib/mqtt/useSchedule.d.ts +3 -23
  82. package/lib/mqtt/useSchedule.js +72 -101
  83. package/lib/mqtt/useSelectRoomClean.d.ts +3 -24
  84. package/lib/mqtt/useSelectRoomClean.js +107 -133
  85. package/lib/mqtt/useSpotClean.d.ts +5 -15
  86. package/lib/mqtt/useSpotClean.js +68 -121
  87. package/lib/mqtt/useVirtualArea.d.ts +6 -17
  88. package/lib/mqtt/useVirtualArea.js +109 -119
  89. package/lib/mqtt/useVirtualWall.d.ts +2 -18
  90. package/lib/mqtt/useVirtualWall.js +69 -90
  91. package/lib/mqtt/useVoice.d.ts +2 -16
  92. package/lib/mqtt/useVoice.js +48 -68
  93. package/lib/mqtt/useWifiMap.d.ts +6 -5
  94. package/lib/mqtt/useWifiMap.js +43 -42
  95. package/lib/mqtt/useZoneClean.d.ts +2 -21
  96. package/lib/mqtt/useZoneClean.js +125 -137
  97. package/lib/myLib/zod/mini/index.d.cts +1 -0
  98. package/lib/myLib/zod/mini/index.d.ts +1 -0
  99. package/lib/myLib/zod/mini/index.js +1 -0
  100. package/lib/myLib/zod/mini/package.json +6 -0
  101. package/lib/myLib/zod/src/mini/index.d.ts +1 -0
  102. package/lib/myLib/zod/src/mini/index.js +1 -0
  103. package/lib/myLib/zod/src/v4/core/api.d.ts +306 -0
  104. package/lib/myLib/zod/src/v4/core/api.js +1256 -0
  105. package/lib/myLib/zod/src/v4/core/checks.d.ts +278 -0
  106. package/lib/myLib/zod/src/v4/core/checks.js +816 -0
  107. package/lib/myLib/zod/src/v4/core/config.d.ts +9 -0
  108. package/lib/myLib/zod/src/v4/core/config.js +5 -0
  109. package/lib/myLib/zod/src/v4/core/core.d.ts +70 -0
  110. package/lib/myLib/zod/src/v4/core/core.js +95 -0
  111. package/lib/myLib/zod/src/v4/core/doc.d.ts +14 -0
  112. package/lib/myLib/zod/src/v4/core/doc.js +42 -0
  113. package/lib/myLib/zod/src/v4/core/errors.d.ts +220 -0
  114. package/lib/myLib/zod/src/v4/core/errors.js +232 -0
  115. package/lib/myLib/zod/src/v4/core/index.d.ts +15 -0
  116. package/lib/myLib/zod/src/v4/core/index.js +15 -0
  117. package/lib/myLib/zod/src/v4/core/json-schema-generator.d.ts +65 -0
  118. package/lib/myLib/zod/src/v4/core/json-schema-generator.js +120 -0
  119. package/lib/myLib/zod/src/v4/core/json-schema-processors.d.ts +49 -0
  120. package/lib/myLib/zod/src/v4/core/json-schema-processors.js +593 -0
  121. package/lib/myLib/zod/src/v4/core/json-schema.d.ts +88 -0
  122. package/lib/myLib/zod/src/v4/core/json-schema.js +1 -0
  123. package/lib/myLib/zod/src/v4/core/parse.d.ts +49 -0
  124. package/lib/myLib/zod/src/v4/core/parse.js +147 -0
  125. package/lib/myLib/zod/src/v4/core/regexes.d.ts +78 -0
  126. package/lib/myLib/zod/src/v4/core/regexes.js +146 -0
  127. package/lib/myLib/zod/src/v4/core/registries.d.ts +35 -0
  128. package/lib/myLib/zod/src/v4/core/registries.js +51 -0
  129. package/lib/myLib/zod/src/v4/core/schemas.d.ts +1136 -0
  130. package/lib/myLib/zod/src/v4/core/schemas.js +2745 -0
  131. package/lib/myLib/zod/src/v4/core/standard-schema.d.ts +126 -0
  132. package/lib/myLib/zod/src/v4/core/standard-schema.js +1 -0
  133. package/lib/myLib/zod/src/v4/core/to-json-schema.d.ts +114 -0
  134. package/lib/myLib/zod/src/v4/core/to-json-schema.js +488 -0
  135. package/lib/myLib/zod/src/v4/core/util.d.ts +199 -0
  136. package/lib/myLib/zod/src/v4/core/util.js +656 -0
  137. package/lib/myLib/zod/src/v4/core/versions.d.ts +5 -0
  138. package/lib/myLib/zod/src/v4/core/versions.js +5 -0
  139. package/lib/myLib/zod/src/v4/core/zsf.d.ts +91 -0
  140. package/lib/myLib/zod/src/v4/core/zsf.js +1 -0
  141. package/lib/myLib/zod/src/v4/mini/checks.d.ts +1 -0
  142. package/lib/myLib/zod/src/v4/mini/checks.js +1 -0
  143. package/lib/myLib/zod/src/v4/mini/coerce.d.ts +7 -0
  144. package/lib/myLib/zod/src/v4/mini/coerce.js +27 -0
  145. package/lib/myLib/zod/src/v4/mini/external.d.ts +11 -0
  146. package/lib/myLib/zod/src/v4/mini/external.js +16 -0
  147. package/lib/myLib/zod/src/v4/mini/index.d.ts +3 -0
  148. package/lib/myLib/zod/src/v4/mini/index.js +3 -0
  149. package/lib/myLib/zod/src/v4/mini/iso.d.ts +22 -0
  150. package/lib/myLib/zod/src/v4/mini/iso.js +46 -0
  151. package/lib/myLib/zod/src/v4/mini/parse.d.ts +1 -0
  152. package/lib/myLib/zod/src/v4/mini/parse.js +1 -0
  153. package/lib/myLib/zod/src/v4/mini/schemas.js +1244 -0
  154. package/lib/utils/index.d.ts +1 -0
  155. package/lib/utils/index.js +2 -1
  156. package/package.json +5 -3
@@ -1,123 +1,73 @@
1
1
  import "core-js/modules/esnext.iterator.map.js";
2
2
  // 定点清扫
3
3
 
4
- import { isArray } from 'lodash-es';
5
- import { createSetCommonParams } from './createCommonOptions';
6
- import { normalResolve } from './promise';
7
- import { SpotCleanEnum } from './type';
8
4
  import { useContext } from 'react';
5
+ import { SpotCleanEnum } from './type';
9
6
  import { SingletonContext } from './mqttProvider';
10
7
  import { encodeSpotClean0x16 } from '@ray-js/robot-protocol';
11
8
  import { pointsToString } from '../utils';
9
+ import { useStructuredMessage, handleMqttError } from './hooks/useStructuredMessage';
10
+ import { validator } from './type/fun';
12
11
  /**
13
- * 定点清扫
14
- * @returns
12
+ * 自定义 Hook,用于定点清扫
13
+ * @returns 包含 requestSpotClean 和 setSpotClean 函数的对象
15
14
  */
16
- export const useSpotClean = devId => {
15
+ export const useSpotClean = () => {
17
16
  const {
18
17
  useMqtt,
19
18
  commandVersion,
20
19
  devices
21
20
  } = useContext(SingletonContext);
21
+ const {
22
+ sendStructuredMessage
23
+ } = useStructuredMessage();
22
24
 
23
- // 请求定点清扫数据
25
+ /**
26
+ * 请求定点清扫数据
27
+ * @param data 可选参数(目前为空对象)
28
+ * @returns Promise<SpotCleanResponse> 响应结果
29
+ */
24
30
 
25
- // 设置定点清扫
26
- // 定点清扫的清扫属性,使用全局的清扫属性
31
+ /**
32
+ * 设置定点清扫
33
+ * 定点清扫的清扫属性,使用全局的清扫属性
34
+ * @param message 包含定点清扫设置参数的对象
35
+ * @param message.spots 定点清扫的点数组
36
+ * @param message.origin 原点坐标(可选)
37
+ * @returns Promise<SpotCleanResponse> 响应结果
38
+ */
27
39
 
28
40
  return {
29
41
  requestSpotClean: () => {
30
- const params = createSetCommonParams({
31
- deviceId: devId,
32
- reqType: SpotCleanEnum.query
42
+ return new Promise((resolve, reject) => {
43
+ try {
44
+ sendStructuredMessage(SpotCleanEnum.query, {}, resolve, error => reject(handleMqttError(error, 'Failed to request spot clean')));
45
+ } catch (error) {
46
+ reject(handleMqttError(error, 'Failed to request spot clean'));
47
+ }
33
48
  });
34
- ty.device.sendMqttMessage(params);
35
- return normalResolve(SpotCleanEnum.query, params.message.taskId);
36
49
  },
37
50
  setSpotClean: message => {
38
- const {
39
- spots,
40
- origin
41
- } = message;
42
- if (useMqtt) {
43
- // if (!isArray(spots) || (isArray(spots) && spots.length === 0)) {
44
- // return Promise.reject(new Error('message is required'));
45
- // }
46
-
47
- const num = spots.length;
48
- const {
49
- suctions = new Array(num).fill(''),
50
- cisterns = new Array(num).fill(''),
51
- cleanCounts = new Array(num).fill(1),
52
- yMops = new Array(num).fill(-1),
53
- sweepMopModes = new Array(num).fill('only_sweep')
54
- } = message;
55
- if (suctions && !isArray(suctions)) {
56
- return Promise.reject(new Error('suctions is illegal'));
57
- }
58
- if (cisterns && !isArray(cisterns)) {
59
- return Promise.reject(new Error('cisterns is illegal'));
60
- }
61
- if (cleanCounts && !isArray(cleanCounts)) {
62
- return Promise.reject(new Error('cleanCounts is illegal'));
63
- }
64
- if (yMops && !isArray(yMops)) {
65
- return Promise.reject(new Error('yMops is illegal'));
66
- }
67
- if (sweepMopModes && !isArray(sweepMopModes)) {
68
- return Promise.reject(new Error('sweepMopModes is illegal'));
69
- }
70
- if (num !== suctions.length || num !== cisterns.length || num !== cleanCounts.length || num !== yMops.length || num !== sweepMopModes.length) {
71
- return Promise.reject(new Error('The length of the parameters is inconsistent'));
72
- }
73
- const params = createSetCommonParams({
74
- deviceId: devId,
75
- reqType: SpotCleanEnum.set,
76
- message: {
77
- num,
78
- polygons: spots.map(spot => pointsToString([spot], origin)),
79
- suctions,
80
- cisterns,
81
- cleanCounts,
82
- yMops,
83
- sweepMopModes
84
- }
85
- });
86
- ty.device.sendMqttMessage(params);
87
- return normalResolve(SpotCleanEnum.query, params.message.taskId);
88
- }
89
- try {
90
- if (useMqtt) {
51
+ return new Promise((resolve, reject) => {
52
+ try {
53
+ // 使用 TypeBox Schema 进行参数验证(包含自定义验证逻辑)
54
+ const validatedData = validator.validate('set_spot_clean', message);
55
+ const {
56
+ spots,
57
+ origin
58
+ } = validatedData;
91
59
  const num = spots.length;
60
+
61
+ // 为可选字段设置默认值
92
62
  const {
93
63
  suctions = new Array(num).fill(''),
94
64
  cisterns = new Array(num).fill(''),
95
65
  cleanCounts = new Array(num).fill(1),
96
66
  yMops = new Array(num).fill(-1),
97
67
  sweepMopModes = new Array(num).fill('only_sweep')
98
- } = message;
99
- if (suctions && !isArray(suctions)) {
100
- throw new Error('suctions is illegal');
101
- }
102
- if (cisterns && !isArray(cisterns)) {
103
- throw new Error('cisterns is illegal');
104
- }
105
- if (cleanCounts && !isArray(cleanCounts)) {
106
- throw new Error('cleanCounts is illegal');
107
- }
108
- if (yMops && !isArray(yMops)) {
109
- throw new Error('yMops is illegal');
110
- }
111
- if (sweepMopModes && !isArray(sweepMopModes)) {
112
- throw new Error('sweepMopModes is illegal');
113
- }
114
- if (num !== suctions.length || num !== cisterns.length || num !== cleanCounts.length || num !== yMops.length || num !== sweepMopModes.length) {
115
- throw new Error('The length of the parameters is inconsistent');
116
- }
117
- const params = createSetCommonParams({
118
- deviceId: devId,
119
- reqType: SpotCleanEnum.set,
120
- message: {
68
+ } = validatedData;
69
+ if (useMqtt) {
70
+ sendStructuredMessage(SpotCleanEnum.set, {
121
71
  num,
122
72
  polygons: spots.map(spot => pointsToString([spot], origin)),
123
73
  suctions,
@@ -125,38 +75,35 @@ export const useSpotClean = devId => {
125
75
  cleanCounts,
126
76
  yMops,
127
77
  sweepMopModes
128
- }
78
+ }, resolve, error => reject(handleMqttError(error, 'Failed to set spot clean')), SpotCleanEnum.query // 响应类型是 query,不是 set
79
+ );
80
+ return;
81
+ }
82
+
83
+ // 非 MQTT 模式:使用命令传输
84
+ const command = encodeSpotClean0x16({
85
+ version: commandVersion,
86
+ origin,
87
+ point: spots[0]
129
88
  });
130
- ty.device.sendMqttMessage(params);
131
- const {
132
- taskId
133
- } = params.message;
134
- return normalResolve(SpotCleanEnum.query, taskId);
89
+ devices.common.model.actions.command_trans.set(command);
90
+ resolve({
91
+ success: true,
92
+ errCode: 0,
93
+ reqType: SpotCleanEnum.query,
94
+ version: commandVersion,
95
+ taskId: `${Date.now()}`,
96
+ polygons: spots.map(spot => pointsToString([spot], origin)),
97
+ suctions,
98
+ cisterns,
99
+ cleanCounts,
100
+ yMops,
101
+ sweepMopModes
102
+ });
103
+ } catch (error) {
104
+ reject(handleMqttError(error, 'Failed to set spot clean'));
135
105
  }
136
-
137
- // 非 MQTT 模式:使用命令传输
138
- const command = encodeSpotClean0x16({
139
- version: commandVersion,
140
- origin,
141
- point: spots[0]
142
- });
143
- devices.common.model.actions.command_trans.set(command);
144
- return Promise.resolve({
145
- success: true,
146
- errCode: 0,
147
- reqType: SpotCleanEnum.query,
148
- version: commandVersion,
149
- taskId: `${Date.now()}`,
150
- polygons: spots.map(spot => pointsToString([spot], origin)),
151
- suctions: message.suctions,
152
- cisterns: message.cisterns,
153
- cleanCounts: message.cleanCounts,
154
- yMops: message.yMops,
155
- sweepMopModes: message.sweepMopModes
156
- });
157
- } catch (error) {
158
- throw error instanceof Error ? error : new Error('Failed to set spot clean');
159
- }
106
+ });
160
107
  }
161
108
  };
162
109
  };
@@ -1,20 +1,9 @@
1
- import { BaseResponse, VirtualAreaEnum } from './type';
2
- import { Point, VirtualArea } from '@ray-js/robot-protocol';
3
- interface VirtualAreaResponse extends BaseResponse {
4
- reqType: VirtualAreaEnum.query;
5
- num: number;
6
- modes: number[];
7
- polygons: string[];
8
- names: string[];
9
- mapId: number;
10
- }
11
- type TSetVirtualArea = (message: {
12
- data: VirtualArea[];
13
- origin: Point;
14
- }) => Promise<VirtualAreaResponse>;
15
- type TRequestVirtualArea = () => Promise<VirtualAreaResponse>;
16
- export declare const useVirtualArea: (devId: string) => {
1
+ import { type TRequestVirtualArea, type TSetVirtualArea } from './type/protocols';
2
+ /**
3
+ * 自定义 Hook,用于虚拟区域(禁区)管理
4
+ * @returns 包含 requestVirtualArea 和 setVirtualArea 函数的对象
5
+ */
6
+ export declare const useVirtualArea: () => {
17
7
  requestVirtualArea: TRequestVirtualArea;
18
8
  setVirtualArea: TSetVirtualArea;
19
9
  };
20
- export {};
@@ -2,27 +2,32 @@ import "core-js/modules/esnext.iterator.constructor.js";
2
2
  import "core-js/modules/esnext.iterator.for-each.js";
3
3
  import "core-js/modules/esnext.iterator.map.js";
4
4
  // 添加禁区
5
- import { useContext } from 'react';
6
- import { createSetCommonParams } from './createCommonOptions';
7
- import { normalResolve } from './promise';
8
- import { VirtualAreaEnum } from './type';
9
- import { SingletonContext } from './mqttProvider';
10
5
  import { encodeVirtualArea0x1a, requestVirtualArea0x1b } from '@ray-js/robot-protocol';
6
+ import { useContext } from 'react';
11
7
  import { pointsToString } from '../utils';
12
-
13
- // 虚拟区域响应类型
14
-
8
+ import { handleMqttError, useStructuredMessage } from './hooks/useStructuredMessage';
9
+ import { SingletonContext } from './mqttProvider';
10
+ import { VirtualAreaEnum } from './type';
11
+ import { validator } from './type/fun';
15
12
  // 禁区
16
13
 
17
- export const useVirtualArea = devId => {
14
+ /**
15
+ * 自定义 Hook,用于虚拟区域(禁区)管理
16
+ * @returns 包含 requestVirtualArea 和 setVirtualArea 函数的对象
17
+ */
18
+ export const useVirtualArea = () => {
18
19
  const {
19
20
  useMqtt,
20
21
  commandVersion,
21
22
  devices
22
23
  } = useContext(SingletonContext);
24
+ const {
25
+ sendStructuredMessage
26
+ } = useStructuredMessage();
23
27
 
24
28
  /**
25
29
  * 请求虚拟区域数据
30
+ * @param data 可选参数(目前为空对象)
26
31
  * @returns Promise<VirtualAreaResponse> 响应结果
27
32
  */
28
33
 
@@ -36,125 +41,110 @@ export const useVirtualArea = devId => {
36
41
 
37
42
  return {
38
43
  requestVirtualArea: () => {
39
- try {
40
- if (useMqtt) {
41
- const params = createSetCommonParams({
42
- deviceId: devId,
43
- reqType: VirtualAreaEnum.query
44
- });
45
- ty.device.sendMqttMessage(params);
46
- const {
47
- taskId
48
- } = params.message;
49
- return normalResolve(VirtualAreaEnum.query, taskId);
50
- }
44
+ return new Promise((resolve, reject) => {
45
+ try {
46
+ if (useMqtt) {
47
+ sendStructuredMessage(VirtualAreaEnum.query, {}, resolve, error => reject(handleMqttError(error, 'Failed to request virtual area')));
48
+ return;
49
+ }
51
50
 
52
- // 非 MQTT 模式:使用命令传输
53
- devices.common.model.actions.command_trans.set(requestVirtualArea0x1b({
54
- version: commandVersion
55
- }));
51
+ // 非 MQTT 模式:使用命令传输
52
+ devices.common.model.actions.command_trans.set(requestVirtualArea0x1b({
53
+ version: commandVersion
54
+ }));
56
55
 
57
- // 非 MQTT 模式下无法立即获取响应数据,返回默认响应
58
- return Promise.resolve({
59
- success: true,
60
- errCode: 0,
61
- reqType: VirtualAreaEnum.query,
62
- version: commandVersion,
63
- taskId: `${Date.now()}`,
64
- num: 0,
65
- modes: [],
66
- polygons: [],
67
- names: [],
68
- mapId: 0
69
- });
70
- } catch (error) {
71
- return Promise.reject(error instanceof Error ? error : new Error('Failed to request virtual area'));
72
- }
73
- },
74
- setVirtualArea: _ref => {
75
- let {
76
- data,
77
- origin
78
- } = _ref;
79
- // 参数验证
80
- if (!Array.isArray(data)) {
81
- throw new Error('data is required and must be a non-empty array');
82
- }
83
- if (!origin || typeof origin.x !== 'number' || typeof origin.y !== 'number') {
84
- throw new Error('origin is required and must be a valid Point object with x and y properties');
85
- }
86
- try {
87
- if (useMqtt) {
88
- const names = [];
89
- const modes = [];
90
- const polygons = [];
91
- data.forEach(_ref2 => {
92
- let {
93
- points,
94
- name,
95
- mode
96
- } = _ref2;
97
- names.push(name);
98
- modes.push(mode);
99
- polygons.push(pointsToString(points, origin));
56
+ // 非 MQTT 模式下无法立即获取响应数据,返回默认响应
57
+ resolve({
58
+ success: true,
59
+ errCode: 0,
60
+ reqType: VirtualAreaEnum.query,
61
+ version: commandVersion,
62
+ taskId: `${Date.now()}`,
63
+ num: 0,
64
+ modes: [],
65
+ polygons: [],
66
+ names: [],
67
+ mapId: 0
100
68
  });
101
- const params = createSetCommonParams({
102
- deviceId: devId,
103
- reqType: VirtualAreaEnum.set,
104
- message: {
69
+ } catch (error) {
70
+ reject(handleMqttError(error, 'Failed to request virtual area'));
71
+ }
72
+ });
73
+ },
74
+ setVirtualArea: message => {
75
+ return new Promise((resolve, reject) => {
76
+ try {
77
+ // 使用 TypeBox Schema 进行参数验证
78
+ const validatedData = validator.validate('set_virtual_area', message);
79
+ const {
80
+ data,
81
+ origin
82
+ } = validatedData;
83
+ if (useMqtt) {
84
+ const names = [];
85
+ const modes = [];
86
+ const polygons = [];
87
+ data.forEach(_ref => {
88
+ let {
89
+ points,
90
+ name,
91
+ mode
92
+ } = _ref;
93
+ names.push(name);
94
+ modes.push(mode);
95
+ polygons.push(pointsToString(points, origin));
96
+ });
97
+ sendStructuredMessage(VirtualAreaEnum.set, {
105
98
  polygons,
106
99
  num: data.length,
107
100
  modes: modes,
108
101
  names: names
109
- }
102
+ }, resolve, error => reject(handleMqttError(error, 'Failed to set virtual area')), VirtualAreaEnum.query // 响应类型是 query,不是 set
103
+ );
104
+ return;
105
+ }
106
+
107
+ // 非 MQTT 模式:使用命令传输
108
+ const command = encodeVirtualArea0x1a({
109
+ version: commandVersion,
110
+ origin,
111
+ virtualAreas: data
110
112
  });
111
- ty.device.sendMqttMessage(params);
112
- const {
113
- taskId
114
- } = params.message;
115
- return normalResolve(VirtualAreaEnum.query, taskId);
113
+ devices.common.model.actions.command_trans.set(command);
114
+ const names = data.map(_ref2 => {
115
+ let {
116
+ name
117
+ } = _ref2;
118
+ return name;
119
+ });
120
+ const modes = data.map(_ref3 => {
121
+ let {
122
+ mode
123
+ } = _ref3;
124
+ return mode;
125
+ });
126
+ const polygons = data.map(_ref4 => {
127
+ let {
128
+ points
129
+ } = _ref4;
130
+ return pointsToString(points, origin);
131
+ });
132
+ resolve({
133
+ success: true,
134
+ errCode: 0,
135
+ reqType: VirtualAreaEnum.query,
136
+ version: commandVersion,
137
+ taskId: `${Date.now()}`,
138
+ num: data.length,
139
+ modes,
140
+ polygons,
141
+ names,
142
+ mapId: 0
143
+ });
144
+ } catch (error) {
145
+ reject(handleMqttError(error, 'Failed to set virtual area'));
116
146
  }
117
-
118
- // 非 MQTT 模式:使用命令传输
119
- const command = encodeVirtualArea0x1a({
120
- version: commandVersion,
121
- origin,
122
- virtualAreas: data
123
- });
124
- devices.common.model.actions.command_trans.set(command);
125
- const names = data.map(_ref3 => {
126
- let {
127
- name
128
- } = _ref3;
129
- return name;
130
- });
131
- const modes = data.map(_ref4 => {
132
- let {
133
- mode
134
- } = _ref4;
135
- return mode;
136
- });
137
- const polygons = data.map(_ref5 => {
138
- let {
139
- points
140
- } = _ref5;
141
- return pointsToString(points, origin);
142
- });
143
- return Promise.resolve({
144
- success: true,
145
- errCode: 0,
146
- reqType: VirtualAreaEnum.query,
147
- version: commandVersion,
148
- taskId: `${Date.now()}`,
149
- num: data.length,
150
- modes,
151
- polygons,
152
- names,
153
- mapId: 0
154
- });
155
- } catch (error) {
156
- throw error instanceof Error ? error : new Error('Failed to set virtual area');
157
- }
147
+ });
158
148
  }
159
149
  };
160
150
  };
@@ -1,25 +1,9 @@
1
- import { BaseResponse, VirtualWallEnum } from './type';
2
- import { Point } from '@ray-js/robot-protocol';
3
- interface VirtualWallResponse extends BaseResponse {
4
- reqType: VirtualWallEnum.query;
5
- modes: number[];
6
- num: number;
7
- mapId: number;
8
- points: string[];
9
- }
10
- interface SetVirtualWallParams {
11
- data: Point[][];
12
- origin: Point;
13
- }
14
- type TRequestVirtualWall = () => Promise<VirtualWallResponse>;
15
- type TSetVirtualWall = (params: SetVirtualWallParams) => Promise<VirtualWallResponse>;
1
+ import { type TRequestVirtualWall, type TSetVirtualWall } from './type/protocols';
16
2
  /**
17
3
  * 自定义 Hook,用于虚拟墙管理
18
- * @param devId 设备ID
19
4
  * @returns 包含 requestVirtualWall 和 setVirtualWall 函数的对象
20
5
  */
21
- export declare const useVirtualWall: (devId: string) => {
6
+ export declare const useVirtualWall: () => {
22
7
  requestVirtualWall: TRequestVirtualWall;
23
8
  setVirtualWall: TSetVirtualWall;
24
9
  };
25
- export {};