RAUploadOperation.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. @interface RAUploadOperation ()
  15. {
  16. // NSString* _threadName;
  17. // NSString* _url;
  18. BOOL executing;
  19. BOOL finished;
  20. NSMutableDictionary* _taskinfo;
  21. int _maxRetry;
  22. NSURLSessionTask *_urlSessionTask;
  23. }
  24. @end
  25. @implementation RAUploadOperation
  26. - (instancetype)initWithTaskinfo:(NSMutableDictionary*)taskinfo retry:(int) maxRetry
  27. {
  28. self = [super init];
  29. if (self) {
  30. // if (name!=nil)
  31. // _threadName = name;
  32. _maxRetry = maxRetry;
  33. // executing = NO;
  34. // finished = NO;
  35. _taskinfo=taskinfo;
  36. }
  37. return self;
  38. }
  39. - (BOOL)isConcurrent {
  40. return YES;
  41. }
  42. //
  43. - (BOOL)isExecuting {
  44. return executing;
  45. }
  46. - (BOOL)isFinished {
  47. return finished;
  48. }
  49. - (void)start
  50. {
  51. [self willChangeValueForKey:@"isExecuting"];
  52. executing = true;
  53. _taskinfo[@"status"]=[NSNumber numberWithInt:TaskStatusStart];
  54. [self didChangeValueForKey:@"isExecuting"];
  55. NSLog(@"---------------start--------------------");
  56. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  57. // sleep(3);
  58. //
  59. // [self willChangeValueForKey:@"isFinished"];
  60. // finished = true;
  61. // [self didChangeValueForKey:@"isFinished"];
  62. //
  63. //
  64. // return;
  65. __weak typeof(self) weakself = self;
  66. __block NSMutableDictionary* block_task = _taskinfo;
  67. // NSString *md5 = [RAUtils md5WithFile:[self filePath:_taskinfo ]];
  68. DebugLog(@"task %@",_taskinfo);
  69. NSString* file_path = [self filePath:_taskinfo];
  70. if(file_path==nil)
  71. {
  72. DebugLog(@"error no file");
  73. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
  74. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  75. [self willChangeValueForKey:@"isFinished"];
  76. finished = true;
  77. [self didChangeValueForKey:@"isFinished"];
  78. return;
  79. }
  80. _urlSessionTask = [NetworkUtils upload:[self filePath:_taskinfo ] Params:_taskinfo[@"params"] ToHost:_taskinfo[@"url"] Result:^(NSMutableDictionary *result) {
  81. blockDebugLog(@"upload result: %@",result);
  82. int r=[result[@"result"] intValue];
  83. if(r==2|| r==RESULT_BARCODE_ERROR)
  84. {
  85. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusFinish];
  86. if(r==2)
  87. block_task[@"msg"]=@"upload successful";
  88. else
  89. block_task[@"msg"]=[@"warning: " stringByAppendingString:result[@"msg"]];
  90. }
  91. else
  92. {
  93. BOOL canUpload = [Appdelegate.uploadManager canUpload];
  94. DebugLog(@"can upload %@",@(canUpload));
  95. if (!canUpload) {
  96. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusStop];
  97. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  98. block_task[@"msg"]= @"Can only use wifi to upload";
  99. }
  100. else if([block_task[@"retry"] intValue]>=_maxRetry)
  101. {
  102. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
  103. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  104. block_task[@"msg"]=result[@"msg"];
  105. }
  106. else
  107. {
  108. block_task[@"retry"]=[NSNumber numberWithInt:[block_task[@"retry"] intValue]+1];
  109. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusWait];
  110. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  111. block_task[@"msg"]=result[@"connection lost, retry..."];
  112. RAUploadOperation* operation = [[RAUploadOperation alloc] initWithTaskinfo:block_task retry:_maxRetry];
  113. operation.updateUI= weakself.updateUI;
  114. operation.completionBlock = weakself.completionBlock;
  115. block_task[@"operation"] = operation;
  116. [Appdelegate.uploadManager.operation_queue addOperation:operation];
  117. // [self upload:url];
  118. }
  119. }
  120. dispatch_async(dispatch_get_main_queue(), ^{
  121. if(weakself.updateUI)
  122. weakself.updateUI();
  123. });
  124. [weakself willChangeValueForKey:@"isFinished"];
  125. finished = true;
  126. [weakself didChangeValueForKey:@"isFinished"];
  127. // [self completeOperation];
  128. } Progress:^(NSURLSessionTask *task,double progress) {
  129. BOOL canUpload = [Appdelegate.uploadManager canUpload];
  130. if (!canUpload) {
  131. printf("cancel task on progress");
  132. [task cancel];
  133. return ;
  134. }
  135. dispatch_async(dispatch_get_main_queue(), ^{
  136. // ((UIProgressView*) block_task[@"ui_pregress"]).progress = progress;
  137. block_task[@"progress"]=[NSNumber numberWithFloat:progress];
  138. // NSLog(@"updateUI progress %p",weakself);
  139. DebugLog(@"update UI %@ p:%f",weakself.updateUI,progress);
  140. if(weakself.updateUI)
  141. {
  142. // NSLog(@"updateUI progress CALL");
  143. weakself.updateUI();
  144. }
  145. });
  146. } DecryptHandler:^id(NSString *result) {
  147. return @"bibibi";
  148. }];
  149. //完成下载
  150. // DebugLog(@"operation finish %@",self);
  151. // [self didChangeValueForKey:@"isExecuting"];
  152. }
  153. -(NSString*)filePath:(NSMutableDictionary*)task
  154. {
  155. NSString *path = [RAUtils appCacheDirectory];
  156. path= [path stringByAppendingPathComponent:task[@"path"]];
  157. path= [path stringByAppendingPathComponent:task[@"file"]];
  158. NSFileManager* fmanager = [NSFileManager new];
  159. bool file_exist=[fmanager fileExistsAtPath:path];
  160. if(!file_exist)
  161. return nil;
  162. return path;
  163. }
  164. //- (void)completeOperation {
  165. //// [self willChangeValueForKey:@"isFinished"];
  166. //// [self willChangeValueForKey:@"isExecuting"];
  167. ////
  168. //// executing = NO;
  169. //// finished = YES;
  170. ////
  171. //// [self didChangeValueForKey:@"isExecuting"];
  172. //// [self didChangeValueForKey:@"isFinished"];
  173. //}
  174. - (void)dealloc
  175. {
  176. // dumpThreads(@"dealloc");
  177. DebugLog(@"operation Dealloc %@",self);
  178. }
  179. - (void)cancel {
  180. DebugLog(@"operation Cancel %@",self);
  181. if (_urlSessionTask.state == NSURLSessionTaskStateRunning || _urlSessionTask.state == NSURLSessionTaskStateSuspended) {
  182. DebugLog(@"cancel session task");
  183. [_urlSessionTask suspend];
  184. [_urlSessionTask cancel];
  185. }
  186. [super cancel];
  187. }
  188. @end