addLogObject.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // addLogObject.m
  3. //
  4. //
  5. // Created by David on 2024/4/11.
  6. //
  7. #import "addLogObject.h"
  8. #import "netWorkManager.h"
  9. #import "fileUploadToFileCenterModel.h"
  10. @implementation addLogObject
  11. static addLogObject *addlog_ShareInstance = nil;
  12. +(addLogObject *)shareInstance;
  13. {
  14. static dispatch_once_t onceToken;
  15. dispatch_once(&onceToken, ^{
  16. addlog_ShareInstance = [[addLogObject alloc]init];
  17. });
  18. return addlog_ShareInstance;
  19. }
  20. - (id)init
  21. {
  22. self = [super init];
  23. if (self) {
  24. _canUploadNowType = YES;
  25. }
  26. return self;
  27. }
  28. //1.读取日志
  29. //2.上传到文件中心
  30. //3.拿到key上传到盒子服务器
  31. - (void)gotoAddLogFun
  32. {
  33. //HLog(@"hxd beginTime");
  34. if(!_canUploadNowType){
  35. return;
  36. }
  37. //一小时内只能上传一次
  38. NSNumber *preSecondNum = [HWDataManager getNumberWithKey:stringKeyAddSn(@"addLog")];
  39. if(preSecondNum){
  40. long seconds = [iTools getNowTimeStamp];
  41. if(seconds - preSecondNum.longValue <= 60*60){
  42. return;
  43. }
  44. }
  45. _canUploadNowType = NO;
  46. //1.读取日志
  47. NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/debug_0.log",CachesPatch];
  48. NSData *logData = [NSData dataWithContentsOfFile:ruiyunLogPath];
  49. HLog(@"%@",logData);
  50. if(!logData || [logData length]==0){
  51. _canUploadNowType = YES;
  52. return;
  53. }
  54. //2.上传到文件中心
  55. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  56. NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  57. if(!snStr){
  58. snStr = [iTools getNowTimeString];
  59. }
  60. NSString*filename = [[NSString alloc] initWithFormat:@"%@_ios.log",snStr];
  61. [paraDict setValue:filename forKey:@"filename"];
  62. [[netWorkManager shareInstance] doUploadFileToFileServiceWithParams:paraDict data:logData success:^(id _Nonnull responseObject) {
  63. HLog(@"%@",responseObject);
  64. fileUploadToFileCenterModel *model = [[fileUploadToFileCenterModel alloc] initWithDictionary:responseObject error:nil];
  65. if(model.code == 200){
  66. NSString * fileKey = model.data.fileKey;
  67. if(fileKey && fileKey.length >0){
  68. [self uploadKeyToServerFunBy:fileKey];
  69. }
  70. else{
  71. self->_canUploadNowType = YES;
  72. }
  73. }
  74. else{
  75. self->_canUploadNowType = YES;
  76. }
  77. } faild:^(NSError * _Nonnull error) {
  78. self->_canUploadNowType = YES;
  79. }];
  80. //[self uploadKeyToServerFunBy:@"LowLevelMultipartUpload_43025714606656109698"];
  81. }
  82. //3.拿到key上传到盒子服务器
  83. - (void)uploadKeyToServerFunBy:(NSString*)fileKey
  84. {
  85. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  86. [paraDict setValue:@"ios" forKey:@"type"];
  87. NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  88. if(!snStr){
  89. _canUploadNowType = YES;
  90. return;
  91. }
  92. [paraDict setValue:snStr forKey:@"sn"];
  93. [paraDict setValue:fileKey forKey:@"key"];
  94. KWeakSelf
  95. [[netWorkManager shareInstance] CommonPostCallBackCode:addLogFun Parameters:paraDict success:^(id _Nonnull responseObject) {
  96. SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  97. if(model.code == 0){
  98. [weakSelf markAddLogTimeFun];
  99. }
  100. else{
  101. self->_canUploadNowType = YES;
  102. }
  103. } failure:^(NSError * _Nonnull error) {
  104. self->_canUploadNowType = YES;
  105. }];
  106. }
  107. - (void)markAddLogTimeFun
  108. {
  109. //
  110. long seconds = [iTools getNowTimeStamp];
  111. NSNumber *secondsNum = [NSNumber numberWithLong:seconds];
  112. if(secondsNum){
  113. [HWDataManager setNumberWithKey:stringKeyAddSn(@"addLog") value:secondsNum];
  114. }
  115. _canUploadNowType = YES;
  116. //HLog(@"hxd endTime");
  117. }
  118. @end