From 6131f5ad5ec16624120df427cf6e14ad5d13514a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9D=E5=8F=91=E5=86=AC?= Date: Sun, 20 Aug 2023 18:18:23 +0800 Subject: [PATCH] =?UTF-8?q?wifi=E7=BB=91=E5=AE=9A=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfigWifiViewController.m | 283 +++++++++++++----- 1 file changed, 205 insertions(+), 78 deletions(-) diff --git a/Ifish/controllers/leftcontrollers/ConfigWifiViewController.m b/Ifish/controllers/leftcontrollers/ConfigWifiViewController.m index df41012..264f9f4 100644 --- a/Ifish/controllers/leftcontrollers/ConfigWifiViewController.m +++ b/Ifish/controllers/leftcontrollers/ConfigWifiViewController.m @@ -32,16 +32,24 @@ #import "ConnectHotpotViewController.h" #import "ConAquarNowWiFiPwdVC.h" #import "InfoByImageViewController.h" +#import "YooseeNextConnectViewController.h" - - +#import "HKPieChartView.h" +#import "dataContorl.h" +#import +#import "IFishHotpotUDPHelper.h" +#import "ESPTouchResult.h" +#import "ESPTouchTask.h" +#import "IfishDataUnity.h" +#import "MyMD5.h" +#import typedef NS_ENUM(NSInteger,lodingViewdissMissStyle) { lodingViewdissMissSuccsess=0,// default is 0 lodingViewdissMissfail, lodingViewdissMissAlreadyBinded }; -@interface ConfigWifiViewController () +@interface ConfigWifiViewController () @property (nonatomic, strong) NSCondition *_condition; @property (atomic, strong) ESPTouchTask *_esptouchTask; @property (nonatomic, strong) EspTouchDelegateImpl *_esptouchDelegate; @@ -56,11 +64,27 @@ typedef NS_ENUM(NSInteger,lodingViewdissMissStyle) { @property(nonatomic,strong) UILabel *maclabel; -@property(nonatomic,strong)IfishConnectingView *connectingView; +@property (nonatomic, assign) BOOL isGoChangeWiFi; @property (nonatomic, strong) JMAirKissConnection *airKissConnection; ; Strong CLLocationManager *locationManager;//iOS13,获取WiFi名称必须开启定位权限 +Strong NSMutableString *trackString; +Strong MBProgressHUD *HUD; +Strong NSTimer *timer; +Strong UITapGestureRecognizer*tap; +/** + 设备返回的mac地址 + */ +Copy NSString *macAddress; +/** + 是否正在绑定设备 + */ +Assign BOOL isBindingDevice; +Strong NSTimer *bindTimer; +Assign NSInteger retryTimes; + + @end //extern BOOL formLogIn;//连接页面是否来自登录界面 extern BOOL isfromCameraView; @@ -189,6 +213,21 @@ extern BOOL isfromCameraView; [self.navigationController pushViewController:info animated:YES]; } +//ap模式 +-(void)goSettingClick:(UIButton*)button +{ + + NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; + + if([[UIApplication sharedApplication] canOpenURL:url]) { + if (@available(iOS 10.0, *)) { + [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; + }else { + [[UIApplication sharedApplication] openURL:url]; + } + self.isGoChangeWiFi = YES; + } +} -(void)creatConnectStateUI{ @@ -199,75 +238,17 @@ extern BOOL isfromCameraView; -(void)connectNormalView { - - if (!self.connectingView) { - CGFloat yOffset = 0; - if (is_iPhone_X) { - yOffset = 88; - } else { - yOffset = 64; - } - - IfishConnectingView *connectingView=[[IfishConnectingView alloc] initWithFrame:self.connectTipLbl.frame]; - - self.connectingView =connectingView; - - [self.connectingView.textChangeView animationWithTexts:[NSArray arrayWithObjects:@"正在拼命连接中...",@"请将水族箱放在wifi信号良好的位置",@"连接成功后过几秒才能看到温度哟",@"连接不上,请点击下方说明书",nil]]; - //self.connectingView.backgroundColor = [UIColor redColor]; - self.connectingView.alpha = 0; - [self.view addSubview:connectingView]; - self.wifiNamelabel.hidden = NO; - self.wifiTextFiled.hidden = NO; - self.connectTipLbl.hidden = NO; - self.connectBtn.hidden = NO; - self.connectTypeBtn.hidden = NO; - - - - }else{ - - [UIView animateWithDuration:2.0 animations:^{ - - self.connectingView.alpha = 0; - //self.connectingView.hidden = YES; - - } completion:^(BOOL finished) { - - [self.connectingView.textChangeView stopAnimation]; - self.wifiNamelabel.hidden = NO; - self.wifiTextFiled.hidden = NO; - self.connectTipLbl.hidden = NO; - self.connectBtn.hidden = NO; - self.connectTypeBtn.hidden = NO; - - - }]; - - } + self.connectingView.hidden=YES; + self.connectBtn.hidden = NO; + [self.indicator stopAnimating]; } --(void)connectNotNormalView{ - - - [UIView animateWithDuration:2.0 animations:^{ - - self.connectingView.alpha = 1; - //self.connectingView.hidden = NO; - } completion:^(BOOL finished) { - - [self.connectingView.textChangeView startTextAnimation]; - - - }]; - -// self.wifiNamelabel.hidden = YES; -// self.wifiTextFiled.hidden = YES; - self.connectTipLbl.hidden = YES; +-(void)connectNotNormalView +{ + self.connectingView.hidden=NO; + [self.indicator startAnimating]; self.connectBtn.hidden = YES; - self.connectTypeBtn.hidden = YES; - - } -(void)initMacIdString:(NSString *)ssid{ @@ -692,6 +673,127 @@ extern BOOL isfromCameraView; } +#pragma mark ap开始 +-(void)viewWillEnterForeground:(NSNotification *)noti{ + if (self.isGoChangeWiFi) { + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self checkWiFi]; + + }); + } +} +-(void)checkWiFi{ + [CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) { + + NSString *wifiName = SSID; + //先判断之前的wifi名称是否和热点的wifi名称重复,如果冲突,则提示wifi名称错误 + if ([wifiName isEqualToString:self.wifiNamelabel.text]) { + [self.view makeToast:@"请先连接设备热点"]; + return; + } + [self.timer invalidate]; + self.timer = nil; + [self sendToDeviceWithSSIDName:self.ssid andSSIDPWD:self.wifiPass]; + // // Do any additional setup after loading the view. + self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; + [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; + + }]; + +} +- (void)sendToDeviceWithSSIDName:(NSString *)name andSSIDPWD:(NSString *)pwd { + [IFishHotpotUDPHelper sharedInstance].delegate = self; + [[IFishHotpotUDPHelper sharedInstance] broadCastHotspotConnectCommandWith:name pwd:pwd]; +} +#pragma mark - IFishHotpotUDPHelperDelegate +//这里是连接到设备的路由器之后的回调方法 +- (void)udpHelperCommandExecutedSuccess:(IFishUDPHelperBackMsgModel *)backModel { + self.ssid = backModel.senderMacAddress; + [self.view resignFirstResponder]; + self.HUD.labelText = @"请稍后"; + self.trackString = [NSMutableString string]; + [self appendTrakContent:@"=================收到设备答复==================="]; + [self appendTrakContent:[NSString stringWithFormat:@"答复状态码:%@", backModel.result]]; + [self appendTrakContent:[NSString stringWithFormat:@"答复Mac地址:%@", backModel.senderMacAddress]]; + self.macAddress = backModel.senderMacAddress; + + [IFishHotpotUDPHelper sharedInstance].delegate = nil; + [[IFishHotpotUDPHelper sharedInstance] broadCastRestartCommand]; + //这里需要连接到之前的路由器,才能绑定设备到 + self.bindTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startBinding) userInfo:nil repeats:YES]; +} + +- (void)startBinding { + + [CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) { + if ([netconnType isEqualToString:@"no network"]||([netconnType isEqualToString:@"Wifi"]&&[SSID.lowercaseString hasPrefix:@"ifish"])) + { + + //无网络 或者连接的还是设备的热点 + } + else + { + if (self.isBindingDevice||self.storeNameView) { + return; + } + if (self.macAddress.length == 0) { + return; + } + + self.retryTimes = 0; + if (self.deviceType==DEVICEPETS) + { + [self showStoreNameView]; + } + else + { + [self bindDeviceWithSsid:self.macAddress]; + } + + } + }]; + +} + + + + + +- (void)udpHelperMessage:(NSString *)msg { + if (!self.timer) + { + return; + } + [MBProgressHUD hideHUDForView:self.view animated:YES]; + self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; + self.HUD.mode = MBProgressHUDModeIndeterminate; + self.HUD.labelText = msg; + if ([msg containsString:@"Socket连接失败"]) + { + [self.HUD hide:YES afterDelay:3]; + + if (self.retryTimes<10) + { + self.retryTimes++; + + WEAK_SELF; + [[IFishHotpotUDPHelper sharedInstance] closeComplete:^{ + [weakSelf sendToDeviceWithSSIDName:weakSelf.ssid andSSIDPWD:weakSelf.wifiPass]; + }]; + } + else + { + ; + } + + } +} + +- (void)appendTrakContent:(NSString *)msg +{ + +} +#pragma mark Ap结束 - (NSArray *) executeForResults { [self._condition lock]; @@ -768,12 +870,21 @@ extern BOOL isfromCameraView; #pragma mark 绑定设备 -(void)bindDeviceWithSsid:(NSString*)bssid{ NSLog(@"设备bssid_%@",bssid); + NSLog(@"设备bssid_%@",bssid); + if (self.isBindingDevice) { + return; + } + else + { + self.isBindingDevice = YES; + if (self.bindTimer) { + [self.bindTimer invalidate]; + self.bindTimer = nil; + } + } + self.HUD.labelText = @"正在绑定设备"; NSString *userId = [dataContorl dataControlGetUserIdInfo]; - [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"" params:@{} success:^(id response) { - - } failure:^(NSError *err) { - - }]; + NSString*url=[NSString stringWithFormat:@"%@%@",kBindDevice,userId]; NSMutableDictionary*bind=[[NSMutableDictionary alloc]initWithDictionary:@{@"macAddress":bssid}]; NSString* timestamp=[NSString stringWithFormat:@"%lld",[[NSDate date] timeIntervalSince1970]*1000]; @@ -790,6 +901,8 @@ extern BOOL isfromCameraView; [AFHttpTool requestWihtMethod:RequestMethodTypePost url:url params:bind success:^(id response) { NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil]; NSLog(@"data:%@",resultDic[@"data"]); + self.isBindingDevice = NO; + self.macAddress = nil; if ([resultDic[@"result"] isEqualToString:@"100"]) { // 保存设备信息 @@ -888,10 +1001,24 @@ extern BOOL isfromCameraView; } failure:^(NSError *err) { - //[self showTitle:@"" messsage:@"网络异常"]; - [self.view makeToast:@"网络异常"]; - self.bakbutton.userInteractionEnabled=YES; - + self.isBindingDevice=NO; + if (self.retryTimes < 10) { + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + self.HUD.labelText = @"自动重连..."; + [self bindDeviceWithSsid:self.macAddress]; + + self.retryTimes += 1; + + }); + } else { + + self.tap.enabled=YES; + self.isBindingDevice = NO; + self.HUD.labelText = err.localizedDescription; + [self.HUD hide:YES afterDelay:2]; + [self appendTrakContent:[NSString stringWithFormat:@"绑定网络请求错误:%@", err.localizedDescription]]; + self.bakbutton.userInteractionEnabled=YES; + } }]; }