ifish/Ifish/controllers/FishTinkController/maincontroller/CenterontrolControllers/TwoControl/TwoControlCenterController.m

650 lines
23 KiB
Objective-C

//
// TwoControlCenterController.m
// Ifish
//
// Created by imac on 16/4/8.
// Copyright © 2016年 imac. All rights reserved.
//
#import "TwoControlCenterController.h"
#import "BackmassegeModel.h"
#import "manulswitchMSModel.h"
#define TWOC_PUMP 211
#define TWOC_LIGHT 210
#import "TwoControlSetTimeViewController.h"
#import "TwoControlnoneTimerViewController.h"
#import "TwoControlNinetyninebiteViewController.h"
#import "TwoControlName.h"
#import "TwoControlNameViewController.h"
#import "UserExtendataArchaver.h"
//两控 返回消息类型
typedef NS_ENUM(NSInteger,TwoControBackMsgTape){
//两控四定时器 返回消息类型57字节
TwoControlBackmsgTapeFiftySevenByteAddFourTimer,
//两控零定时器 返回消息类型57字节
TwoControlBackmsgTapeFiftySevenByteAddZoreTimer,
// 零控四定时器 返回消息类型 99 字节
TwoControlBackmsgTapeNinetyNineByteAddFourTimer
};
@interface TwoControlCenterController ()
@property (nonatomic ,assign) TwoControBackMsgTape BackMsgTape;
@property(nonatomic,strong)BackmassegeModel*backmodel;
@property(nonatomic,strong)TwoControlBackmsgmodel*twoControlBackModel;
@property(nonatomic,strong) TwoControlSetTimeViewController *twoControlSetTimeVC;
@property(nonatomic,strong) TwoControlnoneTimerViewController *twocontrolNonetimerVC;
@property(nonatomic,strong) TwoControlNinetyninebiteViewController * twoControlNinetyninebiteVC;
@property (nonatomic,strong) TwoControlNameViewController *changeNameVC ;
@end
@implementation TwoControlCenterController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[Socketsingleton sharedInstance].communiteDelegate=self;
_backmodel=[[BackmassegeModel alloc]init];
_twoControlBackModel=[[TwoControlBackmsgmodel alloc]init];
//主界面按钮事件分离
[self.setbackBtn addTarget:self action:@selector(twoControlSetBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.SetTimeButton addTarget:self action:@selector(twoControlSetBtnAction:) forControlEvents:UIControlEventTouchUpInside];
UITapGestureRecognizer*tapgestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(twoCenterViewSetTap)];
[self.setImag addGestureRecognizer:tapgestureRecognizer];
UITapGestureRecognizer*tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TwoControlViewclockViewClick)];
self.clockView.userInteractionEnabled=YES;
[self.clockView addGestureRecognizer:tapGesture];
[self initTwoControlButton];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 两控界面设置btn点击事件
-(void)twoControlSetBtnAction:(UIButton*)btn{
NSLog(@"点击了设置");
if ([self.binddeviceArr count]==0) {
[self pushWiFiConnectVC];
}else{
[self pushSetTimeVC];
}
}
#pragma mark - 设置图片点击事件
-(void)twoCenterViewSetTap{
NSLog(@"点击了图片");
if ([self.binddeviceArr count]==0) {
[self pushWiFiConnectVC];
}else{
[self pushSetTimeVC];
}
}
#pragma mark - 锁屏图片点击事件
-(void)TwoControlViewclockViewClick{
[self pushSetTimeVC];
}
#pragma mark - 转跳设置界面
-(void)pushSetTimeVC{
switch (self.BackMsgTape) {
case TwoControlBackmsgTapeNinetyNineByteAddFourTimer:
{
BackmassegeModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackInfo"];
if (backmodel) {
_twoControlNinetyninebiteVC=[[TwoControlNinetyninebiteViewController alloc]init];
_twoControlNinetyninebiteVC.currentdevice=self.currentDevice;
[self.navigationController pushViewController:_twoControlNinetyninebiteVC animated:YES];
//self.navigationBarHidden=NO;
}else{
[self showLableAction:@"设备已离线"];
[self deviceNotOnlineState];
[self.indicatorView stopAnimating];
}
}
break;
case TwoControlBackmsgTapeFiftySevenByteAddFourTimer:
{
TwoControlBackmsgmodel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackTwoControlInfo"];
if (backmodel) {
_twoControlSetTimeVC=[[TwoControlSetTimeViewController alloc]init];
_twoControlSetTimeVC.currentdevice=self.currentDevice;
[self.navigationController pushViewController:_twoControlSetTimeVC animated:YES];
//self.navigationBarHidden=NO;
}else{
[self.indicatorView stopAnimating];
[self deviceNotOnlineState];
[self showLableAction:@"设备已离线"];
}
}
break;
case TwoControlBackmsgTapeFiftySevenByteAddZoreTimer:
{
TwoControlBackmsgmodel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackTwoControlInfo"];
if (backmodel) {
_twocontrolNonetimerVC=[[TwoControlnoneTimerViewController alloc]init];
_twocontrolNonetimerVC.currentdevice=self.currentDevice;
[self.navigationController pushViewController:_twocontrolNonetimerVC animated:YES];
// self.navigationBarHidden=NO;
}else{
[self.indicatorView stopAnimating];
[self deviceNotOnlineState];
[self showLableAction:@"设备已离线"];
}
}
break;
default:
break;
}
}
#pragma mark - 初始化两控按钮
-(void)initTwoControlButton{
if (self.button) {
self.button=nil;
}
// AppDelegate*mydelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
NSArray*twoControlNormalImgArr=@[@"PFLight1_off.png",@"ChongLang_off.png"];
CGFloat buttonSize=kScreenSize.width*7/32;
CGFloat space=(kScreenSize.width-2*buttonSize)/3;
for (int i=0; i<twoControlNormalImgArr.count; i++) {
self.button=[UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(space+i*(buttonSize+space) + 10 ,kScreenSize.height - buttonSize - 20 - 64 - 20 , buttonSize * 105 / 122 , buttonSize );
[self.button setImage:[UIImage imageNamed:twoControlNormalImgArr[i]] forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(TwoControlButtonClick:) forControlEvents:UIControlEventTouchUpInside];
// self.button.backgroundColor = [UIColor blackColor];
self.button.tag=210+i;
[self.tableView addSubview:self.button];
if (i==0) {
self.light1Button=self.button;
}else if (i==1){
self.pumpButton=self.button;
}
}
// CGFloat wSpace=(kScreenSize.width-wSpace2*2-2*60)/2;
NSArray*arry=@[@"",@"水泵"];
for (int j=0; j<arry.count; j++){
self.buttonNameLabel=[[UILabel alloc]initWithFrame:CGRectMake(space+j*(buttonSize+space) + 10, kScreenSize.height - 20 - 64 - 30 , buttonSize * 105 / 122, 20)];
self.buttonNameLabel.text=arry[j];
self.buttonNameLabel.font=[UIFont systemFontOfSize:15];
self.buttonNameLabel.textAlignment=NSTextAlignmentCenter;
self.buttonNameLabel.tag=220+j;
self.buttonNameLabel.textColor=[UIColor colorWithRed:101/255.0f green:103/255.0f blue:103/254.0f alpha:1];
[self.tableView addSubview:self.buttonNameLabel];
if (j==0) {
self.light1name=self.buttonNameLabel;
}else if (j==1){
self.pumpname=self.buttonNameLabel;
}
}
[self initCHNames];
}
#pragma mark 两控按钮点击事件
-(void)TwoControlButtonClick:(UIButton*)btn{
if (self.dataLength==198) {
BackmassegeModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackInfo"];
if (backmodel) {
}else{
[self showLableAction:@"设备已离线"];
[self.indicatorView stopAnimating];
}
manulswitchMSModel*manulModel=[[manulswitchMSModel alloc]init];
[[Socketsingleton sharedInstance] setSoketLightDataWith:manulModel];
NSString*switchString1=manulModel.description;
if (btn.selected) {
manulModel.switchBtn=@"00";//手动模式开
btn.selected=NO;
}else {
manulModel.switchBtn=@"01";// 手动模式关
btn.selected=YES;
}
manulModel.crc16Str=@"0000";// CRC16验证码
switch (btn.tag) {
case TWOC_PUMP:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"ChongLang_on.png"] forState:UIControlStateSelected];
manulModel.lightNumber=@"02";// 02 --水泵
NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,manulModel.lightNumber,manulModel.switchBtn,manulModel.crc16Str];
NSData*manulData=[dataContorl stringToHexData:switchString2];
[[Socketsingleton sharedInstance] soketWriteData:manulData];
if ([Socketsingleton sharedInstance].clientSocket.isConnected) {
//soket 未断开时显示
[self initHUD];
[self.myhud myhudstart];
}
}
break;
case TWOC_LIGHT:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"PFLight1_on.png"] forState:UIControlStateSelected];
manulModel.lightNumber=@"01";// 00 --灯1
NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,manulModel.lightNumber,manulModel.switchBtn,manulModel.crc16Str];
NSData*manulData=[dataContorl stringToHexData:switchString2];
[[Socketsingleton sharedInstance] soketWriteData:manulData];
if ([Socketsingleton sharedInstance].clientSocket.isConnected) {
//soket 未断开时显示
[self initHUD];
[self.myhud myhudstart];
}
}
break;
default:
break;
}
}else if (self.dataLength==114){
TwoControlBackmsgmodel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackTwoControlInfo"];
if (backmodel) {
}else{
[self showLableAction:@"设备已离线"];
[self.indicatorView stopAnimating];
}
manulswitchMSModel*manulModel=[[manulswitchMSModel alloc]init];
[[Socketsingleton sharedInstance] setSoketLightDataWith:manulModel];
NSString*switchString1=manulModel.description;
if (btn.selected) {
manulModel.switchBtn=@"00";//手动模式开
btn.selected=NO;
}else {
manulModel.switchBtn=@"01";// 手动模式关
btn.selected=YES;
}
manulModel.crc16Str=@"0000";// CRC16验证码
switch (btn.tag) {
case TWOC_PUMP:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"ChongLang_on.png"] forState:UIControlStateSelected];
manulModel.lightNumber=@"02";// 02 --水泵
NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,manulModel.lightNumber,manulModel.switchBtn,manulModel.crc16Str];
NSData*manulData=[dataContorl stringToHexData:switchString2];
[ [Socketsingleton sharedInstance] soketWriteData:manulData];
if ([Socketsingleton sharedInstance].clientSocket.isConnected) {
//soket 未断开时显示
[self initHUD];
[self.myhud myhudstart];
}
}
break;
case TWOC_LIGHT:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"PFLight1_on.png"] forState:UIControlStateSelected];
manulModel.lightNumber=@"01";// 00 --灯1
NSString*switchString2=[NSString stringWithFormat:@"%@%@%@%@",switchString1,manulModel.lightNumber,manulModel.switchBtn,manulModel.crc16Str];
NSData*manulData=[dataContorl stringToHexData:switchString2];
[ [Socketsingleton sharedInstance] soketWriteData:manulData];
if ([Socketsingleton sharedInstance].clientSocket.isConnected) {
//soket 未断开时显示
[self initHUD];
[self.myhud myhudstart];
}
}
break;
default:
break;
}
}
}
#pragma mark 登陆成功后收到服务端的回执之后,调用的代理方法
-(void)socketDidGetBackmsgData:(NSData *)data onsoket:(AsyncSocket *)sock{
NSLog(@"TwoControlView收到服务器的回执--->%@",data);
NSString*string1=[dataContorl dataToHexString:data];
self.temperature.hidden=NO;
//设备在线显示与设备离线相对应
self.dulabel.hidden=NO;
//设备在线显示与设备离线相对应 收到返回数据改变显示状态
self.notOnLineLabel.hidden = YES;
self.temperaturebackImg.hidden=NO;
[self.indicatorView stopAnimating];
//[_HUD hide:YES];
[self.myhud hidmyHud];
if (_twoControlSetTimeVC.indicatorView) {
[_twoControlSetTimeVC.indicatorView stopAnimating];
}
if (_twocontrolNonetimerVC.indicatorView) {
[_twocontrolNonetimerVC.indicatorView stopAnimating];
}
if (_twoControlNinetyninebiteVC.indicatorView) {
[_twoControlNinetyninebiteVC.indicatorView stopAnimating];
}
NSLog(@"收到服务器的回执2--->%@",data);
self.dataLength=string1.length;
int timerAmount=[self.currentDevice.timerAmount intValue];
if (string1.length==IFISH_BACKMASGTYAP99) {
//99 字节 全用BackmassegeModel
// 99字节两控 4定时器
self.BackMsgTape = TwoControlBackmsgTapeNinetyNineByteAddFourTimer;
[DeviceCommunicateDataUnity readSoketbackMassageOfThreeControlData:data WithModel:_backmodel];
if ([_backmodel.yunxingmoshi isEqualToString:@"00"]) {
self.waringLabel.hidden=YES;
self.clockView.hidden=YES;
}else{
self.clockView.hidden=NO;
self.waringLabel.hidden=NO;
}
if ([_backmodel.baojingSwitch isEqualToString:@"01"]) {
self.temperaturebackImg.hidden=NO;
// self.shuixiangwendu.hidden=YES;
}else{
self.temperaturebackImg.hidden=YES;
//self.shuixiangwendu.hidden=NO;
}
}else if (string1.length==IFISH_BACKMASGTYAP57){
[DeviceCommunicateDataUnity readSoketbackMassageOfTwoControlData:data WithModel:_twoControlBackModel];
if ( timerAmount == 0) {
self.BackMsgTape = TwoControlBackmsgTapeFiftySevenByteAddZoreTimer;
self.waringLabel.hidden=YES;
self.clockView.hidden=YES;
self.light1Button.userInteractionEnabled=YES;
self.pumpButton.userInteractionEnabled=YES;
if ([_twoControlBackModel.baojingSwitch isEqualToString:@"01"]) {
self.temperaturebackImg.hidden=NO;
// self.shuixiangwendu.hidden=YES;
}else{
self.temperaturebackImg.hidden=YES;
// self.shuixiangwendu.hidden=NO;
}
}else if (timerAmount == 4){
self.BackMsgTape = TwoControlBackmsgTapeFiftySevenByteAddFourTimer;
if ([_twoControlBackModel.yunxingmoshi isEqualToString:@"00"]) {
self.waringLabel.hidden=YES;
self.clockView.hidden=YES;
}else{
self.clockView.hidden=NO;
self.waringLabel.hidden=NO;
}
//背景图片状态
if ([_twoControlBackModel.baojingSwitch isEqualToString:@"01"]) {
self.temperaturebackImg.hidden=NO;
//self.shuixiangwendu.hidden=YES;
}else{
self.temperaturebackImg.hidden=YES;
//self.shuixiangwendu.hidden=NO;
}
}
}
//按钮状态
dispatch_async(dispatch_get_main_queue(), ^{
switch (self.BackMsgTape) {
case TwoControlBackmsgTapeFiftySevenByteAddZoreTimer:
{
//两控零定时器 //暂适配闽江
[DeviceCommunicateDataUnity resetTwoControlButtonState:self.light1Button addpump:self.pumpButton withBackModel:_twoControlBackModel];
// 实时刷新设置界面 时间设置
_twocontrolNonetimerVC.timeArr=[DeviceCommunicateDataUnity refreshTwoControlTimerWithBackMassageModel:_twoControlBackModel];
[_twocontrolNonetimerVC.tableView reloadData];
}
break;
case TwoControlBackmsgTapeFiftySevenByteAddFourTimer:
{
//57字节两控四定时器按钮状态
[DeviceCommunicateDataUnity resetTwoControlButtonState:_light1Button addpump:_pumpButton withBackModel:_twoControlBackModel];
// 实时刷新设置界面 时间设置
_twoControlSetTimeVC.timeArr=[DeviceCommunicateDataUnity refreshTwoControlTimerAddTemperatureWithBackMassageModel:_twoControlBackModel];
[_twoControlSetTimeVC.tableView reloadData];
}
break;
case TwoControlBackmsgTapeNinetyNineByteAddFourTimer:
{
//99字节两控四定时器按钮状态
[DeviceCommunicateDataUnity resettwoControlbutton:_light1Button addpump:_pumpButton withbackmsgModel:_backmodel];
//对应设置界面刷新UI
_twoControlNinetyninebiteVC.timeArr=[DeviceCommunicateDataUnity refreshTwoControlSetViewWithBackMassageModel:_backmodel];
[_twoControlNinetyninebiteVC.tableView reloadData];
}
break;
default:
break;
}
});
//温度label
if (string1.length==IFISH_BACKMASGTYAP99) {
[DeviceCommunicateDataUnity setTemperaturelabel:self.temperature addTemperaturebackImg:self.temperaturebackImg addbubleImge:self.bubble WithBackData:data addWithBackMassageModel:_backmodel addWithTableview:self.tableView];
}else if (string1.length==IFISH_BACKMASGTYAP57){
[DeviceCommunicateDataUnity setTwoControlTemperaturelabel:self.temperature addTemperaturebackImg:self.temperaturebackImg addbubleImg:self.bubble WithBackData:data addWithBackMassageModel:_twoControlBackModel addWithTableview:self.tableView];
}
[self endRefreshing];
}
//设备登陆成功
-(void)ifishDeviceLogInSuccees{
self.isconnect=YES;
NSLog(@"app登录成功");
}
-(void)ifishDeviceLogInFail{
[self endRefreshing];
[self.indicatorView stopAnimating];
dispatch_async(dispatch_get_main_queue(), ^{
[self deviceNotOnlineState];//设备离线
});
}
#pragma mark -父类方法
-(void)shezhiMingCheng
{
_changeNameVC = [[TwoControlNameViewController 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{
TwoControlName *name = [UserExtendataArchaver currentTwoControlName:self.currentDevice.macAddress];
self.light1name.text = name.lightName;
self.pumpname.text = name.pumpName;
}
@end