RAUploadOperation.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // RAUploadOperation.m
  3. // test_autolayout
  4. //
  5. // Created by Ray on 03/05/2017.
  6. // Copyright © 2017 USAI. All rights reserved.
  7. //
  8. #import "RAUploadOperation.h"
  9. #import "NetworkUtils.h"
  10. #import <UIKit/UIKit.h>
  11. #import "AppDelegate.h"
  12. #import "RAUtils.h"
  13. #import "const.h"
  14. static const NSTimeInterval waitTimeIntervalDelta = 60 * 3;
  15. @interface RAUploadOperation ()
  16. {
  17. // NSString* _threadName;
  18. // NSString* _url;
  19. BOOL executing;
  20. BOOL finished;
  21. NSMutableDictionary* _taskinfo;
  22. int _maxRetry;
  23. NSURLSessionTask *_urlSessionTask;
  24. }
  25. @property (nonatomic,assign) NSTimeInterval joinTimeInterval;///<开始等待时间
  26. @property (nonatomic,assign) NSTimeInterval waitTimeInterval;///<需要等待的时间
  27. @end
  28. @implementation RAUploadOperation
  29. - (instancetype)initWithTaskinfo:(NSMutableDictionary*)taskinfo retry:(int) maxRetry
  30. {
  31. self = [super init];
  32. if (self) {
  33. // if (name!=nil)
  34. // _threadName = name;
  35. _maxRetry = maxRetry;
  36. // executing = NO;
  37. // finished = NO;
  38. _taskinfo=taskinfo;
  39. }
  40. return self;
  41. }
  42. - (BOOL)isConcurrent {
  43. return YES;
  44. }
  45. //
  46. - (BOOL)isExecuting {
  47. return executing;
  48. }
  49. - (BOOL)isFinished {
  50. return finished;
  51. }
  52. - (void)start
  53. {
  54. [self willChangeValueForKey:@"isExecuting"];
  55. executing = true;
  56. _taskinfo[@"status"]=[NSNumber numberWithInt:TaskStatusStart];
  57. [self didChangeValueForKey:@"isExecuting"];
  58. NSLog(@"---------------start--------------------");
  59. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  60. // sleep(3);
  61. //
  62. // [self willChangeValueForKey:@"isFinished"];
  63. // finished = true;
  64. // [self didChangeValueForKey:@"isFinished"];
  65. //
  66. //
  67. // return;
  68. __weak typeof(self) weakself = self;
  69. __block NSMutableDictionary* block_task = _taskinfo;
  70. // NSString *md5 = [RAUtils md5WithFile:[self filePath:_taskinfo ]];
  71. DebugLog(@"task %@",_taskinfo);
  72. NSString* file_path = [self filePath:_taskinfo];
  73. BOOL noFileTag = [[_taskinfo objectForKey:@"noFile"] boolValue]; // Apex Drivers 2018.6.6
  74. if(file_path==nil && !noFileTag)
  75. {
  76. DebugLog(@"error no file");
  77. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
  78. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  79. [self willChangeValueForKey:@"isFinished"];
  80. finished = true;
  81. [self didChangeValueForKey:@"isFinished"];
  82. return;
  83. }
  84. _urlSessionTask = [NetworkUtils upload:[self filePath:_taskinfo ] Params:_taskinfo[@"params"] ToHost:_taskinfo[@"url"] Result:^(NSMutableDictionary *result) {
  85. blockDebugLog(@"upload result: %@",result);
  86. int r=[result[@"result"] intValue];
  87. if(r==2|| r==RESULT_BARCODE_ERROR)
  88. {
  89. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusFinish];
  90. if(r==2) {
  91. block_task[@"msg"]=@"upload successful";
  92. // after V1.07 上传成功后移除文件
  93. NSString *path = [weakself filePath:_taskinfo];
  94. if (path != nil) {
  95. [RAUtils removeFileAtPath:path];
  96. }
  97. }
  98. else
  99. block_task[@"msg"]=[@"warning: " stringByAppendingString:result[@"msg"]];
  100. }
  101. else
  102. {
  103. BOOL canUpload = [Appdelegate.uploadManager canUpload];
  104. DebugLog(@"can upload %@",@(canUpload));
  105. if (!canUpload) {
  106. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusStop];
  107. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  108. block_task[@"msg"]= @"Can only use wifi to upload";
  109. }
  110. else if([block_task[@"retry"] intValue]>=_maxRetry)
  111. {
  112. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
  113. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  114. block_task[@"msg"]=result[@"msg"];
  115. }
  116. else
  117. {
  118. block_task[@"retry"]=[NSNumber numberWithInt:[block_task[@"retry"] intValue]+1];
  119. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusWait];
  120. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  121. block_task[@"msg"]=result[@"connection lost, retry..."];
  122. RAUploadOperation* operation = [[RAUploadOperation alloc] initWithTaskinfo:block_task retry:_maxRetry];
  123. operation.updateUI= weakself.updateUI;
  124. operation.completionBlock = weakself.completionBlock;
  125. block_task[@"operation"] = operation;
  126. // [Appdelegate.uploadManager.operation_queue addOperation:operation];
  127. // retry:
  128. operation.joinTimeInterval = [[NSDate date] timeIntervalSince1970];
  129. operation.waitTimeInterval = weakself.waitTimeInterval + Appdelegate.uploadManager.retryTimeInterval;
  130. [Appdelegate.uploadManager addRetryOperation:operation];
  131. // [self upload:url];
  132. }
  133. }
  134. dispatch_async(dispatch_get_main_queue(), ^{
  135. if(weakself.updateUI)
  136. weakself.updateUI();
  137. });
  138. [weakself willChangeValueForKey:@"isFinished"];
  139. finished = true;
  140. [weakself didChangeValueForKey:@"isFinished"];
  141. // [self completeOperation];
  142. } Progress:^(NSURLSessionTask *task,double progress) {
  143. BOOL canUpload = [Appdelegate.uploadManager canUpload];
  144. if (!canUpload) {
  145. printf("cancel task on progress");
  146. [task cancel];
  147. return ;
  148. }
  149. dispatch_async(dispatch_get_main_queue(), ^{
  150. // ((UIProgressView*) block_task[@"ui_pregress"]).progress = progress;
  151. block_task[@"progress"]=[NSNumber numberWithFloat:progress];
  152. // NSLog(@"updateUI progress %p",weakself);
  153. DebugLog(@"update UI %@ p:%f",weakself.updateUI,progress);
  154. if(weakself.updateUI)
  155. {
  156. // NSLog(@"updateUI progress CALL");
  157. weakself.updateUI();
  158. }
  159. });
  160. } DecryptHandler:^id(NSString *result) {
  161. return @"bibibi";
  162. }];
  163. //完成下载
  164. // DebugLog(@"operation finish %@",self);
  165. // [self didChangeValueForKey:@"isExecuting"];
  166. }
  167. -(NSString*)filePath:(NSMutableDictionary*)task
  168. {
  169. // NSString *path = [RAUtils appCacheDirectory];
  170. // path= [path stringByAppendingPathComponent:task[@"path"]];
  171. // path= [path stringByAppendingPathComponent:task[@"file"]];
  172. // V1.07 之后图片不再保存到Temp
  173. NSString *file = [task objectForKey:@"file"];
  174. if (file.length == 0) { // Apex Drivers 2018.6.6
  175. return nil;
  176. }
  177. NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  178. NSString *path = [dir stringByAppendingPathComponent:file];
  179. NSFileManager* fmanager = [NSFileManager new];
  180. bool file_exist=[fmanager fileExistsAtPath:path];
  181. if(!file_exist)
  182. return nil;
  183. return path;
  184. }
  185. //- (void)completeOperation {
  186. //// [self willChangeValueForKey:@"isFinished"];
  187. //// [self willChangeValueForKey:@"isExecuting"];
  188. ////
  189. //// executing = NO;
  190. //// finished = YES;
  191. ////
  192. //// [self didChangeValueForKey:@"isExecuting"];
  193. //// [self didChangeValueForKey:@"isFinished"];
  194. //}
  195. - (void)dealloc
  196. {
  197. // dumpThreads(@"dealloc");
  198. DebugLog(@"operation Dealloc %@",self);
  199. }
  200. - (void)cancel {
  201. DebugLog(@"operation Cancel %@",self);
  202. if (_urlSessionTask.state == NSURLSessionTaskStateRunning || _urlSessionTask.state == NSURLSessionTaskStateSuspended) {
  203. DebugLog(@"cancel session task");
  204. [_urlSessionTask suspend];
  205. [_urlSessionTask cancel];
  206. }
  207. [super cancel];
  208. }
  209. #pragma mark - Retry
  210. - (NSTimeInterval)theRestOfWaitTimeInterval {
  211. NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
  212. return self.waitTimeInterval - (currentTime - self.joinTimeInterval);
  213. }
  214. @end