ifish/Ifish/controllers/FishTinkController/maincontroller/CenterontrolControllers/RuSunTemp/RuSunHaveHeateViewController.m

433 lines
14 KiB
Objective-C

//
// RuSunHaveHeateViewController.m
// Ifish
//
// Created by imac on 17/1/4.
// Copyright © 2017年 lianxiang. All rights reserved.
//
#import "RuSunHaveHeateViewController.h"
#define RuSun_LIGR1TAG 2000031
#define RuSun_LIGR2TAG 2000032
#define RuSun_PUMPTAG 2000033
#define RuSun_JIARE 2000034
#import "RuSunProtocol.h"
#import "RusunHaveHeateSetViewController.h"
#import "RuSunBackMessageUtils.h"
#import "RuSunLightOrder.h"
#import "XuToWenDuPicview.h"
#import "RuSunTempNameViewController.h"
#import "RunSunControlName.h"
#import "UserExtendataArchaver.h"
@interface RuSunHaveHeateViewController ()
{
dispatch_queue_t _ruSunMainQueue;
}
@property(nonatomic,strong) RusunHaveHeateSetViewController *rusunHaveHeatSetVC;
@property(nonatomic,strong) RuSunProtocol *backmodel;
@property(nonatomic,strong) XuToWenDuPicview *wenduPicview;
@property(nonatomic,strong) RuSunTempNameViewController *changeNameVC;
@end
@implementation RuSunHaveHeateViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_backmodel=[[RuSunProtocol alloc] init];
[Socketsingleton sharedInstance].communiteDelegate=self;
_ruSunMainQueue = dispatch_get_main_queue();
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 initRuSunCenterViewBtn];
}
#pragma mark -转跳设置界面
-(void)RuSunCenterViewSetAction:(UIButton*)btn
{
RuSunProtocol*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"RuSunBackMessageInfo"];
if (backmodel) {
[self pushSetTimeVC];
}else{
[self deviceNotOnlineState];
[self showLableAction:@"设备已离线"];
[self.indicatorView stopAnimating];
}
}
-(void)RuSunCenterViewSetTap
{
RuSunProtocol*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"RuSunBackMessageInfo"];
if (backmodel) {
[self pushSetTimeVC];
}else{
[self deviceNotOnlineState];
[self showLableAction:@"设备已离线"];
[self.indicatorView stopAnimating];
}
}
-(void)pushSetTimeVC{
_rusunHaveHeatSetVC=[[RusunHaveHeateSetViewController alloc]init];
//_SetTimeVC.clientSocket= self.singletonSocket.clientSocket;
_rusunHaveHeatSetVC.currentdevice=self.currentDevice;
[self.navigationController pushViewController:_rusunHaveHeatSetVC animated:YES];
}
#pragma mark - 按钮初始化
-(void)initRuSunCenterViewBtn
{
NSArray*nomalarr=@[@"PFLight1_off.png",@"PFLight1_off.png",@"ChongLang_off.png",@"JiaReBang_off.png"];
CGFloat wspace=25;
CGFloat btnwidth= (kScreenSize.width-wspace * 5)/4;
for (int i=0; i<nomalarr.count; i++) {
self.button=[UIButton buttonWithType:UIButtonTypeCustom];
self.button.layer.masksToBounds=YES;
self.button.layer.cornerRadius=8;
self.button.tag=2000031+i;
self.button.frame=CGRectMake((wspace+i * (btnwidth+wspace)), (kScreenSize.height -180 + 10),btnwidth, btnwidth * 243/210);
//self.button.frame=CGRectMake1(wSpace1+(i%3)*(60+wSpace1), kScreenSize.height-80-64,60, 60);
[self.button setImage:[UIImage imageNamed:nomalarr[i]] forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(RuSunHeatControlBtnCilck:) forControlEvents:UIControlEventTouchUpInside];
[self.tableView addSubview:self.button];
if (i==0) {
_light1Button = self.button;
}else if (i==1){
_light2Button = self.button;
}else if (i==2){
_pumpButton = self.button;
}else if (i==3){
_heatBtn = self.button;
}
}
NSArray*arry=@[@"灯1",@"灯2",@"水泵",@"加热棒"];
for (int j=0; j<nomalarr.count; j++){
UILabel *buttonNameLabel=[[UILabel alloc]initWithFrame:CGRectMake((wspace+j * (btnwidth+wspace)) - 5,(kScreenSize.height-180+btnwidth+20),btnwidth +10, 20)];
buttonNameLabel.text=arry[j];
buttonNameLabel.font=[UIFont systemFontOfSize:10];
buttonNameLabel.textColor=[UIColor whiteColor];
buttonNameLabel.textAlignment=NSTextAlignmentCenter;
buttonNameLabel.textColor=[UIColor colorWithRed:101/255.0f green:103/255.0f blue:103/254.0f alpha:1];
buttonNameLabel.font = [UIFont boldSystemFontOfSize:13];
[self.tableView addSubview: buttonNameLabel];
if (j==0) {
_light1Name = buttonNameLabel;
}else if (j==1){
_light2Name = buttonNameLabel;
}else if (j==2){
_pumpName = buttonNameLabel;
}else if (j==3){
_heatName = buttonNameLabel;
}
}
[self initCHNames];
}
#pragma mark - 控制事件
-(void)RuSunHeatControlBtnCilck:(UIButton*)btn{
RuSunProtocol *backmodel=[[DataCenter defaultDtacenter]valueForKey:@"RuSunBackMessageInfo"];
if (backmodel) {
}else{
[self.indicatorView stopAnimating];
[self deviceNotOnlineState];
[self showLableAction:@"设备已离线"];
return;
}
RuSunLightOrder*lightOrder=[[RuSunLightOrder alloc]init];
lightOrder.sendmacId = self.currentDevice.macAddress;
lightOrder.resavemacId = self.currentDevice.macAddress;
NSString*switchString1=lightOrder.description;
if (btn.selected) {
lightOrder.switchBtn=@"00";//手动模式开
btn.selected=NO;
}else {
lightOrder.switchBtn=@"01";// 手动模式关
btn.selected=YES;
}
lightOrder.crc16Str=@"0000";// CRC16验证码
switch (btn.tag) {
case RuSun_LIGR1TAG:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"PFLight1_on.png"] forState:UIControlStateSelected];
lightOrder.lightNumber=@"01";// --灯1
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];
}
}
break;
case RuSun_LIGR2TAG:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"PFLight1_on.png"] forState:UIControlStateSelected];
lightOrder.lightNumber=@"02";// --灯2
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];
}
}
break;
case RuSun_PUMPTAG:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"ChongLang_on.png"] forState:UIControlStateSelected];
lightOrder.lightNumber=@"03";// --水泵
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];
}
}
break;
case RuSun_JIARE :
{
if (backmodel) {
_wenduPicview=[[XuToWenDuPicview 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];
[self ruSunJiaReMianQue];
}else{
[self showLableAction:@"设备已离线"];
}
}
break;
default:
break;
}
}
-(void)ruSunJiaReMianQue
{
RuSunProtocol *backmodel=[[DataCenter defaultDtacenter]valueForKey:@"RuSunBackMessageInfo"];
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)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];
}
#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.myhud hidmyHud];
if (_rusunHaveHeatSetVC.indicatorView) {
[_rusunHaveHeatSetVC.indicatorView stopAnimating];
}
//存储
[RuSunBackMessageUtils readSoketDataWithBaackMassgModel:_backmodel addWithBackData:data];
// 温度 等状态
dispatch_async(dispatch_get_main_queue(), ^{
[RuSunBackMessageUtils setTemperaturelabel:self.temperature addbubleImge:self.bubble WithBackData:data addWithBackMassageModel:_backmodel addWithTableview:self.tableView];
[RuSunBackMessageUtils resetRuSunButtonState:_light1Button addlight2:_light2Button addWaterPump:_pumpButton addJiaRe:_heatBtn withBackModel:_backmodel];
});
// 刷新设置界面
dispatch_async(dispatch_get_main_queue(), ^{
if (_rusunHaveHeatSetVC) {
_rusunHaveHeatSetVC.timeArr= [RuSunBackMessageUtils refreshRuSunTimerAddTemperatureWithBackMassageModel:_backmodel];
[_rusunHaveHeatSetVC.tableView reloadData];
}
});
}
//设备登陆成功
-(void)ifishDeviceLogInSuccees{
self.isconnect=YES;
NSLog(@"app登录成功");
}
-(void)ifishDeviceLogInFail{
[self endRefreshing];
[self.indicatorView stopAnimating];
dispatch_async(dispatch_get_main_queue(), ^{
[self deviceNotOnlineState];//设备离线
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -父类方法
-(void)shezhiMingCheng
{
_changeNameVC = [[RuSunTempNameViewController 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{
RunSunControlName* ruSunCH = [UserExtendataArchaver currentRuSunTemp:self.currentDevice.macAddress];
self.light1Name.text = ruSunCH.light1;
self.light2Name.text = ruSunCH.light2;
self.pumpName.text = ruSunCH.pump;
self.heatName.text = ruSunCH.JiaRe;
}
@end