diff --git a/Ifish/controllers/BaseVIewContorller/BaseViewController.h b/Ifish/controllers/BaseVIewContorller/BaseViewController.h index d36eaef..18f46ce 100644 --- a/Ifish/controllers/BaseVIewContorller/BaseViewController.h +++ b/Ifish/controllers/BaseVIewContorller/BaseViewController.h @@ -7,7 +7,6 @@ // #import - @interface BaseViewController : UIViewController @property(nonatomic,strong)UIBarButtonItem*back; diff --git a/Ifish/controllers/ConnectAauariumVC/ConnectingAquarVC.m b/Ifish/controllers/ConnectAauariumVC/ConnectingAquarVC.m index 16430b6..3b11dda 100644 --- a/Ifish/controllers/ConnectAauariumVC/ConnectingAquarVC.m +++ b/Ifish/controllers/ConnectAauariumVC/ConnectingAquarVC.m @@ -14,7 +14,7 @@ #import "ESPTouchResult.h" #import "ESPTouchTask.h" #import "IfishDataUnity.h" -//#import +#import @interface ConnectingAquarVC () Strong HKPieChartView *progressView; Strong UILabel *titleL; @@ -32,6 +32,7 @@ Copy NSString *macAddress; Assign BOOL isBindingDevice; Strong NSTimer *bindTimer; Assign NSInteger retryTimes; +Copy NSString *deviceId; @end @implementation ConnectingAquarVC @@ -52,22 +53,105 @@ Assign NSInteger retryTimes; }]; [self.progressView updatePercent:100 animation:YES]; [self.progressView endProgressWithString:@"设备连接中..."]; + self.HUD.labelText = @"正在绑定设备"; //AP模式配网 -// [[GWP2PDeviceLinker shareInstance] p2pAPLinkDeviceWithWiFiSSID:self.wifiName wifiPassword:self.wifiPassword devicePassword:@"123" deviceReceive:^(NSString *deviceId, BOOL isSupport) { -// // -// NSLog(@"设备%@收到WiFi,isSupport:%d",deviceId,isSupport); -// } deviceLinkIn:^(NSDictionary *deviceDict) { -// //成功 -// }]; + [[GWP2PDeviceLinker shareInstance] p2pAPLinkDeviceWithWiFiSSID:self.wifiName wifiPassword:self.wifiPassword devicePassword:@"123" deviceReceive:^(NSString *deviceId, BOOL isSupport) { + //已经连接到设备,此处回调之后,会断开热点,重新连接到路由器wifi + NSLog(@"设备%@收到WiFi,isSupport:%d",deviceId,isSupport); + self.deviceId = deviceId; + } deviceLinkIn:^(NSDictionary *deviceDict) { + //AP模式配网成功//连接到路由器wifi成功之后,会走此回调方法 + [self bindDeviceWithDeviceId:self.deviceId]; + }]; //传入设备要连接的wifi名称和密码 - [self sendToDeviceWithSSIDName:self.wifiName andSSIDPWD:self.wifiPassword]; - // Do any additional setup after loading the view. - +// [self sendToDeviceWithSSIDName:self.wifiName andSSIDPWD:self.wifiPassword]; +// // 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)bindDeviceWithDeviceId:(NSString*)deviceId{ + NSLog(@"设备bssid_%@",deviceId); + [self appendTrakContent:@"=================进入绑定函数================="]; + NSString *userId = [dataContorl dataControlGetUserIdInfo]; + self.HUD.labelText = @"正在绑定设备"; + [self appendTrakContent:[NSString stringWithFormat:@"绑定参数 userID:%@", userId]]; + [self appendTrakContent:[NSString stringWithFormat:@"绑定参数 macaddress:%@", deviceId]]; + [AFHttpTool bindDeviceWithUserId:userId macAddress:deviceId success:^(id response) { + [self endProgress]; + self.isBindingDevice = NO; + self.macAddress = nil; + + NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil]; + NSLog(@"data:%@",resultDic[@"data"]); + [self appendTrakContent:[NSString stringWithFormat:@"绑定请求成功。返回数据为:%@", resultDic]]; + if ([resultDic[@"result"] isEqualToString:@"100"]) { + // 保存设备信息 + NSDictionary*dataDic=resultDic[@"data"]; + DeviceModel*devicemodel=[[DeviceModel alloc] initWithDict:dataDic]; + [self appendTrakContent:[NSString stringWithFormat:@"绑定返回状态码正确。设备信息为:%@", dataDic]]; + if (!devicemodel.type){ + self.HUD.labelText = @"用户绑定设备成功,但设备初次入网失败请重新登陆"; + + }else{ + NSMutableArray * _deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"]; + + [_deviceArry insertObject:devicemodel atIndex:0]; + + [[DataCenter defaultDtacenter]setValue:_deviceArry forKey:@"deviceInfo"]; + + if ([devicemodel.type isEqual:[NSNull null]]){ + + //进入此循环 可能因为设备所在服务器 不在 本程序所在 服务器 + self.HUD.labelText = @"设备不在服务器"; + + }else{ + + self.HUD.labelText = @"设备绑定成功"; + [[IfishDataUnity shareDataInstance] initAppCenterVcWith:devicemodel addWithdissMisVc:nil]; + + //绑定成功 加 经验值 + [[IfishUserObsever sharedInstance] xinZengJingYanZhi:IFISHADDEXP_BIDDEVICE addType:IFISHADDEXPTYPE1]; + } + } + + }else if ([resultDic[@"result"] isEqualToString:@"101"]){ + [self appendTrakContent:@"绑定返回状态码错误。设备信息为:%@"]; + self.HUD.labelText = @"绑定失败"; + self.bakbutton.userInteractionEnabled=YES; + + }else if ([resultDic[@"result"] isEqualToString:@"301"]){ + [self appendTrakContent:@"绑定返回状态码错误。设备信息为:%@"]; + + self.HUD.labelText = @"请求验证失败,请重新登录"; + self.bakbutton.userInteractionEnabled=YES; + }else if ([resultDic[@"result"] isEqualToString:@"302"]){ + // 请求被舍弃未执行 + [self appendTrakContent:@"绑定返回状态码错误。设备信息为:%@"]; + + self.bakbutton.userInteractionEnabled=YES; + self.HUD.labelText = @"绑定失败"; + [self.HUD hide:YES afterDelay:2]; + } + + + } failure:^(NSError *err) { + if (self.retryTimes < 3) { + [self bindDeviceWithSsid:self.macAddress]; + self.retryTimes += 1; + } else { + [self endProgress]; + self.isBindingDevice = NO; + self.HUD.labelText = @"请求异常"; + [self.HUD hide:YES afterDelay:2]; + [self appendTrakContent:[NSString stringWithFormat:@"绑定网络请求错误:%@", err.localizedDescription]]; + self.bakbutton.userInteractionEnabled=YES; + } + }]; + +} - (void)dealloc { if (!self.timer) { @@ -182,7 +266,7 @@ Assign NSInteger retryTimes; [self appendTrakContent:[NSString stringWithFormat:@"绑定参数 userID:%@", userId]]; [self appendTrakContent:[NSString stringWithFormat:@"绑定参数 macaddress:%@", bssid]]; [AFHttpTool bindDeviceWithUserId:userId macAddress:bssid success:^(id response) { - [self.progressView endEditing:YES]; + [self endProgress]; self.isBindingDevice = NO; self.macAddress = nil;