Sample Code (New version)

Last Updated on : 2024-12-31 07:38:31download

This topic describes sample code related to the P2P operation based on the robot vacuum SDK.

Sample code

#import "ThingSmartSweeperBestPractices.h"
#import <ThingSmartSweeperKit/ThingSmartSweeperP2PManager.h>

@interface ThingSmartSweeperP2PBestPractices()<ThingSmartSweeperP2PManagerDelegate>

@property (strong, nonatomic) NSMutableArray *fileList;
@property (strong, nonatomic) ThingSmartSweeperP2PManager *p2pManager;
@property (nonatomic, copy)   NSString        *devId;

@end

@implementation ThingSmartSweeperP2PBestPractices

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - P2P data channel

- (void)initP2PSDK:(NSString*)devId {
    self.devId = devId;
    self.p2pManager = [[ThingSmartSweeperP2PManager alloc] init];
    self.p2pManager.p2pFileManagerDelegate = self;
    
    NSString *userId = [ThingSmartUser sharedInstance].uid;
    [self.p2pManager initP2PSDKWithUserId:userId success:^{
        [self appendLogStr:@"p2p init success"];
    } failure:^(NSError * _Nullable error) {
        [self appendLogStr:@"p2p init fail"];
    }];
}

- (void)p2pConnect{
    ThingSmartSweeperP2PConnectionParams *params = [ThingSmartSweeperP2PConnectionParams new];
    params.deviceId = self.devId;
    params.mode = 0;
    params.timeout = 15000;
    
    [self.p2pManager connectDevice:params success:^{
        [self appendLogStr:@"p2p connect success"];
        [self queryAlbumFileIndexs];
    } failure:^(NSError * _Nullable error) {
        [self appendLogStr:@"p2p connect fail"];
    }];
}

- (void)queryAlbumFileIndexs{
    ThingSmartSweeperP2PAlbum *params = [ThingSmartSweeperP2PAlbum new];
    params.deviceId = self.devId;
    params.albumName = @"ipc_sweeper_robot";
    
    [self.p2pManager queryAlbumFileIndexs:params
                                  success:^(ThingSmartSweeperP2PAlbumFileIndexs * _Nonnull data) {
        [self appendLogStr:@"queryAlbumFileIndexs success"];

        NSMutableArray *items = [NSMutableArray array];
        for (ThingSmartSweeperP2PAlbumFileIndex *fileIndex in data.items) {
            if (fileIndex.filename && [fileIndex.filename hasSuffix:@"stream"]) {
                [items addObject:fileIndex.filename];
            }
        }
        
        self.fileList = items;
        [self startDownload];
    } failure:^(NSError * _Nullable error) {
        [self appendLogStr:@"queryAlbumFileIndexs fail"];
    }];
}

- (void)startDownload{
    ThingSmartSweeperP2PDownloadFile *params = [ThingSmartSweeperP2PDownloadFile new];
    params.deviceId = self.devId;
    params.albumName = @"ipc_sweeper_robot";
    params.filePath = [ThingSmartSweeperP2PUtil sweeper_directoryPath:self.devId 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"];
    }];
}

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

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

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

- (void)appendLogStr:(NSString *)logStr{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *newStr = self.textView.text;
        newStr = [newStr stringByAppendingString:logStr];
        newStr = [newStr stringByAppendingString:@"\n"];
        self.textView.text = newStr;
    });
}

#pragma mark - ThingSmartSweeperP2PManagerDelegate

- (void)sessionStatusChangeWithModel:(ThingSmartSweeperP2PSessionStatus *)model{
    
}

- (void)uploadProgressUpdateWithModel:(ThingSmartSweeperFileManagerProgress *)progress{
    
}

- (void)downloadProgressUpdateWithModel:(ThingSmartSweeperFileDownloadProgress *)progress{
    NSString *string = [NSString stringWithFormat:@"File%@ is downloading, progress%f",progress.fileName,progress.progress];
    [self appendLogStr:string];
}

- (void)downloadTotalProgressUpdateWithModel:(ThingSmartSweeperFileDownloadTotalProgress *)progress{
    NSString *string = [NSString stringWithFormat:@"Total progress%f",progress.progress];
    [self appendLogStr:string];
}

- (void)fileDownloadCompleteWithModel:(ThingSmartSweeperFileDownloadCompletion *)model{
    NSString *string = [NSString stringWithFormat:@"File%@ download complete",model.fileName];
    [self appendLogStr:string];
}

- (void)streamPacketReceiveWithModel:(ThingSmartSweeperFileManagerPacketReceived *)model{
    NSString *string = [NSString stringWithFormat:@"%@Stream packet received",model.fileName];
    [self appendLogStr:string];
}

@end