@ray-js/robot-data-stream 0.0.13-beta-8 → 0.0.13-beta-9

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.
@@ -81,6 +81,7 @@ export declare class SweeperP2p extends P2pApi {
81
81
  offDownLoadProgressUpdate: () => void;
82
82
  offTotalDownLoadProgressUpdate: () => void;
83
83
  onDefineStructuredMode: (isStructured: boolean) => void;
84
+ enableCustomLog: boolean;
84
85
  constructor();
85
86
  private setStreamFilePath;
86
87
  private setDataFilePath;
@@ -5,8 +5,9 @@ import "core-js/modules/esnext.iterator.map.js";
5
5
  import Base64 from 'base64-js';
6
6
  import { join, map, padStart, once } from 'lodash-es';
7
7
  import { trace } from '../trace';
8
- import { logger } from '../utils';
8
+ import { logger, logP2PDataIfEnabled } from '../utils';
9
9
  import P2pApi from './p2pApi';
10
+
10
11
  /**
11
12
  * 基于P2p工具类的扫地机扩展实现
12
13
  */
@@ -67,6 +68,7 @@ export class SweeperP2p extends P2pApi {
67
68
  this.cacheData = {};
68
69
  this.exitFiles = [];
69
70
  this.firstPackageTime = Date.now();
71
+ this.enableCustomLog = false;
70
72
  this.packetTotalMap = new Map([['map.bin', -1], ['map_structured.bin', -1], ['cleanPath.bin', -1], ['ai.bin', -1], ['aiHD_XXXX_YYYY.bin', -1], ['map.bin.stream', -1], ['map_structured.bin.stream', -1], ['cleanPath.bin.stream', -1], ['ai.bin.stream', -1], ['aiHD_XXXX_YYYY.bin.stream', -1]]);
71
73
  this.packetSerialNumberCacheMap = new Map([['map.bin', -1], ['map_structured.bin', -1], ['cleanPath.bin', -1], ['ai.bin', -1], ['aiHD_XXXX_YYYY.bin', -1], ['map.bin.stream', -1], ['map_structured.bin.stream', -1], ['cleanPath.bin.stream', -1], ['ai.bin.stream', -1], ['aiHD_XXXX_YYYY.bin.stream', -1]]);
72
74
  this.fileLengthCacheMap = new Map([['map.bin', -1], ['map_structured.bin', -1], ['cleanPath.bin', -1], ['ai.bin', -1], ['aiHD_XXXX_YYYY.bin', -1], ['map.bin.stream', -1], ['map_structured.bin.stream', -1], ['cleanPath.bin.stream', -1], ['ai.bin.stream', -1], ['aiHD_XXXX_YYYY.bin.stream', -1]]);
@@ -221,9 +223,12 @@ export class SweeperP2p extends P2pApi {
221
223
  msg: 'receive disconnect notice ==>',
222
224
  status
223
225
  }, this.onLogger);
226
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'session_status_disconnect', `Session disconnected, status: ${status}`);
224
227
  this.isConnected = false;
225
228
  if (!this.isConnecting) {
229
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'session_reconnect_start', 'Starting reconnect after session disconnect');
226
230
  this.reconnectP2p(() => {
231
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'session_reconnect_success', 'Reconnected successfully, restarting data observer');
227
232
  // 重连之后重新开启文件下载, 这里的成功不需要注册断开事件
228
233
  this.startObserverSweeperDataByP2P(this.downloadType, this.devId, this.onReceiveMapData, this.onReceivePathData, this.onReceiveAIPicData, this.onReceiveAIPicHDData, this.onReceiveWifiMapData);
229
234
  }, '');
@@ -231,6 +236,7 @@ export class SweeperP2p extends P2pApi {
231
236
  logger('warn', {
232
237
  msg: 'receive disconnect notice, but connectDevice is connecting'
233
238
  }, this.onLogger);
239
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'session_disconnect_ignored', 'Disconnect notice ignored, device is already connecting');
234
240
  }
235
241
  }
236
242
  }
@@ -262,6 +268,7 @@ export class SweeperP2p extends P2pApi {
262
268
  logger('info', {
263
269
  msg: 'startObserverSweeperDataByP2P ==>'
264
270
  }, this.onLogger);
271
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'start_observer_sweeper_data', `Starting observer, downloadType: ${downloadType}`);
265
272
  trace.pointFn({
266
273
  devId,
267
274
  eventName: 'startObserverSweeperDataByP2P'
@@ -273,8 +280,10 @@ export class SweeperP2p extends P2pApi {
273
280
  this.removeP2pDownloadEvent();
274
281
  this.registerP2pDownloadEvent();
275
282
  if (!this.file) {
283
+ var _this$file;
276
284
  // @ts-ignore
277
285
  this.file = await this.queryAlbumFileIndexs(this.albumName);
286
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'query_album_files', `Query album files completed, file count: ${((_this$file = this.file) === null || _this$file === void 0 || (_this$file = _this$file.items) === null || _this$file === void 0 ? void 0 : _this$file.length) || 0}`);
278
287
  }
279
288
  if (this.file && ((_this$file$items = this.file.items) === null || _this$file$items === void 0 ? void 0 : _this$file$items.length) > 0) {
280
289
  if (this.downloadType === 0) {
@@ -282,22 +291,27 @@ export class SweeperP2p extends P2pApi {
282
291
  // 赋值
283
292
  this.exitFiles = exitFiles;
284
293
  if (exitFiles.length > 0) {
294
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_files_start', `Starting download, files: ${exitFiles.join(',')}, downloadType: ${downloadType}`);
285
295
  if (shouldDownloadStream) {
286
296
  // 开启p2p流传输
297
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_stream_start', `Starting stream download, files count: ${exitFiles.length}`);
287
298
  await this.downloadStream({
288
299
  files: exitFiles
289
300
  }, this.albumName, () => {
290
301
  logger('info', {
291
302
  msg: `${downloadType} p2p downloadStream success===>`
292
303
  }, this.onLogger);
304
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_stream_success', `Stream download success, downloadType: ${downloadType}`);
293
305
  }, () => {
294
306
  logger('error', {
295
307
  msg: `${downloadType} p2p downloadStream failed and try reConnect Device===>`
296
308
  }, this.onLogger);
309
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_stream_fail', `Stream download failed, attempting reconnect, downloadType: ${downloadType}`);
297
310
  this.removeP2pDownloadEvent();
298
311
  this.disconnectDevice().then(() => {
299
312
  this.isConnected = false;
300
313
  this.isConnecting = false;
314
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_stream_reconnect', 'Reconnecting device after stream download failure');
301
315
  this.connectDevice(() => {
302
316
  // 重连之后重新开启文件下载, 这里的成功不需要注册断开事件
303
317
  this.startObserverSweeperDataByP2P(this.downloadType, this.devId, this.onReceiveMapData, this.onReceivePathData, this.onReceiveAIPicData, this.onReceiveAIPicHDData, this.onReceiveWifiMapData);
@@ -305,33 +319,42 @@ export class SweeperP2p extends P2pApi {
305
319
  });
306
320
  });
307
321
  } else if (await this.initFilePath(this.dataFilePath)) {
322
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_file_start', `Starting file download, files count: ${exitFiles.length}`);
308
323
  await this.downloadFile({
309
324
  files: exitFiles
310
325
  }, this.albumName, this.dataFilePath);
326
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_file_complete', `File download completed, files count: ${exitFiles.length}`);
311
327
  }
312
328
  this.firstPackageTime = Date.now();
329
+ } else {
330
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'no_files_to_download', 'No files found to download');
313
331
  }
314
332
  } else if (this.downloadType === 1) {
315
333
  const exitFiles = this.queryNeedFiles(this.file.items);
316
334
  // 赋值
317
335
  this.exitFiles = exitFiles;
318
336
  if (exitFiles.length > 0) {
337
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_files_start', `Starting download, files: ${exitFiles.join(',')}, downloadType: ${downloadType}`);
319
338
  if (shouldDownloadStream) {
320
339
  // 开启p2p流传输
340
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_stream_start', `Starting stream download, files count: ${exitFiles.length}`);
321
341
  await this.downloadStream({
322
342
  files: exitFiles
323
343
  }, this.albumName, () => {
324
344
  logger('info', {
325
345
  msg: `${downloadType} p2p downloadStream success===>`
326
346
  }, this.onLogger);
347
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_stream_success', `Stream download success, downloadType: ${downloadType}`);
327
348
  }, () => {
328
349
  logger('error', {
329
350
  msg: `${downloadType} p2p downloadStream failed and try reConnect Device===>`
330
351
  }, this.onLogger);
352
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_stream_fail', `Stream download failed, attempting reconnect, downloadType: ${downloadType}`);
331
353
  this.removeP2pDownloadEvent();
332
354
  this.disconnectDevice().then(() => {
333
355
  this.isConnected = false;
334
356
  this.isConnecting = false;
357
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_stream_reconnect', 'Reconnecting device after stream download failure');
335
358
  this.connectDevice(() => {
336
359
  // 重连之后重新开启文件下载, 这里的成功不需要注册断开事件
337
360
  this.startObserverSweeperDataByP2P(this.downloadType, this.devId, this.onReceiveMapData, this.onReceivePathData, this.onReceiveAIPicData, this.onReceiveAIPicHDData, this.onReceiveWifiMapData);
@@ -340,11 +363,15 @@ export class SweeperP2p extends P2pApi {
340
363
  });
341
364
  } else if (await this.initFilePath(this.streamFilePath)) {
342
365
  // 每次要下载前都需要先检查文件目录是否存在 防止中间过程被删除掉文件目录
366
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_file_start', `Starting file download, files count: ${exitFiles.length}`);
343
367
  await this.downloadFile({
344
368
  files: exitFiles
345
369
  }, this.albumName, this.streamFilePath);
370
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'download_file_complete', `File download completed, files count: ${exitFiles.length}`);
346
371
  }
347
372
  this.firstPackageTime = Date.now();
373
+ } else {
374
+ logP2PDataIfEnabled(this.enableCustomLog, devId, 'no_files_to_download', 'No files found to download');
348
375
  }
349
376
  }
350
377
  }
@@ -358,6 +385,7 @@ export class SweeperP2p extends P2pApi {
358
385
  * @returns
359
386
  */
360
387
  appendDownloadStreamDuringTask = (files, successCb, failCb) => {
388
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'append_download_stream', `Appending files to download stream, files: ${files.join(',')}`);
361
389
  return this.appendDownloadStream({
362
390
  files: [...this.exitFiles, ...files]
363
391
  }, this.albumName, successCb, failCb);
@@ -370,6 +398,7 @@ export class SweeperP2p extends P2pApi {
370
398
  logger('info', {
371
399
  msg: 'registerP2pDownloadEvent ==>'
372
400
  }, this.onLogger);
401
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'register_download_event', `Registering download events, shouldDownloadStream: ${shouldDownloadStream}`);
373
402
  if (shouldDownloadStream) {
374
403
  // p2p数据流监听
375
404
  this.offP2pStreamPacketReceive = this.onP2pStreamPacketReceive(this.p2pStreamPacketReceiveCallback);
@@ -393,6 +422,7 @@ export class SweeperP2p extends P2pApi {
393
422
  logger('info', {
394
423
  msg: 'removeP2pDownloadEvent ==>'
395
424
  }, this.onLogger);
425
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'remove_download_event', 'Removing download events');
396
426
  if (shouldDownloadStream) {
397
427
  [...this.packetSerialNumberCacheMap.keys()].forEach(key => {
398
428
  this.packetSerialNumberCacheMap.set(key, -1);
@@ -426,6 +456,7 @@ export class SweeperP2p extends P2pApi {
426
456
  index
427
457
  } = data;
428
458
  if (fileName) {
459
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'file_download_complete', `File download completed, fileName: ${fileName}, index: ${index}`);
429
460
  this.fileIndex = index;
430
461
  const path = this.downloadType === 0 ? this.getDataFilePath(fileName) : this.getStreamFilePath(fileName);
431
462
  this.readFileFromPath(fileName, path);
@@ -458,6 +489,11 @@ export class SweeperP2p extends P2pApi {
458
489
  this.initStructuredMode(((_FILE_NAME_MAP$fileNa = FILE_NAME_MAP[fileName]) === null || _FILE_NAME_MAP$fileNa === void 0 ? void 0 : _FILE_NAME_MAP$fileNa.type) === 6);
459
490
  }
460
491
 
492
+ // 记录第一个包和最后一个包的接收
493
+ if (packetType === 1 || packetType === 3) {
494
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'packet_first_received', `First packet received, fileName: ${fileName}, fileSerialNumber: ${fileSerialNumber}, packetIndex: ${packetIndex}`);
495
+ }
496
+
461
497
  // 因为XXXX_YYYYY会被替换为坐标值,所以这里需要替换一下
462
498
 
463
499
  if (/^aiHD.*\.bin$/.test(fileName)) {
@@ -470,6 +506,7 @@ export class SweeperP2p extends P2pApi {
470
506
  logger('warn', {
471
507
  msg: `p2pStreamPacketReceiveCallback: fileName : ${fileName} is not support`
472
508
  }, this.onLogger);
509
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'packet_unsupported_file', `Unsupported file name: ${fileName}`);
473
510
  return;
474
511
  }
475
512
  const cacheSerialNumber = this.packetSerialNumberCacheMap.get(fileName);
@@ -506,6 +543,7 @@ export class SweeperP2p extends P2pApi {
506
543
  logger('warn', {
507
544
  msg: `p2pStreamPacketReceiveCallback: fileName : ${fileName} length mismatch, expected: ${cacheFileLength * 2}, received: ${hexValue.length}`
508
545
  }, this.onLogger);
546
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'packet_length_mismatch', `Packet length mismatch, fileName: ${fileName}, expected: ${cacheFileLength * 2}, received: ${hexValue.length}`);
509
547
  return;
510
548
  }
511
549
  if (this.firstPackageTime > 0 && fileName.indexOf('map') !== -1) {
@@ -513,6 +551,7 @@ export class SweeperP2p extends P2pApi {
513
551
  logger('info', {
514
552
  msg: `receive first full package, cost time: ${packageTime} ms, fileName: ${fileName}`
515
553
  }, this.onLogger);
554
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'first_full_package_received', `First full package received, fileName: ${fileName}, costTime: ${packageTime}ms`);
516
555
  trace.pointFn({
517
556
  devId: this.devId,
518
557
  eventName: 'receiveFirstFullPackage'
@@ -523,6 +562,7 @@ export class SweeperP2p extends P2pApi {
523
562
  type
524
563
  } = FILE_NAME_MAP[fileName];
525
564
  if (this.cacheData[type] !== hexValue) {
565
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'data_received', `Data received and processed, fileName: ${fileName}, type: ${type}, dataLength: ${hexValue.length}`);
526
566
  if (type === 0 || type === 6) {
527
567
  this.onReceiveMapData(hexValue);
528
568
  this.cacheData[type] = hexValue;
@@ -555,6 +595,7 @@ export class SweeperP2p extends P2pApi {
555
595
  */
556
596
  readFileFromPath = (fileName, filePath) => {
557
597
  if (!this.setReading(fileName)) return;
598
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'read_file_start', `Starting to read file, fileName: ${fileName}`);
558
599
  try {
559
600
  ty.getFileSystemManager().readFile({
560
601
  filePath,
@@ -570,6 +611,7 @@ export class SweeperP2p extends P2pApi {
570
611
  logger('warn', {
571
612
  msg: 'receive empty data'
572
613
  }, this.onLogger);
614
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'read_file_empty', `Read file returned empty data, fileName: ${fileName}`);
573
615
  return;
574
616
  }
575
617
  if (type === 0 || type === 6) {
@@ -588,6 +630,7 @@ export class SweeperP2p extends P2pApi {
588
630
  this.onReceiveWifiMapData(hexValue);
589
631
  }
590
632
  this.cacheData[type] = hexValue;
633
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'read_file_success', `File read and processed successfully, fileName: ${fileName}, type: ${type}`);
591
634
  }
592
635
  },
593
636
  fail: e => {
@@ -595,6 +638,8 @@ export class SweeperP2p extends P2pApi {
595
638
  msg: 'readFileFromPath failed ==>',
596
639
  e
597
640
  }, this.onLogger);
641
+ const errorMsg = (e === null || e === void 0 ? void 0 : e.errorMsg) || (e === null || e === void 0 ? void 0 : e.errorCode) || JSON.stringify(e) || 'unknown';
642
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'read_file_fail', `File read failed, fileName: ${fileName}, error: ${errorMsg}`);
598
643
  this.resetReading(fileName);
599
644
  }
600
645
  });
@@ -604,6 +649,8 @@ export class SweeperP2p extends P2pApi {
604
649
  msg: 'readFileFromPath ==>',
605
650
  e
606
651
  }, this.onLogger);
652
+ const errorMsg = (e === null || e === void 0 ? void 0 : e.errorMsg) || (e === null || e === void 0 ? void 0 : e.errorCode) || JSON.stringify(e) || 'unknown';
653
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'read_file_error', `File read error, fileName: ${fileName}, error: ${errorMsg}`);
607
654
  }
608
655
  };
609
656
  setReading = fileName => {
@@ -679,10 +726,12 @@ export class SweeperP2p extends P2pApi {
679
726
  logger('info', {
680
727
  msg: `isConnected: ${this.isConnected}, devId: ${this.devId}`
681
728
  }, this.onLogger);
729
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'stop_observer_start', `Stopping observer, isConnected: ${this.isConnected}`);
682
730
  if (this.isConnected) {
683
731
  logger('info', {
684
732
  msg: `try disconnect device, devId: ${this.devId}`
685
733
  }, this.onLogger);
734
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'stop_observer_disconnect', 'Disconnecting device');
686
735
  this.disconnectDevice();
687
736
  }
688
737
  this.removeP2pDownloadEvent();
@@ -691,12 +740,14 @@ export class SweeperP2p extends P2pApi {
691
740
 
692
741
  // 先销毁断开监听
693
742
  typeof this.offSessionStatusChange === 'function' && this.offSessionStatusChange();
743
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'stop_observer_complete', 'Observer stopped successfully');
694
744
  // 销毁初始化时,先进行断连操作
695
745
  } catch (e) {
696
746
  logger('error', {
697
747
  msg: 'stopObserverSweeperDataByP2P occur error ==>',
698
748
  e
699
749
  }, this.onLogger);
750
+ logP2PDataIfEnabled(this.enableCustomLog, this.devId, 'stop_observer_error', `Error stopping observer: ${(e === null || e === void 0 ? void 0 : e.message) || 'unknown'}`);
700
751
  return false;
701
752
  }
702
753
  };
package/lib/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from './mqtt/type/requestType';
3
3
  type TOnLogger = (type: 'warn' | 'error' | 'info', data: string) => void;
4
4
  declare const useP2PDataStream: (devId: string, onReceiveMapData: (data: string) => void, onReceivePathData: (data: string) => void, opt?: {
5
5
  logTag?: string | undefined;
6
+ enableCustomLog?: boolean | undefined;
6
7
  onReceiveAIPicData?: ((data: string) => void) | undefined;
7
8
  onReceiveAIPicHDData?: ((data: string) => void) | undefined;
8
9
  onReceiveWifiMapData?: ((data: string) => void) | undefined;
package/lib/index.js CHANGED
@@ -2,9 +2,9 @@
2
2
  import log4js from '@ray-js/log4js';
3
3
  import { getSystemInfoSync, usePageEvent } from '@ray-js/ray';
4
4
  import { useCallback, useEffect, useRef } from 'react';
5
- import { trace } from './trace';
6
- import { emitter, logger } from './utils';
7
5
  import { SweeperP2pInstance } from './api';
6
+ import { trace } from './trace';
7
+ import { emitter, logger, logP2PDataIfEnabled } from './utils';
8
8
  export * from './mqtt';
9
9
  export * from './mqtt/type/requestType';
10
10
  const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
@@ -12,12 +12,15 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
12
12
  global.robotPanelTraceId = traceId;
13
13
  const {
14
14
  logTag,
15
+ enableCustomLog = false,
15
16
  onReceiveAIPicData,
16
17
  onReceiveAIPicHDData,
17
18
  onReceiveWifiMapData,
18
19
  onLogger,
19
20
  onDefineStructuredMode
20
21
  } = opt || {};
22
+ SweeperP2pInstance.enableCustomLog = enableCustomLog;
23
+ logP2PDataIfEnabled(enableCustomLog, devId, 'hooks_init', `useP2PDataStream initialized, traceId: ${traceId}`);
21
24
  const isInit = useRef(false);
22
25
  const offSessionStatusChange = useRef(null);
23
26
  const isAppOnBackground = useRef(false);
@@ -27,6 +30,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
27
30
  logger('info', {
28
31
  msg: 'hooks onAppHide'
29
32
  }, onLogger);
33
+ logP2PDataIfEnabled(enableCustomLog, devId, 'app_hide', 'Application hidden');
30
34
  isAppOnBackground.current = true;
31
35
  if (isInit.current) {
32
36
  timer.current = setTimeout(() => {
@@ -34,6 +38,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
34
38
  msg: `background timer has been exe,isAppOnBackground: ${isAppOnBackground.current}`
35
39
  }, onLogger);
36
40
  if (isAppOnBackground.current) {
41
+ logP2PDataIfEnabled(enableCustomLog, devId, 'background_timeout', 'Background timeout, unmounting P2P connection');
37
42
  unmount();
38
43
  }
39
44
  clearTimeout(timer.current);
@@ -48,6 +53,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
48
53
  logger('info', {
49
54
  msg: `clear timer ${timer.current}`
50
55
  }, onLogger);
56
+ logP2PDataIfEnabled(enableCustomLog, devId, 'app_show', 'Application shown');
51
57
  isAppOnBackground.current = false;
52
58
  timer.current && clearTimeout(timer.current);
53
59
  setTimeout(() => {
@@ -60,6 +66,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
60
66
  if ((!isInit.current || !((_ty$p2p$isP2PActiveSy2 = (_ty$p2p2 = ty.p2p).isP2PActiveSync) !== null && _ty$p2p$isP2PActiveSy2 !== void 0 && _ty$p2p$isP2PActiveSy2.call(_ty$p2p2, {
61
67
  deviceId: devId
62
68
  }))) && !SweeperP2pInstance.isConnecting) {
69
+ logP2PDataIfEnabled(enableCustomLog, devId, 'app_show_reconnect', 'Reconnecting P2P after app show');
63
70
  connectP2p();
64
71
  }
65
72
  }, 500);
@@ -75,12 +82,14 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
75
82
  logger('warn', {
76
83
  msg: `You are using the IDE environment. To perform P2P and map debugging, please ensure the 'Robot Vacuum Debugger' plugin is installed.`
77
84
  }, onLogger);
85
+ logP2PDataIfEnabled(enableCustomLog, devId, 'ide_mode', 'IDE environment detected, using mock P2P');
78
86
  SweeperP2pInstance.onReceiveMapData = onReceiveMapData;
79
87
  SweeperP2pInstance.onReceivePathData = onReceivePathData;
80
88
  SweeperP2pInstance.onDefineStructuredMode = onDefineStructuredMode;
81
89
  SweeperP2pInstance.registerP2pDownloadEvent();
82
90
  SweeperP2pInstance.onLogger = onLogger;
83
91
  return () => {
92
+ logP2PDataIfEnabled(enableCustomLog, devId, 'ide_mode_cleanup', 'Cleaning up IDE mode P2P events');
84
93
  SweeperP2pInstance.removeP2pDownloadEvent();
85
94
  };
86
95
  }
@@ -88,16 +97,20 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
88
97
  logger('warn', {
89
98
  msg: 'virtual device cannot use p2p'
90
99
  }, onLogger);
100
+ logP2PDataIfEnabled(enableCustomLog, devId, 'virtual_device', 'Virtual device detected, P2P not available');
91
101
  return;
92
102
  }
93
103
  SweeperP2pInstance.onLogger = onLogger;
104
+ logP2PDataIfEnabled(enableCustomLog, devId, 'init_p2p_sdk_start', 'Starting P2P SDK initialization');
94
105
  SweeperP2pInstance.initP2pSdk(devId).then(() => {
106
+ logP2PDataIfEnabled(enableCustomLog, devId, 'init_p2p_sdk_success', 'P2P SDK initialized successfully');
95
107
  connectP2p();
96
108
  trace.pointFn({
97
109
  devId,
98
110
  eventName: 'initP2pSdk'
99
111
  });
100
- }).catch(() => {
112
+ }).catch(e => {
113
+ logP2PDataIfEnabled(enableCustomLog, devId, 'init_p2p_sdk_fail', `P2P SDK initialization failed: ${(e === null || e === void 0 ? void 0 : e.message) || 'unknown error'}`);
101
114
  trace.pointFn({
102
115
  devId,
103
116
  eventName: 'initP2pSdkFail'
@@ -107,6 +120,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
107
120
  ty.onAppShow(handleAppShow);
108
121
  }, []);
109
122
  usePageEvent('onUnload', () => {
123
+ logP2PDataIfEnabled(enableCustomLog, devId, 'page_unload', 'Page unloading, cleaning up P2P connection');
110
124
  unmount();
111
125
  ty.offAppHide(handleAppHide);
112
126
  ty.offAppShow(handleAppShow);
@@ -120,27 +134,33 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
120
134
  logger('info', {
121
135
  msg: 'hooks has been started connectP2p'
122
136
  }, onLogger);
137
+ logP2PDataIfEnabled(enableCustomLog, devId, 'connect_p2p_start', 'Starting P2P connection');
123
138
  // 开始进行连接时作为整个p2p连接的开始,将trace的开始时间戳记录下来
124
139
  trace.initTimeStamps = Date.now();
125
140
  SweeperP2pInstance.connectDevice(() => {
141
+ logP2PDataIfEnabled(enableCustomLog, devId, 'connect_p2p_success', 'P2P connection established successfully');
126
142
  trace.pointFn({
127
143
  devId,
128
144
  eventName: 'connectDeviceSuccess'
129
145
  });
130
146
  isInit.current = true;
147
+ logP2PDataIfEnabled(enableCustomLog, devId, 'start_observer_data', 'Starting to observe sweeper data by P2P');
131
148
  SweeperP2pInstance.startObserverSweeperDataByP2P(1, devId, onReceiveMapData, onReceivePathData, onReceiveAIPicData, onReceiveAIPicHDData, onReceiveWifiMapData, onDefineStructuredMode);
132
149
  offSessionStatusChange.current = SweeperP2pInstance.onSessionStatusChange(SweeperP2pInstance.sessionStatusCallback);
133
150
  }, () => {
151
+ logP2PDataIfEnabled(enableCustomLog, devId, 'connect_p2p_fail', 'P2P connection failed, attempting reconnect');
134
152
  trace.pointFn({
135
153
  devId,
136
154
  eventName: 'connectDeviceFail'
137
155
  });
138
156
  SweeperP2pInstance.reconnectP2p(() => {
157
+ logP2PDataIfEnabled(enableCustomLog, devId, 'reconnect_p2p_success', 'P2P reconnected successfully');
139
158
  isInit.current = true;
140
159
  trace.pointFn({
141
160
  devId,
142
161
  eventName: 'reconnectP2p'
143
162
  });
163
+ logP2PDataIfEnabled(enableCustomLog, devId, 'start_observer_data_after_reconnect', 'Starting to observe sweeper data after reconnect');
144
164
  SweeperP2pInstance.startObserverSweeperDataByP2P(1, devId, onReceiveMapData, onReceivePathData, onReceiveAIPicData, onReceiveAIPicHDData, onReceiveWifiMapData);
145
165
  // 这里失败重连需要注册断开重连的事件
146
166
  offSessionStatusChange.current = SweeperP2pInstance.onSessionStatusChange(SweeperP2pInstance.sessionStatusCallback);
@@ -155,6 +175,7 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
155
175
  logger('info', {
156
176
  msg: 'hooks has been started unmount'
157
177
  }, onLogger);
178
+ logP2PDataIfEnabled(enableCustomLog, devId, 'unmount_p2p', 'Unmounting P2P connection');
158
179
  isInit.current = false;
159
180
  SweeperP2pInstance.stopObserverSweeperDataByP2P();
160
181
  if (offSessionStatusChange.current) {
@@ -10,3 +10,11 @@ export declare const logger: (type: 'warn' | 'error' | 'info', params: {
10
10
  [key: string]: any;
11
11
  msg: string;
12
12
  }, onLogger: (type: 'warn' | 'error' | 'info', v: string) => void) => void;
13
+ /**
14
+ * 封装的 P2P 日志记录函数,根据 enableCustomLog 参数决定是否记录日志
15
+ * @param enableCustomLog 是否启用自定义日志
16
+ * @param devId 设备ID
17
+ * @param event 事件名称
18
+ * @param message 日志消息
19
+ */
20
+ export declare const logP2PDataIfEnabled: (enableCustomLog: boolean, devId: string, event: string, message: string) => void;
@@ -1,6 +1,7 @@
1
1
  import mitt from 'mitt';
2
2
  import { floor, join, map } from 'lodash-es';
3
3
  import log4js from '@ray-js/log4js';
4
+ import { logP2PData as originalLogP2PData } from '@ray-js/robot-custom-log';
4
5
  export const emitter = mitt();
5
6
  const pointToString = point => {
6
7
  return `${point.x},${point.y}`;
@@ -59,4 +60,20 @@ export const logger = (type, params, onLogger) => {
59
60
  if (onLogger) {
60
61
  onLogger(type, msg);
61
62
  }
63
+ };
64
+
65
+ /**
66
+ * 封装的 P2P 日志记录函数,根据 enableCustomLog 参数决定是否记录日志
67
+ * @param enableCustomLog 是否启用自定义日志
68
+ * @param devId 设备ID
69
+ * @param event 事件名称
70
+ * @param message 日志消息
71
+ */
72
+ export const logP2PDataIfEnabled = (enableCustomLog, devId, event, message) => {
73
+ if (enableCustomLog) {
74
+ originalLogP2PData(devId, JSON.stringify({
75
+ event,
76
+ message
77
+ }));
78
+ }
62
79
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/robot-data-stream",
3
- "version": "0.0.13-beta-8",
3
+ "version": "0.0.13-beta-9",
4
4
  "description": "扫地机P2P数据流标准化组件",
5
5
  "main": "lib/index",
6
6
  "files": [