// // addLogObject.m // 双子星云手机 // // Created by xd h on 2024/4/11. // #import "addLogObject.h" #import "netWorkManager.h" #import "connectDeviceManager.h" #import "fileUploadToFileCenterModel.h" #import @implementation addLogObject static addLogObject *addlog_ShareInstance = nil; +(addLogObject *)shareInstance; { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ addlog_ShareInstance = [[addLogObject alloc]init]; }); return addlog_ShareInstance; } - (id)init { self = [super init]; if (self) { _canUploadNowType = YES; } return self; } //1.读取日志 //2.上传到文件中心 //3.拿到key上传到盒子服务器 - (void)gotoAddLogFunBySuccess:(AddLog_Success)success { //HLog(@"hxd beginTime"); if(!_canUploadNowType){ success(@""); return; } //一小时内只能上传一次 NSNumber *preSecondNum = [HWDataManager getNumberWithKey:stringKeyAddSn(@"addLog")]; if(preSecondNum){ long seconds = [iTools getNowTimeStamp]; if(seconds - preSecondNum.longValue <= 60*60){ success(@""); return; } } _canUploadNowType = NO; NSString *zipPath = [NSString stringWithFormat:@"%@/logs/archive.zip",CachesPatch]; NSFileManager *fileManager = [NSFileManager defaultManager]; // 检查文件是否存在 BOOL fileExists = [fileManager fileExistsAtPath:zipPath]; if (fileExists) { // 删除文件 NSError *error = nil; BOOL success = [fileManager removeItemAtPath:zipPath error:&error]; if (success) { HLog(@"文件已成功删除: %@", zipPath); } else { HLog(@"删除文件失败: %@", [error localizedDescription]); } } //2.读取日志 //NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/debug_0.log",CachesPatch]; NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/webrtc_log_0",CachesPatch]; NSString *ruiyunLogPath2 = [NSString stringWithFormat:@"%@/logs/app.log",CachesPatch]; //3. 将文件压缩成ZIP NSArray *filesToZip = @[ruiyunLogPath, ruiyunLogPath2]; [SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:filesToZip]; NSData *logData = [NSData dataWithContentsOfFile:zipPath]; HLog(@"%@",logData); if(!logData || [logData length]==0){ _canUploadNowType = YES; success(@""); return; } //4.上传到文件中心 NSMutableDictionary *paraDict = [NSMutableDictionary new]; NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn; if(!snStr){ snStr = [iTools getNowTimeString]; } NSString*filename = [[NSString alloc] initWithFormat:@"%@_ios.log",snStr]; [paraDict setValue:filename forKey:@"filename"]; [paraDict setValue:@7 forKey:@"days"]; KWeakSelf [[netWorkManager shareInstance] doUploadFileToFileServiceWithParams:paraDict data:logData success:^(id _Nonnull responseObject) { HLog(@"%@",responseObject); fileUploadToFileCenterModel *model = [[fileUploadToFileCenterModel alloc] initWithDictionary:responseObject error:nil]; if(model.code == 200){ NSString * fileKey = model.data.fileKey; if(fileKey && fileKey.length >0){ success(fileKey); [weakSelf markAddLogTimeFun]; //[self uploadKeyToServerFunBy:fileKey]; } else{ success(@""); self->_canUploadNowType = YES; } } else{ success(@""); self->_canUploadNowType = YES; } } faild:^(NSError * _Nonnull error) { success(@""); self->_canUploadNowType = YES; }]; //[self uploadKeyToServerFunBy:@"LowLevelMultipartUpload_43025714606656109698"]; } //3.拿到key上传到盒子服务器 //- (void)uploadKeyToServerFunBy:(NSString*)fileKey //{ // NSMutableDictionary *paraDict = [NSMutableDictionary new]; // [paraDict setValue:@"ios" forKey:@"type"]; // // NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn; // if(!snStr){ // _canUploadNowType = YES; // return; // } // [paraDict setValue:snStr forKey:@"sn"]; // [paraDict setValue:fileKey forKey:@"key"]; // // KWeakSelf // [[netWorkManager shareInstance] CommonPostCallBackCode:addLogFun Parameters:paraDict success:^(id _Nonnull responseObject) { // SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil]; // if(model.code == 0){ // [weakSelf markAddLogTimeFun]; // } // else{ // self->_canUploadNowType = YES; // } // // } failure:^(NSError * _Nonnull error) { // self->_canUploadNowType = YES; // }]; //} - (void)markAddLogTimeFun { // long seconds = [iTools getNowTimeStamp]; NSNumber *secondsNum = [NSNumber numberWithLong:seconds]; if(secondsNum){ [HWDataManager setNumberWithKey:stringKeyAddSn(@"addLog") value:secondsNum]; } _canUploadNowType = YES; //HLog(@"hxd endTime"); } @end