// // FirstViewController.m // Talk // // Created by xuzs on 22/10/11. // Copyright (c) 2022年 xuzs. All rights reserved. // #import "Opendoor.h" #import "UI.h" #import "HttpManager.h" #import "Msg.h" #import "Pub.h" #import "sysInfo.h" #import "Call.h" #import "EquAdd.h" #import "EquConfig.h" #import "P2pTest.h" #import "Sformat.h" @interface Opendoor () @property (nonatomic, strong)UIView *rview,*headView,*topView,*bottomView,*openView,*openBgView; @property (nonatomic, strong)UILabel *titleLabel; @property (nonatomic, strong)UIImageView *rightImage,*watchImage,*openImage,*wave1Image,*wave2Image,*wave3Image; @property (nonatomic, strong)UIButton *selequButton; @property (nonatomic, strong)UIAlertController *openAlert; @property NSArray *equlist; @property int selIndex; @property NSString *pwd; @property long p2p_click_times; @property long p2p_click_time; @end @implementation Opendoor - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self setNavTitle:@"手机开门"]; [self setNavRight:@"setting" action:@selector(ConfigEqu) ]; [self setNavLeft:@"blank" action:@selector(P2ptest) ]; [self setupViews]; [self setUI]; [Opendoor refEqulist:nil]; self.p2p_click_time = 0; } - (void)setupViews { [self.view addSubview:self.rview]; //[self.rview addSubview:self.headView]; [self.rview addSubview:self.topView]; [self.rview addSubview:self.bottomView]; //[self.headView addSubview:self.titleLabel]; //[self.headView addSubview:self.rightImage]; [self.topView addSubview:self.selequButton]; [self.topView addSubview:self.watchImage]; [self.topView addSubview:self.wave1Image]; [self.topView addSubview:self.wave2Image]; [self.topView addSubview:self.wave3Image]; [self.bottomView addSubview:self.openView]; [self.openView addSubview:self.openBgView]; [self.openView addSubview:self.openImage]; [self.watchImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Watch)]]; [self.selequButton addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SelEqu)]]; [self.openView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OpenDoor)]]; } - (void)setUI{ [UI SetRView:self.rview Top:@"0" Right:@"0" Bottom:@"0" Left:@"0"]; //[UI setFrameInView:self.headView Width:@"100vw" Height:@"70" TranslateX:@"0" TranslateY:@"0" InView:nil Position:UITopMid]; [UI setFrameInView:self.topView Width:@"100vw" Height:@"55vh" TranslateX:@"0" TranslateY:@"0" InView:nil Position:UITopMid]; [UI setFrameInView:self.bottomView Width:@"100vw" Height:@"45vh" TranslateX:@"0" TranslateY:@"0" InView:nil Position:UIBottomMid]; [UI chgMargin:self.topView Css:[[NSString alloc] initWithFormat:@"%f 0 0 0",[UI getStatuAndStatuHeight]]]; //[UI setFrameInView:self.titleLabel Width:@"100vw" Height:@"50" TranslateX:@"0" TranslateY:@"0" InView:nil Position:UIBottomMid]; //[UI chgMargin:self.titleLabel Css:@"0 50 0 50"]; //[UI setFrameInView:self.rightImage Width:@"20" Height:@"20" TranslateX:@"15" TranslateY:@"15" InView:nil Position:UIBottomRight]; [UI setFrameInView:self.selequButton Width:@"70vw" Height:@"50" TranslateX:@"0" TranslateY:@"30%" InView:nil Position:UITopMid]; [UI chgMargin:self.selequButton Css:@"0 55 0 0"]; [UI setFrameByView:self.watchImage Width:@"50" Height:@"50" TranslateX:@"5" TranslateY:@"0" ByView:self.selequButton Position:RightAlignCenter]; [UI setFrameInView:self.wave1Image Width:@"122vw" Height:@"66" TranslateX:@"0" TranslateY:@"-6" InView:nil Position:UIBottomMid]; [UI setFrameInView:self.wave2Image Width:@"112vw" Height:@"68" TranslateX:@"8vw" TranslateY:@"-4" InView:nil Position:UIBottomMid]; [UI setFrameInView:self.wave3Image Width:@"104vw" Height:@"70" TranslateX:@"0" TranslateY:@"-2" InView:nil Position:UIBottomMid]; [UI setFrameInView:self.openView Width:@"35vw" Height:@"35vw" TranslateX:@"0" TranslateY:@"-20%" InView:nil Position:UICenter]; [UI setFrameInView:self.openBgView Width:@"35vw" Height:@"35vw" TranslateX:@"0" TranslateY:@"0" InView:nil Position:UICenter]; [UI setFrameInView:self.openImage Width:@"35%" Height:@"35%" TranslateX:@"0" TranslateY:@"0" InView:nil Position:UICenter]; //设置圆角 [UI setCornerRadius:self.openView]; [UI setCornerRadius:self.openBgView]; [UI setGradientBorder:self.openBgView withColor:0x378fe8]; } - (void)SelEqu{ if([self hasEqu]==NO)return; [Msg Select:self SourceView:self.selequButton Title:@"选择设备" Options:self.equlist SelectedHandler:^(int index) { if(index>0){ self.selIndex = index; [_selequButton setTitle:[self.equlist[index] valueForKey:@"name"] forState:UIControlStateNormal]; } }]; } - (BOOL)hasEqu{ BOOL ret = YES; if(self.selIndex==-2){ ret = NO; // [self alert:@"正在获取设备,请稍候"]; } else if(self.selIndex==-1){ ret = NO; ////是否添加设备 [Msg MessageBox:self Msg:@"您尚未添加设备,是否立即添加?" SelectedHandler:^(int index) { EquAdd *equadd = [[EquAdd alloc] init]; [self.navigationController pushViewController:equadd animated:YES]; }]; } return ret; } - (void)Watch{ if([self hasEqu]==NO)return; [Pub getApp].callOut = [[Call alloc] init]; [Pub getApp].callOut.CallTag = @"WATCH"; [[Pub getApp].talk SetRemoteEqu:[self.equlist[self.selIndex] valueForKey:@"equid"]]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[Pub getApp].callOut]; nav.modalPresentationStyle = UIModalPresentationFullScreen; [[Pub getApp].window.rootViewController presentViewController:nav animated:YES completion:nil]; } - (void)ConfigEqu{ NSLog(@"ConfigEqu"); if([self hasEqu]==NO)return; EquConfig *equconfig = [[EquConfig alloc] init]; equconfig.equid = [self.equlist[self.selIndex] valueForKey:@"equid"]; [self.navigationController pushViewController:equconfig animated:YES]; } - (void)P2ptest{ if([Sformat timestamp] - self.p2p_click_time<=1000){ self.p2p_click_times ++; } else{ self.p2p_click_times = 0; } self.p2p_click_time = [Sformat timestamp]; if(self.p2p_click_times>5){ [Pub getApp].p2ptest = [[P2pTest alloc] init]; [self.navigationController pushViewController:[Pub getApp].p2ptest animated:YES]; } } - (void)OpenDoor{ if([self hasEqu]==NO)return; [[Pub getApp].talk SetRemoteEqu:[self.equlist[self.selIndex] valueForKey:@"equid"]]; self.openAlert = [Msg OpendoorByPwd:self SelectedHandler:^(int index) { [self alert:@"请输入6位数字密码"]; }]; } - (void)OpenDoorStep1:(NSString *)pwd{ if(self.openAlert){ [self.openAlert dismissViewControllerAnimated:YES completion:nil]; //[[Pub getApp].talk openDoor:pwd]; self.pwd = pwd; [[Pub getApp].talk watchByServer]; CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * 1 * 1 ]; rotationAnimation.duration = 1; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = 15.0; rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; self.openBgView.hidden = NO; [self.openBgView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } } - (void)OpenDoorStep2{ [[Pub getApp].talk openDoor:self.pwd]; } - (void)OpenDoorEnd{ NSLog(@"OpenDoorEnd11"); dispatch_async(dispatch_get_main_queue(), ^{//转到主线程处理 //[self.openBgView.layer removeAllAnimations]; [self.openBgView.layer removeAnimationForKey:@"transform.rotation.z"]; self.openBgView.hidden = YES; }); } + (void)refEqulist:(void (^)(void)) handler{ UITabBarController *tab = (UITabBarController *)[Pub getApp].window.rootViewController; if(tab){ BaseNavigationController *nav = tab.viewControllers[1] ; Opendoor *opendoor = nav.childViewControllers[0]; if(opendoor){ opendoor.selIndex = -2;//未获取到设备 [HttpManager EquList:[Pub getSysInfo].username Token:[Pub getSysInfo].token HttpResultHandler:^(NSDictionary *json) { if([[json objectForKey:@"result"] isEqualToString:@"ok"]){ opendoor.equlist = [json objectForKey:@"data"]; NSLog(@"设备数:%lu",(unsigned long)opendoor.equlist.count); if(opendoor.equlist.count>0){ opendoor.selIndex = 0; [opendoor.selequButton setTitle:[opendoor.equlist[0] valueForKey:@"name"] forState:UIControlStateNormal]; } else{ opendoor.selIndex = -1;//没有设备 [opendoor.selequButton setTitle:@"无设备" forState:UIControlStateNormal]; } if(handler){ handler(); } } }]; } } } + (NSArray *)getEqulist{ UITabBarController *tab = (UITabBarController *)[Pub getApp].window.rootViewController; if(tab){ BaseNavigationController *nav = tab.viewControllers[1] ; Opendoor *opendoor = nav.childViewControllers[0]; if(opendoor){ return opendoor.equlist; } } return nil; } + (NSString *)EquNameById:(NSString *)equid{ NSArray *list = [Opendoor getEqulist]; if(list){ for(int i=0;i