// // MonitorBootmView.m // Ifish // // Created by imac on 16/6/28. // Copyright © 2016年 lianxiang. All rights reserved. // #import "MonitorBootmView.h" #import "DeviceModel.h" #import "ifishdeviceListCell.h" #import "FishControlFirstCell.h" #import "FishControlSecondCell.h" #import "FishControlThreedViewCell.h" #import "FishControlFourthCell.h" #import "DeviceCameraModel.h" #import "manulswitchMSModel.h" #import "IfishDeviceInfo.h" #import "RunSunCHNameCustom.h" #import "RunSunCHControl.h" #import "RunSunControlName.h" #import "UserExtendataArchaver.h" #import "SongNuo86Name.h" #import "SongNuo128Name.h" #import "TwoControlName.h" #import "ThreeControlName.h" #import "FourControlName.h" #import "XuToControlName.h" #import "RuiMeiCHNameCustom.h" #import "RuiMeiCHControl.h" #import "Xuanduo2DataUtility.h" #import "NSString+Add.h" #import "Xuanduo2TimerModel.h" #define ktempTag 600 @implementation MonitorBootmView { NSTimer *_switchTimer; XuanduoCycleModel * _cycleModel; } -(id)initWithFrame:(CGRect)frame { self=[super initWithFrame:frame]; if (self) { //[self initTableDatasource]; frameHeight = frame.size.height; //fram 高度 viewFrame = frame; _isBindIfishDevice = [self testIsbindDeviceOrNot]; if ( _isBindIfishDevice) { //已建立关系 [self setCollectionFrame:frame]; [self initSoket]; }else{ //未建立关系 [self setTabViewWithFrame:frame]; } self.backgroundColor = [UIColor whiteColor]; } return self; } -(BOOL)testIsbindDeviceOrNot{ NSUserDefaults*userdefult=[NSUserDefaults standardUserDefaults]; NSString *cameraId=[userdefult valueForKey:@"OnShowCameraId"]; NSArray *devicCameraArr = [[DataCenter defaultDtacenter] valueForKey:@"devicamerArr"]; NSMutableArray *camerwIdArr = [[NSMutableArray alloc] init]; for (DeviceCameraModel *camera in devicCameraArr) { [camerwIdArr addObject:camera.cameraId]; } BOOL exst=[camerwIdArr containsObject:cameraId]; if (exst) { return YES; } return NO; } -(void)initTableDatasource{ NSArray*devicearray=[[DataCenter defaultDtacenter] valueForKey:@"deviceInfo"]; NSMutableArray * devicCameraArr = [[DataCenter defaultDtacenter] valueForKey:@"devicamerArr"]; NSMutableArray*arr=[[NSMutableArray alloc] init]; NSMutableArray *devceIdArr=[[NSMutableArray alloc] init]; for (DeviceModel *model in devicearray) { //判断是否授权 未授权不显示 而且是主控制 在列表中显示 if ([model.isBlacklist isEqual:[NSNull null]]) { model.isBlacklist = @"1"; } if (!model.isMaster) { model.isMaster = @"0"; } for (DeviceCameraModel *camera in devicCameraArr) { [devceIdArr addObject:camera.deviceId]; } BOOL isHaveRalate=[devceIdArr containsObject:model.deviceId]; if ([model.isBlacklist isEqualToString:@"0"]&&[model.isMaster isEqualToString:@"1"]&& !isHaveRalate) { //为关联摄像头 展示 [arr addObject:model]; } } self.ifishDeviceArr=arr; } -(void)setCollectionFrame:(CGRect)frame{ NSLog(@"控制界面"); self.device = [self getDeviceModel]; [self setDataArr]; self.flowLayout = [[UICollectionViewFlowLayout alloc] init]; self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,self.frame.size.width, self.frame.size.height) collectionViewLayout:self.flowLayout]; self.collectionView.backgroundColor =[UIColor whiteColor]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.showsVerticalScrollIndicator = NO; [self addSubview:self.collectionView]; [self.collectionView registerClass:[FishControlFirstCell class] forCellWithReuseIdentifier:@"FishControlFirstCell"]; [self.collectionView registerClass:[FishControlSecondCell class] forCellWithReuseIdentifier:@"FishControlSecondCell"]; [self.collectionView registerClass:[FishControlThreedViewCell class] forCellWithReuseIdentifier:@"FishControlThreedViewCell"]; [self.collectionView registerClass:[FishControlFourthCell class] forCellWithReuseIdentifier:@"FishControlFourthCell"]; } #pragma mark 获取设备model collection 数据源 /** * 只有建立关系后才会展示控制view */ -(DeviceModel *)getDeviceModel{ DeviceModel *devmodel=nil; NSUserDefaults*userdefult=[NSUserDefaults standardUserDefaults]; NSString *cameraId=[userdefult valueForKey:@"OnShowCameraId"]; NSString *deviceId =nil; NSArray *devicCameraArr = [[DataCenter defaultDtacenter] valueForKey:@"devicamerArr"]; //根据camerID 找到设备id for (DeviceCameraModel *model in devicCameraArr) { if ([model.cameraId isEqual:cameraId]) { deviceId= model.deviceId; } } //根据设备id获取对应model NSArray*devicearray=[[DataCenter defaultDtacenter] valueForKey:@"deviceInfo"]; for (DeviceModel *dmodel in devicearray) { if ([dmodel.deviceId isEqual:deviceId]) { devmodel = dmodel; } } return devmodel; } //-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ // // NSLog(@"开始滑动"); //} -(void)setTabViewWithFrame:(CGRect)frame{ [self initTableDatasource]; if ([self.ifishDeviceArr count]!=0) { // 存在水族箱设备 self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.scrollEnabled = YES; //self.tableView.backgroundColor = [UIColor greenColor]; self.tableView.backgroundColor = RGB(242, 242, 242); [self.tableView registerClass:[ifishdeviceListCell class] forCellReuseIdentifier:@"ifishdeviceListCell"]; [self addSubview:self.tableView]; // UIButton *sureBtn=[UIButton buttonWithType:UIButtonTypeCustom]; // sureBtn.frame =CGRectMake(10,frameHeight - frameHeight/7, kScreenSize.width -10*2, frameHeight/7); // [sureBtn setTitle:@"确定" forState:UIControlStateNormal]; // sureBtn.layer.masksToBounds = YES; // sureBtn.layer.cornerRadius =5; // sureBtn.backgroundColor = COLOR_LABEL_TITLE; // [sureBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; // [self.tableView addSubview:sureBtn]; }else{ UIButton *notbindBtn=[UIButton buttonWithType:UIButtonTypeCustom]; NSString *title=@"您暂无可用的水族箱或宠物笼,前往绑定。"; [notbindBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:title]; NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:12]; [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [title length])]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0 green:170.0/255.0 blue:218.0/255.0 alpha:1] range:NSMakeRange(13,5)]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,10)]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange([title length]-1,1)]; [notbindBtn setAttributedTitle:attributedString forState:UIControlStateNormal]; [notbindBtn sizeToFit]; notbindBtn.titleLabel.textAlignment = NSTextAlignmentCenter; CGFloat btnTitleW =[self widthForString:notbindBtn.titleLabel.text fontSize:20 andHeight:10]; CGFloat btnTitleH = [self heightForString:notbindBtn.titleLabel.text fontSize:20 andWidth:btnTitleW]; notbindBtn.frame =CGRectMake(self.frame.size.width/2 -btnTitleW/2,self.frame.size.height/2 -btnTitleH/2, btnTitleW, btnTitleH); [notbindBtn addTarget:self action:@selector(noCanUserDeviceClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:notbindBtn]; [self.tableView removeFromSuperview]; } } //无可用水族箱前往绑定按钮 -(void)noCanUserDeviceClick{ [self.MonitorBottomDelegate noCanuserDevicePush]; } #pragma mark -表格 //多少段 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 5; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ //最多绑定 5个水族箱设备 所以这里取最多情况 return frameHeight/7; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return frameHeight/7; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return frameHeight/7; } //头视图 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *headerView = [[UIView alloc] init]; //[headerView setBackgroundColor:XBGAlpha]; [headerView setBackgroundColor:RGB(242, 242, 242)]; UILabel *readLabel=[[UILabel alloc] init ]; readLabel.text = @"请选择一个水族箱或宠物笼,和摄像头建立关系"; NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:readLabel.text ]; CGFloat labelW=[self widthForString:readLabel.text fontSize:20 andHeight:10]; CGFloat labelH=[self heightForString:readLabel.text fontSize:20 andWidth:labelW]; //frameHeight/7 -labelH/2 readLabel.frame = CGRectMake(30,5,labelW,labelH); [readLabel setAttributedText:attributedString]; readLabel.textColor = [UIColor blackColor]; [headerView addSubview:readLabel]; [tableView setTableHeaderView:headerView]; return headerView; } //脚视图 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ UIView *footerView = [[UIView alloc] init]; //[headerView setBackgroundColor:XBGAlpha]; [footerView setBackgroundColor:RGB(242, 242, 242)]; UIButton *sureBtn=[UIButton buttonWithType:UIButtonTypeCustom]; sureBtn.frame =CGRectMake(10, 0, kScreenSize.width -10*2, frameHeight/7); [sureBtn setTitle:@"确定" forState:UIControlStateNormal]; sureBtn.layer.masksToBounds = YES; sureBtn.layer.cornerRadius =5; sureBtn.backgroundColor = COLOR_LABEL_TITLE; [sureBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [footerView addSubview:sureBtn]; [tableView setTableFooterView:footerView]; return footerView; } #pragma mark- 确定绑定点击事件 -(void)btnClick:(UIButton *)btn{ NSArray*devicearray=[[DataCenter defaultDtacenter] valueForKey:@"deviceInfo"]; NSMutableArray*arr=[[NSMutableArray alloc]init]; NSMutableArray *devceIdArr=[[NSMutableArray alloc] init]; NSMutableArray * devicCameraArr = [[DataCenter defaultDtacenter] valueForKey:@"devicamerArr"]; for (DeviceModel *model in devicearray) { //判断是否授权 未授权不显示 而且是主控制 在列表中显示 还要判断设备是否已经关联摄像头 已经关联不展示 if ([model.isBlacklist isEqual:[NSNull null]]) { model.isBlacklist = @"1"; } if (!model.isMaster) { model.isMaster = @"0"; } for (DeviceCameraModel *camera in devicCameraArr) { [devceIdArr addObject:camera.deviceId]; } BOOL isHaveRalate=[devceIdArr containsObject:model.deviceId]; if ([model.isBlacklist isEqualToString:@"0"]&&[model.isMaster isEqualToString:@"1"]&&!isHaveRalate) { //为关联摄像头 展示 [arr addObject:model]; } } NSInteger index=tapIndex.row; if (!index) { index = 0; } self.device = arr[index]; [self bidDeviceAddCamerawithdeviceId:self.device]; } -(void)bidDeviceAddCamerawithdeviceId:(DeviceModel *)device{ NSString *usId =[dataContorl dataControlGetUserIdInfo]; NSNumber *userId =(NSNumber *)usId ; [AFNOHeaderHttpTool deviceBindCameraWith:self.cameraId userId:userId deviceId:device success:^(id response) { NSDictionary *reDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil]; NSString *result= reDic[@"result"] ; if ([result isEqualToString:IFISHERROCODE]) { // 设置代理展示解绑按钮 [self.MonitorBottomDelegate cameraBindDeviceSuccess]; [self makeToast:@"设置成功"]; //存储摄像头设备关系数组 NSArray *devicCameraArr = [[DataCenter defaultDtacenter] valueForKey:@"devicamerArr"]; DeviceCameraModel *devicecamera=[[DeviceCameraModel alloc] init]; devicecamera.cameraId = self.cameraId; devicecamera.deviceId = device.deviceId; NSMutableArray *camerwIdArr = [[NSMutableArray alloc] initWithArray:devicCameraArr]; if (!camerwIdArr) { camerwIdArr =[[NSMutableArray alloc] init]; } [camerwIdArr addObject:devicecamera]; [[DataCenter defaultDtacenter]setValue:camerwIdArr forKey:@"devicamerArr"]; [self removeTable]; }else if([result isEqualToString:IFFISHFAIL]){ [self makeToast:@"设置失败"]; }else if([result isEqualToString:IFISHDEVICE_REMOVE]){ [self makeToast:@"设备不存在,错误码207"]; NSArray*deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"]; NSMutableArray *newArr = [NSMutableArray arrayWithArray:deviceArry]; for (DeviceModel *model in deviceArry) { if ([model.deviceId isEqual:device.deviceId]) { [newArr removeObject:model]; } } [[DataCenter defaultDtacenter] setValue: newArr forKey:@"deviceInfo"]; [self setTabViewWithFrame:viewFrame]; }else if([result isEqualToString:IFISHCAMERA_REMOVE]){ NSArray*cameraArry=[[DataCenter defaultDtacenter]valueForKey:@"cameraArr"]; NSMutableArray *newArr = [NSMutableArray arrayWithArray:cameraArry]; for (IfishCameraModel *model in cameraArry) { if ([model.cameraId isEqual:self.cameraId]) { [newArr removeObject:model]; } } [[DataCenter defaultDtacenter] setValue: newArr forKey:@"cameraArr"]; [self makeToast:@"摄像头不存在,错误码210"]; [self performSelector:@selector(cameraJieBang) withObject:self afterDelay:1.6]; } } failure:^(NSError *err) { [self makeToast:@"网络异常"]; }]; } -(void)removeTable{ // __weak typeof(self) wekself =self; [UIView animateWithDuration:2.0 animations:^{ [self.tableView removeFromSuperview]; } completion:^(BOOL finished) { [self setCollectionFrame: viewFrame]; [self initSoket]; }]; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { //cell.backgroundColor = COLOR_MIAN; } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row