P2P Capabilities (New)

Last Updated on : 2025-01-13 08:41:32download

A robot vacuum that supports peer-to-peer (P2P) capabilities provides P2P download channels. Map data and cleaning routes are directly transmitted from the robot vacuum to the app through P2P channels. The data does not need to be uploaded to a cloud storage server for downloading. Therefore, data transmission becomes more efficient at a lower cost of cloud storage.

Entry to the feature

ThingSmartSweeperP2PManager *p2pManager = [[ThingSmartSweeperP2PManager alloc] init];

Initialization interfaces

Initialization

API description

- (void)initP2PSDKWithUserId:(nullable NSString *)userId
                     success:(void (^)(void))success
                     failure:(void (^)(NSError * _Nullable error))failure;

Parameter description

Parameter Description
userId The user ID.
success The success callback that is executed when a P2P connection is created.
failure The failure callback that is executed when a P2P connection failed to be created.

Sample code

[self.p2pManager initP2PSDKWithUserId:userId success:^{
		[self appendLogStr:@"p2p init success"];
} failure:^(NSError * _Nullable error) {
		[self appendLogStr:@"p2p init fail"];
}];

Deinitialization

API description

- (void)deInitSDK:(void (^)(void))success
          failure:(void (^)(NSError * _Nullable error))failure;

Sample code

[self.p2pManager deInitSDK:^{
		[self appendLogStr:@"deInitSDK success"];
} failure:^(NSError * _Nullable error) {
		[self appendLogStr:@"deInitSDK failure"];
}];

P2P connection

Create a P2P connection

API description

- (void)connectDevice:(ThingSmartSweeperP2PConnectionParams*)params
              success:(void (^)(void))success
              failure:(void (^)(NSError * _Nullable error))failure;

Parameter description

ThingSmartSweeperP2PConnectionParams

Parameter Description
mode The connection mode. Valid values:
  • 0: internet
  • 1: LAN
timeout The timeout value, in milliseconds.
0 indicates a default value is used.

Sample code

ThingSmartSweeperP2PConnectionParams *params = [ThingSmartSweeperP2PConnectionParams new];
params.deviceId = @"6c40873f680224bc01ssjl";
params.mode = 0;
params.timeout = 15000;

[self.p2pManager connectDevice:params success:^{
	   [self appendLogStr:@"p2p connect success"];
} failure:^(NSError * _Nullable error) {
     [self appendLogStr:@"p2p connect fail"];
}];

Close a P2P connection

API description

- (void)disconnectDevice:(NSString*)devId
                 success:(void (^)(void))success
                 failure:(void (^)(NSError * _Nullable error))failure;

Parameter description

Parameter Description
devId The device ID.
success The success callback that is executed when a P2P connection is closed.
failure The failure callback that is executed when a P2P connection failed to be closed.

Sample code

[self.p2pManager disconnectDevice:devId success:^{
		[self appendLogStr:@"breakP2PBtn success"];
} failure:^(NSError * _Nullable error) {
		[self appendLogStr:@"breakP2PBtn success"];
}];

Check P2P connection status

API description

- (void)isP2PActive:(NSString*)devId
            success:(void (^)(void))success
            failure:(void (^)(NSError * _Nullable error))failure;

Parameter description

Parameter Description
devId The device ID.
success The P2P connection is created.
failure The P2P connection is not created.

Sample code

[self.p2pManager isP2PActive:devId success:^{
      [self appendLogStr:@"isP2PActive function success"];
} failure:^(NSError * _Nullable error) {
			[self appendLogStr:@"isP2PActive function fail"];
}];

Download

Download files

Call queryAlbumFileIndexs to get the list of map files, and then call this interface to download the final map files.

The file extension .stream indicates continuous downloading of data generated by the device until the device automatically stops sending data or the download is canceled by calling cancelDownload.

API description

- (void)downloadFile:(ThingSmartSweeperP2PDownloadFile*)params
             success:(void (^)(void))success
             failure:(void (^)(NSError * _Nullable error))failure;

Parameter description

ThingSmartSweeperP2PDownloadFile

Parameter Description
deviceId The device ID.
albumName The album name. Default value: ipc_sweeper_robot.
filePath The local storage path of the downloaded file in the app.
jsonfiles The downloaded file in the format of {"files":["filesname1", "filesname2", "filesname3" ]}.

Sample code

ThingSmartSweeperP2PDownloadFile *params = [ThingSmartSweeperP2PDownloadFile new];
params.deviceId = @"6c408731420094bc01skjl";
params.albumName = @"ipc_sweeper_robot";
params.filePath = [ThingSmartSweeperP2PUtil sweeper_directoryPath:@"6c408731420094bc01skjl" albumName:[ThingSmartSweeperP2PUtil sweeper_filealbumNameWithDownloadType:1]];
NSDictionary *reqData = @{@"files":self.fileList};
NSString *jsonfiles = [reqData yy_modelToJSONString];
params.jsonfiles = jsonfiles;

[self.p2pManager downloadFile:params success:^{
    [self appendLogStr:@"downloadFile success"];
} failure:^(NSError * _Nullable error) {
    [self appendLogStr:@"downloadFile fail"];
}];

Cancel a download task

API description

- (void)cancelDownloadTask:(NSString*)devId
                   success:(void (^)(void))success
                   failure:(void (^)(NSError * _Nullable error))failure;

Sample code

[self.p2pManager cancelDownloadTask:@"6c40873f680094bc01skjl" success:^{
    [self appendLogStr:@"cancel downloadTask success"];
} failure:^(NSError * _Nullable error) {
    [self appendLogStr:@"cancel downloadTask fail"];
}];

Query the list of file indexes

API description

- (void)queryAlbumFileIndexs:(ThingSmartSweeperP2PAlbum*)params
                     success:(void (^)(ThingSmartSweeperP2PAlbumFileIndexs *data))success
                     failure:(void (^)(NSError * _Nullable error))failure;

Parameter description

ThingSmartSweeperP2PAlbum

Parameter Description
deviceId The device ID.
albumName The field agreed with the device.

ThingSmartSweeperP2PAlbumFileIndexs

Parameter Description
count The number of files.
items The file index.

Other parameters

Parameter Description
params The model for request parameters.
success The success callback.
failure The failure callback.

Sample code

ThingSmartSweeperP2PAlbum *params = [ThingSmartSweeperP2PAlbum new];
params.deviceId = @"6c40873ss83394b66501skjl";
params.albumName = @"ipc_sweeper_robot";

[self.p2pManager queryAlbumFileIndexs:params
                              success:^(ThingSmartSweeperP2PAlbumFileIndexs * _Nonnull data) {
    NSMutableArray *items = [NSMutableArray array];
    for (ThingSmartSweeperP2PAlbumFileIndex *fileIndex in data.items) {
        if (fileIndex.filename && [fileIndex.filename hasSuffix:@"stream"]) {
            [items addObject:fileIndex.filename];
        }
    }

    self.fileList = items;
} failure:^(NSError * _Nullable error) {
    [self appendLogStr:@"queryAlbumFileIndexs fail"];
}];

Register a listener

API description

@property (nonatomic, weak) id <ThingSmartSweeperP2PManagerDelegate> p2pFileManagerDelegate;

Callback description

@protocol ThingSmartSweeperP2PManagerDelegate  <NSObject>

@optional

/**
 * The callback for connection status changes.
 *
 * @param model The status model.
 */
- (void)sessionStatusChangeWithModel:(ThingSmartSweeperP2PSessionStatus *)model;

/**
 The callback for the upload progress.
 *
 * @param progress The progress model.
 */
- (void)uploadProgressUpdateWithModel:(ThingSmartSweeperFileManagerProgress *)progress;

/**
 * The callback for the download progress of a single file.
 *
 * @param progress The progress model.
 */
- (void)downloadProgressUpdateWithModel:(ThingSmartSweeperFileDownloadProgress *)progress;

/**
 * The callback for the overall download progress.
 *
 * @param progress The progress model.
 */
- (void)downloadTotalProgressUpdateWithModel:(ThingSmartSweeperFileDownloadTotalProgress *)progress;

/**
 * The event that a single file is downloaded.
 *
 * @param model The data model of download completion.
 */
- (void)fileDownloadCompleteWithModel:(ThingSmartSweeperFileDownloadCompletion *)model;

/**
 * The event that a data packet is received.
 *
 * @param model The data packet model.
 */
- (void)streamPacketReceiveWithModel:(ThingSmartSweeperFileManagerPacketReceived *)model;

@end

Parameter description

ThingSmartSweeperP2PSessionStatus

Parameter Description
deviceId The device ID.
status The status value. A value less than 0 indicates disconnection. For more information, see Error codes for P2P SDK.

ThingSmartSweeperFileDownloadCompletion

Parameter Description
deviceId The device ID.
fileName The file name.
index The index.

ThingSmartSweeperFileDownloadProgress

Parameter Description
deviceId The device ID.
fileName The file name.
progress The progress.

ThingSmartSweeperFileDownloadTotalProgress

Parameter Description
deviceId The device ID.
progress The progress.