@ray-js/robot-data-stream 0.0.1 → 0.0.2
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 +1 -0
- package/lib/api/p2pApi.js +27 -25
- package/lib/api/sweeperP2p.js +18 -14
- package/lib/index.js +8 -7
- package/package.json +4 -3
- package/lib/utils/index.d.ts +0 -2
- package/lib/utils/index.js +0 -2
- package/lib/utils/logger.d.ts +0 -15
- package/lib/utils/logger.js +0 -44
package/lib/api/p2pApi.d.ts
CHANGED
package/lib/api/p2pApi.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable prefer-promise-reject-errors */
|
|
2
2
|
import { p2p } from '@ray-js/ray';
|
|
3
3
|
import moment from 'moment';
|
|
4
|
-
import
|
|
4
|
+
import log4js from '@ray-js/log4js';
|
|
5
5
|
/**
|
|
6
6
|
* P2P 工具类
|
|
7
7
|
*/
|
|
@@ -10,6 +10,7 @@ export default class P2pApi {
|
|
|
10
10
|
|
|
11
11
|
constructor() {
|
|
12
12
|
this.isConnected = false;
|
|
13
|
+
this.isConnecting = false;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
/**
|
|
@@ -23,17 +24,17 @@ export default class P2pApi {
|
|
|
23
24
|
*/
|
|
24
25
|
this.isConnected = false;
|
|
25
26
|
if (!this.isConnected) {
|
|
26
|
-
|
|
27
|
+
log4js.info('start p2p reconnect ==>', moment().format('YYYY-MM-DD HH:mm:ss'));
|
|
27
28
|
this.connectDevice(() => {
|
|
28
|
-
|
|
29
|
+
log4js.info('p2p reconnect success ==>', moment().format('YYYY-MM-DD HH:mm:ss'));
|
|
29
30
|
this.isConnected = true;
|
|
30
31
|
if (this.isConnected) {
|
|
31
32
|
typeof successCb === 'function' && successCb();
|
|
32
33
|
}
|
|
33
34
|
}, () => {}, () => {
|
|
34
|
-
|
|
35
|
+
log4js.info('reconnect complete ==>', String(this.isConnected));
|
|
35
36
|
if (!this.isConnected) {
|
|
36
|
-
|
|
37
|
+
log4js.warn('p2p reconnect failed ==>', moment().format('YYYY-MM-DD HH:mm:ss'));
|
|
37
38
|
this.reconnectP2p(successCb);
|
|
38
39
|
}
|
|
39
40
|
});
|
|
@@ -51,11 +52,11 @@ export default class P2pApi {
|
|
|
51
52
|
this.devId = devId;
|
|
52
53
|
p2p.P2PSDKInit({
|
|
53
54
|
success: () => {
|
|
54
|
-
|
|
55
|
+
log4js.info('P2PSDKInit success');
|
|
55
56
|
resolve(true);
|
|
56
57
|
},
|
|
57
58
|
fail: params => {
|
|
58
|
-
|
|
59
|
+
log4js.warn('P2PSDKInit fail', params);
|
|
59
60
|
resolve(false);
|
|
60
61
|
}
|
|
61
62
|
});
|
|
@@ -77,13 +78,14 @@ export default class P2pApi {
|
|
|
77
78
|
timeout: 5000,
|
|
78
79
|
mode: 0,
|
|
79
80
|
success: () => {
|
|
80
|
-
|
|
81
|
+
log4js.info('p2p connectDevice success');
|
|
81
82
|
this.isConnected = true;
|
|
83
|
+
this.isConnecting = false;
|
|
82
84
|
typeof successCb === 'function' && successCb();
|
|
83
85
|
resolve(true);
|
|
84
86
|
},
|
|
85
87
|
fail: params => {
|
|
86
|
-
|
|
88
|
+
log4js.warn('p2p connectDevice failed ==>', params);
|
|
87
89
|
typeof failCb === 'function' && failCb();
|
|
88
90
|
resolve(false);
|
|
89
91
|
},
|
|
@@ -92,7 +94,7 @@ export default class P2pApi {
|
|
|
92
94
|
}
|
|
93
95
|
});
|
|
94
96
|
} catch (e) {
|
|
95
|
-
|
|
97
|
+
log4js.error('p2p connectDevice occur an error ==>', e);
|
|
96
98
|
reject(false);
|
|
97
99
|
}
|
|
98
100
|
});
|
|
@@ -109,16 +111,16 @@ export default class P2pApi {
|
|
|
109
111
|
deviceId: this.devId,
|
|
110
112
|
success: () => {
|
|
111
113
|
this.isConnected = false;
|
|
112
|
-
|
|
114
|
+
log4js.info('p2p disconnectDevice success');
|
|
113
115
|
resolve(true);
|
|
114
116
|
},
|
|
115
117
|
fail: () => {
|
|
116
|
-
|
|
118
|
+
log4js.warn('p2p disconnectDevice failed');
|
|
117
119
|
resolve(false);
|
|
118
120
|
}
|
|
119
121
|
});
|
|
120
122
|
} catch (e) {
|
|
121
|
-
|
|
123
|
+
log4js.warn('p2p disconnectDevice occur an error ==>', e);
|
|
122
124
|
reject(false);
|
|
123
125
|
}
|
|
124
126
|
});
|
|
@@ -142,12 +144,12 @@ export default class P2pApi {
|
|
|
142
144
|
albumName: albumName,
|
|
143
145
|
jsonfiles: JSON.stringify(files),
|
|
144
146
|
success: () => {
|
|
145
|
-
|
|
147
|
+
log4js.info('p2p downloadStream success');
|
|
146
148
|
typeof successCb === 'function' && successCb();
|
|
147
149
|
resolve(true);
|
|
148
150
|
},
|
|
149
151
|
fail: params => {
|
|
150
|
-
|
|
152
|
+
log4js.warn('p2p downloadStream failed ==>', params);
|
|
151
153
|
setTimeout(() => {
|
|
152
154
|
typeof failedCb === 'function' && failedCb();
|
|
153
155
|
}, 500);
|
|
@@ -158,7 +160,7 @@ export default class P2pApi {
|
|
|
158
160
|
});
|
|
159
161
|
}
|
|
160
162
|
} catch (e) {
|
|
161
|
-
|
|
163
|
+
log4js.warn('p2p downloadStream occur an error ==>', e);
|
|
162
164
|
}
|
|
163
165
|
};
|
|
164
166
|
|
|
@@ -189,12 +191,12 @@ export default class P2pApi {
|
|
|
189
191
|
filePath: filePath,
|
|
190
192
|
jsonfiles: JSON.stringify(files),
|
|
191
193
|
success: () => {
|
|
192
|
-
|
|
194
|
+
log4js.info('p2p downloadFile success');
|
|
193
195
|
typeof successCb === 'function' && successCb();
|
|
194
196
|
resolve(true);
|
|
195
197
|
},
|
|
196
198
|
fail: params => {
|
|
197
|
-
|
|
199
|
+
log4js.warn('p2p downloadFile failed ==>', params);
|
|
198
200
|
setTimeout(() => {
|
|
199
201
|
typeof failedCb === 'function' && failedCb();
|
|
200
202
|
}, 500);
|
|
@@ -205,7 +207,7 @@ export default class P2pApi {
|
|
|
205
207
|
});
|
|
206
208
|
}
|
|
207
209
|
} catch (e) {
|
|
208
|
-
|
|
210
|
+
log4js.error('p2p downloadFile occur an error ==>', e);
|
|
209
211
|
}
|
|
210
212
|
return null;
|
|
211
213
|
};
|
|
@@ -279,7 +281,7 @@ export default class P2pApi {
|
|
|
279
281
|
}
|
|
280
282
|
});
|
|
281
283
|
} catch (e) {
|
|
282
|
-
|
|
284
|
+
log4js.info('cancelDownloadTask occur an error', e);
|
|
283
285
|
reject(false);
|
|
284
286
|
}
|
|
285
287
|
});
|
|
@@ -296,11 +298,11 @@ export default class P2pApi {
|
|
|
296
298
|
deviceId: this.devId,
|
|
297
299
|
albumName,
|
|
298
300
|
success: params => {
|
|
299
|
-
|
|
301
|
+
log4js.info('queryAlbumFileIndexs ==>', params);
|
|
300
302
|
resolve(params);
|
|
301
303
|
},
|
|
302
304
|
fail: params => {
|
|
303
|
-
|
|
305
|
+
log4js.warn('queryAlbumFileIndexs failed ==>', params);
|
|
304
306
|
resolve(null);
|
|
305
307
|
}
|
|
306
308
|
});
|
|
@@ -322,11 +324,11 @@ export default class P2pApi {
|
|
|
322
324
|
try {
|
|
323
325
|
p2p.deInitSDK({
|
|
324
326
|
success: () => {
|
|
325
|
-
|
|
327
|
+
log4js.info('deInitP2pSDK success');
|
|
326
328
|
resolve(true);
|
|
327
329
|
},
|
|
328
330
|
fail: () => {
|
|
329
|
-
|
|
331
|
+
log4js.info('deInitP2pSDK failed');
|
|
330
332
|
resolve(false);
|
|
331
333
|
},
|
|
332
334
|
complete: () => {
|
|
@@ -334,7 +336,7 @@ export default class P2pApi {
|
|
|
334
336
|
}
|
|
335
337
|
});
|
|
336
338
|
} catch (e) {
|
|
337
|
-
|
|
339
|
+
log4js.error('deInitP2pSDK occur an error ==>', e);
|
|
338
340
|
reject(false);
|
|
339
341
|
}
|
|
340
342
|
});
|
package/lib/api/sweeperP2p.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import P2pApi from './p2pApi';
|
|
3
3
|
import Base64 from 'base64-js';
|
|
4
4
|
import { padStart } from 'lodash-es';
|
|
5
|
-
import
|
|
5
|
+
import log4js from '@ray-js/log4js';
|
|
6
6
|
/**
|
|
7
7
|
* 基于P2p工具类的扫地机扩展实现
|
|
8
8
|
*/
|
|
@@ -161,17 +161,21 @@ export class SweeperP2p extends P2pApi {
|
|
|
161
161
|
* @param data
|
|
162
162
|
*/
|
|
163
163
|
sessionStatusCallback = data => {
|
|
164
|
-
|
|
164
|
+
log4js.info('sessionStatusCallback ==>', data);
|
|
165
165
|
if (data) {
|
|
166
166
|
const {
|
|
167
167
|
status
|
|
168
168
|
} = data;
|
|
169
169
|
if (status < 0) {
|
|
170
|
-
|
|
171
|
-
this.
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
170
|
+
log4js.info('receive disconnect notice ==>', status);
|
|
171
|
+
if (!this.isConnecting) {
|
|
172
|
+
this.reconnectP2p(() => {
|
|
173
|
+
// 重连之后重新开启文件下载, 这里的成功不需要注册断开事件
|
|
174
|
+
this.startObserverSweeperDataByP2P(this.downloadType, this.devId, this.onReceiveMapData, this.onReceivePathData);
|
|
175
|
+
});
|
|
176
|
+
} else {
|
|
177
|
+
log4js.warn('receive disconnect notice, but connectDevice is connecting');
|
|
178
|
+
}
|
|
175
179
|
}
|
|
176
180
|
}
|
|
177
181
|
};
|
|
@@ -184,12 +188,12 @@ export class SweeperP2p extends P2pApi {
|
|
|
184
188
|
startObserverSweeperDataByP2P = async (downloadType, devId, onReceiveMapData, onReceivePathData) => {
|
|
185
189
|
var _this$file$items;
|
|
186
190
|
if (![0, 1].some(item => item === downloadType)) {
|
|
187
|
-
|
|
191
|
+
log4js.warn('download type must be 0 or 1');
|
|
188
192
|
return;
|
|
189
193
|
}
|
|
190
194
|
this.onReceiveMapData = onReceiveMapData;
|
|
191
195
|
this.onReceivePathData = onReceivePathData;
|
|
192
|
-
|
|
196
|
+
log4js.info('startObserverSweeperDataByP2P ==>');
|
|
193
197
|
this.downloadType = downloadType;
|
|
194
198
|
this.setDataFilePath(devId);
|
|
195
199
|
this.setStreamFilePath(devId);
|
|
@@ -388,7 +392,7 @@ export class SweeperP2p extends P2pApi {
|
|
|
388
392
|
const type = this.getFileType(fileName);
|
|
389
393
|
if (this.cacheData[type] !== hexValue) {
|
|
390
394
|
if (hexValue.length === 0) {
|
|
391
|
-
|
|
395
|
+
log4js.warn('receive empty data');
|
|
392
396
|
return;
|
|
393
397
|
}
|
|
394
398
|
if (type === 0) {
|
|
@@ -401,13 +405,13 @@ export class SweeperP2p extends P2pApi {
|
|
|
401
405
|
}
|
|
402
406
|
},
|
|
403
407
|
fail: e => {
|
|
404
|
-
|
|
408
|
+
log4js.warn('readFileFromPath failed ==>', e);
|
|
405
409
|
this.resetReading(fileName);
|
|
406
410
|
}
|
|
407
411
|
});
|
|
408
412
|
} catch (e) {
|
|
409
413
|
this.resetReading(fileName);
|
|
410
|
-
|
|
414
|
+
log4js.error('readFileFromPath ==>', e);
|
|
411
415
|
}
|
|
412
416
|
};
|
|
413
417
|
setReading = fileName => {
|
|
@@ -467,10 +471,10 @@ export class SweeperP2p extends P2pApi {
|
|
|
467
471
|
try {
|
|
468
472
|
this.removeP2pDownloadEvent();
|
|
469
473
|
const isCancel = await this.cancelDownloadTask();
|
|
470
|
-
|
|
474
|
+
log4js.info('cancelDownloadTask ==>', isCancel);
|
|
471
475
|
return isCancel;
|
|
472
476
|
} catch (e) {
|
|
473
|
-
|
|
477
|
+
log4js.error('stopObserverSweeperDataByP2P occur error ==>', e);
|
|
474
478
|
return false;
|
|
475
479
|
}
|
|
476
480
|
};
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useEffect, useRef } from 'react';
|
|
2
2
|
import mitt from 'mitt';
|
|
3
3
|
import { SweeperP2pInstance } from './api';
|
|
4
|
-
import
|
|
4
|
+
import log4js from '@ray-js/log4js';
|
|
5
5
|
const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData) => {
|
|
6
6
|
const isInit = useRef(null);
|
|
7
7
|
const offSessionStatusChange = useRef(null);
|
|
@@ -9,7 +9,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData) => {
|
|
|
9
9
|
const timer = useRef(null);
|
|
10
10
|
useEffect(() => {
|
|
11
11
|
if (devId.startsWith('vdevo')) {
|
|
12
|
-
|
|
12
|
+
log4js.warn('virtual device cannot use p2p');
|
|
13
13
|
return;
|
|
14
14
|
}
|
|
15
15
|
isInitP2p();
|
|
@@ -25,9 +25,10 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData) => {
|
|
|
25
25
|
* p2p连接
|
|
26
26
|
*/
|
|
27
27
|
const isInitP2p = async () => {
|
|
28
|
-
|
|
28
|
+
log4js.info('hooks has been started initP2p');
|
|
29
29
|
isInit.current = await SweeperP2pInstance.initP2pSdk(devId);
|
|
30
30
|
if (isInit.current) {
|
|
31
|
+
SweeperP2pInstance.isConnecting = true;
|
|
31
32
|
SweeperP2pInstance.connectDevice(() => {
|
|
32
33
|
SweeperP2pInstance.startObserverSweeperDataByP2P(1, devId, onReceiveMapData, onReceivePathData);
|
|
33
34
|
offSessionStatusChange.current = SweeperP2pInstance.onSessionStatusChange(SweeperP2pInstance.sessionStatusCallback);
|
|
@@ -46,12 +47,12 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData) => {
|
|
|
46
47
|
*/
|
|
47
48
|
const onEnterBackground = () => {
|
|
48
49
|
ty.onAppHide(() => {
|
|
49
|
-
|
|
50
|
+
log4js.info('hooks onAppHide');
|
|
50
51
|
isAppOnBackground.current = true;
|
|
51
52
|
if (isInit.current) {
|
|
52
53
|
// 判断进入后台之后,维持定时器,如果进入后台超过2分钟, 则断开P2P
|
|
53
54
|
timer.current = setTimeout(() => {
|
|
54
|
-
|
|
55
|
+
log4js.info('background timer has been exe');
|
|
55
56
|
if (isAppOnBackground.current) {
|
|
56
57
|
unmount();
|
|
57
58
|
}
|
|
@@ -66,7 +67,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData) => {
|
|
|
66
67
|
* p2p断开
|
|
67
68
|
*/
|
|
68
69
|
const unmount = async () => {
|
|
69
|
-
|
|
70
|
+
log4js.info('hooks has been started unmount');
|
|
70
71
|
isInit.current = false;
|
|
71
72
|
if (offSessionStatusChange.current) {
|
|
72
73
|
offSessionStatusChange.current();
|
|
@@ -81,7 +82,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData) => {
|
|
|
81
82
|
*/
|
|
82
83
|
const onEnterForeground = () => {
|
|
83
84
|
ty.onAppShow(() => {
|
|
84
|
-
|
|
85
|
+
log4js.info('hooks onAppShow');
|
|
85
86
|
isAppOnBackground.current = false;
|
|
86
87
|
if (!isInit.current) {
|
|
87
88
|
isInitP2p();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/robot-data-stream",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "扫地机P2P数据流标准化组件",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"files": [
|
|
@@ -34,10 +34,11 @@
|
|
|
34
34
|
"@ray-js/ray": "^1.5.0"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
+
"@ray-js/log4js": "^0.0.2",
|
|
37
38
|
"clsx": "^1.2.1",
|
|
38
39
|
"lodash-es": "^4.17.21",
|
|
39
|
-
"
|
|
40
|
-
"
|
|
40
|
+
"mitt": "^3.0.1",
|
|
41
|
+
"moment": "^2.30.1"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@commitlint/cli": "^7.2.1",
|
package/lib/utils/index.d.ts
DELETED
package/lib/utils/index.js
DELETED
package/lib/utils/logger.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
declare class Logger {
|
|
2
|
-
options: {
|
|
3
|
-
performance: string;
|
|
4
|
-
error: string;
|
|
5
|
-
info: string;
|
|
6
|
-
warn: string;
|
|
7
|
-
};
|
|
8
|
-
log(color: string, title: string, ...args: any): void;
|
|
9
|
-
performance(title: string, ...args: any): void;
|
|
10
|
-
warn(title: string, ...args: any): void;
|
|
11
|
-
error(title: string, ...args: any): void;
|
|
12
|
-
info(title: string, ...args: any): void;
|
|
13
|
-
}
|
|
14
|
-
declare const logger: Logger;
|
|
15
|
-
export default logger;
|
package/lib/utils/logger.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
2
|
-
class Logger {
|
|
3
|
-
options = {
|
|
4
|
-
performance: '#00cca3',
|
|
5
|
-
error: '#f81c1c',
|
|
6
|
-
info: '#5091f3',
|
|
7
|
-
warn: '#ffaa00'
|
|
8
|
-
};
|
|
9
|
-
log(color, title) {
|
|
10
|
-
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
11
|
-
args[_key - 2] = arguments[_key];
|
|
12
|
-
}
|
|
13
|
-
console.log(`%c 【Robot Data Stream Log】: ${title}`, `background: ${color}; color: #FFFFFF; font-size: 20px`, ...args);
|
|
14
|
-
}
|
|
15
|
-
performance(title) {
|
|
16
|
-
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
17
|
-
args[_key2 - 1] = arguments[_key2];
|
|
18
|
-
}
|
|
19
|
-
this.log(this.options.performance, title, ...args);
|
|
20
|
-
}
|
|
21
|
-
warn(title) {
|
|
22
|
-
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
23
|
-
args[_key3 - 1] = arguments[_key3];
|
|
24
|
-
}
|
|
25
|
-
this.log(this.options.warn, title, ...args);
|
|
26
|
-
}
|
|
27
|
-
error(title) {
|
|
28
|
-
if (title === 'system error') {
|
|
29
|
-
debugger;
|
|
30
|
-
}
|
|
31
|
-
for (var _len4 = arguments.length, args = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
|
|
32
|
-
args[_key4 - 1] = arguments[_key4];
|
|
33
|
-
}
|
|
34
|
-
this.log(this.options.error, title, ...args);
|
|
35
|
-
}
|
|
36
|
-
info(title) {
|
|
37
|
-
for (var _len5 = arguments.length, args = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) {
|
|
38
|
-
args[_key5 - 1] = arguments[_key5];
|
|
39
|
-
}
|
|
40
|
-
this.log(this.options.info, title, ...args);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
const logger = new Logger();
|
|
44
|
-
export default logger;
|