TVCReport.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // TVCReport.m
  3. // TXMUploader
  4. //
  5. // Created by carolsuo on 2018/3/28.
  6. // Copyright © 2018年 lynxzhang. All rights reserved.
  7. //
  8. #import "TVCReport.h"
  9. #import "TVCUtils.h"
  10. #define MAXCACHES 100
  11. @implementation TVCReport
  12. static TVCReport *_shareInstance = nil;
  13. + (instancetype)shareInstance
  14. {
  15. static dispatch_once_t predicate;
  16. dispatch_once(&predicate, ^{
  17. _shareInstance = [[TVCReport alloc] init];
  18. });
  19. return _shareInstance;
  20. }
  21. - (instancetype)init
  22. {
  23. if (self = [super init])
  24. {
  25. _reportCaches = [NSMutableArray new];
  26. _timer = nil;
  27. }
  28. return self;
  29. }
  30. - (void) addReportInfo:(TVCReportInfo *)info {
  31. if (_timer == nil) {
  32. dispatch_async(dispatch_get_main_queue(), ^{
  33. self.timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(reportAll) userInfo:nil repeats:YES];
  34. });
  35. }
  36. TVCReportInfo* copy = [[TVCReportInfo alloc] init];
  37. copy.reqType = info.reqType;
  38. copy.errCode = info.errCode;
  39. copy.errMsg = info.errMsg;
  40. copy.reqTime = info.reqTime;
  41. copy.reqTimeCost = info.reqTimeCost;
  42. copy.fileSize = info.fileSize;
  43. copy.fileType = info.fileType;
  44. copy.fileName = info.fileName;
  45. copy.fileId = info.fileId;
  46. copy.appId = info.appId;
  47. copy.reqServerIp = info.reqServerIp;
  48. copy.reportId = info.reportId;
  49. copy.reqKey = info.reqKey;
  50. copy.vodSessionKey = info.vodSessionKey;
  51. copy.retryCount = info.retryCount;
  52. copy.reporting = info.reporting;
  53. @synchronized (self.reportCaches) {
  54. if (self.reportCaches.count > MAXCACHES) {
  55. [self.reportCaches removeObjectAtIndex:0];
  56. }
  57. [self.reportCaches addObject:copy];
  58. }
  59. [self reportAll];
  60. }
  61. - (void)reportAll {
  62. if ([TVCUtils tvc_getNetWorkType] == 0) {
  63. return;
  64. }
  65. @synchronized (self.reportCaches) {
  66. NSMutableArray *delList = [NSMutableArray array];
  67. for (TVCReportInfo *obj in self.reportCaches) {
  68. if (obj.retryCount < 4) {
  69. if (obj.reporting == NO) {
  70. [self report:obj];
  71. }
  72. } else {
  73. [delList addObject:obj];
  74. }
  75. }
  76. [self.reportCaches removeObjectsInArray:delList];
  77. }
  78. }
  79. - (void) report:(TVCReportInfo *)info {
  80. NSMutableDictionary *dictParam = [[NSMutableDictionary alloc] init];
  81. [dictParam setValue:TVCVersion forKey:@"version"];
  82. [dictParam setValue:[NSNumber numberWithInt:info.reqType] forKey:@"reqType"];
  83. [dictParam setValue:[NSNumber numberWithInt:info.errCode] forKey:@"errCode"];
  84. [dictParam setValue:info.errMsg forKey:@"errMsg"];
  85. [dictParam setValue:[NSNumber numberWithLongLong:info.reqTimeCost] forKey:@"reqTimeCost"];
  86. [dictParam setValue:[NSNumber numberWithLongLong:info.reqTime] forKey:@"reqTime"];
  87. [dictParam setValue:info.reqServerIp forKey:@"reqServerIp"];
  88. [dictParam setValue:[NSNumber numberWithInt:1000] forKey:@"platform"]; // 1000 - iOS, 2000 - Android
  89. [dictParam setValue:[TVCUtils tvc_deviceModelName] forKey:@"device"];
  90. [dictParam setValue:[[UIDevice currentDevice] systemVersion] forKey:@"osType"];
  91. [dictParam setValue:[NSNumber numberWithInt:[TVCUtils tvc_getNetWorkType]] forKey:@"netType"];
  92. [dictParam setValue:info.reportId forKey:@"reportId"];
  93. [dictParam setValue:[TVCUtils tvc_getDevUUID] forKey:@"uuid"];
  94. [dictParam setValue:info.reqKey forKey:@"reqKey"];
  95. [dictParam setValue:[NSNumber numberWithLongLong:info.appId] forKey:@"appId"];
  96. [dictParam setValue:[NSNumber numberWithLongLong:info.fileSize] forKey:@"fileSize"];
  97. [dictParam setValue:info.fileType forKey:@"fileType"];
  98. [dictParam setValue:info.fileName forKey:@"fileName"];
  99. [dictParam setValue:info.vodSessionKey forKey:@"vodSessionKey"];
  100. [dictParam setValue:info.fileId forKey:@"fileId"];
  101. NSError *error = nil;
  102. NSData *bodyData = [NSJSONSerialization dataWithJSONObject:dictParam options:0 error:&error];
  103. if (error) {
  104. return;
  105. }
  106. // set url
  107. NSString *baseUrl = @"https://vodreport.qcloud.com/ugcupload";
  108. // create request
  109. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:baseUrl]];
  110. [request setValue:[NSString stringWithFormat:@"%ld", (long) [bodyData length]] forHTTPHeaderField:@"Content-Length"];
  111. [request setHTTPMethod:@"POST"];
  112. [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
  113. [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
  114. [request setHTTPBody:bodyData];
  115. ++info.retryCount;
  116. info.reporting = YES;
  117. __weak TVCReport *ws = self;
  118. NSURLSession *session = [NSURLSession sharedSession];
  119. NSURLSessionTask *initTask = [session dataTaskWithRequest:request completionHandler:^(NSData *_Nullable initData, NSURLResponse *_Nullable response, NSError *_Nullable error) {
  120. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  121. if (error == nil && httpResponse.statusCode == 200) {
  122. [ws delReportInfo:info];
  123. } else {
  124. info.reporting = NO;
  125. }
  126. }];
  127. [initTask resume];
  128. }
  129. -(void) delReportInfo:(TVCReportInfo *)info {
  130. @synchronized (self.reportCaches) {
  131. [self.reportCaches removeObject:info];
  132. }
  133. info = nil;
  134. }
  135. @end