From ee03c2b4c870488867d5e392a02084c8adfa0851 Mon Sep 17 00:00:00 2001 From: xuemh Date: Mon, 27 Aug 2018 21:34:34 +0800 Subject: [PATCH] temp --- .../SettingResetViewController.m | 58 +++++++++------ .../ConnectHotpotViewController.m | 74 +++++++++++-------- .../leftcontrollers/IFishHotpotUDPHelper.h | 2 - .../leftcontrollers/IFishHotpotUDPHelper.m | 25 ++----- 4 files changed, 87 insertions(+), 72 deletions(-) diff --git a/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m b/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m index 5558acc..ca61e48 100644 --- a/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m +++ b/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m @@ -20,6 +20,10 @@ @property (nonatomic, strong) MBProgressHUD *HUD; @property (nonatomic, assign) BOOL isDeleteingDevice; +@property (nonatomic, strong) NSTimer *deleteTimer; + +@property (nonatomic, assign) NSInteger retryTimes; + @end @implementation SettingResetViewController @@ -37,29 +41,32 @@ } [IFishHotpotUDPHelper sharedInstance].delegate = self; [[IFishHotpotUDPHelper sharedInstance] broadCastRestCommand]; - - __weak typeof(self) weakSelf = self; - [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { - __strong typeof(weakSelf) strongSelf = weakSelf; - BOOL networkOk = NO; - if (status == AFNetworkReachabilityStatusReachableViaWiFi) { - if (![[strongSelf currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) { - networkOk = YES; - } - } else if (status == AFNetworkReachabilityStatusReachableViaWWAN) { +} + +- (void)deleteNow { + BOOL networkOk = NO; + AFNetworkReachabilityStatus status = [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus; + if (status == AFNetworkReachabilityStatusReachableViaWiFi) { + if (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) { networkOk = YES; } - if (!networkOk) { - return; - } - if (strongSelf.isDeleteingDevice) { - return; - } - strongSelf.isDeleteingDevice = YES; - [strongSelf deletDevice]; - }]; - [[AFNetworkReachabilityManager sharedManager] startMonitoring]; + } else if (status == AFNetworkReachabilityStatusReachableViaWWAN) { + networkOk = YES; + } + if (!networkOk) { + return; + } + if (self.isDeleteingDevice) { + return; + } + if (self.deleteTimer) { + [self.deleteTimer invalidate]; + self.deleteTimer = nil; + } + self.isDeleteingDevice = YES; + [self deletDevice]; } + - (IBAction)tipBtnClicked:(id)sender { ConnectHotspotTipViewController *vc = [[ConnectHotspotTipViewController alloc] initWithNibName:nil bundle:nil]; [self.navigationController pushViewController:vc animated:YES]; @@ -73,6 +80,7 @@ [deviceArry enumerateObjectsUsingBlock:^(DeviceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj.macAddress isEqualToString:backModel.senderMacAddress]) { self.currentDevice = obj; + self.deleteTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(deleteNow) userInfo:nil repeats:YES]; *stop = YES; } }]; @@ -112,6 +120,7 @@ [[DataCenter defaultDtacenter] setValue:guanxiiArr forKey:@"devicamerArr"]; } } + self.retryTimes = 0; [self deleteDeviceRequset]; } @@ -155,8 +164,13 @@ } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - self.HUD.labelText = @"请求异常"; - [self.HUD hide:YES afterDelay:2]; + if (self.retryTimes < 3) { + [self deleteDeviceRequset]; + self.retryTimes += 1; + } else { + self.HUD.labelText = @"请求异常"; + [self.HUD hide:YES afterDelay:2]; + } }]; } diff --git a/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.m b/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.m index abe68d6..e0cfefb 100644 --- a/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.m +++ b/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.m @@ -39,6 +39,10 @@ */ @property (nonatomic, assign) BOOL isBindingDevice; +@property (nonatomic, strong) NSTimer *bindTimer; + +@property (nonatomic, assign) NSInteger retryTimes; + @end @implementation ConnectHotpotViewController @@ -166,33 +170,37 @@ [self appendTrakContent:[NSString stringWithFormat:@"答复Mac地址:%@", backModel.senderMacAddress]]; self.macAddress = backModel.senderMacAddress; - __weak typeof(self) weakSelf = self; - [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { - __strong typeof(weakSelf) strongSelf = weakSelf; - BOOL networkOk = NO; - if (status == AFNetworkReachabilityStatusReachableViaWiFi) { - if (![[strongSelf currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) { - networkOk = YES; - } - } else if (status == AFNetworkReachabilityStatusReachableViaWWAN) { - networkOk = YES; - } - if (!networkOk) { - return; - } - if (strongSelf.isBindingDevice) { - return; - } - if (strongSelf.macAddress.length == 0) { - return; - } - strongSelf.isBindingDevice = YES; - [strongSelf bindDeviceWithSsid:self.macAddress]; - }]; - [[AFNetworkReachabilityManager sharedManager] startMonitoring]; - [IFishHotpotUDPHelper sharedInstance].delegate = nil; [[IFishHotpotUDPHelper sharedInstance] broadCastRestartCommand]; + self.bindTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startBinding) userInfo:nil repeats:YES]; +} + +- (void)startBinding { + BOOL networkOk = NO; + AFNetworkReachabilityStatus status = [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus; + if (status == AFNetworkReachabilityStatusReachableViaWiFi) { + if (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) { + networkOk = YES; + } + } else if (status == AFNetworkReachabilityStatusReachableViaWWAN) { + networkOk = YES; + } + if (!networkOk) { + return; + } + if (self.isBindingDevice) { + return; + } + if (self.macAddress.length == 0) { + return; + } + self.isBindingDevice = YES; + if (self.bindTimer) { + [self.bindTimer invalidate]; + self.bindTimer = nil; + } + self.retryTimes = 0; + [self bindDeviceWithSsid:self.macAddress]; } - (void)udpHelperMessage:(NSString *)msg { @@ -267,12 +275,16 @@ } failure:^(NSError *err) { - self.isBindingDevice = NO; - self.HUD.labelText = @"网络异常"; - [self.HUD hide:YES afterDelay:2]; - [self appendTrakContent:[NSString stringWithFormat:@"绑定网络请求错误:%@", err.localizedDescription]]; - self.bakbutton.userInteractionEnabled=YES; - + if (self.retryTimes < 3) { + [self bindDeviceWithSsid:self.macAddress]; + self.retryTimes += 1; + } else { + self.isBindingDevice = NO; + self.HUD.labelText = @"请求异常"; + [self.HUD hide:YES afterDelay:2]; + [self appendTrakContent:[NSString stringWithFormat:@"绑定网络请求错误:%@", err.localizedDescription]]; + self.bakbutton.userInteractionEnabled=YES; + } }]; } diff --git a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h index 5de0669..fdbdaeb 100644 --- a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h +++ b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h @@ -24,8 +24,6 @@ + (IFishHotpotUDPHelper *)sharedInstance; -@property (nonatomic,strong) NSTimer *loopTimer; - @property (nonatomic, weak) id delegate; - (void)broadCastHotspotConnectCommandWith:(NSString *)ssidName pwd:(NSString *)pwd; diff --git a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m index 80e9991..09b14a3 100644 --- a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m +++ b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m @@ -21,6 +21,7 @@ NSString *_pwd; } + @end @@ -43,7 +44,6 @@ UInt16 bindPort = 9954; NSError *error; - [self.clientSocket enableBroadcast:YES error:&error]; //本地接收端口 [self.clientSocket bindToPort:bindPort error:&error]; [self.clientSocket receiveWithTimeout:-1 tag:0]; @@ -60,10 +60,10 @@ - (void)broadCastRestCommand { [self postmsg:@"命令已发出"]; - [self sendRestSocketData]; + [self sendResetSocketData]; } -- (void)sendRestSocketData { +- (void)sendResetSocketData { NSString* hostS = @"192.168.4.1"; ResetDeviceModel *model = [ResetDeviceModel new]; NSString *request=[NSString stringWithFormat:@"%@", model.description]; @@ -72,12 +72,12 @@ UInt16 port = 333; - [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1]; + [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:0]; } - (void)broadCastRestartCommand { [self postmsg:@"命令已发出"]; - [self sendRestSocketData]; + [self sendRestartSocketData]; } - (void)sendRestartSocketData { @@ -90,7 +90,7 @@ UInt16 port = 333; - [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1]; + [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:0]; } -(void)sendConnectHotspotSoketdata{ @@ -105,14 +105,11 @@ NSData *data=[dataContorl stringToHexData:request]; UInt16 port = 333; - [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1]; + [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:0]; } - -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port { - [self.loopTimer invalidate]; - NSString* result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; IFishUDPHelperBackMsgModel *model = [[IFishUDPHelperBackMsgModel alloc] initWithData:data]; if ([model.result isEqualToString:@"01"]) { @@ -128,9 +125,7 @@ } -(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{ - - [self.loopTimer invalidate]; - NSLog(@"关闭"); + NSLog(@"关闭"); } @@ -144,16 +139,12 @@ NSLog(@"dueToError%@",error); [self postmsg:error.localizedDescription]; - - [self.loopTimer invalidate]; } -(void)dealloc{ [self.clientSocket close]; - [self.loopTimer invalidate]; - } - (void)postmsg:(NSString *)msg {