addLogObject.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // addLogObject.m
  3. // Private-X
  4. //
  5. // Created by xd h on 2024/4/11.
  6. //
  7. #import "addLogObject.h"
  8. #import "netWorkManager.h"
  9. #import "connectDeviceManager.h"
  10. #import "fileUploadToFileCenterModel.h"
  11. #import <SSZipArchive/SSZipArchive.h>
  12. @implementation addLogObject
  13. static addLogObject *addlog_ShareInstance = nil;
  14. +(addLogObject *)shareInstance;
  15. {
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. addlog_ShareInstance = [[addLogObject alloc]init];
  19. });
  20. return addlog_ShareInstance;
  21. }
  22. - (id)init
  23. {
  24. self = [super init];
  25. if (self) {
  26. _canUploadNowType = YES;
  27. }
  28. return self;
  29. }
  30. //1.读取日志
  31. //2.上传到文件中心
  32. //3.拿到key上传到盒子服务器
  33. - (void)gotoAddLogFunBySuccess:(AddLog_Success)success
  34. {
  35. //HLog(@"hxd beginTime");
  36. if(!_canUploadNowType){
  37. success(@"");
  38. return;
  39. }
  40. //一小时内只能上传一次
  41. NSNumber *preSecondNum = [HWDataManager getNumberWithKey:stringKeyAddSn(@"addLog")];
  42. if(preSecondNum){
  43. long seconds = [iTools getNowTimeStamp];
  44. if(seconds - preSecondNum.longValue <= 60*60){
  45. success(@"");
  46. return;
  47. }
  48. }
  49. _canUploadNowType = NO;
  50. NSString *zipPath = [NSString stringWithFormat:@"%@/logs/archive.zip",CachesPatch];
  51. NSFileManager *fileManager = [NSFileManager defaultManager];
  52. // 检查文件是否存在
  53. BOOL fileExists = [fileManager fileExistsAtPath:zipPath];
  54. if (fileExists) {
  55. // 删除文件
  56. NSError *error = nil;
  57. BOOL success = [fileManager removeItemAtPath:zipPath error:&error];
  58. if (success) {
  59. HLog(@"文件已成功删除: %@", zipPath);
  60. } else {
  61. HLog(@"删除文件失败: %@", [error localizedDescription]);
  62. }
  63. }
  64. //2.读取日志
  65. //NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/debug_0.log",CachesPatch];
  66. NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/webrtc_log_0",CachesPatch];
  67. NSString *ruiyunLogPath2 = [NSString stringWithFormat:@"%@/logs/app.log",CachesPatch];
  68. //3. 将文件压缩成ZIP
  69. NSArray *filesToZip = @[ruiyunLogPath, ruiyunLogPath2];
  70. [SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:filesToZip];
  71. NSData *logData = [NSData dataWithContentsOfFile:zipPath];
  72. HLog(@"%@",logData);
  73. if(!logData || [logData length]==0){
  74. _canUploadNowType = YES;
  75. success(@"");
  76. return;
  77. }
  78. //4.上传到文件中心
  79. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  80. NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  81. if(!snStr){
  82. snStr = [iTools getNowTimeString];
  83. }
  84. NSString*filename = [[NSString alloc] initWithFormat:@"%@_ios.log",snStr];
  85. [paraDict setValue:filename forKey:@"filename"];
  86. [paraDict setValue:@7 forKey:@"days"];
  87. HLog(@"hxd 222222");
  88. KWeakSelf
  89. [[netWorkManager shareInstance] doUploadFileToFileServiceWithParams:paraDict data:logData success:^(id _Nonnull responseObject) {
  90. HLog(@"%@",responseObject);
  91. fileUploadToFileCenterModel *model = [[fileUploadToFileCenterModel alloc] initWithDictionary:responseObject error:nil];
  92. if(model.code == 200){
  93. NSString * fileKey = model.data.fileKey;
  94. if(fileKey && fileKey.length >0){
  95. [weakSelf markAddLogTimeFun];
  96. //[self uploadKeyToServerFunBy:fileKey];
  97. success(fileKey);
  98. }
  99. else{
  100. success(@"");
  101. self->_canUploadNowType = YES;
  102. }
  103. }
  104. else{
  105. success(@"");
  106. self->_canUploadNowType = YES;
  107. }
  108. } faild:^(NSError * _Nonnull error) {
  109. success(@"");
  110. self->_canUploadNowType = YES;
  111. }];
  112. //[self uploadKeyToServerFunBy:@"LowLevelMultipartUpload_43025714606656109698"];
  113. }
  114. //3.拿到key上传到盒子服务器
  115. //- (void)uploadKeyToServerFunBy:(NSString*)fileKey
  116. //{
  117. // NSMutableDictionary *paraDict = [NSMutableDictionary new];
  118. // [paraDict setValue:@"ios" forKey:@"type"];
  119. //
  120. // NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  121. // if(!snStr){
  122. // _canUploadNowType = YES;
  123. // return;
  124. // }
  125. // [paraDict setValue:snStr forKey:@"sn"];
  126. // [paraDict setValue:fileKey forKey:@"key"];
  127. //
  128. // KWeakSelf
  129. // [[netWorkManager shareInstance] CommonPostCallBackCode:addLogFun Parameters:paraDict success:^(id _Nonnull responseObject) {
  130. // SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  131. // if(model.code == 0){
  132. // [weakSelf markAddLogTimeFun];
  133. // }
  134. // else{
  135. // self->_canUploadNowType = YES;
  136. // }
  137. //
  138. // } failure:^(NSError * _Nonnull error) {
  139. // self->_canUploadNowType = YES;
  140. // }];
  141. //}
  142. - (void)markAddLogTimeFun
  143. {
  144. //
  145. long seconds = [iTools getNowTimeStamp];
  146. NSNumber *secondsNum = [NSNumber numberWithLong:seconds];
  147. if(secondsNum){
  148. [HWDataManager setNumberWithKey:stringKeyAddSn(@"addLog") value:secondsNum];
  149. }
  150. _canUploadNowType = YES;
  151. //HLog(@"hxd endTime");
  152. }
  153. @end