@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,52 +1,60 @@
1
1
  import { hexToUriDecodedString } from '@ray-js/robot-protocol';
2
2
  import { useContext } from 'react';
3
- import { createSetCommonParams } from './createCommonOptions';
3
+ import { handleMqttError, useStructuredMessage } from './hooks/useStructuredMessage';
4
4
  import { SingletonContext } from './mqttProvider';
5
- import { normalResolve } from './promise';
6
5
  import { DevInfoEnum } from './type';
7
6
 
8
- // 设备信息响应类型
7
+ // 导出类型供外部使用
9
8
 
10
- // 请求设备信息
11
- export const useDevInfo = devId => {
9
+ /**
10
+ * 自定义 Hook,用于设备信息管理
11
+ * @returns 包含 requestDevInfo 函数的对象
12
+ */
13
+ export const useDevInfo = () => {
12
14
  const {
13
15
  useMqtt,
14
- devices
16
+ devices,
17
+ commandVersion
15
18
  } = useContext(SingletonContext);
19
+ const {
20
+ sendStructuredMessage
21
+ } = useStructuredMessage();
22
+
23
+ /**
24
+ * 请求设备信息
25
+ * @returns Promise<DevInfoResponse> 响应结果,包含设备信息
26
+ */
27
+
16
28
  return {
17
29
  requestDevInfo: () => {
18
- if (useMqtt) {
19
- const params = createSetCommonParams({
20
- deviceId: devId,
21
- reqType: DevInfoEnum.query,
22
- message: {}
23
- });
24
- ty.device.sendMqttMessage(params);
25
- const {
26
- taskId
27
- } = params.message;
28
- return normalResolve(DevInfoEnum.query, taskId);
29
- }
30
+ return new Promise((resolve, reject) => {
31
+ try {
32
+ var _devices$common$model;
33
+ if (useMqtt) {
34
+ // request_dev_info 不需要参数,直接发送空对象
35
+ sendStructuredMessage(DevInfoEnum.query, {}, resolve, error => reject(handleMqttError(error, 'Failed to request device info')));
36
+ return;
37
+ }
30
38
 
31
- // 非 MQTT 模式:从设备属性中获取设备信息
32
- try {
33
- var _devices$common$model;
34
- const dpDevInfo = (_devices$common$model = devices.common.model.props) === null || _devices$common$model === void 0 ? void 0 : _devices$common$model.device_info;
35
- if (!dpDevInfo) {
36
- return Promise.reject(new Error('Device info is not available in device properties'));
39
+ // 非 MQTT 模式:从设备属性中获取设备信息
40
+ const dpDevInfo = (_devices$common$model = devices.common.model.props) === null || _devices$common$model === void 0 ? void 0 : _devices$common$model.device_info;
41
+ if (!dpDevInfo) {
42
+ reject(new Error('Device info is not available in device properties'));
43
+ return;
44
+ }
45
+ const info = hexToUriDecodedString(dpDevInfo);
46
+ resolve({
47
+ info,
48
+ success: true,
49
+ errCode: 0,
50
+ reqType: DevInfoEnum.query,
51
+ version: commandVersion || '1.0.0',
52
+ taskId: `${Date.now()}`
53
+ });
54
+ } catch (error) {
55
+ reject(handleMqttError(error, 'Failed to request device info'));
37
56
  }
38
- const info = hexToUriDecodedString(dpDevInfo);
39
- return Promise.resolve({
40
- info,
41
- success: true,
42
- errCode: 0,
43
- reqType: DevInfoEnum.query,
44
- version: '1.0.0',
45
- taskId: `${Date.now()}`
46
- });
47
- } catch (error) {
48
- return Promise.reject(error instanceof Error ? error : new Error('Failed to get device info from device properties'));
49
- }
57
+ });
50
58
  }
51
59
  };
52
60
  };
@@ -1,22 +1,9 @@
1
- import { BaseResponse } from './type';
2
- import { DeviceModelEnum } from './type/requestType';
3
- interface DeviceModelInfo {
4
- id: number;
5
- modelType: number;
6
- scale: number;
7
- size: [number, number, number];
8
- rotation: [number, number, number];
9
- position: [number, number, number];
10
- subDevId: string;
11
- }
12
- interface DeviceModelResponse extends BaseResponse {
13
- reqType: DeviceModelEnum.query;
14
- modelInfo: DeviceModelInfo[];
15
- }
16
- type TRequestDeviceList = () => Promise<DeviceModelResponse>;
17
- type TSetDeviceModel = (modelInfos: DeviceModelInfo[]) => Promise<DeviceModelResponse>;
18
- export declare const useDeviceModel: (devId: string) => {
1
+ import { type TRequestDeviceList, type TSetDeviceModel } from './type/protocols';
2
+ /**
3
+ * 自定义 Hook,用于设备模型管理
4
+ * @returns 包含 requestDeviceList 和 setDeviceModel 函数的对象
5
+ */
6
+ export declare const useDeviceModel: () => {
19
7
  requestDeviceList: TRequestDeviceList;
20
8
  setDeviceModel: TSetDeviceModel;
21
9
  };
22
- export {};
@@ -1,60 +1,47 @@
1
- import { createSetCommonParams } from './createCommonOptions';
2
- import { normalResolve } from './promise';
3
1
  import { DeviceModelEnum } from './type/requestType';
2
+ import { useStructuredMessage, handleMqttError } from './hooks/useStructuredMessage';
3
+ import { validator } from './type/fun';
4
+ /**
5
+ * 自定义 Hook,用于设备模型管理
6
+ * @returns 包含 requestDeviceList 和 setDeviceModel 函数的对象
7
+ */
8
+ export const useDeviceModel = () => {
9
+ const {
10
+ sendStructuredMessage
11
+ } = useStructuredMessage();
4
12
 
5
- // 请求设备列表函数定义
6
-
7
- // 设置设备模型函数定义
8
-
9
- export const useDeviceModel = devId => {
10
13
  /**
11
14
  * 请求设备列表
15
+ * @param data 可选参数(目前为空对象)
12
16
  * @returns Promise<DeviceModelResponse> 响应结果
13
17
  */
14
18
 
15
19
  /**
16
20
  * 设置设备模型
17
- * @param modelInfos 设备模型信息数组
21
+ * @param data 包含设备模型信息数组的对象
18
22
  * @returns Promise<DeviceModelResponse> 响应结果
19
23
  */
20
24
 
21
25
  return {
22
26
  requestDeviceList: () => {
23
- try {
24
- const params = createSetCommonParams({
25
- deviceId: devId,
26
- reqType: DeviceModelEnum.query
27
- });
28
- const {
29
- taskId
30
- } = params.message;
31
- ty.device.sendMqttMessage(params);
32
- return normalResolve(DeviceModelEnum.query, taskId);
33
- } catch (error) {
34
- return Promise.reject(error instanceof Error ? error : new Error('Failed to request device list'));
35
- }
27
+ return new Promise((resolve, reject) => {
28
+ try {
29
+ sendStructuredMessage(DeviceModelEnum.query, {}, resolve, error => reject(handleMqttError(error, 'Failed to request device list')));
30
+ } catch (error) {
31
+ reject(handleMqttError(error, 'Failed to request device list'));
32
+ }
33
+ });
36
34
  },
37
- setDeviceModel: modelInfos => {
38
- // 参数验证
39
- if (!Array.isArray(modelInfos)) {
40
- throw new Error('modelInfos is required and must be an array');
41
- }
42
- try {
43
- const params = createSetCommonParams({
44
- deviceId: devId,
45
- reqType: DeviceModelEnum.set,
46
- message: {
47
- modelInfos
48
- }
49
- });
50
- ty.device.sendMqttMessage(params);
51
- const {
52
- taskId
53
- } = params.message;
54
- return normalResolve(DeviceModelEnum.query, taskId);
55
- } catch (error) {
56
- throw error instanceof Error ? error : new Error('Failed to set device model');
57
- }
35
+ setDeviceModel: data => {
36
+ return new Promise((resolve, reject) => {
37
+ try {
38
+ // 使用 TypeBox Schema 进行参数验证
39
+ const validatedData = validator.validate('set_device_model', data);
40
+ sendStructuredMessage(DeviceModelEnum.set, validatedData, resolve, error => reject(handleMqttError(error, 'Failed to set device model')), DeviceModelEnum.query);
41
+ } catch (error) {
42
+ reject(handleMqttError(error, 'Failed to set device model'));
43
+ }
44
+ });
58
45
  }
59
46
  };
60
47
  };
@@ -1,21 +1,9 @@
1
- import { BaseResponse } from './type';
2
- import { FurnitureModelEnum } from './type/requestType';
3
- interface FurnitureModelInfo {
4
- id: number;
5
- modelType: number;
6
- scale: number;
7
- size: [number, number, number];
8
- rotation: [number, number, number];
9
- position: [number, number, number];
10
- }
11
- interface FurnitureModelResponse extends BaseResponse {
12
- reqType: FurnitureModelEnum.query;
13
- modelInfo: FurnitureModelInfo[];
14
- }
15
- type TRequestFurnitureList = () => Promise<FurnitureModelResponse>;
16
- type TSetFurnitureModel = (modelInfos: FurnitureModelInfo[]) => Promise<FurnitureModelResponse>;
17
- export declare const useFurnitureModel: (devId: string) => {
1
+ import { type TRequestFurnitureList, type TSetFurnitureModel } from './type/protocols';
2
+ /**
3
+ * 自定义 Hook,用于家具模型管理
4
+ * @returns 包含 requestFurnitureList 和 setFurnitureModel 函数的对象
5
+ */
6
+ export declare const useFurnitureModel: () => {
18
7
  requestFurnitureList: TRequestFurnitureList;
19
8
  setFurnitureModel: TSetFurnitureModel;
20
9
  };
21
- export {};
@@ -1,61 +1,47 @@
1
- import { createSetCommonParams } from './createCommonOptions';
2
- import { normalResolve } from './promise';
3
1
  import { FurnitureModelEnum } from './type/requestType';
2
+ import { useStructuredMessage, handleMqttError } from './hooks/useStructuredMessage';
3
+ import { validator } from './type/fun';
4
+ /**
5
+ * 自定义 Hook,用于家具模型管理
6
+ * @returns 包含 requestFurnitureList 和 setFurnitureModel 函数的对象
7
+ */
8
+ export const useFurnitureModel = () => {
9
+ const {
10
+ sendStructuredMessage
11
+ } = useStructuredMessage();
4
12
 
5
- // 请求家具列表函数定义
6
-
7
- // 设置家具模型函数定义
8
-
9
- export const useFurnitureModel = devId => {
10
13
  /**
11
14
  * 请求家具列表
15
+ * @param data 可选参数(目前为空对象)
12
16
  * @returns Promise<FurnitureModelResponse> 响应结果
13
17
  */
14
18
 
15
19
  /**
16
20
  * 设置家具模型
17
- * @param modelInfos 家具模型信息数组
21
+ * @param data 包含家具模型信息数组的对象
18
22
  * @returns Promise<FurnitureModelResponse> 响应结果
19
23
  */
20
24
 
21
25
  return {
22
26
  requestFurnitureList: () => {
23
- try {
24
- const params = createSetCommonParams({
25
- deviceId: devId,
26
- reqType: FurnitureModelEnum.query
27
- });
28
- const {
29
- taskId
30
- } = params.message;
31
- ty.device.sendMqttMessage(params);
32
- return normalResolve(FurnitureModelEnum.query, taskId);
33
- } catch (error) {
34
- return Promise.reject(error instanceof Error ? error : new Error('Failed to request furniture list'));
35
- }
27
+ return new Promise((resolve, reject) => {
28
+ try {
29
+ sendStructuredMessage(FurnitureModelEnum.query, {}, resolve, error => reject(handleMqttError(error, 'Failed to request furniture list')));
30
+ } catch (error) {
31
+ reject(handleMqttError(error, 'Failed to request furniture list'));
32
+ }
33
+ });
36
34
  },
37
- setFurnitureModel: modelInfos => {
38
- // 参数验证
39
- if (!Array.isArray(modelInfos)) {
40
- throw new Error('modelInfos is required and must be an array');
41
- }
42
- try {
43
- const params = createSetCommonParams({
44
- deviceId: devId,
45
- reqType: FurnitureModelEnum.set,
46
- message: {
47
- modelInfos
48
- }
49
- });
50
- console.log('========================params', params);
51
- ty.device.sendMqttMessage(params);
52
- const {
53
- taskId
54
- } = params.message;
55
- return normalResolve(FurnitureModelEnum.query, taskId);
56
- } catch (error) {
57
- throw error instanceof Error ? error : new Error('Failed to set furniture model');
58
- }
35
+ setFurnitureModel: data => {
36
+ return new Promise((resolve, reject) => {
37
+ try {
38
+ // 使用 TypeBox Schema 进行参数验证
39
+ const validatedData = validator.validate('set_furniture_model', data);
40
+ sendStructuredMessage(FurnitureModelEnum.set, validatedData, resolve, error => reject(handleMqttError(error, 'Failed to set furniture model')), FurnitureModelEnum.query);
41
+ } catch (error) {
42
+ reject(handleMqttError(error, 'Failed to set furniture model'));
43
+ }
44
+ });
59
45
  }
60
46
  };
61
47
  };
@@ -1,26 +1,10 @@
1
- import { BaseResponse, DeleteMapEnum, SaveCurrentMapEnum, UseMapEnum } from './type';
2
- interface DeleteMapResponse extends BaseResponse {
3
- reqType: DeleteMapEnum.rst;
4
- }
5
- interface SaveMapResponse extends BaseResponse {
6
- reqType: SaveCurrentMapEnum.query;
7
- mapId: number;
8
- }
9
- interface UseMapResponse extends BaseResponse {
10
- reqType: UseMapEnum.query;
11
- mapId: number;
12
- }
13
- type TDeleteHistoryMap = (mapId: number) => Promise<DeleteMapResponse>;
14
- type TSaveMap = () => Promise<SaveMapResponse>;
15
- type TChangeCurrentMap = (mapId: number, url: string) => Promise<UseMapResponse>;
1
+ import { type TDeleteHistoryMap, type TSaveMap, type TChangeCurrentMap } from './type/protocols/historyMapProtocol';
16
2
  /**
17
3
  * 自定义 Hook,用于管理历史地图
18
- * @param devId 设备ID
19
4
  * @returns 包含删除历史地图、更改当前地图和保存地图的函数
20
5
  */
21
- export declare const useHistoryMap: (devId: string) => {
6
+ export declare const useHistoryMap: () => {
22
7
  deleteHistoryMap: TDeleteHistoryMap;
23
8
  changeCurrentMap: TChangeCurrentMap;
24
9
  saveMap: TSaveMap;
25
10
  };
26
- export {};
@@ -1,35 +1,26 @@
1
1
  import { useContext } from 'react';
2
2
  import { deleteMultipleMapFile } from '../api/deleteMultipleMapFile';
3
- import { createSetCommonParams } from './createCommonOptions';
4
- import { normalResolve } from './promise';
5
3
  import { DeleteMapEnum, SaveCurrentMapEnum, UseMapEnum } from './type';
6
4
  import { SingletonContext } from './mqttProvider';
7
5
  import { encodeDeleteMap0x2c, encodeSaveMap0x2a, encodeUseMap0x2e } from '@ray-js/robot-protocol';
8
-
9
- // 删除历史地图响应类型
10
-
11
- // 保存地图响应类型
12
-
13
- // 更改当前地图响应类型
14
-
15
- // 删除历史地图函数类型定义
16
-
17
- // 保存当前地图函数类型定义
18
-
19
- // 更改当前使用的地图函数类型定义
20
-
6
+ import { useStructuredMessage, handleMqttError } from './hooks/useStructuredMessage';
7
+ import { validator } from './type/fun';
21
8
  /**
22
9
  * 自定义 Hook,用于管理历史地图
23
- * @param devId 设备ID
24
10
  * @returns 包含删除历史地图、更改当前地图和保存地图的函数
25
11
  */
26
- export const useHistoryMap = devId => {
12
+ export const useHistoryMap = () => {
27
13
  const {
28
14
  useMqtt,
29
15
  devices,
30
16
  commandVersion
31
17
  } = useContext(SingletonContext);
32
-
18
+ const {
19
+ sendStructuredMessage
20
+ } = useStructuredMessage();
21
+ const {
22
+ devId
23
+ } = devices.common.getDevInfo();
33
24
  /**
34
25
  * 删除历史地图
35
26
  * @param mapId 地图ID
@@ -38,6 +29,7 @@ export const useHistoryMap = devId => {
38
29
 
39
30
  /**
40
31
  * 保存当前地图
32
+ * @param data 包含操作类型的参数对象(可选,默认为 oper: 0)
41
33
  * @returns Promise<SaveMapResponse> 响应结果
42
34
  */
43
35
 
@@ -50,119 +42,110 @@ export const useHistoryMap = devId => {
50
42
 
51
43
  return {
52
44
  deleteHistoryMap: async mapId => {
53
- // 参数验证
54
- if (!mapId || typeof mapId !== 'number') {
55
- throw new Error('mapId is required and must be a number');
56
- }
57
- try {
58
- // 先删除地图文件
59
- await deleteMultipleMapFile(mapId, devId);
60
- if (useMqtt) {
61
- const params = createSetCommonParams({
62
- deviceId: devId,
63
- reqType: DeleteMapEnum.set,
64
- message: {
45
+ return new Promise((resolve, reject) => {
46
+ (async () => {
47
+ try {
48
+ // 使用 TypeBox Schema 进行参数验证
49
+ const validatedData = validator.validate('delete_history_map', {
65
50
  mapId
51
+ });
52
+
53
+ // 先删除地图文件
54
+ await deleteMultipleMapFile(validatedData.mapId, devId);
55
+ if (useMqtt) {
56
+ sendStructuredMessage(DeleteMapEnum.set, {
57
+ mapId: validatedData.mapId
58
+ }, resolve, error => reject(handleMqttError(error, 'Failed to delete history map')), DeleteMapEnum.rst // 响应类型是 rst,不是 set
59
+ );
60
+ return;
66
61
  }
67
- });
68
- ty.device.sendMqttMessage(params);
69
- const {
70
- taskId
71
- } = params.message;
72
- return normalResolve(DeleteMapEnum.rst, taskId);
73
- }
74
62
 
75
- // 非 MQTT 模式:使用命令传输
76
- const command = encodeDeleteMap0x2c({
77
- id: mapId
78
- });
79
- devices.common.model.actions.command_trans.set(command);
80
- return Promise.resolve({
81
- success: true,
82
- errCode: 0,
83
- reqType: DeleteMapEnum.rst,
84
- version: commandVersion,
85
- taskId: `${Date.now()}`
86
- });
87
- } catch (error) {
88
- throw error instanceof Error ? error : new Error('Failed to delete history map');
89
- }
63
+ // 非 MQTT 模式:使用命令传输
64
+ const command = encodeDeleteMap0x2c({
65
+ id: validatedData.mapId
66
+ });
67
+ devices.common.model.actions.command_trans.set(command);
68
+ resolve({
69
+ success: true,
70
+ errCode: 0,
71
+ reqType: DeleteMapEnum.rst,
72
+ version: commandVersion,
73
+ taskId: `${Date.now()}`
74
+ });
75
+ } catch (error) {
76
+ reject(handleMqttError(error, 'Failed to delete history map'));
77
+ }
78
+ })();
79
+ });
90
80
  },
91
81
  changeCurrentMap: async (mapId, url) => {
92
- // 参数验证
93
- if (!mapId || typeof mapId !== 'number') {
94
- throw new Error('mapId is required and must be a number');
95
- }
96
- if (!url || typeof url !== 'string') {
97
- throw new Error('url is required and must be a string');
98
- }
99
- try {
100
- if (useMqtt) {
101
- const params = createSetCommonParams({
102
- deviceId: devId,
103
- reqType: UseMapEnum.set,
104
- message: {
82
+ return new Promise((resolve, reject) => {
83
+ (async () => {
84
+ try {
85
+ // 构建符合 Schema 的数据
86
+ const mapData = {
105
87
  mapId,
106
88
  url,
107
- urlLen: url.length // URL 长度
89
+ urlLen: url.length
90
+ };
91
+
92
+ // 使用 TypeBox Schema 进行参数验证
93
+ const validatedData = validator.validate('change_current_map', mapData);
94
+ if (useMqtt) {
95
+ sendStructuredMessage(UseMapEnum.set, validatedData, resolve, error => reject(handleMqttError(error, 'Failed to change current map')), UseMapEnum.query // 响应类型是 query,不是 set
96
+ );
97
+ return;
108
98
  }
109
- });
110
- ty.device.sendMqttMessage(params);
111
- const {
112
- taskId
113
- } = params.message;
114
- return normalResolve(UseMapEnum.query, taskId);
115
- }
116
99
 
117
- // 非 MQTT 模式:使用命令传输
118
- const command = encodeUseMap0x2e({
119
- mapId,
120
- url
121
- });
122
- devices.common.model.actions.command_trans.set(command);
123
- return Promise.resolve({
124
- success: true,
125
- errCode: 0,
126
- reqType: UseMapEnum.query,
127
- version: commandVersion,
128
- taskId: `${Date.now()}`,
129
- mapId
130
- });
131
- } catch (error) {
132
- throw error instanceof Error ? error : new Error('Failed to change current map');
133
- }
100
+ // 非 MQTT 模式:使用命令传输
101
+ const command = encodeUseMap0x2e({
102
+ mapId: validatedData.mapId,
103
+ url: validatedData.url
104
+ });
105
+ devices.common.model.actions.command_trans.set(command);
106
+ resolve({
107
+ success: true,
108
+ errCode: 0,
109
+ reqType: UseMapEnum.query,
110
+ version: commandVersion,
111
+ taskId: `${Date.now()}`,
112
+ mapId: validatedData.mapId
113
+ });
114
+ } catch (error) {
115
+ reject(handleMqttError(error, 'Failed to change current map'));
116
+ }
117
+ })();
118
+ });
134
119
  },
135
- saveMap: () => {
136
- try {
137
- if (useMqtt) {
138
- const params = createSetCommonParams({
139
- deviceId: devId,
140
- reqType: SaveCurrentMapEnum.set,
141
- message: {
142
- oper: 0 // 操作类型:0 表示保存
143
- }
120
+ saveMap: function () {
121
+ let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
122
+ oper: 0
123
+ };
124
+ return new Promise((resolve, reject) => {
125
+ try {
126
+ // 使用 TypeBox Schema 进行参数验证
127
+ const validatedData = validator.validate('save_map', data);
128
+ if (useMqtt) {
129
+ sendStructuredMessage(SaveCurrentMapEnum.set, validatedData, resolve, error => reject(handleMqttError(error, 'Failed to save current map')), SaveCurrentMapEnum.query // 响应类型是 query,不是 set
130
+ );
131
+ return;
132
+ }
133
+
134
+ // 非 MQTT 模式:使用命令传输
135
+ const command = encodeSaveMap0x2a(1);
136
+ devices.common.model.actions.command_trans.set(command);
137
+ resolve({
138
+ success: true,
139
+ errCode: 0,
140
+ reqType: SaveCurrentMapEnum.query,
141
+ version: commandVersion,
142
+ taskId: `${Date.now()}`,
143
+ mapId: 0 // 非 MQTT 模式下无法获取 mapId
144
144
  });
145
- ty.device.sendMqttMessage(params);
146
- const {
147
- taskId
148
- } = params.message;
149
- return normalResolve(SaveCurrentMapEnum.query, taskId);
145
+ } catch (error) {
146
+ reject(handleMqttError(error, 'Failed to save current map'));
150
147
  }
151
-
152
- // 非 MQTT 模式:使用命令传输
153
- const command = encodeSaveMap0x2a(1);
154
- devices.common.model.actions.command_trans.set(command);
155
- return Promise.resolve({
156
- success: true,
157
- errCode: 0,
158
- reqType: SaveCurrentMapEnum.query,
159
- version: commandVersion,
160
- taskId: `${Date.now()}`,
161
- mapId: 0 // 非 MQTT 模式下无法获取 mapId
162
- });
163
- } catch (error) {
164
- throw error instanceof Error ? error : new Error('Failed to save current map');
165
- }
148
+ });
166
149
  }
167
150
  };
168
151
  };
@@ -1,15 +1,8 @@
1
- import { Point } from '../utils';
2
- import { BaseResponse, PartDivisionEnum } from './type';
3
- interface PartDivisionResponse extends BaseResponse {
4
- reqType: PartDivisionEnum.query;
5
- }
6
- type TSetPartDivision = (points: Point[], roomId: number, origin: Point) => Promise<PartDivisionResponse>;
1
+ import { type TSetPartDivision } from './type/protocols';
7
2
  /**
8
3
  * 自定义 Hook,用于房间分隔
9
- * @param devId 设备ID
10
4
  * @returns 包含 setPartDivision 函数的对象
11
5
  */
12
- export declare const usePartDivision: (devId: string) => {
6
+ export declare const usePartDivision: () => {
13
7
  setPartDivision: TSetPartDivision;
14
8
  };
15
- export {};