// // NoneControlNonetimerViewController.m // Ifish // // Created by imac on 16/3/8. // Copyright © 2016年 imac. All rights reserved. // #import "NoneControlNonetimerViewController.h" #import "SetTimeCell.h" #import "SecondSetTimeCell.h" #import "SetTimeModel.h" #import "SetSelectorTime.h" //#import "AsyncSocket.h" #import "setWaringTpModel.h" #import "appLogin.h" #import "workTypeModel.h" #import "SetSelectorTime.h" #define TIME_TAG 300 #import "MyPickerView.h" #import "searchDeviceModel.h" #import "MydatePickerView.h" #define SET_CELL_TAG 2200 #define MYSWITCH_TAG 3200 #import "CenterViewController.h" #import "TimerSateModel.h" #import "TemperatureSetCell.h" #import "TemperatureSetModel.h" #import "ZeroControlBackmsgModel.h" #import "BrightnessCell.h" #import "GuiDengViewCell.h" #import "ChangeWaterCell.h" #import "GuidengKaiGuanModel.h" #import "BrightnessModel.h" #import "remindCyclePic.h" #import "setRemindWaterModel.h" #import "CreatErWeiMaController.h" @interface NoneControlNonetimerViewController () { MydatePickerView*_mydatepicView; MyPickerView* _mypicView; dispatch_queue_t _mainQueue; remindCyclePic*_remindPic; BOOL _huanshuiSwitchIsOn; } @property(nonatomic,strong)NSIndexPath*indexpath; @property(nonatomic,copy)NSString*statString; @property(nonatomic,copy)NSString*endString; @property(nonatomic,copy)NSString*nextChangeDateLabel; @property(nonatomic,strong)setRemindWaterModel*waterInfmodel; @end @implementation NoneControlNonetimerViewController - (void)viewDidLoad { [super viewDidLoad]; _waterInfmodel=[[setRemindWaterModel alloc]init]; _mainQueue = dispatch_get_main_queue(); self.navigationController.navigationBar.backItem.title=@""; [self addTitleViewWithTitle:@"设置"]; [self initData]; self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; [self creatIndicatorView]; self.view.backgroundColor = COLOR_MIAN; self.tableView.backgroundColor = COLOR_MIAN; // if ([self.currentdevice.isMaster isEqualToString:@"1"]) { // UIBarButtonItem*rightItem=[[UIBarButtonItem alloc]initWithTitle:@"分享设备" style: UIBarButtonItemStyleDone target:self action:@selector(noneItemClick)]; // rightItem.tintColor=[UIColor whiteColor]; // self.navigationItem.rightBarButtonItem=rightItem; // } // } -(void)noneItemClick{ CreatErWeiMaController*vc=[[CreatErWeiMaController alloc]init] ; vc.erdevicemodel=self.currentdevice; [self.navigationController pushViewController:vc animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)initData{ self.titleArr=@[@"预警设置",@"背光亮度",@"柜灯开关",@"换水提醒",@"定时模式"]; ZeroControlBackmsgModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackZeroControlInfo"]; if (backmodel) { NSString*low=backmodel.miniTp; NSString*heigh=backmodel.maxTp; UInt64 lowten=[dataContorl hexToTen:low]; UInt64 heighten=[dataContorl hexToTen:heigh]; float formatlow=lowten/10; float formatheigh=heighten/10; NSString*wenduString=[NSString stringWithFormat:@"%.0f~%.0f°C",formatlow,formatheigh]; self.timeArr=@[wenduString]; }else{ [self showTitle:@"" messsage:@"设备已离线"]; self.timeArr=@[@"20~30°C"]; } } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ZeroControlBackmsgModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackZeroControlInfo"]; static NSString *cellID=@"kongCell"; UITableViewCell *kongCell=[tableView dequeueReusableCellWithIdentifier:cellID]; if (kongCell==nil) { kongCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; } if (indexPath.row==0){ TemperatureSetCell*cell=[tableView dequeueReusableCellWithIdentifier:@"TemperatureSetCell"]; if (cell==nil) { cell=[[[NSBundle mainBundle]loadNibNamed:@"TemperatureSetCell" owner:self options:nil]lastObject]; } TemperatureSetModel*model=[[TemperatureSetModel alloc]init]; model.title=self.titleArr[indexPath.row]; cell.titleLabel.text=model.title; model.temperatureLabel=self.timeArr[indexPath.row]; cell.temperaturelabel.text=model.temperatureLabel; [cell.temPSwitch addTarget:self action:@selector(tempSwitchAction:) forControlEvents:UIControlEventValueChanged]; if ([backmodel.baojingSwitch isEqualToString:@"01"]) { [cell.temPSwitch setBackgroundImage:[UIImage imageNamed:@"greenbtn_big"] forState:UIControlStateNormal]; cell.temPSwitch.selected = NO; }else{ [cell.temPSwitch setBackgroundImage:[UIImage imageNamed:@"graybtn_big"] forState:UIControlStateNormal]; cell.temPSwitch.selected = YES; } return cell; }else if (indexPath.row==1){ //背光 BrightnessCell*cell=[tableView dequeueReusableCellWithIdentifier:@"BrightnessCell"]; if (cell==nil) { cell=[[[NSBundle mainBundle]loadNibNamed:@"BrightnessCell" owner:self options:nil]lastObject]; } if (![_currentdevice.isLightness isEqual:[NSNull null]]) { if ([_currentdevice.isLightness isEqualToString:@"1"]) { //有背光 cell.brightTitleLabel.text=_titleArr[indexPath.row]; // 系统默认YES 这里拖拽结束时再传值需设为NO cell.btightnessSlider.continuous=NO; if (backmodel) { UInt64 lightTen=[dataContorl hexToTen:backmodel.brightness]; [cell.btightnessSlider setValue:lightTen animated:YES]; // int sliderValue=(int)(lightTen+0.5); // int percent=(sliderValue*100)/250; // cell.persentLabel.text=[NSString stringWithFormat:@"%d%",percent]; // cell.persentLabel.textColor=COLOR_LABEL_TITLE; } [cell.btightnessSlider addTarget:self action:@selector(zerobtightnessSlider:) forControlEvents:UIControlEventValueChanged]; cell.brightTitleLabel.hidden=NO; cell.btightnessSlider.hidden=NO; cell.persentLabel.hidden=NO; return cell; }else{ //无背光 return kongCell; } }else{ return kongCell; } }else if (indexPath.row==2){ // 柜灯 GuiDengViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"GuiDengViewCell"]; if (cell==nil) { cell=[[[NSBundle mainBundle]loadNibNamed:@"GuiDengViewCell" owner:self options:nil]lastObject]; } if (![_currentdevice.isSarkLamp isEqual:[NSNull null]]) { if ([_currentdevice.isSarkLamp isEqualToString:@"1"]) { cell.guidengtitle=_titleArr[indexPath.row]; [cell.guidengSwitch addTarget:self action:@selector(zeroguidengSwitchAtion:) forControlEvents:UIControlEventTouchUpInside]; if ([backmodel.tankLight isEqualToString:@"01"]) { [cell.guidengSwitch setBackgroundImage:[UIImage imageNamed:@"greenbtn_big"] forState:UIControlStateNormal]; cell.guidengSwitch.selected = NO; }else{ [cell.guidengSwitch setBackgroundImage:[UIImage imageNamed:@"graybtn_big"] forState:UIControlStateNormal]; cell.guidengSwitch.selected = YES; } return cell; }else{ return kongCell; } }else{ return kongCell; } }else if (indexPath.row==3){ // 换水提醒 ChangeWaterCell*cell=[tableView dequeueReusableCellWithIdentifier:@"ChangeWaterCell"]; if (cell==nil) { cell=[[[NSBundle mainBundle]loadNibNamed:@"ChangeWaterCell" owner:self options:nil]lastObject]; } [cell.ChangeWaterSwitch addTarget:self action:@selector(zerochangeWaterSwitch:) forControlEvents:UIControlEventTouchUpInside]; if (![self.currentdevice.waterRemind isEqual:[NSNull null]]) { if ([self.waterInfmodel.waterremind isEqualToString:@"1"]) { //[cell.ChangeWaterSwitch setOn:YES]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"firstSetCell"]]; cell.ChangeWaterSwitch.selected = NO; [cell.ChangeWaterSwitch setBackgroundImage:[UIImage imageNamed:@"greenbtn_big"] forState:UIControlStateNormal]; _huanshuiSwitchIsOn=YES; }else{ // [cell.ChangeWaterSwitch setOn:NO]; _huanshuiSwitchIsOn=NO; cell.ChangeWaterSwitch.selected = YES; [cell.ChangeWaterSwitch setBackgroundImage:[UIImage imageNamed:@"graybtn_big"] forState:UIControlStateNormal]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"set_cellBack"]]; } if (!self.waterInfmodel.remindcycle) { //在未获取换水信息时暂时显示 cell.huanshuiDaylabel.text=@"换水时间"; }else{ cell.huanshuiDaylabel.text=[NSString stringWithFormat:@"%ld天",(long)self.waterInfmodel.remindcycle]; } NSLog(@"你"); return cell; }else{ kongCell.textLabel.text =@"错误信息 waterRemind null"; return kongCell; } }else if(indexPath.row==4){ static NSString*nextHuanShuiLablecellId=@"labelcell"; UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:nextHuanShuiLablecellId]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nextHuanShuiLablecellId]; } if (_huanshuiSwitchIsOn) { cell.backgroundColor=COLOR_BACK_; cell.textLabel.text=[NSString stringWithFormat:@"下次换水时间:%@",self.waterInfmodel.remindDate]; cell.textLabel.font=[UIFont systemFontOfSize:12]; [cell.textLabel sizeToFit]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"secondSetCell"]]; }else{ cell.textLabel.text=@""; } cell.userInteractionEnabled = NO; return cell; } return nil; } #pragma mark 柜光灯开关Action -(void)zeroguidengSwitchAtion:(UIButton*)sender{ UIButton *switchButton = (UIButton*)sender; // BOOL isButtonOn=[switchButton isOn]; GuidengKaiGuanModel*guidengModel=[[GuidengKaiGuanModel alloc]init]; guidengModel.sendmacId = self.currentdevice.macAddress; guidengModel.resavemacId = self.currentdevice.macAddress; guidengModel.crc16Str=@"0000"; if (switchButton.selected == YES) { NSLog(@"开"); guidengModel.onOff=@"01"; switchButton.selected = NO; NSString* guidengOrder=[NSString stringWithFormat:@"%@%@%@",guidengModel.description,guidengModel.onOff,guidengModel.crc16Str]; NSData*guidengData=[dataContorl stringToHexData:guidengOrder]; [[Socketsingleton sharedInstance] soketWriteData:guidengData]; TwoControlBackmsgmodel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackTwoControlInfo"]; backmodel.tankLight=@"01"; [_indicatorView startAnimating]; }else{ NSLog(@"关"); guidengModel.onOff=@"00"; switchButton.selected = YES; NSString* guidengOrder=[NSString stringWithFormat:@"%@%@%@",guidengModel.description,guidengModel.onOff,guidengModel.crc16Str]; NSData*guidengData=[dataContorl stringToHexData:guidengOrder]; [[Socketsingleton sharedInstance] soketWriteData:guidengData]; TwoControlBackmsgmodel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackTwoControlInfo"]; backmodel.tankLight=@"00"; [_indicatorView startAnimating]; } } #pragma mark 背光灯亮度Action -(void)zerobtightnessSlider:(UISlider*)sender{ UISlider*slider=(UISlider*)sender; NSLog(@"%f",slider.value); //这里 只传整数 //1.强转四舍五入 int sliderValue=(int)(slider.value+0.5); // //2.高斯函数,向下取整 // int sliderValue1=floor(slider.value); // //3. ceil 函数,向上取整 // int sliderValue2=ceil(slider.value); NSLog(@"%d",sliderValue); self.percent=(sliderValue*100)/250; NSString*hexString=[dataContorl ToHex:sliderValue]; if (hexString.length<2) { hexString=[NSString stringWithFormat:@"0%@",hexString]; } NSLog(@"%@",hexString); BrightnessModel*brightModel=[[BrightnessModel alloc]init ]; brightModel.sendmacId = self.currentdevice.macAddress; brightModel.resavemacId = self.currentdevice.macAddress; brightModel.liangdu=hexString; brightModel.crc16Code=@"0000"; NSString*changeLightOder=[NSString stringWithFormat:@"%@%@%@",brightModel.description,brightModel.liangdu,brightModel.crc16Code]; NSData*changeLghtData=[dataContorl stringToHexData:changeLightOder]; [[Socketsingleton sharedInstance] soketWriteData:changeLghtData]; [_indicatorView startAnimating]; } #pragma mark 换水提醒Action -(void)zerochangeWaterSwitch:(UIButton*)sender{ UIButton*huanShuiSwitch=(UIButton*)sender; //BOOL isButtonOn=[huanShuiSwitch isOn]; NSUserDefaults*defuat=[NSUserDefaults standardUserDefaults]; NSString*picResult=[defuat valueForKey:@"zeropicResult"]; int pic = picResult.intValue; if (pic==0) { [self showTitle:@"" messsage:@"请设置提醒天数"]; if (huanShuiSwitch.selected == YES) { huanShuiSwitch.selected = NO; }else{ huanShuiSwitch.selected = YES; } }else{ if (huanShuiSwitch.selected == YES) { //开 huanShuiSwitch.selected = NO; [IFISHHTTPTOOL setRemindWaterInfWith:self.currentdevice.deviceId waterRemind:@"1" remindCycle:picResult huishuiShiJian:^(setRemindWaterModel *remindModel) { self.nextChangeDateLabel=remindModel.remindDate; }]; //更新选择状态 设置界面 在不断 刷新 self.waterInfmodel.waterremind=@"1"; [self.tableView reloadData]; }else{ //关 huanShuiSwitch.selected = YES; [IFISHHTTPTOOL setRemindWaterInfWith:self.currentdevice.deviceId waterRemind:@"0" remindCycle:picResult huishuiShiJian:^(setRemindWaterModel *remindModel) { self.nextChangeDateLabel=remindModel.remindDate; }]; self.waterInfmodel.waterremind=@"0"; [self.tableView reloadData]; } } } #pragma mark 开启关闭报警 -(void)tempSwitchAction:(UIButton*)temperatureSwitch{ ZeroControlBackmsgModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackZeroControlInfo"]; setWaringTpModel*warModel1=[[setWaringTpModel alloc]init]; warModel1.resavemacId = self.currentdevice.macAddress; warModel1.sendmacId = self.currentdevice.macAddress; //BOOL isOn=[temperatureSwitch isOn]; if (temperatureSwitch.selected ==YES) { NSLog(@"开"); temperatureSwitch.selected =NO; warModel1.kaiguan=@"01";// 开 warModel1.miniwaterTp=@"0000"; // 0 warModel1.maxwaterTp=@"0000";//0 }else{ temperatureSwitch.selected =YES; warModel1.kaiguan=@"00";// 关 warModel1.miniwaterTp=@"0000"; // 0 warModel1.maxwaterTp=@"0000";//0 NSLog(@"关"); } [self setTmperatureStateWithModel:warModel1 addBackModel:backmodel]; } -(void)setTmperatureStateWithModel:(setWaringTpModel*)waringModel addBackModel:(ZeroControlBackmsgModel*)backModel{ NSString*maxminTpString=waringModel.description; waringModel.crc16Ing=@"0000"; NSString*minString=[NSString stringWithFormat:@"%@%@%@%@%@",maxminTpString,waringModel.kaiguan,waringModel.miniwaterTp,waringModel.maxwaterTp,waringModel.crc16Ing]; NSData*minwenDuData=[dataContorl stringToHexData:minString]; [[Socketsingleton sharedInstance] soketWriteData:minwenDuData]; [_indicatorView startAnimating]; backModel.miniTp=waringModel.miniwaterTp; backModel.maxTp=waringModel.maxwaterTp; } #pragma mark tableView 头视图和脚视图 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row==4) { //ChangeWaterCell*cell=(ChangeWaterCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath]; if (_huanshuiSwitchIsOn) { return 50; }else{ return 0; } }else if (indexPath.row==1){ if (![_currentdevice.isLightness isEqual:[NSNull null]]) { if ([_currentdevice.isLightness isEqualToString:@"1"]) { return 100; }else{ return 0; } }else{ return 0; } }else if (indexPath.row==2){ // if (![_currentdevice.isSarkLamp isEqual:[NSNull null]]) { if ([_currentdevice.isSarkLamp isEqualToString:@"1"]) { return 50; }else{ return 0; } }else{ return 0; } }else if (indexPath.row==5){ //_currentdevice.isWorkModel=@"0"; //运行模式 if (![_currentdevice.isWorkModel isEqual:[NSNull null]]) { if ([_currentdevice.isWorkModel isEqualToString:@"1"]) { return 50; }else{ return 0; } }else{ return 0; } }else if (indexPath.row==3){ if (![_currentdevice.waterRemind isEqual:[NSNull null]]) { return 50; }else{ return 0; } }else if (indexPath.row==0){ //会否有 预警 if (![_currentdevice.isPushWendu isEqual:[NSNull null]]) { if ([_currentdevice.isPushWendu isEqualToString:@"1"]) { return 50; }else{ return 0; } }else{ return 0; } }else{ return 50; } } #pragma mark tableView 点击事件 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ _indexpath=indexPath; if (indexPath.row==0&&indexPath.section==0) { _mypicView=[[MyPickerView alloc]init]; _mypicView.frame=CGRectMake(0, 0, kScreenSize.width, kScreenSize.height); [self.view.window addSubview:_mypicView]; [_mypicView.cancleButton addTarget:self action:@selector(mypicviewRemoveByCancle) forControlEvents:UIControlEventTouchUpInside]; [_mypicView.sureButton addTarget:self action:@selector(mypicViewRemoveByDone) forControlEvents:UIControlEventTouchUpInside]; [self temperatureMainQueue]; }else if (indexPath.section==0&&indexPath.row==3){ //换水提醒日期选择 _remindPic=[[remindCyclePic alloc]init]; _remindPic.frame=CGRectMake(0, 0, kScreenSize.width, kScreenSize.height); [self.view.window addSubview:_remindPic]; [_remindPic.cancleBtn addTarget:self action:@selector(zeroremindCyclePicRemove) forControlEvents:UIControlEventTouchUpInside]; [_remindPic.sureBtn addTarget:self action:@selector(zeroremindCyclePicDone) forControlEvents:UIControlEventTouchUpInside]; [self huanShuiMainQueue]; } } #pragma mark 移除换水时间选择器 -(void)zeroremindCyclePicRemove{ [_remindPic removeFromSuperview]; // if (_indexpath.section==0&&_indexpath.row==3) { // ChangeWaterCell*cell=[self.tableView cellForRowAtIndexPath:_indexpath]; // cell.huanshuiDaylabel.text=[NSString stringWithFormat:@"%@天",_currentdevice.remindCycle]; // [self.tableView reloadData]; // // } } #pragma mark 换水时间设置 -(void)zeroremindCyclePicDone{ if (_indexpath.section==0&&_indexpath.row==3) { ChangeWaterCell*cell=[self.tableView cellForRowAtIndexPath:_indexpath]; NSString *str=_remindPic.picViewResultString; if (str==nil) { //str=@"1"; NSLog(@"ni hua dong tai kuai"); str=[NSString stringWithFormat:@"%ld",(long)self.waterInfmodel.remindcycle]; [_remindPic makeToast:@"请设置换水天数"]; return; } cell.huanshuiDaylabel.text=[NSString stringWithFormat:@"%@天",str]; //设置换水日期 [IFISHHTTPTOOL setRemindWaterInfWith:self.currentdevice.deviceId waterRemind:@"1" remindCycle:str huishuiShiJian:^(setRemindWaterModel *remindModel) { self.nextChangeDateLabel=remindModel.remindDate; self.waterInfmodel.waterremind=@"1"; self.waterInfmodel.remindDate=remindModel.remindDate; self.waterInfmodel.remindcycle = remindModel.remindcycle; [self.tableView reloadData]; }]; NSUserDefaults*defuat=[NSUserDefaults standardUserDefaults]; [defuat setValue:str forKey:@"picResult"]; [defuat synchronize]; [_remindPic removeFromSuperview]; } } #pragma mark 默认换水时间 -(void)huanShuiMainQueue{ if (_remindPic.picViewResultString !=nil || (_remindPic.picViewResultString ==nil && self.waterInfmodel.remindcycle !=0 )) { NSArray*shijianArr=[_remindPic.picArr objectAtIndex:0]; dispatch_async(_mainQueue, ^{ NSLog(@"a"); for (NSInteger k=0; k