Commit e169a434 by GuoJianPeng

update

parent f530f5e7
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
// //
#import "CRMNetDetectService.h" #import "CRMNetDetectService.h"
#import <sys/time.h>
@interface CRMNetDetectService()<ASPingServiceDelegate,ASReachabilityDelegate> @interface CRMNetDetectService()<ASPingServiceDelegate,ASReachabilityDelegate>
{ {
...@@ -87,18 +88,47 @@ ...@@ -87,18 +88,47 @@
#pragma mark - Ping #pragma mark - Ping
- (void)_readHostAndPing - (void)_readHostAndPing
{ {
if(!_pingService){ NSString * host = _pingingHostList.firstObject;
_pingService = [[ASPingService alloc] initWithDelegate:self]; NSInteger index = [_businessHostList indexOfObject:host];
} NSMutableURLRequest*request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[@"https://" stringByAppendingString:_pingingHostList.firstObject]]];
if (!_pingingHostList.count){ request.HTTPMethod = @"HEAD";
[self.delegate didFinishedBusinessService:_successPingHostCount == _businessHostList.count]; CFTimeInterval startTime = CACurrentMediaTime();
[self stopDetecting]; __typeof(self)__weak weakSelf = self;
return; [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
} __typeof(weakSelf)__strong self = weakSelf;
_pingService.targetHost = _pingingHostList.firstObject; if(!self){return;}
_pingService.pingCount = 1;
_pingService.timeoutSeconds = 1; CFTimeInterval endTime = CACurrentMediaTime();
[_pingService starts]; NSTimeInterval duration = 0;
if(endTime < startTime){
endTime = startTime+1;
}
duration = (endTime - startTime)*1000;
[self->_pingingHostList removeObjectAtIndex:0];
[self.delegate didGotBusinessServiceState:!error host:host hostIndex:index millionSeconds:duration];
if(!error){
self->_successPingHostCount++;
}
if(!self->_pingingHostList.count){
[self.delegate didFinishedBusinessService:self->_successPingHostCount == self.businessHostList.count];
[self stopDetecting];
}else{
[self _readHostAndPing];
}
}] resume];
// if(!_pingService){
// _pingService = [[ASPingService alloc] initWithDelegate:self];
// }
// if (!_pingingHostList.count){
// [self.delegate didFinishedBusinessService:_successPingHostCount == _businessHostList.count];
// [self stopDetecting];
// return;
// }
// _pingService.targetHost = _pingingHostList.firstObject;
// _pingService.pingCount = 1;
// _pingService.timeoutSeconds = 1;
// [_pingService starts];
} }
- (void)_stopPingService - (void)_stopPingService
{ {
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
@property(nonatomic,strong)UIButton * checkButton; @property(nonatomic,strong)UIButton * checkButton;
@property(nonatomic,strong)UIImageView * signalImageView; @property(nonatomic,strong)UIImageView * signalImageView;
@property(nonatomic,strong)NSMutableArray<CRMNetDetectCellData*>*cellDatas; @property(nonatomic,strong)NSMutableArray<CRMNetDetectCellData*>*cellDatas;
@property(nonatomic,strong)UIActivityIndicatorView * checkingIndicateView;
@end @end
@implementation CRMNetDetectView @implementation CRMNetDetectView
...@@ -108,6 +109,7 @@ ...@@ -108,6 +109,7 @@
_signalStrengthData.rightText =(strength==CRMNetSignalStrengthStrong)?@"强":@"弱"; _signalStrengthData.rightText =(strength==CRMNetSignalStrengthStrong)?@"强":@"弱";
} }
[self.tableView reloadData]; [self.tableView reloadData];
[self.tableView setTableFooterView:[self _footView]];
} }
- (void)didGotIPState:(BOOL)isNormal - (void)didGotIPState:(BOOL)isNormal
{ {
...@@ -116,15 +118,29 @@ ...@@ -116,15 +118,29 @@
} }
- (void)didGotBusinessServiceState:(BOOL)isNormal host:(nonnull NSString *)host hostIndex:(NSInteger)hostIndex millionSeconds:(NSTimeInterval)millionSeconds - (void)didGotBusinessServiceState:(BOOL)isNormal host:(nonnull NSString *)host hostIndex:(NSInteger)hostIndex millionSeconds:(NSTimeInterval)millionSeconds
{ {
CRMNetDetectCellData * data = _cellDatas[hostIndex+_staticCellDataCount]; __typeof(self)__weak weakSelf = self;
data.state = isNormal? CRMNetDetectCellStateNormal :CRMNetDetectCellStateNotNormal; dispatch_async(dispatch_get_main_queue(), ^{
data.rightText = isNormal ? [NSString stringWithFormat:@"正常 %ld ms",(NSInteger)millionSeconds] : @"异常"; __typeof(weakSelf)__strong self = weakSelf;
[self.tableView reloadData]; if(!self){return;}
CRMNetDetectCellData * data = self.cellDatas[hostIndex+self->_staticCellDataCount];
data.state = isNormal? CRMNetDetectCellStateNormal :CRMNetDetectCellStateNotNormal;
data.rightText = isNormal ? [NSString stringWithFormat:@"正常 %ld ms",(NSInteger)millionSeconds] : @"异常";
[self.tableView reloadData];
});
} }
- (void)didFinishedBusinessService:(BOOL)isNormal - (void)didFinishedBusinessService:(BOOL)isNormal
{ {
_serverConnectData.state =isNormal? CRMNetDetectCellStateNormal :CRMNetDetectCellStateNotNormal; __typeof(self)__weak weakSelf = self;
[self.tableView reloadData]; dispatch_async(dispatch_get_main_queue(), ^{
__typeof(weakSelf)__strong self = weakSelf;
if(!self){return;}
self->_serverConnectData.state =isNormal? CRMNetDetectCellStateNormal :CRMNetDetectCellStateNotNormal;
[self.tableView reloadData];
[self.tableView setTableFooterView:[self _footView]];
});
} }
#pragma mark - Getters #pragma mark - Getters
-(UIButton *)checkButton -(UIButton *)checkButton
...@@ -135,19 +151,51 @@ ...@@ -135,19 +151,51 @@
_checkButton.backgroundColor = [UIColor colorWithRed:238/255.0 green:131/255.0 blue:68/255.0 alpha:1]; _checkButton.backgroundColor = [UIColor colorWithRed:238/255.0 green:131/255.0 blue:68/255.0 alpha:1];
[_checkButton setTitle:@"重新检测" forState:UIControlStateNormal]; [_checkButton setTitle:@"重新检测" forState:UIControlStateNormal];
[_checkButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal]; [_checkButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
[_checkButton.titleLabel setFont:[UIFont systemFontOfSize:14]]; [_checkButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
[_checkButton addTarget:self action:@selector(startsChecking) forControlEvents:UIControlEventTouchUpInside]; [_checkButton addTarget:self action:@selector(startsChecking) forControlEvents:UIControlEventTouchUpInside];
}return _checkButton; }return _checkButton;
} }
-(UIActivityIndicatorView *)checkingIndicateView
{
if(!_checkingIndicateView){
if (@available(iOS 13.0, *)) {
_checkingIndicateView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
} else {
_checkingIndicateView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
}
_checkingIndicateView.translatesAutoresizingMaskIntoConstraints = false;
_checkingIndicateView.color = UIColor.whiteColor;
}return _checkingIndicateView;
}
- (UIView*)_footView - (UIView*)_footView
{ {
CGFloat marginTop = 50; CGFloat marginTop = 50;
CGFloat marginBottom = 20; CGFloat marginBottom = 20;
CGFloat btnHeight = 40; CGFloat btnHeight = 45;
UIButton * btn = self.checkButton; UIButton * btn = self.checkButton;
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, marginTop+marginBottom+btnHeight)]; UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, marginTop+marginBottom+btnHeight)];
[view addSubview:btn]; [view addSubview:btn];
btn.frame = CGRectMake(20, marginTop, view.bounds.size.width-40, btnHeight); btn.frame = CGRectMake(20, marginTop, view.bounds.size.width-40, btnHeight);
if(_service.isDetecting){
[btn setTitle:@"正在检测" forState:UIControlStateNormal];
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -35);
btn.enabled = false;
btn.backgroundColor = [UIColor colorWithRed:248/255.0 green:218/255.0 blue:195/255.0 alpha:1];
[self.checkingIndicateView setHidden:false];
[view addSubview:_checkingIndicateView];
[[_checkingIndicateView.centerYAnchor constraintEqualToAnchor:btn.centerYAnchor] setActive:true];
[[_checkingIndicateView.centerXAnchor constraintEqualToAnchor:btn.centerXAnchor constant:-35] setActive:true];
[[_checkingIndicateView.widthAnchor constraintEqualToConstant:20] setActive:true];
[[_checkingIndicateView.heightAnchor constraintEqualToConstant:20] setActive:true];
[_checkingIndicateView startAnimating];
}else{
btn.alpha = 1;
[btn setTitle:@"重新检测" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor colorWithRed:238/255.0 green:131/255.0 blue:68/255.0 alpha:1];
btn.enabled = true;
btn.titleEdgeInsets = UIEdgeInsetsZero;
}
return view; return view;
} }
- (UIView*)_headView - (UIView*)_headView
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
@"host":@"my-uat1.orangebank.com.cn" @"host":@"my-uat1.orangebank.com.cn"
} }
]]; ]];
[_detectView startsChecking];
// Do any additional setup after loading the view, typically from a nib. // Do any additional setup after loading the view, typically from a nib.
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment