@ray-js/robot-data-stream 0.0.6 → 0.0.7

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.
package/lib/api/p2pApi.js CHANGED
@@ -1,3 +1,5 @@
1
+ /* eslint-disable consistent-return */
2
+ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
1
3
  /* eslint-disable prefer-promise-reject-errors */
2
4
  import { p2p } from '@ray-js/ray';
3
5
  import moment from 'moment';
@@ -31,11 +33,15 @@ export default class P2pApi {
31
33
  if (this.isConnected) {
32
34
  typeof successCb === 'function' && successCb();
33
35
  }
34
- }, () => {}, () => {
36
+ }, () => {
37
+ //
38
+ }, () => {
35
39
  log4js.info('reconnect complete ==>', String(this.isConnected));
36
40
  if (!this.isConnected) {
37
41
  log4js.warn('p2p reconnect failed ==>', moment().format('YYYY-MM-DD HH:mm:ss'));
38
- this.reconnectP2p(successCb);
42
+ setTimeout(() => {
43
+ this.reconnectP2p(successCb);
44
+ }, 3000);
39
45
  }
40
46
  });
41
47
  }
@@ -155,7 +161,9 @@ export default class P2pApi {
155
161
  }, 500);
156
162
  resolve(false);
157
163
  },
158
- complete: () => {}
164
+ complete: () => {
165
+ //
166
+ }
159
167
  });
160
168
  });
161
169
  }
@@ -192,7 +200,9 @@ export default class P2pApi {
192
200
  }, 500);
193
201
  resolve(false);
194
202
  },
195
- complete: () => {}
203
+ complete: () => {
204
+ //
205
+ }
196
206
  });
197
207
  });
198
208
  }
@@ -239,7 +249,9 @@ export default class P2pApi {
239
249
  }, 500);
240
250
  resolve(false);
241
251
  },
242
- complete: () => {}
252
+ complete: () => {
253
+ //
254
+ }
243
255
  });
244
256
  });
245
257
  }
@@ -351,12 +363,6 @@ export default class P2pApi {
351
363
  * @returns
352
364
  */
353
365
  deInitP2PSDK = async () => {
354
- // 先销毁断开监听
355
- typeof this.offSessionStatusChange === 'function' && this.offSessionStatusChange();
356
- // 销毁初始化时,先进行断连操作
357
- if (this.isConnected) {
358
- await this.disconnectDevice();
359
- }
360
366
  return new Promise((resolve, reject) => {
361
367
  try {
362
368
  p2p.deInitSDK({
@@ -169,7 +169,7 @@ export declare class SweeperP2p extends P2pApi {
169
169
  /**
170
170
  * 停止文件下载
171
171
  */
172
- stopObserverSweeperDataByP2P: () => Promise<unknown>;
172
+ stopObserverSweeperDataByP2P: () => Promise<false | undefined>;
173
173
  }
174
174
  declare const sweeperP2pInstance: SweeperP2p;
175
175
  export default sweeperP2pInstance;
@@ -535,12 +535,18 @@ export class SweeperP2p extends P2pApi {
535
535
  /**
536
536
  * 停止文件下载
537
537
  */
538
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, consistent-return
538
539
  stopObserverSweeperDataByP2P = async () => {
539
540
  try {
540
541
  this.removeP2pDownloadEvent();
541
- const isCancel = await this.cancelDownloadTask();
542
- log4js.info('cancelDownloadTask ==>', isCancel);
543
- return isCancel;
542
+ await this.cancelDownloadTask();
543
+
544
+ // 先销毁断开监听
545
+ typeof this.offSessionStatusChange === 'function' && this.offSessionStatusChange();
546
+ // 销毁初始化时,先进行断连操作
547
+ if (this.isConnected) {
548
+ this.disconnectDevice();
549
+ }
544
550
  } catch (e) {
545
551
  log4js.error('stopObserverSweeperDataByP2P occur error ==>', e);
546
552
  return false;
package/lib/index.js CHANGED
@@ -11,7 +11,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
11
11
  onReceiveAIPicData,
12
12
  onReceiveAIPicHDData
13
13
  } = opt || {};
14
- const isInit = useRef(null);
14
+ const isInit = useRef(false);
15
15
  const offSessionStatusChange = useRef(null);
16
16
  const isAppOnBackground = useRef(false);
17
17
  const timer = useRef(null);
@@ -33,11 +33,14 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
33
33
  SweeperP2pInstance.removeP2pDownloadEvent();
34
34
  };
35
35
  }
36
- isInitP2p();
36
+ SweeperP2pInstance.initP2pSdk(devId).then(() => {
37
+ connectP2p();
38
+ });
37
39
  onEnterBackground();
38
40
  onEnterForeground();
39
41
  return () => {
40
42
  unmount();
43
+ SweeperP2pInstance.deInitP2PSDK();
41
44
  timer && clearInterval(timer);
42
45
  };
43
46
  }, []);
@@ -45,22 +48,21 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
45
48
  /**
46
49
  * p2p连接
47
50
  */
48
- const isInitP2p = async () => {
49
- log4js.info('hooks has been started initP2p');
50
- isInit.current = await SweeperP2pInstance.initP2pSdk(devId);
51
- if (isInit.current) {
52
- SweeperP2pInstance.isConnecting = true;
53
- SweeperP2pInstance.connectDevice(() => {
51
+ const connectP2p = async () => {
52
+ log4js.info('hooks has been started connectP2p');
53
+ SweeperP2pInstance.isConnecting = true;
54
+ SweeperP2pInstance.connectDevice(() => {
55
+ isInit.current = true;
56
+ SweeperP2pInstance.startObserverSweeperDataByP2P(1, devId, onReceiveMapData, onReceivePathData, onReceiveAIPicData, onReceiveAIPicHDData);
57
+ offSessionStatusChange.current = SweeperP2pInstance.onSessionStatusChange(SweeperP2pInstance.sessionStatusCallback);
58
+ }, () => {
59
+ SweeperP2pInstance.reconnectP2p(() => {
60
+ isInit.current = true;
54
61
  SweeperP2pInstance.startObserverSweeperDataByP2P(1, devId, onReceiveMapData, onReceivePathData, onReceiveAIPicData, onReceiveAIPicHDData);
62
+ // 这里失败重连需要注册断开重连的事件
55
63
  offSessionStatusChange.current = SweeperP2pInstance.onSessionStatusChange(SweeperP2pInstance.sessionStatusCallback);
56
- }, () => {
57
- SweeperP2pInstance.reconnectP2p(() => {
58
- SweeperP2pInstance.startObserverSweeperDataByP2P(1, devId, onReceiveMapData, onReceivePathData, onReceiveAIPicData, onReceiveAIPicHDData);
59
- // 这里失败重连需要注册断开重连的事件
60
- offSessionStatusChange.current = SweeperP2pInstance.onSessionStatusChange(SweeperP2pInstance.sessionStatusCallback);
61
- });
62
64
  });
63
- }
65
+ });
64
66
  };
65
67
 
66
68
  /**
@@ -79,7 +81,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
79
81
  }
80
82
  clearTimeout(timer.current);
81
83
  timer.current = null;
82
- }, 2 * 60 * 1000);
84
+ }, 10 * 1000);
83
85
  }
84
86
  });
85
87
  };
@@ -94,8 +96,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
94
96
  offSessionStatusChange.current();
95
97
  offSessionStatusChange.current = null;
96
98
  }
97
- await SweeperP2pInstance.stopObserverSweeperDataByP2P();
98
- await SweeperP2pInstance.deInitP2PSDK();
99
+ SweeperP2pInstance.stopObserverSweeperDataByP2P();
99
100
  };
100
101
 
101
102
  /**
@@ -104,10 +105,14 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
104
105
  const onEnterForeground = () => {
105
106
  ty.onAppShow(() => {
106
107
  log4js.info('hooks onAppShow');
107
- isAppOnBackground.current = false;
108
- if (!isInit.current) {
109
- isInitP2p();
110
- }
108
+ setTimeout(() => {
109
+ var _ty$p2p$isP2PActiveSy, _ty$p2p;
110
+ if (!isInit.current || !((_ty$p2p$isP2PActiveSy = (_ty$p2p = ty.p2p).isP2PActiveSync) !== null && _ty$p2p$isP2PActiveSy !== void 0 && _ty$p2p$isP2PActiveSy.call(_ty$p2p, {
111
+ deviceId: devId
112
+ }))) {
113
+ connectP2p();
114
+ }
115
+ }, 500);
111
116
  });
112
117
  };
113
118
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/robot-data-stream",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "扫地机P2P数据流标准化组件",
5
5
  "main": "lib/index",
6
6
  "files": [