RAUploadOperation.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. if(file_path==nil)
  74. {
  75. DebugLog(@"error no file");
  76. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
  77. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  78. [self willChangeValueForKey:@"isFinished"];
  79. finished = true;
  80. [self didChangeValueForKey:@"isFinished"];
  81. return;
  82. }
  83. _urlSessionTask = [NetworkUtils upload:[self filePath:_taskinfo ] Params:_taskinfo[@"params"] ToHost:_taskinfo[@"url"] Result:^(NSMutableDictionary *result) {
  84. blockDebugLog(@"upload result: %@",result);
  85. int r=[result[@"result"] intValue];
  86. if(r==2|| r==RESULT_BARCODE_ERROR)
  87. {
  88. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusFinish];
  89. if(r==2) {
  90. block_task[@"msg"]=@"upload successful";
  91. // after V1.07 上传成功后移除文件
  92. NSString *path = [weakself filePath:_taskinfo];
  93. if (path != nil) {
  94. [RAUtils removeFileAtPath:path];
  95. }
  96. }
  97. else
  98. block_task[@"msg"]=[@"warning: " stringByAppendingString:result[@"msg"]];
  99. }
  100. else
  101. {
  102. BOOL canUpload = [Appdelegate.uploadManager canUpload];
  103. DebugLog(@"can upload %@",@(canUpload));
  104. if (!canUpload) {
  105. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusStop];
  106. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  107. block_task[@"msg"]= @"Can only use wifi to upload";
  108. }
  109. else if([block_task[@"retry"] intValue]>=_maxRetry)
  110. {
  111. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
  112. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  113. block_task[@"msg"]=result[@"msg"];
  114. }
  115. else
  116. {
  117. block_task[@"retry"]=[NSNumber numberWithInt:[block_task[@"retry"] intValue]+1];
  118. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusWait];
  119. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  120. block_task[@"msg"]=result[@"connection lost, retry..."];
  121. RAUploadOperation* operation = [[RAUploadOperation alloc] initWithTaskinfo:block_task retry:_maxRetry];
  122. operation.updateUI= weakself.updateUI;
  123. operation.completionBlock = weakself.completionBlock;
  124. block_task[@"operation"] = operation;
  125. // [Appdelegate.uploadManager.operation_queue addOperation:operation];
  126. // retry:
  127. operation.joinTimeInterval = [[NSDate date] timeIntervalSince1970];
  128. operation.waitTimeInterval = weakself.waitTimeInterval + Appdelegate.uploadManager.retryTimeInterval;
  129. [Appdelegate.uploadManager addRetryOperation:operation];
  130. // [self upload:url];
  131. }
  132. }
  133. dispatch_async(dispatch_get_main_queue(), ^{
  134. if(weakself.updateUI)
  135. weakself.updateUI();
  136. });
  137. [weakself willChangeValueForKey:@"isFinished"];
  138. finished = true;
  139. [weakself didChangeValueForKey:@"isFinished"];
  140. // [self completeOperation];
  141. } Progress:^(NSURLSessionTask *task,double progress) {
  142. BOOL canUpload = [Appdelegate.uploadManager canUpload];
  143. if (!canUpload) {
  144. printf("cancel task on progress");
  145. [task cancel];
  146. return ;
  147. }
  148. dispatch_async(dispatch_get_main_queue(), ^{
  149. // ((UIProgressView*) block_task[@"ui_pregress"]).progress = progress;
  150. block_task[@"progress"]=[NSNumber numberWithFloat:progress];
  151. // NSLog(@"updateUI progress %p",weakself);
  152. DebugLog(@"update UI %@ p:%f",weakself.updateUI,progress);
  153. if(weakself.updateUI)
  154. {
  155. // NSLog(@"updateUI progress CALL");
  156. weakself.updateUI();
  157. }
  158. });
  159. } DecryptHandler:^id(NSString *result) {
  160. return @"bibibi";
  161. }];
  162. //完成下载
  163. // DebugLog(@"operation finish %@",self);
  164. // [self didChangeValueForKey:@"isExecuting"];
  165. }
  166. -(NSString*)filePath:(NSMutableDictionary*)task
  167. {
  168. // NSString *path = [RAUtils appCacheDirectory];
  169. // path= [path stringByAppendingPathComponent:task[@"path"]];
  170. // path= [path stringByAppendingPathComponent:task[@"file"]];
  171. // V1.07 之后图片不再保存到Temp
  172. NSString *file = [task objectForKey:@"file"];
  173. NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  174. NSString *path = [dir stringByAppendingPathComponent:file];
  175. NSFileManager* fmanager = [NSFileManager new];
  176. bool file_exist=[fmanager fileExistsAtPath:path];
  177. if(!file_exist)
  178. return nil;
  179. return path;
  180. }
  181. //- (void)completeOperation {
  182. //// [self willChangeValueForKey:@"isFinished"];
  183. //// [self willChangeValueForKey:@"isExecuting"];
  184. ////
  185. //// executing = NO;
  186. //// finished = YES;
  187. ////
  188. //// [self didChangeValueForKey:@"isExecuting"];
  189. //// [self didChangeValueForKey:@"isFinished"];
  190. //}
  191. - (void)dealloc
  192. {
  193. // dumpThreads(@"dealloc");
  194. DebugLog(@"operation Dealloc %@",self);
  195. }
  196. - (void)cancel {
  197. DebugLog(@"operation Cancel %@",self);
  198. if (_urlSessionTask.state == NSURLSessionTaskStateRunning || _urlSessionTask.state == NSURLSessionTaskStateSuspended) {
  199. DebugLog(@"cancel session task");
  200. [_urlSessionTask suspend];
  201. [_urlSessionTask cancel];
  202. }
  203. [super cancel];
  204. }
  205. #pragma mark - Retry
  206. - (NSTimeInterval)theRestOfWaitTimeInterval {
  207. NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
  208. return self.waitTimeInterval - (currentTime - self.joinTimeInterval);
  209. }
  210. @end