addLogObject.m 3.9 KB

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