// // Xuanduo2SettingController.m // Ifish // // Created by Alex on 2019/4/28. // Copyright © 2019 lianlian. All rights reserved. // #import "XuanduoTimerSettingViewController.h" #import "UINavigationBar+Background.h" #import "TemperatureSetCell.h" #import "ChangeWaterCell.h" #import "Xuanduo2Model.h" #import "TemperatureSetModel.h" #import "setWaringTpModel.h" #import "remindCyclePic.h" #import "MyPickerView.h" #import "PlusTableViewCell.h" #import "SetTimerCell.h" #import "XuanduoSectionHeaderView.h" #import "XuanduoSelectHeader.h" #import "MydatePickerView.h" #import "SetTimerModel.h" #import "ReadTimerModel.h" #import "Xuanduo2DataUtility.h" #import "Xuanduo2TimerModel.h" #import "IfishDeviceInfo.h" #import "NSString+Add.h" #import "XuToControlName.h" #import "UserExtendataArchaver.h" #import "TimerTypeTableViewCell.h" #import "CycleTimerCell.h" #define kBtnTag 500 #define kSwitchBtnTag 600 static NSString *temperatureCellid = @"TemperatureSetCell"; static NSString *changeWaterCellid = @"ChangeWaterCell"; static NSString *addCellid = @"PlusTableViewCell"; static NSString *plusflag = @"plusCell"; static NSString *setTimerflag = @"SetTimerCell"; static NSString *timerTypeFlag = @"TimerTypeTableViewCell"; static NSString *cycleTimerflag = @"CycleTimerCell"; @interface XuanduoTimerSettingViewController () @property(nonatomic,strong) setRemindWaterModel *waterInfmodel; @property(nonatomic,strong) Xuanduo2TimerModel *timerModel; @property(nonatomic,strong) XuanduoCycleModel *cycleModel; @property (nonatomic,strong) NSMutableDictionary *timeDic; @property(nonatomic,copy) NSString *nextChangeDateLabel; @property(nonatomic,strong) MyHud *XuTohud; @end @implementation XuanduoTimerSettingViewController { NSString *_wenduString; NSIndexPath *_indexPath; MyPickerView *_mypicView; NSArray *_titieArr; NSArray *_imgArr; remindCyclePic*_remindPic; BOOL _huanShuiSwitchIsOn; NSInteger _selectSection; MydatePickerView*_mydatepicView; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (_currentdevice) { [IFISHHTTPTOOL getRemindWaterInfWith:_currentdevice.deviceId huishuiInfo:^(setRemindWaterModel *remindModel) { self.waterInfmodel=remindModel; NSUserDefaults*defuat=[NSUserDefaults standardUserDefaults]; [defuat setInteger:self.waterInfmodel.remindcycle forKey:@"xuto2picResult"]; [defuat synchronize]; self.nextChangeDateLabel=self.waterInfmodel.remindDate; [self.tableView reloadData]; }]; } } - (void)viewDidLoad { [super viewDidLoad]; [self setup]; [self connect]; } - (void)viewWillDisappear:(BOOL)animated{ [_indicatorView stopAnimating]; } - (void)setup { [self.navigationController.navigationBar resetBackgroundImage]; NSString*title=self.readMode.selectorName; [self addTitleViewWithTitle:title]; [self refreshWithData:self.dataModel]; [self setupTable]; [self creatIndicatorView]; [Socketsingleton sharedInstance].communiteDelegate = self; } - (void)refreshWithData:(Xuanduo3fModel*)model { NSString*low = model.miniTemp; NSString*heigh = model.maxTemp; UInt64 lowten = [dataContorl hexToTen:low]; UInt64 heighten = [dataContorl hexToTen:heigh]; float formatlow = lowten/10; float formatheigh = heighten/10; _wenduString = [NSString stringWithFormat:@"%.0f~%.0f°C",formatlow,formatheigh]; } - (void)setupTable { NSString*type=@""; if (self.timerType==1) { type=@"定时模式"; } else if(self.timerType==2) { type=@"循环模式"; } else if(self.timerType==3) { type=@"定时自恢复模式"; } _titieArr = @[type]; //读取具体开关定时 ReadTimerModel*readModel=self.readMode; if (readModel) { if (self.timerType==1)//灯编为原始 { readModel.selectorNumber=[readModel.selectorNumber stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"0"]; } else if (self.timerType==2)//灯编号为e { readModel.selectorNumber=[readModel.selectorNumber stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"e"]; } else if (self.timerType==3)//灯编号为d { readModel.selectorNumber=[readModel.selectorNumber stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"d"]; } self.lightNumber=readModel.selectorNumber; } _selectSection = 0; if (self.timerType==1||self.timerType==3||self.timerType==2) { for (int i = 0; i<_titieArr.count; i++) { NSMutableArray *dataArr = [NSMutableArray arrayWithCapacity:0]; [dataArr addObject:plusflag]; [self.timeDic setObject:dataArr forKey:_titieArr[i]]; } } self.tableView.backgroundColor=COLOR_MIAN; self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; [self.tableView registerNib:[UINib nibWithNibName:temperatureCellid bundle:nil] forCellReuseIdentifier:temperatureCellid]; [self.tableView registerNib:[UINib nibWithNibName:changeWaterCellid bundle:nil] forCellReuseIdentifier:changeWaterCellid]; [self.tableView registerNib:[UINib nibWithNibName:addCellid bundle:nil] forCellReuseIdentifier:addCellid]; [self.tableView registerNib:[UINib nibWithNibName:cycleTimerflag bundle:nil] forCellReuseIdentifier:cycleTimerflag]; [self.tableView registerNib:[UINib nibWithNibName:setTimerflag bundle:nil] forCellReuseIdentifier:setTimerflag]; if (self.timerType==2||self.timerType==3) { UILabel*label=[[UILabel alloc]initWithFrame:CGRectZero]; label.textColor=[UIColor whiteColor]; label.font=[UIFont systemFontOfSize:12]; label.numberOfLines=0; if (self.timerType==2) { label.text=@"开启时间为设备运行的时间,关闭时间为设备停止的时间,例如,设置开启1小时,关闭2小时,设备将会运行1小时,停止2小时,以此规律循环运行。"; } else if (self.timerType==3) { label.text=@"在定时自恢复模式下设定好定时时间段,如果在定时开启的时间段内手动关闭设备,10分钟以后会自动开启,如果在关闭的时间段内手动开启设备,10分钟以后会自动关闭。例如,循环泵设定了2段定时,分别为:时段一8:00-12:00,时段二15:00-20:00,如果我们在8:00-12:00之间手动将循环泵关闭,10分钟以后系统会自动将循环泵开启,如果在12:00-15:00之间手动将循环泵打开,10分钟以后系统会自动将循环泵关闭。如果不需要设备在定时时间段内自动恢复开启状态,可在定时模式下设定时间。"; } CGSize size= [label.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:(NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName: label.font} context:NULL].size; label.frame=CGRectMake((kScreenWidth-size.width)/2.0, kScreenHeight-NavigationBarHeight-size.height-88, size.width, size.height); [self.view addSubview:label]; } } -(void)clickCell:(TimerTypeTableViewCell *)cell button:(UIButton *)button timerType:(NSInteger )type { } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return (self.timerType==3)?2:1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (self.timerType==1) { NSArray *dataArr = [self.timeDic objectForKey:_titieArr[0]]; return dataArr.count; } else if(self.timerType==2) { return 1; } else if(self.timerType==3) { if (section==0) { NSArray *dataArr = [self.timeDic objectForKey:_titieArr[0]]; return dataArr.count; } else { return 0; } } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (self.timerType==1) { NSArray *dataArr = [self.timeDic objectForKey:_titieArr[indexPath.section]]; BOOL havePlus = [dataArr containsObject:plusflag]; if (havePlus && (indexPath.row == dataArr.count-1)) { PlusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:addCellid forIndexPath:indexPath]; cell.backImageView.image = [UIImage imageNamed:@"bottomRound"]; cell.addTimerBtn.tag = kBtnTag + indexPath.section; [cell.addTimerBtn addTarget:self action:@selector(addTimer:) forControlEvents:UIControlEventTouchUpInside]; return cell; }else { SetTimerCell *cell = [tableView dequeueReusableCellWithIdentifier:setTimerflag forIndexPath:indexPath]; cell.backImageView.image = [UIImage imageNamed:@"centerrect"]; cell.switchBtn.tag = kSwitchBtnTag + indexPath.row; [cell.switchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside]; XuanduoTimerGroup *group = [dataArr objectAtIndex:havePlus?indexPath.row+1:indexPath.row]; cell.model=group; if ([group isKindOfClass:[XuanduoTimerGroup class]]) { BOOL isOn = NO; if ([group.status isEqualToString:@"01"]) { isOn = YES; } [cell configTitleLabel:[NSString stringWithFormat:@"时段%@",group.groupNumber] timer:group.time switchStatus:isOn]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } else if([group isKindOfClass:[XuanduoCycleModel class]]) { XuanduoCycleModel*model=group; BOOL isOn = NO; if ([model.status isEqualToString:@"00"]) { isOn = YES; } [cell configTitleLabel:@"循环开关" timer:[NSString stringWithFormat:@"持续时长%d分钟",[dataContorl hexToTen:model.lastTime]] switchStatus:isOn]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } return cell; } } else if (self.timerType==2) {//循环开关 NSArray *dataArr = [self.timeDic objectForKey:_titieArr[indexPath.section]]; BOOL havePlus = [dataArr containsObject:plusflag]; { CycleTimerCell *cell = [tableView dequeueReusableCellWithIdentifier:cycleTimerflag forIndexPath:indexPath]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottomRound"]]; [cell.switchBtn setImage:[UIImage imageNamed:@"graybtn_big.png"] forState:UIControlStateNormal]; [cell.switchBtn setImage:[UIImage imageNamed:@"greenbtn_big"] forState:UIControlStateSelected]; [cell.switchBtn addTarget:self action:@selector(cycleModeAction:) forControlEvents:UIControlEventTouchUpInside]; cell.model=_cycleModel; if([_cycleModel isKindOfClass:[XuanduoCycleModel class]]) { cell.openTilmeLabel.text= [NSString stringWithFormat:@"开启:%@",[dataContorl hexStringToDateString:_cycleModel.openTime]]; cell.closeTimeLabel.text=[NSString stringWithFormat:@"关闭:%@",[dataContorl hexStringToDateString:_cycleModel.closeTime]]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if ([_cycleModel.openTime isEqualToString:@"000000"]&&[_cycleModel.closeTime isEqualToString:@"000000"]) { cell.switchBtn.selected=NO; } else { cell.switchBtn.selected=YES; } } else { } return cell; } } else if (self.timerType==3) { NSArray *dataArr = [self.timeDic objectForKey:_titieArr[indexPath.section]]; BOOL havePlus = [dataArr containsObject:plusflag]; if (havePlus && (indexPath.row == dataArr.count-1)) { PlusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:addCellid forIndexPath:indexPath]; cell.backImageView.image = [UIImage imageNamed:@"bottomRound"]; cell.addTimerBtn.tag = kBtnTag + indexPath.section; [cell.addTimerBtn addTarget:self action:@selector(addTimer:) forControlEvents:UIControlEventTouchUpInside]; return cell; }else { SetTimerCell *cell = [tableView dequeueReusableCellWithIdentifier:setTimerflag forIndexPath:indexPath]; cell.backImageView.image = [UIImage imageNamed:@"centerrect"]; cell.switchBtn.tag = kSwitchBtnTag + indexPath.row; [cell.switchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside]; XuanduoTimerGroup *group = [dataArr objectAtIndex:havePlus?indexPath.row+1:indexPath.row]; cell.model=group; if ([group isKindOfClass:[XuanduoTimerGroup class]]) { BOOL isOn = NO; if ([group.status isEqualToString:@"01"]) { isOn = YES; } [cell configTitleLabel:[NSString stringWithFormat:@"时段%@",group.groupNumber] timer:group.time switchStatus:isOn]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } else if([group isKindOfClass:[XuanduoCycleModel class]]) { XuanduoCycleModel*model=group; BOOL isOn = NO; if ([model.status isEqualToString:@"00"]) { isOn = YES; } [cell configTitleLabel:@"循环开关" timer:[NSString stringWithFormat:@"持续时长%d分钟",[dataContorl hexToTen:model.lastTime]] switchStatus:isOn]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } return cell; } } return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ CGFloat height=40; if (self.timerType==1||self.timerType==3) { NSArray *dataArr = [self.timeDic objectForKey:_titieArr[indexPath.section]]; BOOL havePlus = [dataArr containsObject:plusflag]; if (havePlus && (indexPath.row == dataArr.count-1)) { height=80; } } return height; } #pragma mark tableView 头视图和脚视图 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { CGFloat height=0; height=50; return height; } - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section == 0) { CGFloat height=0; height=section==0?50:40; UIImageView*imageView=[[UIImageView alloc]init]; imageView.frame=CGRectMake(0, 0, self.tableView.frame.size.width, height); imageView.image=[UIImage imageNamed:@"topround"]; UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, self.tableView.frame.size.width-20, height)]; label.text=_titieArr[section]; label.textColor=[UIColor blackColor]; [imageView addSubview:label]; return imageView; }else if(section == 1) { CGFloat height=0; height=50; UIImageView*imageView=[[UIImageView alloc]init]; imageView.frame=CGRectMake(0, 0, self.tableView.frame.size.width, height); imageView.image=[UIImage imageNamed:@"set_cellBack"]; UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, self.tableView.frame.size.width-20, height)]; label.text=@"全天自恢复模式"; label.textColor=[UIColor blackColor]; [imageView addSubview:label]; UIButton*swichBtn=[UIButton buttonWithType:UIButtonTypeCustom]; [swichBtn setBackgroundImage:[UIImage imageNamed:@"graybtn_big.png"] forState:UIControlStateNormal]; [swichBtn setBackgroundImage:[UIImage imageNamed:@"greenbtn_big"] forState:UIControlStateSelected]; if ([self.timerTypeModel.type isEqualToString:@"01"]) { swichBtn.selected=YES; } else { swichBtn.selected=NO; } [swichBtn addTarget:self action:@selector(allDayModesSwitchAction:) forControlEvents:UIControlEventTouchUpInside]; swichBtn.frame=CGRectMake(CGRectGetWidth(imageView.frame)-8-60, (CGRectGetHeight(imageView.frame)-33)/2.0, 60, 33); [imageView addSubview:swichBtn]; imageView.userInteractionEnabled=YES; return imageView; } return nil; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { CGFloat height=0; if (section==0) { height=40; } return height; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *oneHeader = [[UIView alloc] init]; oneHeader.backgroundColor = COLOR_MIAN; return oneHeader; } #pragma mark tableView 点击事件 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { _indexPath = indexPath; if(self.timerType==1||self.timerType==3) { SetTimerCell *cell = [self.tableView cellForRowAtIndexPath:_indexPath]; if ([cell isKindOfClass:[PlusTableViewCell class]]) { return; } else if([cell isKindOfClass:[SetTimerCell class]]&&[cell.model isKindOfClass:[XuanduoCycleModel class]]) { //恒温设置 _cycleModel=cell.model; _remindPic=[[remindCyclePic alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height) type:@"cycle"]; _remindPic.type=@"cycle"; _remindPic.frame=CGRectMake(0, 0, kScreenSize.width, kScreenSize.height); [self.view.window addSubview:_remindPic]; [_remindPic.sureBtn addTarget:self action:@selector(xuToremindCyclePicDone) forControlEvents:UIControlEventTouchUpInside]; [self huanShuiMainQueue]; } else { [self addTimer:nil]; } } else if(self.timerType==2) { CycleTimerCell *cell = [self.tableView cellForRowAtIndexPath:_indexPath]; if ([cell isKindOfClass:[PlusTableViewCell class]]) { return; } else if([cell isKindOfClass:[CycleTimerCell class]]&&[cell.model isKindOfClass:[XuanduoCycleModel class]]) { [self addCycleTimer:nil]; } else { } } } -(void)temperatureMainQueue{ NSArray*lowArr=[_mypicView.picArr objectAtIndex:0]; NSArray*heighArr=[_mypicView.picArr objectAtIndex:2]; for (NSInteger i=0; i 10) { [dataArr removeObjectAtIndex:0]; }else if (!havePlus && dataArr.count < 10){ [dataArr insertObject:plusflag atIndex:0]; } NSLog(@"dataarr.count = %ld",dataArr.count); } else if (self.timerType==2&&[lightNumber containsString:@"e"])//循环 { _cycleModel=[[XuanduoCycleModel alloc]init]; [Xuanduo2DataUtility readCycleSocketDataWithBackMsgModel:_cycleModel addWithBackStr:string1 type:self.currentdevice.type]; NSMutableArray *dataArr = [self.timeDic objectForKey:_titieArr[_selectSection]]; [dataArr removeAllObjects]; [dataArr addObject:_cycleModel]; } else if (self.timerType==3&&[lightNumber containsString:@"d"])//自恢复 { if (!_timerModel) { _timerModel = [[Xuanduo2TimerModel alloc] init]; _timerModel.groupModelArr = [NSMutableArray array]; }else { [_timerModel.groupModelArr removeAllObjects]; } [Xuanduo2DataUtility readTimerSocketDataWithBackMsgModel:_timerModel addWithBackStr:string1 type:self.currentdevice.type]; NSMutableArray *dataArr = [self.timeDic objectForKey:_titieArr[_selectSection]]; [dataArr removeAllObjects]; [dataArr addObject:plusflag]; [dataArr addObjectsFromArray:_timerModel.groupModelArr]; BOOL havePlus = [dataArr containsObject:plusflag]; if (havePlus && dataArr.count > 10) { [dataArr removeObjectAtIndex:0]; }else if (!havePlus && dataArr.count < 10){ [dataArr insertObject:plusflag atIndex:0]; } NSLog(@"dataarr.count = %ld",dataArr.count); }//读取定时模式 else if (self.timerType==3&&[lightNumber.uppercaseString containsString:@"F"]) { _timerTypeModel=[[XuanduoTimerTypeModel alloc]init]; _timerTypeModel.lightNumber=lightNumber; _timerTypeModel.type=[string1 substringWithRange:NSMakeRange(32, 2)]; } } else if (_selectSection<_titieArr.count && _selectSection>=0 && [readTimer isEqualToString:@"0119"]) { _cycleModel=[[XuanduoCycleModel alloc]init]; [Xuanduo2DataUtility readCycleSocketDataWithBackMsgModel:_cycleModel addWithBackStr:string1 type:self.currentdevice.type]; NSMutableArray *dataArr = [self.timeDic objectForKey:_titieArr[_selectSection]]; [dataArr removeAllObjects]; [dataArr addObject:_cycleModel]; } if ([readTimer isEqualToString:@"0108"] || [readTimer isEqualToString:@"0105"]) { [Xuanduo2DataUtility readSocketDataWithBackMsgModel:self.dataModel addWithBackData:data type:self.currentdevice.type]; [self refreshWithData:self.dataModel]; } [self.XuTohud hidmyHud]; [self.indicatorView stopAnimating]; [self.tableView reloadData]; } -(void)ifishDeviceLogInFail{ } -(void)ifishDeviceLogInSuccees{ [self readTimer];//读取定时器 } -(void)readTimer { //读取具体开关定时 ReadTimerModel*readModel=self.readMode; readModel.functionCode=@"16"; if (readModel) { NSString*readString=[NSString stringWithFormat:@"%@%@%@",readModel.description,readModel.selectorNumber,readModel.crc16str]; NSData*readData=[dataContorl stringToHexData:readString]; [[Socketsingleton sharedInstance] soketWriteData:readData]; [_indicatorView startAnimating]; } } -(void)readAllDayRecoveryMode { ReadTimerModel *readModel = [[ReadTimerModel alloc] init]; readModel.functionCode=@"16"; readModel.sendmacId =self.currentdevice.macAddress; readModel.resavemacId = self.currentdevice.macAddress; readModel.crc16str=@"0000"; readModel.selectorNumber=[self.lightNumber stringByReplacingOccurrencesOfString:@"d" withString:@"f"]; NSString*readString=[NSString stringWithFormat:@"%@%@%@",readModel.description,readModel.selectorNumber,readModel.crc16str]; NSData*readData=[dataContorl stringToHexData:readString]; [[Socketsingleton sharedInstance] soketWriteData:readData]; [_indicatorView startAnimating]; } -(void)ifishSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{ } @end