// // YooseConnectFirstViewController.m // Ifish // // Created by imac on 16/6/27. // Copyright © 2016年 lianxiang. All rights reserved. // #import "YooseConnectFirstViewController.h" #import "AFNetworking.h" #import "YooseeNextConnectViewController.h" #import "UDPManager.h" #import "UDManager.h" #import "LoginResult.h" //#import "P2PClient.h" #import @interface YooseConnectFirstViewController () @property(nonatomic)BOOL isInitDevice; @property(nonatomic,strong) UIButton *nextBtn; @end @implementation YooseConnectFirstViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self initUI]; self.view.backgroundColor = COLOR_MIAN; [self addTitleViewWithTitle:@"连接设备"]; } -(void)viewDidAppear:(BOOL)animated{ [self connectDevice]; if( _isInitDevice){ NSLog(@"p2pConnect success.成功,你可以操作设备了"); }else{//new added [self connectDevice]; NSLog(@"p2pConnect failure.失败,你将不能操作设备"); return; } } -(void)viewWillAppear:(BOOL)animated{ [self connectDevice]; } -(void)connectDevice{ [[UDPManager sharedDefault] ScanLanDevice]; _isInitDevice = NO; // LoginResult *loginResult = [UDManager getLoginInfo]; UserModel *model=[[DataCenter defaultDtacenter] valueForKey:@"UserLogIn"]; NSString *userIDName=[NSString stringWithFormat:@"%d",(int)[model.gwellUserID integerValue]&0x7fffffff]; NSLog(@"%@%@%@",userIDName,model.P2PVerifyCode1,model.P2PVerifyCode2); if ([userIDName isEqualToString:@"0"]) { // [self.view makeToast:@"摄像头用户名空错误码10001"]; return; } if ([model.P2PVerifyCode1 isEqualToString:@""]) { [self.view makeToast:@"摄像头P2PVerifyCode1空错误码10002"]; return; } if ([model.P2PVerifyCode2 isEqualToString:@""]) { [self.view makeToast:@"摄像头P2PVerifyCode2空错误码10003"]; return; } _isInitDevice = [[GWP2PClient sharedClient] connectWithAccount:userIDName codeStr1:model.P2PVerifyCode1 codeStr2:model.P2PVerifyCode2 sessionID1:model.sessionID sessionID2:model.sessionID2 customerIDs:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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. } */ -(void)initUI{ //屏幕宽高比 CGFloat spaceH=50; //CGFloat percent = 0.82 ;//263/320; CGFloat imgWid =kScreenSize.width - 200; CGFloat imgH = imgWid/0.82 +0.5 ;//244//200/0.82 UIImageView *deivceimg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ifish_yoosee_device"]]; deivceimg.frame = CGRectMake(kScreenSize.width/2-imgWid/2, spaceH + 64, imgWid, imgH); [self.view addSubview:deivceimg]; UILabel *detailabel=[[UILabel alloc] init]; detailabel.text = @"接通电源 ,等待摄像头发出连接提示音。\n若未听到,触碰背后的[Reset]进行复位。"; detailabel.textColor = [UIColor whiteColor]; detailabel.numberOfLines = 0; NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:detailabel.text]; NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:8]; [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [detailabel.text length])]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0 green:170.0/255.0 blue:218.0/255.0 alpha:1] range:NSMakeRange(30,7)]; [detailabel setAttributedText:attributedString]; detailabel.textAlignment = NSTextAlignmentCenter; [detailabel sizeToFit]; CGFloat labelSizeW = [self widthForString:detailabel.text fontSize:18 andHeight:10]; CGFloat labelH= [self heightForString:detailabel.text fontSize:18 andWidth:labelSizeW]; detailabel.frame = CGRectMake(kScreenSize.width/2 - labelSizeW/2 , CGRectGetMaxY(deivceimg.frame) + spaceH, labelSizeW , labelH*2); [self.view addSubview:detailabel]; //下一步 _nextBtn=[UIButton buttonWithType:UIButtonTypeCustom]; [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal]; CGFloat btnTitleW =[self widthForString:_nextBtn.titleLabel.text fontSize:20 andHeight:10]; CGFloat btnTitleH = [self heightForString:_nextBtn.titleLabel.text fontSize:20 andWidth:btnTitleW]; CGFloat spaceHY= kScreenSize.height - CGRectGetMaxY(detailabel.frame) - 20 - btnTitleH *2 ; _nextBtn.frame = CGRectMake(10, CGRectGetMaxY(detailabel.frame) + spaceHY, kScreenSize.width - 2*10, btnTitleH *2); [_nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_nextBtn addTarget:self action:@selector(nextBtn:) forControlEvents:UIControlEventTouchUpInside]; _nextBtn.backgroundColor = COLOR_LABEL_TITLE; _nextBtn.layer.masksToBounds = YES; _nextBtn.layer.cornerRadius = 5; [self.view addSubview:_nextBtn]; } -(void)nextBtn:(UIButton*)btn{ if ( self.isInitDevice) { [self testWifi]; }else{ UserModel *model=[[DataCenter defaultDtacenter] valueForKey:@"UserLogIn"]; NSString *userIDName=[NSString stringWithFormat:@"%d",(int)[model.gwellUserID integerValue]&0x7fffffff]; if ([userIDName isEqualToString:@"0"]) { //空错误吗 装逼专用没啥用 [self.view makeToast:@"摄像头用户名空错误码10001,请联系爱鱼奇"]; return; } if ([model.P2PVerifyCode1 isEqualToString:@""]) { [self.view makeToast:@"摄像头P2PVerifyCode1空错误码10002,请联系爱鱼奇"]; return; } if ([model.P2PVerifyCode2 isEqualToString:@""]) { [self.view makeToast:@"摄像头P2PVerifyCode2空错误码10003,请联系爱鱼奇"]; return; } [self showTitle:@"" messsage:@" 暂未检测到设备"]; // self.isInitDevice = [[P2PClient sharedClient] p2pConnectWithId:userIDName codeStr1:model.P2PVerifyCode1 codeStr2:model.P2PVerifyCode2]; self.isInitDevice = [[GWP2PClient sharedClient] connectWithAccount:userIDName codeStr1:model.P2PVerifyCode1 codeStr2:model.P2PVerifyCode2 sessionID1:model.sessionID sessionID2:model.sessionID2 customerIDs:nil]; //loginResult.contactId 用户ID号 NSLog(@"%@%@%@",userIDName,model.P2PVerifyCode1,model.P2PVerifyCode2); } } #pragma -mark -functions //获取字符串的宽度 -(float) widthForString:(NSString *)value fontSize:(float)fontSize andHeight:(float)height { CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(CGFLOAT_MAX, height) lineBreakMode:NSLineBreakByWordWrapping];//此处的换行类型(lineBreakMode)可根据自己的实际情况进行设置 return sizeToFit.width; } //获得字符串的高度 -(float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width { CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByCharWrapping];//此处的换行类型(lineBreakMode)可根据自己的实际情况进行设置 return sizeToFit.height; } -(void)testWifi{ [[AFNetworkReachabilityManager sharedManager] startMonitoring]; [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSLog(@"status%ld", (long)status); if (status==AFNetworkReachabilityStatusReachableViaWiFi) { YooseeNextConnectViewController *nextvc=[[YooseeNextConnectViewController alloc] init]; [self.navigationController pushViewController:nextvc animated:YES]; nextvc.conectType = 0; }else{ [self showTitle:@"" messsage:@"请切换到WiFi环境下绑定"]; } }]; } @end