From d15e9301a4ef549bec15ee7b0063a02a6bce9dbc Mon Sep 17 00:00:00 2001 From: xuemh Date: Mon, 6 Aug 2018 23:40:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=BE=E5=A4=87=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E8=BF=87=E7=A8=8B;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mineControllers/RestartViewController.m | 2 +- .../SettingResetViewController.m | 2 +- .../ConnectHotpotViewController.h | 3 + .../ConnectHotpotViewController.m | 167 +++++++++++++++++- .../leftcontrollers/ConnectHotspotModel.m | 2 +- .../leftcontrollers/IFishHotpotUDPHelper.h | 3 +- .../leftcontrollers/IFishHotpotUDPHelper.m | 5 +- .../IFishUDPHelperBackMsgModel.h | 1 + .../IFishUDPHelperBackMsgModel.m | 1 + .../SecondConnectWifiController.m | 2 + 10 files changed, 179 insertions(+), 9 deletions(-) diff --git a/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.m b/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.m index 74fa16f..ce8d442 100644 --- a/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.m +++ b/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.m @@ -27,7 +27,7 @@ #pragma mark - IFishHotpotUDPHelperDelegate -- (void)udpHelperCommandExecutedSuccess { +- (void)udpHelperCommandExecutedSuccess:(IFishUDPHelperBackMsgModel *)backModel { [self.view makeToast:@"设备即将重启"]; } diff --git a/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m b/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m index 0533e53..73a0328 100644 --- a/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m +++ b/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m @@ -27,7 +27,7 @@ #pragma mark - IFishHotpotUDPHelperDelegate -- (void)udpHelperCommandExecutedSuccess { +- (void)udpHelperCommandExecutedSuccess:(IFishUDPHelperBackMsgModel *)backModel { [self.view makeToast:@"设备即将重置"]; } diff --git a/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.h b/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.h index 10b4367..904af7e 100644 --- a/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.h +++ b/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.h @@ -10,4 +10,7 @@ @interface ConnectHotpotViewController : BaseVIewContorller +@property (nonatomic, copy) NSString *ssid; +@property (nonatomic, copy) NSString *bssid; + @end diff --git a/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.m b/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.m index 7258781..f5c1e69 100644 --- a/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.m +++ b/Ifish/controllers/leftcontrollers/ConnectHotpotViewController.m @@ -11,6 +11,8 @@ #import "dataContorl.h" #import #import "IFishHotpotUDPHelper.h" +#import "ESPTouchResult.h" +#import "ESPTouchTask.h" @interface ConnectHotpotViewController () @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @@ -19,6 +21,8 @@ @property (weak, nonatomic) IBOutlet UITextField *pwdTf; @property (weak, nonatomic) IBOutlet UIView *containerView; +@property (nonatomic, strong) ESPTouchTask* epTask; + @end @implementation ConnectHotpotViewController @@ -128,12 +132,171 @@ #pragma mark - IFishHotpotUDPHelperDelegate -- (void)udpHelperCommandExecutedSuccess { - [self.view makeToast:@"连接设置成功"]; +- (void)udpHelperCommandExecutedSuccess:(IFishUDPHelperBackMsgModel *)backModel { + [self.view makeToast:[NSString stringWithFormat:@"连接设置成功,mac 地址:%@",backModel.senderMacAddress]]; + [self tapConfirmForResults]; } - (void)udpHelperMessage:(NSString *)msg { [self.view makeToast:msg]; } +#pragma mark - bind + + +- (void) tapConfirmForResults{ + + NSLog(@"ViewController do confirm action..."); + dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); + dispatch_async(queue, ^{ + NSLog(@"ViewController do the execute work..."); + // execute the task + NSArray *esptouchResultArray = [self executeForResults]; + // show the result to the user in UI Main Thread + dispatch_async(dispatch_get_main_queue(), ^{ + // [self._spinner stopAnimating]; + // [self enableConfirmBtn]; + + ESPTouchResult *firstResult = [esptouchResultArray objectAtIndex:0]; + // check whether the task is cancelled and no results received + if (!firstResult.isCancelled) + { + NSMutableString *mutableStr = [[NSMutableString alloc]init]; + NSUInteger count = 0; + // max results to be displayed, if it is more than maxDisplayCount, + // just show the count of redundant ones + const int maxDisplayCount = 5; + if ([firstResult isSuc]) + { + + NSString * _deviceBssid=firstResult.bssid; + + NSArray*arry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"]; + DeviceModel*model=[[DeviceModel alloc]init]; + if (arry.count<5) { + NSMutableArray*macdressArr=[[NSMutableArray alloc]init]; + for (model in arry) { + + [macdressArr addObject:model.macAddress]; + + } + BOOL exst=[macdressArr containsObject:_deviceBssid]; + if (exst) { + self.bakbutton.userInteractionEnabled=YES; + //[self.navigationController popViewControllerAnimated:YES]; + [self.view makeToast:@"该设备已存在"]; + + }else{ + + [self bindDeviceWithSsid:_deviceBssid]; + + } + }else{ + [self.view makeToast:@"您的设备数量已超过限制"]; + } + + + for (int i = 0; i < [esptouchResultArray count]; ++i) + { + ESPTouchResult *resultInArray = [esptouchResultArray objectAtIndex:i]; + [mutableStr appendString:[resultInArray description]]; + [mutableStr appendString:@"\n"]; + count++; + if (count >= maxDisplayCount) + { + break; + } + } + + if (count < [esptouchResultArray count]) + { + [mutableStr appendString:[NSString stringWithFormat:@"\nthere's %lu more result(s) without showing\n",(unsigned long)([esptouchResultArray count] - count)]]; + } + + } + + else + { + self.bakbutton.userInteractionEnabled=YES; + [self.view makeToast:@"未获取到设备mac"]; + } + } + + }); + }); + +} +- (NSArray *) executeForResults +{ + NSString *apSsid = self.ssid; + NSString *apPwd = self.pwdTf.text; + NSString *apBssid = self.bssid; + BOOL isSsidHidden = YES; + int taskCount=1; + self.epTask = + [[ESPTouchTask alloc]initWithApSsid:apSsid andApBssid:apBssid andApPwd:apPwd andIsSsidHiden:isSsidHidden]; + NSArray * esptouchResults = [self.epTask executeForResults:taskCount]; + NSLog(@"ViewController executeForResult() result is: %@",esptouchResults); + return esptouchResults; + +} + +-(void)bindDeviceWithSsid:(NSString*)bssid{ + NSLog(@"设备bssid_%@",bssid); + NSString *userId = [dataContorl dataControlGetUserIdInfo]; + [AFHttpTool bindDeviceWithUserId:userId macAddress:bssid success:^(id response) { + NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil]; + NSLog(@"data:%@",resultDic[@"data"]); + if ([resultDic[@"result"] isEqualToString:@"100"]) { + + // 保存设备信息 + NSDictionary*dataDic=resultDic[@"data"]; + DeviceModel*devicemodel=[[DeviceModel alloc] initWithDict:dataDic]; + + if (!devicemodel.type){ + + [self.view makeToast:@"用户绑定设备成功,但设备初次入网失败请重新登陆"]; + + }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 showTitle:@"" messsage:@"设备不在服务器"]; + + }else{ + //绑定成功 加 经验值 + [[IfishUserObsever sharedInstance] xinZengJingYanZhi:IFISHADDEXP_BIDDEVICE addType:IFISHADDEXPTYPE1]; + } + } + + }else if ([resultDic[@"result"] isEqualToString:@"101"]){ + + [self.view makeToast:@"绑定失败"]; + self.bakbutton.userInteractionEnabled=YES; + + }else if ([resultDic[@"result"] isEqualToString:@"301"]){ + + [self.view makeToast:@"请求验证失败,请重新登录"]; + self.bakbutton.userInteractionEnabled=YES; + }else if ([resultDic[@"result"] isEqualToString:@"302"]){ + // 请求被舍弃未执行 + self.bakbutton.userInteractionEnabled=YES; + + } + + + } failure:^(NSError *err) { + [self.view makeToast:@"网络异常"]; + self.bakbutton.userInteractionEnabled=YES; + + }]; + +} + @end diff --git a/Ifish/controllers/leftcontrollers/ConnectHotspotModel.m b/Ifish/controllers/leftcontrollers/ConnectHotspotModel.m index 3d68dc2..ccbb79c 100644 --- a/Ifish/controllers/leftcontrollers/ConnectHotspotModel.m +++ b/Ifish/controllers/leftcontrollers/ConnectHotspotModel.m @@ -35,10 +35,10 @@ } else { NSInteger left = 40 - origin.length; NSMutableString *str = [NSMutableString string]; + [str appendString:origin]; for (NSInteger index = 0; index < left; index++) { [str appendString:@"0"]; } - [str appendString:origin]; return [str copy]; } } diff --git a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h index e769b56..5de0669 100644 --- a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h +++ b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h @@ -8,12 +8,13 @@ #import #import "AsyncUdpSocket.h" +#import "IFishUDPHelperBackMsgModel.h" @protocol IFishHotpotUDPHelperDelegate @optional - (void)udpHelperMessage:(NSString *)msg; -- (void)udpHelperCommandExecutedSuccess; +- (void)udpHelperCommandExecutedSuccess:(IFishUDPHelperBackMsgModel *)backModel; @end diff --git a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m index 30fb838..2272c0d 100644 --- a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m +++ b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m @@ -12,7 +12,6 @@ #import "ConnectHotspotModel.h" #import "ResetDeviceModel.h" #import "RestartDeviceModel.h" -#import "IFishUDPHelperBackMsgModel.h" @interface IFishHotpotUDPHelper () { @@ -138,8 +137,8 @@ NSLog(@"port%hu",port); IFishUDPHelperBackMsgModel *model = [[IFishUDPHelperBackMsgModel alloc] initWithData:data]; if ([model.result isEqualToString:@"01"]) { - if ([self.delegate respondsToSelector:@selector(udpHelperCommandExecutedSuccess)]) { - [self.delegate udpHelperCommandExecutedSuccess]; + if ([self.delegate respondsToSelector:@selector(udpHelperCommandExecutedSuccess:)]) { + [self.delegate udpHelperCommandExecutedSuccess:model]; } } else { AppDelegate *app =(AppDelegate*)[UIApplication sharedApplication].delegate; diff --git a/Ifish/controllers/leftcontrollers/IFishUDPHelperBackMsgModel.h b/Ifish/controllers/leftcontrollers/IFishUDPHelperBackMsgModel.h index a38be29..3158168 100644 --- a/Ifish/controllers/leftcontrollers/IFishUDPHelperBackMsgModel.h +++ b/Ifish/controllers/leftcontrollers/IFishUDPHelperBackMsgModel.h @@ -10,6 +10,7 @@ @interface IFishUDPHelperBackMsgModel : NSObject +@property (nonatomic, copy) NSString *senderMacAddress; @property (nonatomic, copy) NSString *result; @property (nonatomic, copy) NSString *changshang; @property (nonatomic, copy) NSString *device; diff --git a/Ifish/controllers/leftcontrollers/IFishUDPHelperBackMsgModel.m b/Ifish/controllers/leftcontrollers/IFishUDPHelperBackMsgModel.m index 4c3b23d..3b1aabb 100644 --- a/Ifish/controllers/leftcontrollers/IFishUDPHelperBackMsgModel.m +++ b/Ifish/controllers/leftcontrollers/IFishUDPHelperBackMsgModel.m @@ -13,6 +13,7 @@ - (instancetype)initWithData:(NSData *)data { if (self = [super init]) { NSString*totalString=[dataContorl dataToHexString:data]; + self.senderMacAddress = [totalString substringWithRange:NSMakeRange(2, 2)]; self.result = [totalString substringWithRange:NSMakeRange(30, 2)]; self.changshang = [totalString substringWithRange:NSMakeRange(32, 2)]; self.device = [totalString substringWithRange:NSMakeRange(34, 2)]; diff --git a/Ifish/controllers/leftcontrollers/SecondConnectWifiController.m b/Ifish/controllers/leftcontrollers/SecondConnectWifiController.m index f802e51..dcb40eb 100644 --- a/Ifish/controllers/leftcontrollers/SecondConnectWifiController.m +++ b/Ifish/controllers/leftcontrollers/SecondConnectWifiController.m @@ -419,6 +419,8 @@ extern BOOL isfromCameraView; - (IBAction)lickHereBtnClicked:(id)sender { ConnectHotpotViewController *vc = [[ConnectHotpotViewController alloc] initWithNibName:@"ConnectHotpotViewController" bundle:nil]; + vc.ssid = self.ssid; + vc.bssid = self.bssid; [self.navigationController pushViewController:vc animated:YES]; }