@ray-js/robot-data-stream 0.0.10-beta-16 → 0.0.10-beta-18
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.d.ts +4 -1
- package/lib/api/p2pApi.js +100 -29
- package/lib/api/sweeperP2p.d.ts +3 -1
- package/lib/api/sweeperP2p.js +66 -20
- package/lib/index.d.ts +2 -0
- package/lib/index.js +30 -12
- package/lib/mqtt/promise.js +8 -7
- package/lib/mqtt/useZoneClean.js +1 -1
- package/lib/utils/index.d.ts +4 -0
- package/lib/utils/index.js +13 -0
- package/package.json +1 -1
package/lib/api/p2pApi.d.ts
CHANGED
|
@@ -5,8 +5,11 @@ export default class P2pApi {
|
|
|
5
5
|
isConnected: boolean;
|
|
6
6
|
isConnecting: boolean;
|
|
7
7
|
offSessionStatusChange: () => void;
|
|
8
|
+
onLogger: (type: 'warn' | 'error' | 'info', v: string) => void;
|
|
8
9
|
devId: string;
|
|
9
|
-
constructor(
|
|
10
|
+
constructor(props: {
|
|
11
|
+
onLogger: (type: 'warn' | 'error' | 'info', data: string) => void;
|
|
12
|
+
});
|
|
10
13
|
/**
|
|
11
14
|
* 设备断开之后的重连
|
|
12
15
|
*/
|
package/lib/api/p2pApi.js
CHANGED
|
@@ -3,16 +3,19 @@
|
|
|
3
3
|
/* eslint-disable prefer-promise-reject-errors */
|
|
4
4
|
import { p2p } from '@ray-js/ray';
|
|
5
5
|
import moment from 'moment';
|
|
6
|
-
import
|
|
6
|
+
import { logger } from '../utils';
|
|
7
7
|
/**
|
|
8
8
|
* P2P 工具类
|
|
9
9
|
*/
|
|
10
10
|
export default class P2pApi {
|
|
11
11
|
// P2p连接状态
|
|
12
12
|
|
|
13
|
-
constructor() {
|
|
13
|
+
constructor(props) {
|
|
14
14
|
this.isConnected = false;
|
|
15
15
|
this.isConnecting = false;
|
|
16
|
+
if (props && props.onLogger) {
|
|
17
|
+
this.onLogger = props === null || props === void 0 ? void 0 : props.onLogger;
|
|
18
|
+
}
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
/**
|
|
@@ -26,9 +29,13 @@ export default class P2pApi {
|
|
|
26
29
|
*/
|
|
27
30
|
this.isConnected = false;
|
|
28
31
|
if (!this.isConnected) {
|
|
29
|
-
|
|
32
|
+
logger('info', {
|
|
33
|
+
msg: `start p2p reconnect ==> ${moment().format('YYYY-MM-DD HH:mm:ss')}`
|
|
34
|
+
}, this.onLogger);
|
|
30
35
|
this.connectDevice(() => {
|
|
31
|
-
|
|
36
|
+
logger('info', {
|
|
37
|
+
msg: `p2p reconnect success ==> ${moment().format('YYYY-MM-DD HH:mm:ss')}`
|
|
38
|
+
}, this.onLogger);
|
|
32
39
|
this.isConnected = true;
|
|
33
40
|
if (this.isConnected) {
|
|
34
41
|
typeof successCb === 'function' && successCb();
|
|
@@ -36,9 +43,13 @@ export default class P2pApi {
|
|
|
36
43
|
}, () => {
|
|
37
44
|
//
|
|
38
45
|
}, () => {
|
|
39
|
-
|
|
46
|
+
logger('info', {
|
|
47
|
+
msg: `reconnect complete ==> ${String(this.isConnected)}`
|
|
48
|
+
}, this.onLogger);
|
|
40
49
|
if (!this.isConnected) {
|
|
41
|
-
|
|
50
|
+
logger('warn', {
|
|
51
|
+
msg: `p2p reconnect failed ==> ${moment().format('YYYY-MM-DD HH:mm:ss')}`
|
|
52
|
+
}, this.onLogger);
|
|
42
53
|
setTimeout(() => {
|
|
43
54
|
this.reconnectP2p(successCb);
|
|
44
55
|
}, 3000);
|
|
@@ -58,11 +69,16 @@ export default class P2pApi {
|
|
|
58
69
|
this.devId = devId;
|
|
59
70
|
p2p.P2PSDKInit({
|
|
60
71
|
success: () => {
|
|
61
|
-
|
|
72
|
+
logger('info', {
|
|
73
|
+
msg: 'P2PSDKInit success'
|
|
74
|
+
}, this.onLogger);
|
|
62
75
|
resolve(true);
|
|
63
76
|
},
|
|
64
77
|
fail: params => {
|
|
65
|
-
|
|
78
|
+
logger('warn', {
|
|
79
|
+
msg: 'P2PSDKInit success',
|
|
80
|
+
params
|
|
81
|
+
}, this.onLogger);
|
|
66
82
|
resolve(false);
|
|
67
83
|
}
|
|
68
84
|
});
|
|
@@ -84,14 +100,19 @@ export default class P2pApi {
|
|
|
84
100
|
timeout: 5000,
|
|
85
101
|
mode: 0,
|
|
86
102
|
success: () => {
|
|
87
|
-
|
|
103
|
+
logger('info', {
|
|
104
|
+
msg: 'p2p connectDevice success'
|
|
105
|
+
}, this.onLogger);
|
|
88
106
|
this.isConnected = true;
|
|
89
107
|
this.isConnecting = false;
|
|
90
108
|
typeof successCb === 'function' && successCb();
|
|
91
109
|
resolve(true);
|
|
92
110
|
},
|
|
93
111
|
fail: params => {
|
|
94
|
-
|
|
112
|
+
logger('warn', {
|
|
113
|
+
msg: 'p2p connectDevice failed ==>',
|
|
114
|
+
params
|
|
115
|
+
}, this.onLogger);
|
|
95
116
|
typeof failCb === 'function' && failCb();
|
|
96
117
|
resolve(false);
|
|
97
118
|
},
|
|
@@ -100,7 +121,10 @@ export default class P2pApi {
|
|
|
100
121
|
}
|
|
101
122
|
});
|
|
102
123
|
} catch (e) {
|
|
103
|
-
|
|
124
|
+
logger('error', {
|
|
125
|
+
msg: 'p2p connectDevice occur an error ==>',
|
|
126
|
+
e
|
|
127
|
+
}, this.onLogger);
|
|
104
128
|
reject(false);
|
|
105
129
|
}
|
|
106
130
|
});
|
|
@@ -117,16 +141,23 @@ export default class P2pApi {
|
|
|
117
141
|
deviceId: this.devId,
|
|
118
142
|
success: () => {
|
|
119
143
|
this.isConnected = false;
|
|
120
|
-
|
|
144
|
+
logger('info', {
|
|
145
|
+
msg: 'p2p disconnectDevice success'
|
|
146
|
+
}, this.onLogger);
|
|
121
147
|
resolve(true);
|
|
122
148
|
},
|
|
123
149
|
fail: () => {
|
|
124
|
-
|
|
150
|
+
logger('warn', {
|
|
151
|
+
msg: 'p2p disconnectDevice failed'
|
|
152
|
+
}, this.onLogger);
|
|
125
153
|
resolve(false);
|
|
126
154
|
}
|
|
127
155
|
});
|
|
128
156
|
} catch (e) {
|
|
129
|
-
|
|
157
|
+
logger('warn', {
|
|
158
|
+
msg: 'p2p disconnectDevice occur an error ==>',
|
|
159
|
+
e
|
|
160
|
+
}, this.onLogger);
|
|
130
161
|
reject(false);
|
|
131
162
|
}
|
|
132
163
|
});
|
|
@@ -150,12 +181,17 @@ export default class P2pApi {
|
|
|
150
181
|
albumName: albumName,
|
|
151
182
|
jsonfiles: JSON.stringify(files),
|
|
152
183
|
success: () => {
|
|
153
|
-
|
|
184
|
+
logger('info', {
|
|
185
|
+
msg: 'p2p downloadStream success'
|
|
186
|
+
}, this.onLogger);
|
|
154
187
|
typeof successCb === 'function' && successCb();
|
|
155
188
|
resolve(true);
|
|
156
189
|
},
|
|
157
190
|
fail: params => {
|
|
158
|
-
|
|
191
|
+
logger('warn', {
|
|
192
|
+
msg: 'p2p downloadStream failed===>',
|
|
193
|
+
params
|
|
194
|
+
}, this.onLogger);
|
|
159
195
|
setTimeout(() => {
|
|
160
196
|
typeof failedCb === 'function' && failedCb();
|
|
161
197
|
}, 500);
|
|
@@ -168,7 +204,10 @@ export default class P2pApi {
|
|
|
168
204
|
});
|
|
169
205
|
}
|
|
170
206
|
} catch (e) {
|
|
171
|
-
|
|
207
|
+
logger('warn', {
|
|
208
|
+
msg: 'p2p downloadStream occur an error ==>',
|
|
209
|
+
e
|
|
210
|
+
}, this.onLogger);
|
|
172
211
|
}
|
|
173
212
|
};
|
|
174
213
|
|
|
@@ -189,12 +228,17 @@ export default class P2pApi {
|
|
|
189
228
|
albumName: albumName,
|
|
190
229
|
jsonfiles: JSON.stringify(files),
|
|
191
230
|
success: () => {
|
|
192
|
-
|
|
231
|
+
logger('info', {
|
|
232
|
+
msg: 'p2p appendDownloadStream success'
|
|
233
|
+
}, this.onLogger);
|
|
193
234
|
typeof successCb === 'function' && successCb();
|
|
194
235
|
resolve(true);
|
|
195
236
|
},
|
|
196
237
|
fail: params => {
|
|
197
|
-
|
|
238
|
+
logger('warn', {
|
|
239
|
+
msg: 'p2p appendDownloadStream failed===>',
|
|
240
|
+
params
|
|
241
|
+
}, this.onLogger);
|
|
198
242
|
setTimeout(() => {
|
|
199
243
|
typeof failedCb === 'function' && failedCb();
|
|
200
244
|
}, 500);
|
|
@@ -207,7 +251,10 @@ export default class P2pApi {
|
|
|
207
251
|
});
|
|
208
252
|
}
|
|
209
253
|
} catch (e) {
|
|
210
|
-
|
|
254
|
+
logger('warn', {
|
|
255
|
+
msg: 'p2p appendDownloadStream failed===>',
|
|
256
|
+
e
|
|
257
|
+
}, this.onLogger);
|
|
211
258
|
}
|
|
212
259
|
};
|
|
213
260
|
|
|
@@ -238,12 +285,17 @@ export default class P2pApi {
|
|
|
238
285
|
filePath: filePath,
|
|
239
286
|
jsonfiles: JSON.stringify(files),
|
|
240
287
|
success: () => {
|
|
241
|
-
|
|
288
|
+
logger('info', {
|
|
289
|
+
msg: 'p2p downloadFile success'
|
|
290
|
+
}, this.onLogger);
|
|
242
291
|
typeof successCb === 'function' && successCb();
|
|
243
292
|
resolve(true);
|
|
244
293
|
},
|
|
245
294
|
fail: params => {
|
|
246
|
-
|
|
295
|
+
logger('warn', {
|
|
296
|
+
msg: 'p2p downloadFile success',
|
|
297
|
+
params
|
|
298
|
+
}, this.onLogger);
|
|
247
299
|
setTimeout(() => {
|
|
248
300
|
typeof failedCb === 'function' && failedCb();
|
|
249
301
|
}, 500);
|
|
@@ -256,7 +308,10 @@ export default class P2pApi {
|
|
|
256
308
|
});
|
|
257
309
|
}
|
|
258
310
|
} catch (e) {
|
|
259
|
-
|
|
311
|
+
logger('error', {
|
|
312
|
+
msg: 'p2p downloadFile occur an error ==>',
|
|
313
|
+
e
|
|
314
|
+
}, this.onLogger);
|
|
260
315
|
}
|
|
261
316
|
return null;
|
|
262
317
|
};
|
|
@@ -330,7 +385,10 @@ export default class P2pApi {
|
|
|
330
385
|
}
|
|
331
386
|
});
|
|
332
387
|
} catch (e) {
|
|
333
|
-
|
|
388
|
+
logger('info', {
|
|
389
|
+
msg: 'cancelDownloadTask occur an error',
|
|
390
|
+
e
|
|
391
|
+
}, this.onLogger);
|
|
334
392
|
reject(false);
|
|
335
393
|
}
|
|
336
394
|
});
|
|
@@ -347,11 +405,17 @@ export default class P2pApi {
|
|
|
347
405
|
deviceId: this.devId,
|
|
348
406
|
albumName,
|
|
349
407
|
success: params => {
|
|
350
|
-
|
|
408
|
+
logger('info', {
|
|
409
|
+
msg: 'queryAlbumFileIndexs ==>',
|
|
410
|
+
params
|
|
411
|
+
}, this.onLogger);
|
|
351
412
|
resolve(params);
|
|
352
413
|
},
|
|
353
414
|
fail: params => {
|
|
354
|
-
|
|
415
|
+
logger('warn', {
|
|
416
|
+
msg: 'queryAlbumFileIndexs failed ==>',
|
|
417
|
+
params
|
|
418
|
+
}, this.onLogger);
|
|
355
419
|
resolve(null);
|
|
356
420
|
}
|
|
357
421
|
});
|
|
@@ -367,11 +431,15 @@ export default class P2pApi {
|
|
|
367
431
|
try {
|
|
368
432
|
p2p.deInitSDK({
|
|
369
433
|
success: () => {
|
|
370
|
-
|
|
434
|
+
logger('info', {
|
|
435
|
+
msg: 'deInitP2pSDK success'
|
|
436
|
+
}, this.onLogger);
|
|
371
437
|
resolve(true);
|
|
372
438
|
},
|
|
373
439
|
fail: () => {
|
|
374
|
-
|
|
440
|
+
logger('info', {
|
|
441
|
+
msg: 'deInitP2pSDK failed'
|
|
442
|
+
}, this.onLogger);
|
|
375
443
|
resolve(false);
|
|
376
444
|
},
|
|
377
445
|
complete: () => {
|
|
@@ -379,7 +447,10 @@ export default class P2pApi {
|
|
|
379
447
|
}
|
|
380
448
|
});
|
|
381
449
|
} catch (e) {
|
|
382
|
-
|
|
450
|
+
logger('error', {
|
|
451
|
+
msg: 'deInitP2pSDK occur an error ==>',
|
|
452
|
+
e
|
|
453
|
+
}, this.onLogger);
|
|
383
454
|
reject(false);
|
|
384
455
|
}
|
|
385
456
|
});
|
package/lib/api/sweeperP2p.d.ts
CHANGED
|
@@ -72,7 +72,9 @@ export declare class SweeperP2p extends P2pApi {
|
|
|
72
72
|
offP2pStreamPacketReceive: () => void;
|
|
73
73
|
offDownLoadProgressUpdate: () => void;
|
|
74
74
|
offTotalDownLoadProgressUpdate: () => void;
|
|
75
|
-
constructor(
|
|
75
|
+
constructor(props: {
|
|
76
|
+
onLogger: (type: 'warn' | 'error' | 'info', data: string) => void;
|
|
77
|
+
});
|
|
76
78
|
private setStreamFilePath;
|
|
77
79
|
private setDataFilePath;
|
|
78
80
|
/**
|
package/lib/api/sweeperP2p.js
CHANGED
|
@@ -5,7 +5,7 @@ import "core-js/modules/esnext.iterator.map.js";
|
|
|
5
5
|
import P2pApi from './p2pApi';
|
|
6
6
|
import Base64 from 'base64-js';
|
|
7
7
|
import { padStart, join, map } from 'lodash-es';
|
|
8
|
-
import
|
|
8
|
+
import { logger } from '../utils';
|
|
9
9
|
/**
|
|
10
10
|
* 基于P2p工具类的扫地机扩展实现
|
|
11
11
|
*/
|
|
@@ -46,8 +46,11 @@ const FILE_NAME_MAP = {
|
|
|
46
46
|
// 走p2p流传输(新) or 读取bin文件(旧)
|
|
47
47
|
const shouldDownloadStream = Boolean(ty.p2p.downloadStream && ty.p2p.onStreamPacketReceive);
|
|
48
48
|
export class SweeperP2p extends P2pApi {
|
|
49
|
-
constructor() {
|
|
50
|
-
super();
|
|
49
|
+
constructor(props) {
|
|
50
|
+
super(props);
|
|
51
|
+
if (props && props.onLogger) {
|
|
52
|
+
this.onLogger = props.onLogger;
|
|
53
|
+
}
|
|
51
54
|
this.file = undefined;
|
|
52
55
|
this.downloadType = 1;
|
|
53
56
|
this.fileIndex = -1; // -1 表示所有文件下载完成
|
|
@@ -112,10 +115,16 @@ export class SweeperP2p extends P2pApi {
|
|
|
112
115
|
dirPath: filePath,
|
|
113
116
|
recursive: true
|
|
114
117
|
});
|
|
115
|
-
|
|
118
|
+
logger('info', {
|
|
119
|
+
msg: 'mkdirSync success: filePath ==>',
|
|
120
|
+
filePath
|
|
121
|
+
}, this.onLogger);
|
|
116
122
|
return true;
|
|
117
123
|
} catch (e) {
|
|
118
|
-
|
|
124
|
+
logger('error', {
|
|
125
|
+
msg: 'mkdirSync error ==>',
|
|
126
|
+
e
|
|
127
|
+
}, this.onLogger);
|
|
119
128
|
return false;
|
|
120
129
|
}
|
|
121
130
|
};
|
|
@@ -130,11 +139,17 @@ export class SweeperP2p extends P2pApi {
|
|
|
130
139
|
ty.getFileSystemManager().access({
|
|
131
140
|
path: filePath,
|
|
132
141
|
success(params) {
|
|
133
|
-
|
|
142
|
+
logger('info', {
|
|
143
|
+
msg: 'file access success ==>',
|
|
144
|
+
params
|
|
145
|
+
}, this.onLogger);
|
|
134
146
|
resolve(true);
|
|
135
147
|
},
|
|
136
148
|
fail(params) {
|
|
137
|
-
|
|
149
|
+
logger('warn', {
|
|
150
|
+
msg: 'file access fail ==>',
|
|
151
|
+
params
|
|
152
|
+
}, this.onLogger);
|
|
138
153
|
resolve(false);
|
|
139
154
|
}
|
|
140
155
|
});
|
|
@@ -183,13 +198,19 @@ export class SweeperP2p extends P2pApi {
|
|
|
183
198
|
* @param data
|
|
184
199
|
*/
|
|
185
200
|
sessionStatusCallback = data => {
|
|
186
|
-
|
|
201
|
+
logger('info', {
|
|
202
|
+
msg: 'sessionStatusCallback ==>',
|
|
203
|
+
data
|
|
204
|
+
}, this.onLogger);
|
|
187
205
|
if (data) {
|
|
188
206
|
const {
|
|
189
207
|
status
|
|
190
208
|
} = data;
|
|
191
209
|
if (status < 0) {
|
|
192
|
-
|
|
210
|
+
logger('info', {
|
|
211
|
+
msg: 'receive disconnect notice ==>',
|
|
212
|
+
status
|
|
213
|
+
}, this.onLogger);
|
|
193
214
|
this.isConnected = false;
|
|
194
215
|
if (!this.isConnecting) {
|
|
195
216
|
this.reconnectP2p(() => {
|
|
@@ -197,7 +218,9 @@ export class SweeperP2p extends P2pApi {
|
|
|
197
218
|
this.startObserverSweeperDataByP2P(this.downloadType, this.devId, this.onReceiveMapData, this.onReceivePathData, this.onReceiveAIPicData, this.onReceiveAIPicHDData);
|
|
198
219
|
});
|
|
199
220
|
} else {
|
|
200
|
-
|
|
221
|
+
logger('warn', {
|
|
222
|
+
msg: 'receive disconnect notice, but connectDevice is connecting'
|
|
223
|
+
}, this.onLogger);
|
|
201
224
|
}
|
|
202
225
|
}
|
|
203
226
|
}
|
|
@@ -211,7 +234,9 @@ export class SweeperP2p extends P2pApi {
|
|
|
211
234
|
startObserverSweeperDataByP2P = async (downloadType, devId, onReceiveMapData, onReceivePathData, onReceiveAIPicData, onReceiveAIPicHDData) => {
|
|
212
235
|
var _this$file$items;
|
|
213
236
|
if (![0, 1].some(item => item === downloadType)) {
|
|
214
|
-
|
|
237
|
+
logger('warn', {
|
|
238
|
+
msg: 'download type must be 0 or 1'
|
|
239
|
+
}, this.onLogger);
|
|
215
240
|
return;
|
|
216
241
|
}
|
|
217
242
|
this.onReceiveMapData = onReceiveMapData;
|
|
@@ -222,7 +247,9 @@ export class SweeperP2p extends P2pApi {
|
|
|
222
247
|
this.onReceiveAIPicHDData = onReceiveAIPicHDData || (() => {
|
|
223
248
|
// do nothing
|
|
224
249
|
});
|
|
225
|
-
|
|
250
|
+
logger('info', {
|
|
251
|
+
msg: 'startObserverSweeperDataByP2P ==>'
|
|
252
|
+
}, this.onLogger);
|
|
226
253
|
this.downloadType = downloadType;
|
|
227
254
|
this.setDataFilePath(devId);
|
|
228
255
|
this.setStreamFilePath(devId);
|
|
@@ -290,7 +317,9 @@ export class SweeperP2p extends P2pApi {
|
|
|
290
317
|
* 注册下载有关的监听
|
|
291
318
|
*/
|
|
292
319
|
registerP2pDownloadEvent = () => {
|
|
293
|
-
|
|
320
|
+
logger('info', {
|
|
321
|
+
msg: 'registerP2pDownloadEvent ==>'
|
|
322
|
+
}, this.onLogger);
|
|
294
323
|
if (shouldDownloadStream) {
|
|
295
324
|
// p2p数据流监听
|
|
296
325
|
this.offP2pStreamPacketReceive = this.onP2pStreamPacketReceive(this.p2pStreamPacketReceiveCallback);
|
|
@@ -311,7 +340,9 @@ export class SweeperP2p extends P2pApi {
|
|
|
311
340
|
* 移除下载有关的监听
|
|
312
341
|
*/
|
|
313
342
|
removeP2pDownloadEvent = () => {
|
|
314
|
-
|
|
343
|
+
logger('info', {
|
|
344
|
+
msg: 'removeP2pDownloadEvent ==>'
|
|
345
|
+
}, this.onLogger);
|
|
315
346
|
if (shouldDownloadStream) {
|
|
316
347
|
[...this.packetSerialNumberCacheMap.keys()].forEach(key => {
|
|
317
348
|
this.packetSerialNumberCacheMap.set(key, -1);
|
|
@@ -374,7 +405,9 @@ export class SweeperP2p extends P2pApi {
|
|
|
374
405
|
}
|
|
375
406
|
const cachePacketMap = this.packetDataCacheMap.get(fileName);
|
|
376
407
|
if (!cachePacketMap) {
|
|
377
|
-
|
|
408
|
+
logger('warn', {
|
|
409
|
+
msg: `p2pStreamPacketReceiveCallback: fileName : ${fileName} is not support`
|
|
410
|
+
}, this.onLogger);
|
|
378
411
|
return;
|
|
379
412
|
}
|
|
380
413
|
const cacheSerialNumber = this.packetSerialNumberCacheMap.get(fileName);
|
|
@@ -407,7 +440,9 @@ export class SweeperP2p extends P2pApi {
|
|
|
407
440
|
}).join('');
|
|
408
441
|
if (this.firstPackageTime > 0 && fileName.indexOf('map') !== -1) {
|
|
409
442
|
const packageTime = Date.now() - this.firstPackageTime;
|
|
410
|
-
|
|
443
|
+
logger('info', {
|
|
444
|
+
msg: `receive first full package, cost time: ${packageTime} ms, fileName: ${fileName}`
|
|
445
|
+
}, this.onLogger);
|
|
411
446
|
this.firstPackageTime = 0;
|
|
412
447
|
}
|
|
413
448
|
const {
|
|
@@ -467,7 +502,9 @@ export class SweeperP2p extends P2pApi {
|
|
|
467
502
|
const type = this.getFileType(fileName);
|
|
468
503
|
if (this.cacheData[type] !== hexValue) {
|
|
469
504
|
if (hexValue.length === 0) {
|
|
470
|
-
|
|
505
|
+
logger('warn', {
|
|
506
|
+
msg: 'receive empty data'
|
|
507
|
+
}, this.onLogger);
|
|
471
508
|
return;
|
|
472
509
|
}
|
|
473
510
|
if (type === 0 || type === 6) {
|
|
@@ -486,13 +523,19 @@ export class SweeperP2p extends P2pApi {
|
|
|
486
523
|
}
|
|
487
524
|
},
|
|
488
525
|
fail: e => {
|
|
489
|
-
|
|
526
|
+
logger('warn', {
|
|
527
|
+
msg: 'readFileFromPath failed ==>',
|
|
528
|
+
e
|
|
529
|
+
}, this.onLogger);
|
|
490
530
|
this.resetReading(fileName);
|
|
491
531
|
}
|
|
492
532
|
});
|
|
493
533
|
} catch (e) {
|
|
494
534
|
this.resetReading(fileName);
|
|
495
|
-
|
|
535
|
+
logger('error', {
|
|
536
|
+
msg: 'readFileFromPath ==>',
|
|
537
|
+
e
|
|
538
|
+
}, this.onLogger);
|
|
496
539
|
}
|
|
497
540
|
};
|
|
498
541
|
setReading = fileName => {
|
|
@@ -577,7 +620,10 @@ export class SweeperP2p extends P2pApi {
|
|
|
577
620
|
this.disconnectDevice();
|
|
578
621
|
}
|
|
579
622
|
} catch (e) {
|
|
580
|
-
|
|
623
|
+
logger('error', {
|
|
624
|
+
msg: 'stopObserverSweeperDataByP2P occur error ==>',
|
|
625
|
+
e
|
|
626
|
+
}, this.onLogger);
|
|
581
627
|
return false;
|
|
582
628
|
}
|
|
583
629
|
};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export * from './mqtt';
|
|
2
2
|
export * from './mqtt/type/requestType';
|
|
3
|
+
type TOnLogger = (type: 'warn' | 'error' | 'info', data: string) => void;
|
|
3
4
|
declare const useP2PDataStream: (devId: string, onReceiveMapData: (data: string) => void, onReceivePathData: (data: string) => void, opt?: {
|
|
4
5
|
logTag?: string | undefined;
|
|
5
6
|
onReceiveAIPicData?: ((data: string) => void) | undefined;
|
|
6
7
|
onReceiveAIPicHDData?: ((data: string) => void) | undefined;
|
|
8
|
+
onLogger?: TOnLogger | undefined;
|
|
7
9
|
} | undefined) => {
|
|
8
10
|
appendDownloadStreamDuringTask: (files: Array<string>, successCb?: () => void, failedCb?: () => void) => Promise<boolean> | void;
|
|
9
11
|
};
|
package/lib/index.js
CHANGED
|
@@ -1,30 +1,37 @@
|
|
|
1
1
|
/* eslint-disable consistent-return */
|
|
2
2
|
import { useEffect, useRef, useCallback } from 'react';
|
|
3
3
|
import log4js from '@ray-js/log4js';
|
|
4
|
-
import {
|
|
5
|
-
import sweeperP2pInstance from './api/sweeperP2p';
|
|
4
|
+
import { SweeperP2p } from './api/sweeperP2p';
|
|
6
5
|
import { getSystemInfoSync, usePageEvent } from '@ray-js/ray';
|
|
7
|
-
import { emitter } from './utils';
|
|
6
|
+
import { emitter, logger } from './utils';
|
|
8
7
|
export * from './mqtt';
|
|
9
8
|
export * from './mqtt/type/requestType';
|
|
10
9
|
const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
|
|
11
10
|
const {
|
|
12
11
|
logTag,
|
|
13
12
|
onReceiveAIPicData,
|
|
14
|
-
onReceiveAIPicHDData
|
|
13
|
+
onReceiveAIPicHDData,
|
|
14
|
+
onLogger
|
|
15
15
|
} = opt || {};
|
|
16
16
|
const isInit = useRef(false);
|
|
17
17
|
const offSessionStatusChange = useRef(null);
|
|
18
18
|
const isAppOnBackground = useRef(false);
|
|
19
19
|
const timer = useRef(null);
|
|
20
20
|
const isIDE = getSystemInfoSync().brand === 'devtools';
|
|
21
|
+
const SweeperP2pInstance = new SweeperP2p({
|
|
22
|
+
onLogger
|
|
23
|
+
});
|
|
21
24
|
const handleAppHide = useCallback(() => {
|
|
22
|
-
|
|
25
|
+
logger('info', {
|
|
26
|
+
msg: 'hooks onAppHide'
|
|
27
|
+
}, onLogger);
|
|
23
28
|
isAppOnBackground.current = true;
|
|
24
29
|
if (isInit.current) {
|
|
25
30
|
// 判断进入后台之后,维持定时器,如果进入后台超过2分钟, 则断开P2P
|
|
26
31
|
timer.current = setTimeout(() => {
|
|
27
|
-
|
|
32
|
+
logger('info', {
|
|
33
|
+
msg: 'background timer has been exe'
|
|
34
|
+
}, onLogger);
|
|
28
35
|
if (isAppOnBackground.current) {
|
|
29
36
|
unmount();
|
|
30
37
|
}
|
|
@@ -34,7 +41,9 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
|
|
|
34
41
|
}
|
|
35
42
|
}, []);
|
|
36
43
|
const handleAppShow = useCallback(() => {
|
|
37
|
-
|
|
44
|
+
logger('info', {
|
|
45
|
+
msg: 'hooks onAppShow'
|
|
46
|
+
}, onLogger);
|
|
38
47
|
timer.current && clearTimeout(timer.current);
|
|
39
48
|
setTimeout(() => {
|
|
40
49
|
var _ty$p2p$isP2PActiveSy, _ty$p2p;
|
|
@@ -50,16 +59,21 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
|
|
|
50
59
|
log4js.setTag(logTag);
|
|
51
60
|
}
|
|
52
61
|
if (isIDE) {
|
|
53
|
-
|
|
62
|
+
logger('warn', {
|
|
63
|
+
msg: `You are using the IDE environment. To perform P2P and map debugging, please ensure the 'Robot Vacuum Debugger' plugin is installed.`
|
|
64
|
+
}, onLogger);
|
|
54
65
|
SweeperP2pInstance.onReceiveMapData = onReceiveMapData;
|
|
55
66
|
SweeperP2pInstance.onReceivePathData = onReceivePathData;
|
|
56
67
|
SweeperP2pInstance.registerP2pDownloadEvent();
|
|
68
|
+
SweeperP2pInstance.onLogger = onLogger;
|
|
57
69
|
return () => {
|
|
58
70
|
SweeperP2pInstance.removeP2pDownloadEvent();
|
|
59
71
|
};
|
|
60
72
|
}
|
|
61
73
|
if (devId.startsWith('vdevo')) {
|
|
62
|
-
|
|
74
|
+
logger('warn', {
|
|
75
|
+
msg: 'virtual device cannot use p2p'
|
|
76
|
+
}, onLogger);
|
|
63
77
|
return;
|
|
64
78
|
}
|
|
65
79
|
SweeperP2pInstance.initP2pSdk(devId).then(() => {
|
|
@@ -80,7 +94,9 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
|
|
|
80
94
|
* p2p连接
|
|
81
95
|
*/
|
|
82
96
|
const connectP2p = async () => {
|
|
83
|
-
|
|
97
|
+
logger('info', {
|
|
98
|
+
msg: 'hooks has been started connectP2p'
|
|
99
|
+
}, onLogger);
|
|
84
100
|
SweeperP2pInstance.isConnecting = true;
|
|
85
101
|
SweeperP2pInstance.connectDevice(() => {
|
|
86
102
|
isInit.current = true;
|
|
@@ -100,7 +116,9 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
|
|
|
100
116
|
* p2p断开
|
|
101
117
|
*/
|
|
102
118
|
const unmount = async () => {
|
|
103
|
-
|
|
119
|
+
logger('info', {
|
|
120
|
+
msg: 'hooks has been started unmount'
|
|
121
|
+
}, onLogger);
|
|
104
122
|
isInit.current = false;
|
|
105
123
|
if (offSessionStatusChange.current) {
|
|
106
124
|
offSessionStatusChange.current();
|
|
@@ -109,7 +127,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
|
|
|
109
127
|
SweeperP2pInstance.stopObserverSweeperDataByP2P();
|
|
110
128
|
};
|
|
111
129
|
return {
|
|
112
|
-
appendDownloadStreamDuringTask:
|
|
130
|
+
appendDownloadStreamDuringTask: SweeperP2pInstance.appendDownloadStreamDuringTask
|
|
113
131
|
};
|
|
114
132
|
};
|
|
115
133
|
const StreamDataNotificationCenter = emitter;
|
package/lib/mqtt/promise.js
CHANGED
|
@@ -22,19 +22,16 @@ export function normalResolve(reqType, taskId) {
|
|
|
22
22
|
return new Promise((resolve, reject) => {
|
|
23
23
|
// 设置超时定时器
|
|
24
24
|
const timer = setTimeout(() => {
|
|
25
|
-
reject(new MyError(
|
|
25
|
+
reject(new MyError(`${taskId} Request timed out `, {
|
|
26
26
|
errCode: -1,
|
|
27
27
|
reqType
|
|
28
28
|
}));
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
// 监听指定类型的消息
|
|
32
|
-
emitter.on(reqType, message => {
|
|
29
|
+
}, 10 * 1000);
|
|
30
|
+
const handle = message => {
|
|
33
31
|
if (`${message.taskId}` !== `${taskId}`) {
|
|
34
32
|
return;
|
|
35
33
|
}
|
|
36
34
|
clearTimeout(timer);
|
|
37
|
-
emitter.off(reqType);
|
|
38
35
|
if (message.errCode === 0) {
|
|
39
36
|
resolve(message);
|
|
40
37
|
} else {
|
|
@@ -43,6 +40,10 @@ export function normalResolve(reqType, taskId) {
|
|
|
43
40
|
reqType
|
|
44
41
|
}));
|
|
45
42
|
}
|
|
46
|
-
|
|
43
|
+
emitter.off(reqType, handle);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// 监听指定类型的消息
|
|
47
|
+
emitter.on(reqType, handle);
|
|
47
48
|
});
|
|
48
49
|
}
|
package/lib/mqtt/useZoneClean.js
CHANGED
package/lib/utils/index.d.ts
CHANGED
|
@@ -6,3 +6,7 @@ export declare const emitter: import("mitt").Emitter<Record<import("mitt").Event
|
|
|
6
6
|
export declare const pointsToString: (points: Point[], origin: Point) => string;
|
|
7
7
|
export declare const isJSONString: (str: any) => boolean;
|
|
8
8
|
export declare const parseJSON: (str: any) => any;
|
|
9
|
+
export declare const logger: (type: 'warn' | 'error' | 'info', params: {
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
msg: string;
|
|
12
|
+
}, onLogger: (type: 'warn' | 'error' | 'info', v: string) => void) => void;
|
package/lib/utils/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
2
|
+
const _excluded = ["msg"];
|
|
1
3
|
import mitt from 'mitt';
|
|
2
4
|
import { floor, join, map } from 'lodash-es';
|
|
5
|
+
import log4js from '@ray-js/log4js';
|
|
3
6
|
export const emitter = mitt();
|
|
4
7
|
const pointToString = point => {
|
|
5
8
|
return `${point.x},${point.y}`;
|
|
@@ -44,4 +47,14 @@ export const parseJSON = str => {
|
|
|
44
47
|
rst = typeof str === 'undefined' ? {} : str;
|
|
45
48
|
}
|
|
46
49
|
return rst;
|
|
50
|
+
};
|
|
51
|
+
export const logger = (type, params, onLogger) => {
|
|
52
|
+
const {
|
|
53
|
+
msg
|
|
54
|
+
} = params,
|
|
55
|
+
others = _objectWithoutProperties(params, _excluded);
|
|
56
|
+
log4js[type](msg, others);
|
|
57
|
+
if (onLogger) {
|
|
58
|
+
onLogger(type, msg);
|
|
59
|
+
}
|
|
47
60
|
};
|