// // Xuanduo2fController.m // Ifish // // Created by Alex on 2019/4/13. // Copyright © 2019 lianlian. All rights reserved. // #import "Xuanduo2fController.h" #import "LXPopView.h" #import "DXPopover.h" #import "DeviceCameraModel.h" #import "XuToControlNameViewController.h" #import "IfishDataUnity.h" #import "CreatErWeiMaController.h" #import "SongBaoViewController.h" #import "XuToControlName.h" #import "UserExtendataArchaver.h" #import "UIButton+WebCache.h" #import "NSString+Add.h" #import "XuanduoHuliModel.h" #import "Xuanduo2DataUtility.h" #import "HaveHotCoolWenDuPicview.h" #import "Xuanduo2SettingController.h" #import "RuSunLightOrder.h" #import "JHRefreshAmazingAniView.h" #import "JHRefresh.h" #import "searchDeviceModel.h" #import "PetStoresViewController.h" #import "UIImage+WaterMark.h" #import "XuanduoSettingController.h" #define kBtnTag 100 @interface Xuanduo2fController () @property(nonatomic,strong) DXPopover *popover; @property (weak, nonatomic) IBOutlet UIView *positionView; @property (weak, nonatomic) IBOutlet UILabel *shajunLight; @property (weak, nonatomic) IBOutlet UILabel *zengyangLabel; @property (weak, nonatomic) IBOutlet UILabel *light1Label; @property (weak, nonatomic) IBOutlet UILabel *light2Label; @property (weak, nonatomic) IBOutlet UILabel *zaolangLabel; @property (weak, nonatomic) IBOutlet UILabel *huliLabel; @property (weak, nonatomic) IBOutlet UILabel *xunhuanLabel; @property (weak, nonatomic) IBOutlet UILabel *jiareLabel; @property (weak, nonatomic) IBOutlet UIButton *brandBtn; @property (weak, nonatomic) IBOutlet UILabel *brandName; @property (nonatomic ,strong) Xuanduo2Model* dataModel; @property (weak, nonatomic) IBOutlet UILabel *tempLabel; @property (weak, nonatomic) IBOutlet UILabel *tipLabel; @property (nonatomic, strong) NSTimer *switchTimer; @property (weak, nonatomic) IBOutlet UIButton *uvLight; @property (weak, nonatomic) IBOutlet UIButton *gasPumpLight; @property (weak, nonatomic) IBOutlet UIButton *light1; @property (weak, nonatomic) IBOutlet UIButton *light2; @property (weak, nonatomic) IBOutlet UIButton *waveLight; @property (weak, nonatomic) IBOutlet UIButton *huliLight; @property (weak, nonatomic) IBOutlet UIButton *waterLight; @property (weak, nonatomic) IBOutlet UIButton *heatLight; @property(nonatomic,copy) UIActivityIndicatorView *indicatorView; @property(nonatomic,strong) HaveHotCoolWenDuPicview *wenduPicview; @property(nonatomic,strong) MyHud *XuTohud; @property (weak, nonatomic) IBOutlet UILabel *offLineLabel; @property (weak, nonatomic) IBOutlet UIScrollView *scrollview; @property(nonatomic)BOOL isRefeshing; @property(nonatomic) BOOL isconnect;// socket 是否连接 @property (weak, nonatomic) IBOutlet UIView *contentView; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @property(nonatomic,strong) XuanduoCycleModel *cycleModel; @property(nonatomic,assign) NSInteger heartCount; @property (weak, nonatomic) IBOutlet UILabel *waterLever; @end @implementation Xuanduo2fController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [Socketsingleton sharedInstance].communiteDelegate = self; self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName :[UIColor blackColor]}; if ([[Socketsingleton sharedInstance].clientSocket isConnected]&&[self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO3F]) { [self readCycleWithNumber:@"03"]; self.heartCount=0; } } - (void)viewDidLoad { [super viewDidLoad]; [self setup]; [self connect]; [self networknotify]; // Do any additional setup after loading the view from its nib. } -(void)networknotify { AFNetworkReachabilityManager*networkManager=[AFNetworkReachabilityManager sharedManager]; [networkManager startMonitoring]; [networkManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { if (![Socketsingleton sharedInstance].clientSocket.isConnected) { [[Socketsingleton sharedInstance] cutOffSocket]; [self InitSocket]; } }]; } - (void)setup{ self.title = self.currentDevice.showName; //右侧菜单 UIImage *MyEquipmentImage = [UIImage imageNamed:@"right"]; UIButton *rightPopButton = [UIButton buttonWithType:UIButtonTypeCustom]; [rightPopButton setImage:MyEquipmentImage forState:UIControlStateNormal]; rightPopButton.frame=CGRectMake(kScreenWidth-60,0,60,44); [rightPopButton setImageEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)]; [rightPopButton addTarget:self action:@selector(presentPopView) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightPopButton]; self.navigationItem.rightBarButtonItem =rightItem; //品牌展示 [_brandBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kGetLogUrl,self.currentDevice.logo]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"brandLogo_hold"]]; _brandName.text = self.currentDevice.brandName; self.dataModel=nil; [self initCHNames]; [Socketsingleton sharedInstance].communiteDelegate = self; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tempSwitch)]; // [self.tempLabel addGestureRecognizer:tap]; [self creatRefreshView]; //宠物笼设备 if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO3F]) { self.imageView.image=[UIImage imageNamed:@"pet"]; self.waterLightTop.constant=55; //照明 [self.waterLight setBackgroundImage:[UIImage imageNamed:@"照明关"] forState:UIControlStateNormal]; [self.waterLight setBackgroundImage:[UIImage imageNamed:@"照明开"] forState:UIControlStateSelected]; // self.xunhuanLabel.text=@"照明"; //换气 [self.gasPumpLight setBackgroundImage:[UIImage imageNamed:@"换气关"] forState:UIControlStateNormal]; [self.gasPumpLight setBackgroundImage:[UIImage imageNamed:@"换气开"] forState:UIControlStateSelected]; // self.zengyangLabel.text=@"换气"; //杀菌 [self.light1 setBackgroundImage:[UIImage imageNamed:@"杀菌关"] forState:UIControlStateNormal]; [self.light1 setBackgroundImage:[UIImage imageNamed:@"杀菌开"] forState:UIControlStateSelected]; // self.light1Label.text=@"杀菌"; //恒温 [self.light2 setBackgroundImage:[UIImage imageNamed:@"恒温关"] forState:UIControlStateNormal]; [self.light2 setBackgroundImage:[UIImage imageNamed:@"恒温开"] forState:UIControlStateSelected]; // self.light2Label.text=@"恒温"; self.tipLabel.text=@"宠物笼温度"; self.controlBtn.hidden=NO; self.cameraBtn.hidden=NO; self.waveLight.hidden=YES; self.zaolangLabel.hidden=YES; self.uvLight.hidden=YES; self.shajunLight.hidden=YES; self.huliLight.hidden=YES; self.huliLabel.hidden=YES; self.heatLight.hidden=YES; self.jiareLabel.hidden=YES; self.heartCount=0; } else if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO4F]||[self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO5F]) { self.imageView.image=[UIImage imageNamed:@"pet"]; self.controlBtn.hidden=NO; self.cameraBtn.hidden=NO; self.heartCount=0; } } - (void)connect { if ([self.currentDevice.isBlacklist isEqualToString:@"0"]) { [self InitSocket];//允许请求数据 } } #pragma mark - 刷新 -(void)creatRefreshView{ self.scrollview.delegate = self; __weak typeof (self)weakSelf=self; [weakSelf.scrollview addRefreshHeaderViewWithAniViewClass:[JHRefreshAmazingAniView class] beginRefresh:^{ if (weakSelf.isRefeshing) { return ; } weakSelf.isRefeshing=YES; if (weakSelf.isconnect) { searchDeviceModel*xinxiModel=[[searchDeviceModel alloc]init]; xinxiModel.resavemacId = self.currentDevice.macAddress; xinxiModel.sendmacId = self.currentDevice.macAddress; NSString*requestStr=xinxiModel.description; NSLog(@"REQUEST = %@",requestStr); NSData*data1=[dataContorl stringToHexData:requestStr]; [[Socketsingleton sharedInstance].clientSocket writeData:data1 withTimeout:-1 tag:0]; [[Socketsingleton sharedInstance].clientSocket readDataWithTimeout:-1 tag:0]; }else{// socket中断 [[Socketsingleton sharedInstance] cutOffSocket]; [weakSelf InitSocket]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf endRefreshing]; }); } }]; } -(void)endRefreshing{ if (self.isRefeshing) { self.isRefeshing=NO; [self.scrollview headerEndRefreshingWithResult:JHRefreshResultSuccess]; } } - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGFloat offset = scrollView.contentOffset.y; if (offset >0) { [self.scrollview setContentOffset:CGPointMake(0, 0)]; } } #pragma mark - 连接socket -(void)InitSocket{ NSLog(@"******%d",[Socketsingleton sharedInstance].clientSocket.isConnected); if (![Socketsingleton sharedInstance].clientSocket.isConnected) { //[[Socketsingleton sharedInstance]; } // 切换设备 先断开然后重连 [Socketsingleton sharedInstance].macAddress = self.currentDevice.macAddress; [[Socketsingleton sharedInstance] socketConnectHost]; } #pragma mark 通信监听communiteDelegate //当已经和服务端断开连接后调用该代理方法 //断线重连功能需要在这里进行 -(void)ifishSocketDidDisconnect:(AsyncSocket *)sock{ _isconnect=NO; [self endRefreshing]; } -(void)ifishSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err{ // NSLog(@"soket错误断开"); // NSLog(@"存留数据%@",[sock unreadData]); _offLineLabel.hidden = NO; self.waterLever.hidden=YES; } -(BOOL)ifishSocketWillConnect:(AsyncSocket *)sock{ // NSLog(@"将要链接"); return YES; } #pragma mark 当已经与服务端建立连接后调用的代理方法 -(void)ifishSocket:(AsyncSocket *)sock ifishSocketdidConnectToHost:(NSString *)host port:(UInt16)port{ //已建立连接 登陆指令同意在soket单例中 _isconnect=YES; } //发送完成处理 -(void)ifishSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{ [self endRefreshing]; } #pragma mark 登陆成功后收到服务端的回执之后,调用的代理方法 //父类里面不执行此方法 接收服务器返回信息 在子类中做处理 //communiteDelegate -(void)socketDidGetBackmsgData:(NSData *)data onsoket:(AsyncSocket *)sock{ //[_HUD hide:YES]; [self.XuTohud hidmyHud]; [self endRefreshing]; NSString*string1=[dataContorl dataToHexString:data]; NSLog(@"return str == %@",string1); NSString *readTimer = [string1 substringToIndex:4]; if ([readTimer isEqualToString:@"0119"]) { _cycleModel=[[XuanduoCycleModel alloc]init]; [Xuanduo2DataUtility readCycleSocketDataWithBackMsgModel:_cycleModel addWithBackStr:string1 type:self.currentDevice.type]; if (_cycleModel.lastTime.length) { UInt64 mac1=[dataContorl hexToTen:_cycleModel.lastTime]; //float TPlabel=mac/10+(mac%10)*0.1; float temp1 = mac1; NSString *stringTemp = [NSString stringWithFormat:@"%f℃",temp1]; UIImage*uvNomal=[UIImage imageNamed:@"杀菌关"]; //关闭时不显示时间 // uvNomal=[uvNomal imageWaterMarkWithString:stringTemp rect:CGRectMake(140, 60, 60, 60) attribute:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:30]}]; [self.light1 setImage:uvNomal forState:UIControlStateNormal]; UIImage*uvsel=[UIImage imageNamed:@"杀菌开"]; uvsel=[uvsel imageWaterMarkWithString:stringTemp rect:CGRectMake(140, 60, 60, 60) attribute:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:30]}]; // [self.light1 setImage:uvsel forState:UIControlStateSelected]; } } else { [Xuanduo2DataUtility readSocketDataWithBackMsgModel:self.dataModel addWithBackData:data type:self.currentDevice.type]; [self setTemperature]; if (self.dataModel) { self.heartCount++; } // if (self.dataModel&&[self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO3F]) { Xuanduo3fModel*model=(Xuanduo3fModel*)_dataModel; NSString *stateString1 = [model.uvLamp substringWithRange:NSMakeRange(2, 2)]; //关闭或者首次时杀菌取设置的值,之后才是心跳的 if ([stateString1 isEqualToString:@"00"]) { if (_cycleModel.lastTime.length) { model.vuPHstatus=_cycleModel.lastTime; } else //未收到设置值不显示 { model.vuPHstatus=@"0000"; } } } } if (self.dataModel) { _offLineLabel.hidden = YES; }else { _offLineLabel.hidden = NO; self.waterLever.hidden=YES; } [Xuanduo2DataUtility resetNewXuanduoButtonState:self.uvLight gasPumpLight:self.gasPumpLight light1:self.light1 light2:self.light2 waveLight:self.waveLight huliLight:self.huliLight waterLight:self.waterLight heatLight:self.heatLight withModel:self.dataModel type:self.currentDevice.type]; if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO2F]) { if (self.dataModel.waterLevel.length&&([self.dataModel.waterLevel isEqualToString:@"01"]||[self.dataModel.waterLevel isEqualToString:@"02"])) { self.waterLever.text=@"水位报警"; self.waterLever.textColor=[UIColor redColor]; self.waterLever.hidden=NO; } else { self.waterLever.hidden=YES; } } //杀菌灯的状态 } -(void)readCycleWithNumber:(NSString*)num { baseModel*readModel=[[baseModel alloc]init]; readModel.sendmacId =self.currentDevice.macAddress; readModel.resavemacId =self.currentDevice.macAddress; readModel.functionCode=@"19"; readModel.massagelegth=@"12"; NSString*readString=[NSString stringWithFormat:@"%@%@%@",readModel.description,num,@"0000"]; NSData*readData=[dataContorl stringToHexData:readString]; [[Socketsingleton sharedInstance] soketWriteData:readData]; } -(void)ifishDeviceLogInFail{ } -(void)ifishDeviceLogInSuccees{ if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO3F]) { [self readCycleWithNumber:@"03"]; self.heartCount=0; } } -(void)ifishSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{ } #pragma mark -- actions -- - (IBAction)cameraBtnClick:(id)sender { NSMutableArray *guanxiiArr= [[DataCenter defaultDtacenter] valueForKey:@"devicamerArr"]; NSString*cameraId=nil; for (DeviceCameraModel *model in guanxiiArr) { if (model.deviceId == self.currentDevice.deviceId ) { cameraId=model.cameraId; break; } } if (cameraId) { IfishCameraModel *camera; NSArray*array=[[DataCenter defaultDtacenter]valueForKey:@"cameraArr"]; for (IfishCameraModel*model in array) { if ([model.cameraId isEqualToString:cameraId]) { camera=model; break; } } if (camera) { [[Socketsingleton sharedInstance] cutOffSocket]; //进入摄像头页面 IfishP2PMonitorController *p2pVC=[[IfishP2PMonitorController alloc] init]; p2pVC.contact = camera; [self.navigationController pushViewController:p2pVC animated:YES]; } else { [self.view makeToast:@"暂无关联的摄像头"]; } } else { [self.view makeToast:@"暂无关联的摄像头"]; } } - (IBAction)controlBtnClick:(id)sender { FormatTankAddCamera*model=[[DataCenter defaultDtacenter] valueForKey:@"currentStore"]; if (model) { PetStoresViewController*store=[[PetStoresViewController alloc]init]; store.store=model; ; self.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:store animated:YES]; } } - (IBAction)btnActions:(UIButton *)btn { if (self.dataModel.gasPump) { }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"; }else {// 关 lightModel.switchBtn = @"01"; } lightModel.crc16Str=@"0000";// CRC16验证码 switch (btn.tag) { case kBtnTag: // 循环泵 { if([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO3F]) { [self changLightStatus:lightModel withLightNumber:@"01" dataStr:switchString1]; } else{ if (btn.selected) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认关闭循环泵?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self changLightStatus:lightModel withLightNumber:@"04" dataStr:switchString1]; }]; [alertController addAction:cancelAction]; [alertController addAction:sureAction]; [self.navigationController presentViewController:alertController animated:YES completion:nil]; }else { [self changLightStatus:lightModel withLightNumber:@"04" dataStr:switchString1]; } } } break; case kBtnTag + 1: //增氧 { NSString*light=@"01"; if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO3F]) { light=@"02"; } [self changLightStatus:lightModel withLightNumber:light dataStr:switchString1]; } break; case kBtnTag + 2: //灯光1 { NSString*light=@"02"; if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO3F]) { light=@"03"; } [self changLightStatus:lightModel withLightNumber:light dataStr:switchString1]; } break; case kBtnTag + 3: //灯光2 { NSString*light=@"03"; if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO3F]) { light=@"04"; } [self changLightStatus:lightModel withLightNumber:light dataStr:switchString1]; } break; case kBtnTag + 4: //造浪泵 { [self changLightStatus:lightModel withLightNumber:@"06" dataStr:switchString1]; } break; case kBtnTag + 5: //杀菌灯 { [self changLightStatus:lightModel withLightNumber:@"05" dataStr:switchString1]; } break; case kBtnTag + 6: //护理灯 单独处理 { XuanduoHuliModel *huliModel = [[XuanduoHuliModel alloc] init]; huliModel.sendmacId = self.currentDevice.macAddress; huliModel.resavemacId = self.currentDevice.macAddress; NSString *switchString = huliModel.description; if (btn.selected) {//开 huliModel.switchBtn = @"00"; }else {// 关 huliModel.switchBtn = @"01"; } huliModel.crc16Str=@"0000";// CRC16验证码 NSString *switchString2 = [NSString stringWithFormat:@"%@%@%@",switchString,huliModel.switchBtn,huliModel.crc16Str]; NSData *manulData=[dataContorl stringToHexData:switchString2]; [[Socketsingleton sharedInstance] soketWriteData:manulData]; if ([Socketsingleton sharedInstance].clientSocket.isConnected) { //soket 未断开时显示 [self creatXuToindicaterView]; [self.XuTohud myhudstart]; } } break; case kBtnTag + 7: //加热棒 { if (self.dataModel.gasPump) { _wenduPicview=[[HaveHotCoolWenDuPicview 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]; _wenduPicview.wenDuPicviewDelegate = self; NSString *stateStr =nil; NSString*status; if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO2F]) { status=((Xuanduo2Model*)self.dataModel).status; } else if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO3F]) { status=((Xuanduo3fModel*)self.dataModel).constTmep; } if ([status isKindOfClass:[NSNull class]]||!status ) { status = @"0000"; } stateStr = [status substringWithRange:NSMakeRange(2, 2)]; [_wenduPicview initCateBtnSate:stateStr]; [self heatAction]; }else{ [self showLableAction:@"设备已离线"]; } } break; 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)heatAction { UInt64 mac=0; NSLog(@"%llu",mac); if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO2F]) { Xuanduo2Model*model=(Xuanduo2Model*)self.dataModel; mac=[dataContorl hexToTen:model.heatingTemperature]; } else if ([self.currentDevice.type isEqualToString:DECICE_TYPE_XUANDUO3F]) { Xuanduo3fModel*model=(Xuanduo3fModel*)self.dataModel; mac=[dataContorl hexToTen:model.heatingTemperature]; } NSLog(@"%llu",mac); float TPlabel=mac/10; int temp= (int)(TPlabel+0.5); NSString *stringFloat = [NSString stringWithFormat:@"%d",temp]; NSLog(@"%@",stringFloat); NSArray*wenDuArr=[_wenduPicview.picArr objectAtIndex:0]; for (NSInteger i=0; i