| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- //
- // RAUploadOperation.m
- // test_autolayout
- //
- // Created by Ray on 03/05/2017.
- // Copyright © 2017 USAI. All rights reserved.
- //
- #import "RAUploadOperation.h"
- #import "NetworkUtils.h"
- #import <UIKit/UIKit.h>
- #import "AppDelegate.h"
- #import "RAUtils.h"
- #import "const.h"
- @interface RAUploadOperation ()
- {
- // NSString* _threadName;
- // NSString* _url;
- BOOL executing;
- BOOL finished;
- NSMutableDictionary* _taskinfo;
- int _maxRetry;
- NSURLSessionTask *_urlSessionTask;
- }
- @end
- @implementation RAUploadOperation
- - (instancetype)initWithTaskinfo:(NSMutableDictionary*)taskinfo retry:(int) maxRetry
- {
- self = [super init];
-
- if (self) {
- // if (name!=nil)
- // _threadName = name;
- _maxRetry = maxRetry;
- // executing = NO;
- // finished = NO;
- _taskinfo=taskinfo;
-
- }
- return self;
- }
- - (BOOL)isConcurrent {
- return YES;
- }
- //
- - (BOOL)isExecuting {
- return executing;
- }
- - (BOOL)isFinished {
- return finished;
- }
- - (void)start
- {
-
- [self willChangeValueForKey:@"isExecuting"];
- executing = true;
-
- _taskinfo[@"status"]=[NSNumber numberWithInt:TaskStatusStart];
-
- [self didChangeValueForKey:@"isExecuting"];
-
- NSLog(@"---------------start--------------------");
-
- AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
-
-
-
- // sleep(3);
- //
- // [self willChangeValueForKey:@"isFinished"];
- // finished = true;
- // [self didChangeValueForKey:@"isFinished"];
- //
- //
- // return;
-
- __weak typeof(self) weakself = self;
-
-
- __block NSMutableDictionary* block_task = _taskinfo;
- // NSString *md5 = [RAUtils md5WithFile:[self filePath:_taskinfo ]];
- DebugLog(@"task %@",_taskinfo);
- NSString* file_path = [self filePath:_taskinfo];
- if(file_path==nil)
- {
- DebugLog(@"error no file");
- block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
- block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
- [self willChangeValueForKey:@"isFinished"];
- finished = true;
- [self didChangeValueForKey:@"isFinished"];
- return;
- }
-
- _urlSessionTask = [NetworkUtils upload:[self filePath:_taskinfo ] Params:_taskinfo[@"params"] ToHost:_taskinfo[@"url"] Result:^(NSMutableDictionary *result) {
- blockDebugLog(@"upload result: %@",result);
- int r=[result[@"result"] intValue];
- if(r==2|| r==RESULT_BARCODE_ERROR)
- {
- block_task[@"status"]=[NSNumber numberWithInt:TaskStatusFinish];
- if(r==2)
- block_task[@"msg"]=@"upload successful";
- else
- block_task[@"msg"]=[@"warning: " stringByAppendingString:result[@"msg"]];
- }
- else
- {
- BOOL canUpload = [Appdelegate.uploadManager canUpload];
- DebugLog(@"can upload %@",@(canUpload));
- if (!canUpload) {
- block_task[@"status"]=[NSNumber numberWithInt:TaskStatusStop];
- block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
- block_task[@"msg"]= @"Can only use wifi to upload";
-
- }
- else if([block_task[@"retry"] intValue]>=_maxRetry)
- {
- block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
- block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
- block_task[@"msg"]=result[@"msg"];
- }
- else
- {
- block_task[@"retry"]=[NSNumber numberWithInt:[block_task[@"retry"] intValue]+1];
- block_task[@"status"]=[NSNumber numberWithInt:TaskStatusWait];
- block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
-
- block_task[@"msg"]=result[@"connection lost, retry..."];
-
- RAUploadOperation* operation = [[RAUploadOperation alloc] initWithTaskinfo:block_task retry:_maxRetry];
- operation.updateUI= weakself.updateUI;
- operation.completionBlock = weakself.completionBlock;
- block_task[@"operation"] = operation;
- [Appdelegate.uploadManager.operation_queue addOperation:operation];
- // [self upload:url];
- }
- }
-
- dispatch_async(dispatch_get_main_queue(), ^{
- if(weakself.updateUI)
- weakself.updateUI();
- });
- [weakself willChangeValueForKey:@"isFinished"];
- finished = true;
- [weakself didChangeValueForKey:@"isFinished"];
- // [self completeOperation];
-
-
- } Progress:^(NSURLSessionTask *task,double progress) {
- BOOL canUpload = [Appdelegate.uploadManager canUpload];
- if (!canUpload) {
- printf("cancel task on progress");
- [task cancel];
- return ;
- }
-
- dispatch_async(dispatch_get_main_queue(), ^{
- // ((UIProgressView*) block_task[@"ui_pregress"]).progress = progress;
- block_task[@"progress"]=[NSNumber numberWithFloat:progress];
- // NSLog(@"updateUI progress %p",weakself);
- DebugLog(@"update UI %@ p:%f",weakself.updateUI,progress);
- if(weakself.updateUI)
- {
- // NSLog(@"updateUI progress CALL");
- weakself.updateUI();
- }
- });
-
- } DecryptHandler:^id(NSString *result) {
-
- return @"bibibi";
-
- }];
-
- //完成下载
- // DebugLog(@"operation finish %@",self);
-
- // [self didChangeValueForKey:@"isExecuting"];
- }
- -(NSString*)filePath:(NSMutableDictionary*)task
- {
- NSString *path = [RAUtils appCacheDirectory];
- path= [path stringByAppendingPathComponent:task[@"path"]];
- path= [path stringByAppendingPathComponent:task[@"file"]];
-
-
- NSFileManager* fmanager = [NSFileManager new];
-
- bool file_exist=[fmanager fileExistsAtPath:path];
- if(!file_exist)
- return nil;
- return path;
- }
- //- (void)completeOperation {
- //// [self willChangeValueForKey:@"isFinished"];
- //// [self willChangeValueForKey:@"isExecuting"];
- ////
- //// executing = NO;
- //// finished = YES;
- ////
- //// [self didChangeValueForKey:@"isExecuting"];
- //// [self didChangeValueForKey:@"isFinished"];
- //}
- - (void)dealloc
- {
- // dumpThreads(@"dealloc");
- DebugLog(@"operation Dealloc %@",self);
- }
- - (void)cancel {
- DebugLog(@"operation Cancel %@",self);
-
- if (_urlSessionTask.state == NSURLSessionTaskStateRunning || _urlSessionTask.state == NSURLSessionTaskStateSuspended) {
- DebugLog(@"cancel session task");
- [_urlSessionTask suspend];
- [_urlSessionTask cancel];
- }
-
- [super cancel];
- }
- @end
|