// // AddDevicePage.m // airtree // // Created by WindShan on 2016/11/18. // Copyright © 2016年 Gloria. All rights reserved. // #import "AddDevicePage.h" #import "smartlinklib_7x.h" #import "HFSmartLink.h" #import "HFSmartLinkDeviceInfo.h" #import #import "GloriaLabel.h" #import "AddDeviceReqModel.h" #import "NetworkSingleton.h" #import "HotelSelPage.h" #import "BaseNaviController.h" @interface AddDevicePage () { GloriaLabel * wifiSSIDLabel; // wifi 名字 UITextField * wifiPwdTextField; // wifi密码 UIButton * connectBtn; NSString * wifiSSID; NSString * hotel_room; //UIProgressView *connectProgressView; // 连接进度 //UISwitch *oneDeviceSwitch; BOOL isconnecting; #ifdef USE_SmartLink HFSmartLink * smtlk; #endif } @property (nonatomic, strong) UITableView * tableView; @end @implementation AddDevicePage - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] init]; _tableView.delegate = self; _tableView.dataSource = self; //_tableView.separatorColor = [UIColor clearColor]; _tableView.backgroundColor = [UIColor clearColor]; //_tableView.userInteractionEnabled = NO; //[_tableView setSeparatorInset:UIEdgeInsetsZero]; //[_tableView setLayoutMargins:UIEdgeInsetsZero]; _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 208); [self.view addSubview:_tableView]; [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(UIEdgeInsetsMake(0 , 0, 0, 0)); }]; _tableView.tableFooterView = [UIView new]; } return _tableView; } #pragma mark - UITableViewDelegate&UITableViewDataSource -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section //{ // return 0; //} // -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 80; } - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section // custom view for footer. will be adjusted to default or { UIView * pView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)]; if(pView) { UIButton* changeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; changeBtn.frame = CGRectMake((SCREEN_WIDTH-350)/2, 20, 350, 40); [changeBtn setBackgroundImage:[UIImage imageNamed:@"ic_change_device_btn_bk" ] forState:UIControlStateNormal]; [changeBtn setTitle:@"开始连接" forState:UIControlStateNormal]; changeBtn.titleLabel.font = [UIFont systemFontOfSize: 18.0]; [changeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; changeBtn.layer.masksToBounds=YES; changeBtn.layer.cornerRadius=8.0f; [changeBtn addTarget:self action:@selector(connectAction) forControlEvents:UIControlEventTouchUpInside]; [pView addSubview:changeBtn]; } return pView; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 3; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString * identifier = @"addDeviceCell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } //cell.delegate = self; //cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.userInteractionEnabled = YES; switch (indexPath.row) { case 0: { cell.image = [UIImage imageNamed:@"ic_device_change"]; if(wifiSSIDLabel==nil) { wifiSSIDLabel = [[GloriaLabel alloc] initWithFrame:CGRectMake(48, 0, SCREEN_WIDTH-60, 50)]; wifiSSIDLabel.font = [UIFont systemFontOfSize:16.0]; wifiSSIDLabel.textAlignment = UITextAlignmentLeft; wifiSSIDLabel.text = wifiSSID != nil? wifiSSID:@"请先设置wifi"; [cell.contentView addSubview:wifiSSIDLabel]; } } break; case 1: { cell.image = [UIImage imageNamed:@"ic_device_change"]; if(wifiPwdTextField==nil) { wifiPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(38, 0, SCREEN_WIDTH-60, cell.frame.size.height)]; //wifiPwdTextField.secureTextEntry = YES; wifiPwdTextField.placeholder = @"请输入wifi密码"; wifiPwdTextField.font = [UIFont fontWithName:@"Arial" size:16]; wifiPwdTextField.delegate = self; wifiPwdTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // 设置一个空白blankView,15.0就是这块View的宽度width,也是光标的位置,根据你自己的需要设定大小 wifiPwdTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(wifiPwdTextField.frame.origin.x,wifiPwdTextField.frame.origin.y,10.0, wifiPwdTextField.frame.size.height)]; wifiPwdTextField.leftViewMode =UITextFieldViewModeAlways; [cell.contentView addSubview:wifiPwdTextField]; } wifiPwdTextField.text = [self getspwdByssid:wifiSSIDLabel.text]; } break; case 2: { cell.image = [UIImage imageNamed:@"ic_device_change"]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.textLabel.text = hotel_room != nil? hotel_room:@"选择酒店房间"; } break; default: break; } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; if(indexPath.row == 2) { // 跳转选择酒店 // 跳转选择酒店 HotelSelPage* page = [[HotelSelPage alloc] initIsFirstPage:NO]; page.bSelRoom = 1; // 跳转界面 //BaseNaviController *baseNav = [[BaseNaviController alloc] initWithRootViewController:page]; //[self presentViewController:baseNav animated:YES completion:nil]; [self.navigationController pushViewController:page animated:YES]; } } #pragma mark - 补全分隔线左侧缺失 - (void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparatorInset:UIEdgeInsetsZero]; } if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { [self.tableView setLayoutMargins:UIEdgeInsetsZero]; } } -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{ if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setSeparatorInset:)]){ [cell setSeparatorInset:UIEdgeInsetsZero]; } } - (void)viewDidLoad { [super viewDidLoad]; #ifdef USE_SmartLink smtlk = [HFSmartLink shareInstence]; smtlk.isConfigOneDevice = true; smtlk.waitTimers = 30; #endif self.title = @"添加设备"; [self setNavigationLeft:@"返回" sel:@selector(backAticon)]; isconnecting = false; [self.tableView reloadData]; // 显示ssid设备名字 [self showWifiSsid]; // Do any additional setup after loading the view. } #pragma mark custom action begin -(void)backAticon { //[self.navigationController popToViewController:self animated:YES]; [self dismissViewControllerAnimated:YES completion:nil]; } -(void)connectAction { NSString * ssidStr = wifiSSIDLabel.text; NSString * pswdStr = wifiPwdTextField.text; [self savePswd]; //connectProgressView.progress = 0.0; if(!isconnecting) { MBProgressHUD *HUD = nil; HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; HUD.labelText = @"正在配网中..."; // 隐藏时候从父控件中移除 HUD.removeFromSuperViewOnHide = YES; // YES代表需要蒙版效果 HUD.dimBackground = YES; isconnecting = true; #ifdef USE_SmartLink [smtlk startWithSSID:ssidStr Key:pswdStr withV3x:true processblock: ^(NSInteger pro) { //connectProgressView.progress = (float)(pro)/100.0; } successBlock:^(HFSmartLinkDeviceInfo *dev) { NSString *path = [[NSString alloc] initWithFormat:ADD_DEVICE]; AddDeviceReqModel *model = [[AddDeviceReqModel alloc] init]; model.mac = dev.mac; model.userID = _loginUser[@"_id"]; model.hotelID = _curHotel_Id._id; model.roomID = _sel_room._id; //params.put("userID", MainApp.userId); //params.put("mac", TextUtils.isEmpty(mac)?"":mac); //params.put("hotelID", TextUtils.isEmpty(hotelID)?"":hotelID); // params.put("roomID", TextUtils.isEmpty(roomID)?"":roomID); MPWeakSelf(self); [NetworkSingleton networkingPostMethod:model.toDic urlName:path success:^(id responseBody) { MPStrongSelf(self); BaseResModel * resModel = [Global toBaseModel:responseBody]; HUD.hidden = YES; if(resModel.code == 0) { if(![Global isEmptyObject:resModel.content]) { if([[resModel.content objectForKey:@"status"] integerValue] == 4) { [Global alertMessageEx:@"该设备已被其他人绑定过" title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self]; } else { [Global alertMessageEx:@"新设备绑定成功" title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self]; [self dismissViewControllerAnimated:YES completion:nil]; } } else { isconnecting = false; [self setButTitle:@"开始连接"]; [Global alertMessageEx:resModel.desc title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self]; } } else { isconnecting = false; [self setButTitle:@"开始连接"]; [Global alertMessageEx:resModel.desc title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self]; } } failure:^(NSString *error) { HUD.hidden = YES; isconnecting = false; [self setButTitle:@"开始连接"]; //LOG_INFO(@"添加失败:%@",error); [Global alertMessageEx:error title:@"添加失败" okTtitle:nil cancelTitle:@"OK" delegate:self]; }]; } failBlock:^(NSString *failmsg) { HUD.hidden = YES; //LOG_INFO(@"绑定失败:%@",failmsg); isconnecting = false; [self setButTitle:@"开始连接"]; [Global alertMessageEx:@"绑定时发生异常,请稍候再试" title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self]; } endBlock:^(NSDictionary *deviceDic) { HUD.hidden = YES; isconnecting = false; [self setButTitle:@"开始连接"]; }]; #endif [self setButTitle:@"正在连接..."]; } else { #ifdef USE_SmartLink [smtlk stopWithBlock:^(NSString *stopMsg, BOOL isOk) { if(isOk) { isconnecting = false; [self setButTitle:@"开始连接"]; [Global alertMessageEx:@"配网模式已被终止" title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self]; } else { [Global alertMessageEx:@"配网模式已被终止" title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self]; } }]; #endif } } //-(void)switchAction:(id)sender //{ // UISwitch *switchBtn = (UISwitch *)sender; // // if(switchBtn.on) // { //#ifdef USE_SmartLink // smtlk.isConfigOneDevice = true; //#endif // } // else // { //#ifdef USE_SmartLink // smtlk.isConfigOneDevice = false; //#endif // } // //} #pragma mark end #pragma mark custom function begin -(void)savePswd { [UserDefault setObject:wifiPwdTextField.text forKey:wifiSSIDLabel.text]; } -(NSString *)getspwdByssid:(NSString * )mssid { return [UserDefault objectForKey:mssid]; } - (void)showWifiSsid { BOOL wifiOK = FALSE; NSDictionary *ifs; NSString *ssid; if (!wifiOK) { ifs = [self fetchSSIDInfo]; ssid = [ifs objectForKey:@"SSID"]; if (ssid != nil) { wifiOK= TRUE; wifiSSID = ssid; } else { [Global alertMessageEx:[NSString stringWithFormat:@"请先连接Wi-Fi"] title:@"提示" okTtitle:nil cancelTitle:@"关闭" delegate:self]; } } } - (id)fetchSSIDInfo { NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces(); LOG_INFO(@"Supported interfaces: %@", ifs); id info = nil; for (NSString *ifnam in ifs) { info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam); LOG_INFO(@"%@ => %@", ifnam, info); if (info && [info count]) { break; } } return info; } - (void) setButTitle:(NSString *) title { NSAttributedString *attributedTitle = [connectBtn attributedTitleForState:UIControlStateNormal]; NSMutableAttributedString *butTitle = [[NSMutableAttributedString alloc] initWithAttributedString:attributedTitle]; [butTitle.mutableString setString:title]; [connectBtn setAttributedTitle:butTitle forState:UIControlStateNormal]; } #pragma mark end -(void) viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; if( _curHotel_Id != nil && _sel_room != nil ) { hotel_room = [NSString stringWithFormat:@"%@ %@号房",_curHotel_Id.name,_sel_room.name]; [self.tableView reloadData]; } } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:YES]; #ifdef USE_SmartLink [smtlk stopWithBlock:^(NSString *stopMsg, BOOL isOk) { isconnecting = false; [self setButTitle:@"开始连接"]; }]; #endif } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(BOOL)textFieldShouldReturn:(UITextField *)textField { if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self textFieldShouldReturn:wifiPwdTextField]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end