ifish/Ifish/controllers/FishTinkController/maincontroller/CenterontrolControllers/RuiMei/RuiMeiViewController.m

599 lines
22 KiB
Objective-C

//
// RuiMeiViewController.m
// Ifish
//
// Created by Minghao Xue on 2018/4/14.
// Copyright © 2018年 lianlian. All rights reserved.
//
#define kPlace ([UIScreen mainScreen].bounds.size.width - btnWid*2) /4
#define RUNSUN_CH1 6000
#define RUNSUN_CH2 6001
#define RUNSUN_CH3 6002
#define RUNSUN_CH4 6003
#define RUNSUN_JARE 6004
#define btnWid 100
#import "RuiMeiViewController.h"
#import "RuiMeiProtocolModel.h"
#import "RuiMeiBackMsgUtils.h"
#import "XuToWenDuPicview.h"
#import "RuSunLightOrder.h"
#import "RuSunGuiDeng.h"
#import "RuiMeiSetViewController.h"
#import "RuiMeiCHControl.h"
#import "RuiMeiCHNameCustom.h"
#import "RuiMeiInputViewController.h"
#import "HaveHotCoolWenDuPicview.h"
@interface RuiMeiViewController ()<IfishCommuniteDelegate>
{
dispatch_queue_t _ruSunMainQueue;
}
@property(nonatomic,strong) RuiMeiProtocolModel *backmodel;
@property(nonatomic,strong) HaveHotCoolWenDuPicview *wenduPicview;
@property(nonatomic,strong) RuiMeiSetViewController *setVC;
//@property(nonatomic,strong) RunSunChangeNameViewController *changenameVC;
@property(nonatomic,strong) RuiMeiInputViewController *changenameVC;
@end
@implementation RuiMeiViewController
- (void)viewDidLoad {
[super viewDidLoad];
_backmodel=[[RuiMeiProtocolModel alloc] init];
[Socketsingleton sharedInstance].communiteDelegate=self;
_ruSunMainQueue = dispatch_get_main_queue();
// Do any additional setup after loading the view.
UITapGestureRecognizer*tapgestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(RuSunCenterViewSetTap)];
[self.setImag addGestureRecognizer:tapgestureRecognizer];
[self.setbackBtn addTarget:self action:@selector(RuSunCenterViewSetAction:) forControlEvents:UIControlEventTouchUpInside];
[self.SetTimeButton addTarget:self action:@selector(RuSunCenterViewSetAction:) forControlEvents:UIControlEventTouchUpInside];
[self initControlNormalBtn];
}
-(void)RuSunCenterViewSetTap
{
RuiMeiProtocolModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:DataCenterRuiMeiModelKey];
if (backmodel) {
[self pushSetTimeVC];
}else{
[self deviceNotOnlineState];
[self showLableAction:@"设备已离线"];
[self.indicatorView stopAnimating];
}
}
#pragma mark -转跳设置界面
-(void)RuSunCenterViewSetAction:(UIButton*)btn
{
RuiMeiProtocolModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:DataCenterRuiMeiModelKey];
if (backmodel) {
[self pushSetTimeVC];
}else{
[self deviceNotOnlineState];
[self showLableAction:@"设备已离线"];
[self.indicatorView stopAnimating];
}
}
-(void)pushSetTimeVC{
self.setVC=[[RuiMeiSetViewController alloc]init];
self.setVC.currentdevice=self.currentDevice;
NSString*stateString=[self.backmodel.customModel substringWithRange:NSMakeRange(2, 2)];
if ([stateString isEqualToString:@"01"]) {
self.setVC.customModeOn=YES;
}else{
self.setVC.customModeOn=NO;
}
[self.navigationController pushViewController:self.setVC animated:YES];
}
-(void)initControlNormalBtn
{
NSArray*nameArr=@[@"PFLight1_off.png",@"PFLight1_off.png",@"PFkilLlight_off.png",@"O2_off.png",@"JiaReBang"];
CGFloat btnControlWith = 70;
CGFloat btnControlPlace = (kScreenSize.width - 3*btnControlWith )/4;
for (int i = 0; i < 3; i ++) {
self.button = [[UIButton alloc] init] ;
self.button.frame = CGRectMake(btnControlPlace +i*(btnControlPlace + btnControlWith),kScreenSize.height - 3*btnControlPlace - 2 *btnControlWith -10 - 10 , btnControlWith, btnControlWith +11);
[self.button setImage:[UIImage imageNamed:nameArr[i]] forState:UIControlStateNormal];
//self.button.backgroundColor = [UIColor redColor];
self.button.tag=6000+i;
[self.button addTarget:self action:@selector(controlBtnCilck:) forControlEvents:UIControlEventTouchUpInside];
self.chName = [[UILabel alloc] initWithFrame:CGRectMake(btnControlPlace +i*(btnControlPlace + btnControlWith),kScreenSize.height - 3*btnControlPlace - 2 *btnControlWith -20 + btnControlWith -5 , btnControlWith, btnControlPlace - 5)];
//textLbl.backgroundColor = [UIColor blackColor];
self.chName.textColor = [UIColor colorWithRed:250/255.0f green:255/255.0f blue:255/254.0f alpha:1];
self.chName.textAlignment = NSTextAlignmentCenter;
NSArray *arr = @[@"S1",@"S2",@"UV/杀菌"];
self.chName.text = arr[i];
self.chName.font = [UIFont boldSystemFontOfSize:14];
[self.tableView addSubview:self.chName];
[self.tableView addSubview:self.button];
if (i==0) {
self.ch1Button = self.button;
self.ch1Name = self.chName;
}else if (i==1){
self.ch2Button = self.button;
self.ch2Name = self.chName;
}else if (i==2){
self.ch3Name = self.chName;
self.ch3Button = self.button;
}
}
//CGFloat btnControlPlace1 = (kScreenSize.width - 2*btnControlWith )/3;
for (int i = 0; i < 2; i ++) {
self.button = [[UIButton alloc] init] ;
self.button.frame = CGRectMake(btnControlPlace +i*(btnControlPlace + btnControlWith), kScreenSize.height - 2*btnControlPlace - btnControlWith -25 -5 , btnControlWith, btnControlWith + 11);
[self.button setBackgroundImage:[UIImage imageNamed:nameArr[i+3]]forState:UIControlStateNormal];
self.button.tag= 6003+i;
[self.button addTarget:self action:@selector(controlBtnCilck:) 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.chName = textLbl;
[self.tableView addSubview:textLbl];
[self.tableView addSubview:self.button];
if (i==0) {
self.ch4Button = self.button;
self.ch4Name = self.chName;
} else if (i==1){
self.heatBtn = self.button;
self.jiaReName = self.chName;
}
}
//重置 self.bubble 的 frame
CGFloat animViewHeight= kScreenSize.width/4;
if (kScreenSize.height==480) {
self.bubble.frame = CGRectMake((kScreenSize.width -(kScreenSize.width - 2 * kPlace)*2/3)/2, kPlace + 60 , (kScreenSize.width - 2 * kPlace)*2/3, (kScreenSize.width - 2 * kPlace)*2/3);
//(kScreenSize.width - 2 * kPlace - 200)*2/3
self.temperature.frame = CGRectMake(30, 70, (kScreenSize.width - 2 * kPlace - 100)*2/3, 50);
self.temperature.font = [UIFont fontWithName:@"Avenir Next Condensed" size:46];
self.dulabel.frame = CGRectMake( (kScreenSize.width - 2 * kPlace - 100)*2/3,74,50, kScreenSize.width - 2 * kPlace - 210);
self.dulabel.font = [UIFont fontWithName:@"Avenir Next Condensed" size:20];
self.animView.frame= CGRectMake(0, kPlace + 80 + (kScreenSize.width - 2 * kPlace) -animViewHeight/2 - 15-80, kScreenSize.width, animViewHeight);
self.notOnLineLabel.frame = CGRectMake(40, 60, kScreenSize.width - 2 * kPlace - 100, kScreenSize.width - 2 * kPlace - 200);
self.notOnLineLabel .font = [UIFont fontWithName:@"Avenir Next Condensed" size:15];
self.hotcolLabe = [[UILabel alloc] init];
self.hotcolLabe.frame = CGRectMake(0, CGRectGetMaxY(self.temperature.frame) + 10, self.bubble.frame.size.width, 15);
//self.hotcolLabe.textColor = JWUIColorFromRGB(fc6638);
self.hotcolLabe.textAlignment = NSTextAlignmentCenter;
self.hotcolLabe.font =[UIFont systemFontOfSize:13];
self.hotcolLabe.text = @"制冷加热";
[self.bubble addSubview:self.hotcolLabe];
self.hotcolTempLabe = [[UILabel alloc] init];
self.hotcolTempLabe.frame = CGRectMake(0, CGRectGetMaxY(self.hotcolLabe.frame) + 5, self.bubble.frame.size.width, 15);
self.hotcolTempLabe.font =[UIFont systemFontOfSize:14];
self.hotcolTempLabe.textAlignment = NSTextAlignmentCenter;
self.hotcolTempLabe.textColor = JWUIColorFromRGB(0x666666);
self.hotcolTempLabe.text = @"温度";
[self.bubble addSubview:self.hotcolTempLabe];
}else{
//kScreenSize.width - 2 * kPlace - 200
self.temperature.frame = CGRectMake(10, 80, kScreenSize.width - 2 * kPlace - 100,70);
self.temperature.font = [UIFont fontWithName:@"Avenir Next Condensed" size:60];
self.bubble.frame = CGRectMake((kScreenSize.width -(kScreenSize.width - 3 * kPlace ))/2, kPlace + 80 , kScreenSize.width - 3 * kPlace, kScreenSize.width - 3 * kPlace);
self.dulabel.frame = CGRectMake((kScreenSize.width - 2 * kPlace)/3*2 -30 , 90 ,50, 60);
self.dulabel.font = [UIFont fontWithName:@"Avenir Next Condensed" size:40];
self.animView.frame= CGRectMake(0, kPlace + 40 + (kScreenSize.width - 2 * kPlace) -animViewHeight/2 - 15, kScreenSize.width, animViewHeight);
CGFloat bubH=kScreenSize.width - 3 * kPlace;
self.notOnLineLabel.frame = CGRectMake(0,bubH/2-15, bubH, 30);
self.notOnLineLabel .font = [UIFont fontWithName:@"Avenir Next Condensed" size:20];
self.hotcolLabe = [[UILabel alloc] init];
self.hotcolLabe.frame = CGRectMake(0, CGRectGetMaxY(self.temperature.frame) + 10, self.bubble.frame.size.width, 15);
//self.hotcolLabe.textColor = JWUIColorFromRGB(fc6638);
self.hotcolLabe.textAlignment = NSTextAlignmentCenter;
self.hotcolLabe.font =[UIFont systemFontOfSize:13];
[self.bubble addSubview:self.hotcolLabe];
self.hotcolTempLabe = [[UILabel alloc] init];
self.hotcolTempLabe.frame = CGRectMake(0, CGRectGetMaxY(self.hotcolLabe.frame) + 5, self.bubble.frame.size.width, 15);
self.hotcolTempLabe.font =[UIFont systemFontOfSize:14];
self.hotcolTempLabe.textAlignment = NSTextAlignmentCenter;
self.hotcolTempLabe.textColor = JWUIColorFromRGB(0x666666);
self.hotcolTempLabe.text = @"温度";
[self.bubble addSubview:self.hotcolTempLabe];
}
[self initCHNames];
}
#pragma mark - 控制按钮事件
-(void)controlBtnCilck:(UIButton *)btn{
RuiMeiProtocolModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:DataCenterRuiMeiModelKey];
if (backmodel) {
}else{
[self.indicatorView stopAnimating];
[self deviceNotOnlineState];
[self showLableAction:@"设备已离线"];
return;
}
NSString *lightSate;
if (btn.selected) {
lightSate=@"00";
btn.selected=NO;
}else {
lightSate=@"01";
btn.selected=YES;
}
switch (btn.tag) {
case RUNSUN_CH1:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"PFLight1_on.png"] forState:UIControlStateSelected];
[self sendOffOnLinghtCommend:@"01" liaghtState:lightSate];
}
break;
case RUNSUN_CH2:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"PFLight1_on.png"] forState:UIControlStateSelected];
[self sendOffOnLinghtCommend:@"02" liaghtState:lightSate];
}
break;
case RUNSUN_CH3:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"PFkilLlight_on.png"] forState:UIControlStateSelected];
[self sendOffOnLinghtCommend:@"03" liaghtState:lightSate];
}
break;
case RUNSUN_CH4:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"O2_on.png"] forState:UIControlStateSelected];
[self sendOffOnLinghtCommend:@"04" liaghtState:lightSate];
}
break;
case RUNSUN_JARE:
{
if (backmodel) {
_wenduPicview=[[HaveHotCoolWenDuPicview alloc] init];
_wenduPicview.cateSwich.hidden = YES;
_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;
if ([backmodel.heatStickState isKindOfClass:[NSNull class]]||!backmodel.heatStickState) {
backmodel.heatStickState = @"00";
}
stateStr = [backmodel.heatStickState substringWithRange:NSMakeRange(0, 2)];
[_wenduPicview initCateBtnSate:stateStr];
[self ruSunJiaReMianQue];
}else{
[self showLableAction:@"设备已离线"];
}
}
break;
default:
break;
}
}
#pragma mark - 发送开关灯指令
-(void)sendOffOnLinghtCommend:(NSString *)linghtNumber liaghtState:(NSString *)state
{
//此款设备无运行模式
RuSunLightOrder*lightOrder=[[RuSunLightOrder alloc]init];
lightOrder.sendmacId = self.currentDevice.macAddress;
lightOrder.resavemacId = self.currentDevice.macAddress;
NSString*switchString1=lightOrder.description;
lightOrder.switchBtn = state;
lightOrder.crc16Str=@"0000";
lightOrder.lightNumber=linghtNumber;
NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,lightOrder.lightNumber,lightOrder.switchBtn,lightOrder.crc16Str];
NSData*manulData=[dataContorl stringToHexData:switchString2];
[ [Socketsingleton sharedInstance] soketWriteData:manulData];
if ([Socketsingleton sharedInstance].clientSocket.isConnected) {
//soket 未断开时显示
[self initHUD];
[self.myhud myhudstart];
}
}
#pragma mark -发送柜灯指令
-(void)sendGuiDengCommend:(NSString *)lightSate
{
RuSunGuiDeng*guidengModel=[[RuSunGuiDeng alloc]init];
guidengModel.resavemacId = self.currentDevice.macAddress;
guidengModel.sendmacId = self.currentDevice.macAddress;
guidengModel.crc16Str=@"0000";
guidengModel.onOff=lightSate;
NSString* guidengOrder=[NSString stringWithFormat:@"%@%@%@",guidengModel.description,guidengModel.onOff,guidengModel.crc16Str];
NSData*guidengData=[dataContorl stringToHexData:guidengOrder];
[[Socketsingleton sharedInstance] soketWriteData:guidengData];
if ([Socketsingleton sharedInstance].clientSocket.isConnected) {
//soket 未断开时显示
[self initHUD];
[self.myhud myhudstart];
}
}
#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)ruSunJiaReMianQue
{
RuiMeiProtocolModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:DataCenterRuiMeiModelKey];
UInt64 mac=[dataContorl hexToTen:backmodel.heatTemperature];
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];
dispatch_async(_ruSunMainQueue, ^{
for (NSInteger i=0; i<wenDuArr.count; i++) {
NSString*picString=wenDuArr[i];
if ([picString isEqualToString:stringFloat ] ) {
[_wenduPicview.pic selectRow:i inComponent:0 animated:NO];
}
}
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 登陆成功后收到服务端的回执之后,调用的代理方法
-(void)socketDidGetBackmsgData:(NSData *)data onsoket:(AsyncSocket *)sock{
[self.indicatorView stopAnimating];
self.temperature.hidden=NO;
//设备在线显示与设备离线相对应
self.dulabel.hidden=NO;
//设备在线显示与设备离线相对应 收到返回数据改变显示状态
self.notOnLineLabel.hidden = YES;
self.temperaturebackImg.hidden=NO;
self.hotcolTempLabe.hidden = NO;
self.hotcolLabe.hidden = NO;
[self.myhud hidmyHud];
if (self.setVC.indicatorView) {
[self.setVC.indicatorView stopAnimating];
}
//存储
[RuiMeiBackMsgUtils readSoketDataWithBaackMassgModel:_backmodel addWithBackData:data];
// 温度 等状态
dispatch_async(dispatch_get_main_queue(), ^{
if (self.setVC) {
NSString*stateString=[self.backmodel.customModel substringWithRange:NSMakeRange(2, 2)];
if ([stateString isEqualToString:@"01"]) {
self.setVC.customModeOn=YES;
}else{
self.setVC.customModeOn=NO;
}
}
[RuiMeiBackMsgUtils setTemperaturelabel:self.temperature addbubleImge:self.bubble WithBackData:data addWithBackMassageModel:_backmodel addWithTableview:self.tableView];
[RuiMeiBackMsgUtils resetRuSunCHState:_ch1Button ch2:_ch2Button ch3:_ch3Button ch4:_ch4Button andJiaRe:_heatBtn withBackModel:_backmodel ];
if (!_backmodel.heatStickState) {
_backmodel.heatStickState = @"00";
}
NSString*stateString=[_backmodel.heatStickState substringWithRange:NSMakeRange(0, IFISH_LIGHTBTN_BYTE)];
self.hotcolLabe.text = [NSString stringWithFormat:@"-室内温度-"];
self.hotcolLabe.textColor = JWUIColorFromRGB(0x38b9fc);
if (!_backmodel.heatStickState) {
_backmodel.heatStickState = @"00";
}
UInt64 mac=[dataContorl hexToTen:_backmodel.shiwen];
float TPlabel=mac/10;
int temp= (int)(TPlabel+0.5);
NSString *stringFloat = [NSString stringWithFormat:@"%d°C",temp];
NSLog(@"制冷加热温度%@",stringFloat);
self.hotcolTempLabe.text = stringFloat;
});
// 刷新设置界面
dispatch_async(dispatch_get_main_queue(), ^{
if (self.setVC) {
self.setVC.timeArr= [RuiMeiBackMsgUtils refreshRuSunTimerAddTemperatureWithBackMassageModel:_backmodel];
[self.setVC.tableView reloadData];
}
});
}
-(void)ifishDeviceLogInSuccees{
self.isconnect=YES;
NSLog(@"设备登录成功");
}
-(void)ifishDeviceLogInFail{
[self endRefreshing];
[self.indicatorView stopAnimating];
dispatch_async(dispatch_get_main_queue(), ^{
[self deviceNotOnlineState];//设备离线
self.hotcolTempLabe.hidden = YES;
self.hotcolLabe.hidden = YES;
});
}
#pragma mark -父类方法
-(void)shezhiMingCheng
{
_changenameVC = [[RuiMeiInputViewController 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{
RuiMeiCHControl * ruSunCH = [RuiMeiCHNameCustom currentdevice:self.currentDevice.macAddress];
self.ch1Name.text = ruSunCH.ch1name;
self.ch2Name.text = ruSunCH.ch2name;
self.ch3Name.text = ruSunCH.ch3name;
self.ch4Name.text = ruSunCH.ch4name;
self.jiaReName.text = ruSunCH.jiaRe;
}
#pragma mark - 制冷制热指令
-(void)didSelectCoolOrHeat:(NSString *)hotCool{
// //由s5 控制 原理同灯
// RuSunLightOrder*lightOrder=[[RuSunLightOrder alloc]init];
// lightOrder.sendmacId = self.currentDevice.macAddress;
// lightOrder.resavemacId = self.currentDevice.macAddress;
// NSString*switchString1=lightOrder.description;
// lightOrder.switchBtn = hotCool;
// lightOrder.crc16Str=@"0000";
// lightOrder.lightNumber=@"05";
//
// NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,lightOrder.lightNumber,lightOrder.switchBtn,lightOrder.crc16Str];
// NSData*manulData=[dataContorl stringToHexData:switchString2];
// [ [Socketsingleton sharedInstance] soketWriteData:manulData];
// if ([Socketsingleton sharedInstance].clientSocket.isConnected) {
// //soket 未断开时显示
// [self initHUD];
// [self.myhud myhudstart];
// }
}
@end