// // XuToCenterViewController.m // Ifish // // Created by imac on 16/4/21. // Copyright © 2016年 imac. All rights reserved. // #import "XuToCenterViewController.h" #import "XuToSetViewController.h" #import "XuToSoketBackMsgDataUnity.h" #import "XutoBackMsgModel.h" #define XUTOLIGHT1_TAG 9001 #define XUTOLIGHT2_TAG 9002 #define XUTOGASPUMP_TAG 9000 #define XUTOHULI_TAG 9003 #define XUTOWATERPUMP_TAG 9004 #define XUTOJIAREBANG_TAG 9005 #import "XuToLightModel.h" #import "MyHud.h" #define kPlace ([UIScreen mainScreen].bounds.size.width - btnWith*2) /4 #define btnWith 100 #import "XuToWenDuPicview.h" #import "JiaReWenDuModel.h" #import "XuToControlName.h" #import "UserExtendataArchaver.h" #import "XuToControlNameViewController.h" @interface XuToCenterViewController () { dispatch_queue_t _xutoMainQueue; } @property(nonatomic,strong) XuToSetViewController *xuToSetVC; @property(nonatomic,strong) XutoBackMsgModel *backMsgModel; @property(nonatomic,strong) XuToWenDuPicview *wenduPicview; @property(nonatomic,strong) MyHud *XuTohud; @property(nonatomic,strong) XuToControlNameViewController *changeNameVC; @end @implementation XuToCenterViewController - (void)viewDidLoad { [super viewDidLoad]; [Socketsingleton sharedInstance].communiteDelegate=self; _backMsgModel = [[XutoBackMsgModel alloc] init]; [self creatFishAnimation]; [self creatUI]; _xutoMainQueue = dispatch_get_main_queue(); // [self.SetTimeButton addTarget:self action:@selector(XuToControlSetBtnAction:) forControlEvents:UIControlEventTouchUpInside]; //self.SetTimeButton.backgroundColor=[UIColor redColor]; [self.setbackBtn addTarget:self action:@selector(XuToSetBtnAction) forControlEvents:UIControlEventTouchUpInside]; UITapGestureRecognizer*tapgestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(XuToControlSetBtnAction:)]; [self.setImag addGestureRecognizer:tapgestureRecognizer]; } -(void)creatFishAnimation{ NSLog(@"*******"); } -(void)creatUI{ self.dulabel.hidden = YES; self.waringLabel.hidden =YES; self.bubble.hidden = YES; UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xutobgm"]]; [self.tableView setBackgroundView:backImage]; self.temperImage = [[UIImageView alloc] initWithFrame:CGRectMake(kPlace, 2 *kPlace + 90, 70, 70)]; self.temperImage.image = [UIImage imageNamed:@"xutowatertemp1"]; self.temperImage.contentMode = UIViewContentModeScaleAspectFill; [self.tableView addSubview:self.temperImage]; self.temLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 60, 60)]; self.temLabel.text = @""; self.temLabel.textColor = [UIColor colorWithRed:215/255.0f green:245/255.0f blue:246/254.0f alpha:1]; //self.temLabel.backgroundColor = [UIColor grayColor]; self.temLabel.font = [UIFont systemFontOfSize:30]; [self.temperImage addSubview:self.temLabel]; CGFloat btnControlWith = 70; CGFloat btnControlPlace = (kScreenSize.width - 3*btnControlWith )/4; // NSLog(@"%f %f %f %f %f",btnControlPlace,kScreenSize.height,kScreenSize.width,self.view.frame.size.height,self.view.frame.size.width); for (int i = 0; i < 3; i ++) { self.btnControl = [[UIButton alloc] init] ; self.btnControl.frame = CGRectMake(btnControlPlace +i*(btnControlPlace + btnControlWith),kScreenSize.height - 3*btnControlPlace - 2 *btnControlWith -10 , btnControlWith, btnControlWith); // [btnControl setImage:[UIImage imageNamed:[NSString stringWithFormat:@"btn%d_off",i + 1]] forState:UIControlStateNormal]; [self.btnControl setImage:[UIImage imageNamed:[NSString stringWithFormat:@"xutobtn%d_off",i + 1]] forState:UIControlStateNormal]; self.btnControl.tag=9000+i; [self.btnControl addTarget:self action:@selector(xuToBtnClick:) forControlEvents:UIControlEventTouchUpInside]; UILabel *textLbl = [[UILabel alloc] initWithFrame:CGRectMake(btnControlPlace +i*(btnControlPlace + btnControlWith),kScreenSize.height - 3*btnControlPlace - 2 *btnControlWith -20 + btnControlWith , btnControlWith, btnControlPlace - 5)]; //textLbl.backgroundColor = [UIColor blackColor]; textLbl.textColor = [UIColor colorWithRed:250/255.0f green:255/255.0f blue:255/254.0f alpha:1]; textLbl.textAlignment = NSTextAlignmentCenter; NSArray *arr = @[@"气泵",@"灯1",@"灯2"]; textLbl.text = arr[i]; textLbl.font = [UIFont boldSystemFontOfSize:14]; [self.tableView addSubview:textLbl]; [self.tableView addSubview:self.btnControl]; if (i==0) { self.XuToAirPump = self.btnControl; self.airName = textLbl; }else if (i==1){ self.XuToLight1 = self.btnControl; self.light1Name = textLbl; }else if (i==2){ self.XuToLight2 = self.btnControl; self.light2Name = textLbl; } } for (int i = 0; i < 3; i ++) { self.btnControl = [[UIButton alloc] init] ; self.btnControl.frame = CGRectMake(btnControlPlace +i*(btnControlPlace + btnControlWith), kScreenSize.height - 2*btnControlPlace - btnControlWith -25 , btnControlWith, btnControlWith); // [btnControl setImage:[UIImage imageNamed:[NSString stringWithFormat:@"btn%d_off",i + 4]] forState:UIControlStateNormal]; if (i==0) { [self.btnControl setImage:[UIImage imageNamed:[NSString stringWithFormat:@"xutobtn%d_on",i + 4]] forState:UIControlStateNormal]; }else{ [self.btnControl setImage:[UIImage imageNamed:[NSString stringWithFormat:@"xutobtn%d_off",i + 4]] forState:UIControlStateNormal]; } self.btnControl.tag= 9003+i; [self.btnControl addTarget:self action:@selector(xuToBtnClick:) forControlEvents:UIControlEventTouchUpInside]; UILabel *textLbl = [[UILabel alloc] initWithFrame:CGRectMake(btnControlPlace +i*(btnControlPlace + btnControlWith), kScreenSize.height - 2*btnControlPlace - btnControlWith -35 + btnControlWith, btnControlWith, btnControlPlace - 5)]; //textLbl.backgroundColor = [UIColor blackColor]; textLbl.textAlignment = NSTextAlignmentCenter; textLbl.textColor = [UIColor colorWithRed:250/255.0f green:255/255.0f blue:255/254.0f alpha:1]; NSArray *arr = @[@"护理灯",@"水泵",@"加热棒"]; textLbl.font = [UIFont boldSystemFontOfSize:14]; textLbl.text = arr[i]; [self.tableView addSubview:textLbl]; [self.tableView addSubview:self.btnControl]; if (i==0) { self.XuToHuli = self.btnControl; self.huLiName = textLbl; }else if (i==1){ self.XuToWaterPump = self.btnControl; self.waterName = textLbl; }else if (i==2){ //加热棒 不可点 self.XuTOJiaRe = self.btnControl; self.jiaReName = textLbl; } } [self initCHNames]; } #pragma mark - 按钮点击事件 -(void)xuToBtnClick:(UIButton*)btn{ XutoBackMsgModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"xutoBackInfo"]; if (backmodel) { }else{ [self showLableAction:@"设备已离线"]; } XuToLightModel * lightModel=[[XuToLightModel alloc] init]; lightModel.sendmacId = self.currentDevice.macAddress; lightModel.resavemacId = self.currentDevice.macAddress; NSString*switchString1=lightModel.description; if (btn.selected) {//开 lightModel.switchBtn=@"00"; btn.selected=NO; }else {// 关 lightModel.switchBtn=@"01"; btn.selected=YES; } lightModel.crc16Str=@"0000";// CRC16验证码 switch (btn.tag) { case XUTOGASPUMP_TAG: { btn.userInteractionEnabled=YES; [btn setImage:[UIImage imageNamed:@"xutobtn1_on.png"] forState:UIControlStateSelected]; lightModel.lightNumber =@"01"; NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,lightModel.lightNumber,lightModel.switchBtn,lightModel.crc16Str]; NSData*manulData=[dataContorl stringToHexData:switchString2]; [[Socketsingleton sharedInstance] soketWriteData:manulData]; if ([Socketsingleton sharedInstance].clientSocket.isConnected) { //soket 未断开时显示 [self creatXuToindicaterView]; [self.XuTohud myhudstart]; } if ([lightModel.switchBtn isEqualToString:@"00"]) { btn.selected=YES; }else{ btn.selected=NO; } } break; case XUTOLIGHT1_TAG: { btn.userInteractionEnabled=YES; [btn setImage:[UIImage imageNamed:@"xutobtn2_on.png"] forState:UIControlStateSelected]; lightModel.lightNumber =@"02";// 01 --灯1 NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,lightModel.lightNumber,lightModel.switchBtn,lightModel.crc16Str]; NSData*manulData=[dataContorl stringToHexData:switchString2]; [ [Socketsingleton sharedInstance] soketWriteData:manulData]; if ([Socketsingleton sharedInstance].clientSocket.isConnected) { //soket 未断开时显示 [self creatXuToindicaterView]; [self.XuTohud myhudstart]; } if ([lightModel.switchBtn isEqualToString:@"00"]) { btn.selected=YES; }else{ btn.selected=NO; } } break; case XUTOLIGHT2_TAG: { btn.userInteractionEnabled=YES; [btn setImage:[UIImage imageNamed:@"xutobtn3_on.png"] forState:UIControlStateSelected]; lightModel.lightNumber=@"03";// NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,lightModel.lightNumber,lightModel.switchBtn,lightModel.crc16Str]; NSData*manulData=[dataContorl stringToHexData:switchString2]; [ [Socketsingleton sharedInstance] soketWriteData:manulData]; if ([Socketsingleton sharedInstance].clientSocket.isConnected) { //soket 未断开时显示 [self creatXuToindicaterView]; [self.XuTohud myhudstart]; } if ([lightModel.switchBtn isEqualToString:@"00"]) { btn.selected=YES; }else{ btn.selected=NO; } } break; case XUTOHULI_TAG: { btn.userInteractionEnabled=YES; [btn setImage:[UIImage imageNamed:@"xutobtn4_off.png"] forState:UIControlStateNormal]; lightModel.lightNumber=@"04";// NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,lightModel.lightNumber,lightModel.switchBtn,lightModel.crc16Str]; NSData*manulData=[dataContorl stringToHexData:switchString2]; [ [Socketsingleton sharedInstance] soketWriteData:manulData]; if ([Socketsingleton sharedInstance].clientSocket.isConnected) { //soket 未断开时显示 [self creatXuToindicaterView]; [self.XuTohud myhudstart]; } if ([lightModel.switchBtn isEqualToString:@"00"]) { btn.selected=YES; }else{ btn.selected=NO; } } break; case XUTOWATERPUMP_TAG: { NSString*stateString=[backmodel.waterpump substringWithRange:NSMakeRange(2, 2)]; //开 if ([stateString isEqualToString:@"01"]) { UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"确认关闭水泵?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [alert show]; }else{ btn.userInteractionEnabled=YES; [btn setImage:[UIImage imageNamed:@"xutobtn5_on.png"] forState:UIControlStateSelected]; lightModel.lightNumber=@"05";// NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,lightModel.lightNumber,lightModel.switchBtn,lightModel.crc16Str]; NSData*manulData=[dataContorl stringToHexData:switchString2]; [ [Socketsingleton sharedInstance] soketWriteData:manulData]; if ([Socketsingleton sharedInstance].clientSocket.isConnected) { //soket 未断开时显示 [self creatXuToindicaterView]; [self.XuTohud myhudstart]; } if ([lightModel.switchBtn isEqualToString:@"00"]) { btn.selected=YES; }else{ btn.selected=NO; } } } break; case XUTOJIAREBANG_TAG: { if (backmodel) { _wenduPicview=[[XuToWenDuPicview alloc] init]; _wenduPicview.frame=CGRectMake(0, 0, kScreenSize.width, kScreenSize.height); [_wenduPicview.sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view.window addSubview:_wenduPicview]; [self jiaReMianQue]; }else{ [self showLableAction:@"设备已离线"]; } } default: break; } } #pragma mark - 确定加热棒温度 -(void)sureBtnClick:(UIButton *)btn{ NSLog(@"%@******** _wenduPicview.picViewResultString",_wenduPicview.picViewResultString); if (_wenduPicview.picViewResultString ==nil) { [_wenduPicview removeFromSuperview]; }else{ JiaReWenDuModel *model=[[JiaReWenDuModel alloc] init]; model.resavemacId = self.currentDevice.macAddress; model.sendmacId = self.currentDevice.macAddress; int intlowString = [_wenduPicview.picViewResultString intValue]; int newIntlowString=intlowString*10; NSLog(@"%d******** newIntlowString",newIntlowString); // 转四位16进制 model.JiaReWenDu=[dataContorl tpIntStringToFourHex:newIntlowString]; model.crc16Code =@"0000"; NSString * hexstring=[NSString stringWithFormat:@"%@%@%@",model.description,model.JiaReWenDu,model.crc16Code]; NSData*data=[dataContorl stringToHexData:hexstring]; [ [Socketsingleton sharedInstance] soketWriteData:data]; } [_wenduPicview removeFromSuperview]; } -(void)jiaReMianQue{ XutoBackMsgModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"xutoBackInfo"]; UInt64 mac=[dataContorl hexToTen:backmodel.heatTemperature]; NSLog(@"%llu",mac); //float TPlabel=mac/10+(mac%10)*0.1; float TPlabel=mac/10; int temp= (int)(TPlabel+0.5); NSString *stringFloat = [NSString stringWithFormat:@"%d",temp]; NSLog(@"%@",stringFloat); NSArray*wenDuArr=[_wenduPicview.picArr objectAtIndex:0]; dispatch_async(_xutoMainQueue, ^{ for (NSInteger i=0; i%@",data); self.dataLength=string1.length; //存储 if (string1.length == IFISH_BACKMASGTYAP73) { [XuToSoketBackMsgDataUnity readSoketDataWithBaackMassgModel:_backMsgModel addWithBackData:data]; } // 温度 [XuToSoketBackMsgDataUnity setTemperaturelabel:self.temLabel addTemperaturebackImg:self.temperImage WithBackData:data addWithBackMassageModel:_backMsgModel addWithTableview:self.tableView]; //灯的状态 dispatch_async(dispatch_get_main_queue(), ^{ [XuToSoketBackMsgDataUnity resetXutoButtonState:self.XuToLight1 addlight2:self.XuToLight2 addGasPump:self.XuToAirPump addWaterPump:self.XuToWaterPump addHuliLight:self.XuToHuli addJiaRe:self.XuTOJiaRe withBackModel:_backMsgModel]; }); // 刷新设置界面 // dispatch_async(dispatch_get_main_queue(), ^{ _xuToSetVC.timeArr= [XuToSoketBackMsgDataUnity refreshXuToTimerAddTemperatureWithBackMassageModel:_backMsgModel]; [_xuToSetVC.tableView reloadData]; }); } -(void)creatXuToindicaterView{ // _HUD=[MBProgressHUD showHUDAddedTo:self.view animated:YES]; // _HUD.delegate=self; // // // _HUD.dimBackground=YES; // _HUD.backgroundColor=[UIColor clearColor]; // [_HUD hide:YES afterDelay:10.0]; self.XuTohud=[[MyHud alloc]init]; self.XuTohud.frame=CGRectMake(0, kScreenSize.height/3, kScreenSize.width,kScreenSize.height*2/3 ); [self.XuTohud myhudtimeOut]; //self.XuTohud.backgroundColor=[UIColor redColor]; [self.view addSubview:self.XuTohud]; NSLog(@"**********绚多加载***********"); } -(void)ifishDeviceLogInSuccees{ self.isconnect=YES; NSLog(@"app登录成功"); } -(void)ifishDeviceLogInFail{ [self endRefreshing]; [self.indicatorView stopAnimating]; dispatch_async(dispatch_get_main_queue(), ^{ [self deviceNotOnlineState];//设备离线 }); } #pragma mark -父类方法 -(void)shezhiMingCheng { _changeNameVC = [[XuToControlNameViewController alloc] init]; _changeNameVC.name = self.name.text; _changeNameVC.device = self.currentDevice; __weak typeof (self)wself = self; _changeNameVC.NameWithBlock = ^(NSString *nameStr){ wself.name.text = nameStr; [wself initCHNames]; }; [ wself.navigationController pushViewController: _changeNameVC animated:YES]; } -(void)initCHNames{ XuToControlName *name = [UserExtendataArchaver currentXuToControlName:self.currentDevice.macAddress]; self.airName.text = name.airPump; self.light1Name.text = name.light1; self.light2Name.text = name.light2; self.huLiName.text = name.huLiDeng; self.waterName.text = name.waterPump; self.jiaReName.text = name.jiaRe; } @end