ifish/Ifish/controllers/FishTinkController/maincontroller/CenterontrolControllers/FourControl/FourControlNormalCenterCont...

367 lines
12 KiB
Objective-C

//
// FourControlNormalCenterController.m
// Ifish
//
// Created by imac on 16/5/24.
// Copyright © 2016年 imac. All rights reserved.
//
#import "FourControlNormalCenterController.h"
#import "FourControlSetViewController.h"
#import "FourControlbackMassge.h"
#define FOURCONTROLNORMAL_LIGR1TAG 231
#define FOURCONTROLNORMAL_LIGR2TAG 232
#define FOURCONTROLNORMAL_LIGR3TAG 233
#define FOURCONTROLNORMAL_PUMPTAG 234
#import "manulswitchMSModel.h"
#import "FourControlDataUnity.h"
#import "FourControlNameViewController.h"
#import "UserExtendataArchaver.h"
#import "FourControlName.h"
@interface FourControlNormalCenterController ()
@property(nonatomic,strong) FourControlSetViewController *forControlSetVC;
@property(nonatomic,strong) FourControlbackMassge *backmodel;
@property(nonatomic,strong) FourControlNameViewController *changeNameVC;
@end
@implementation FourControlNormalCenterController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_backmodel=[[FourControlbackMassge alloc] init];
[Socketsingleton sharedInstance].communiteDelegate=self;
UITapGestureRecognizer*tapgestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fourControrlCenterViewSetTap)];
[self.setImag addGestureRecognizer:tapgestureRecognizer];
[self.setbackBtn addTarget:self action:@selector(fourControrlCenterViewSetTap) forControlEvents:UIControlEventTouchUpInside];
[self.SetTimeButton addTarget:self action:@selector(fourControrlCenterViewSetTap) forControlEvents:UIControlEventTouchUpInside];
[self initFourControlNormalBtn];
}
#pragma mark -转跳设置界面
-(void)fourControrlCenterViewSetTap{
if (self.binddeviceArr.count ==0) {
[self.view makeToast:@"暂未绑定设备"];
}else{
// BackmassegeModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"FourControlMassgeInfo"];
// if (backmodel) {
[self pushFourSetvc];
// }else{
// [self showLableAction:@"设备已离线"];
// [self deviceNotOnlineState];
// [self.indicatorView stopAnimating];
// }
}
}
-(void)pushFourSetvc{
_forControlSetVC=[[FourControlSetViewController alloc]init];
_forControlSetVC.currentdevice=self.currentDevice;
[self.navigationController pushViewController:_forControlSetVC animated:YES];
//self.navigationBarHidden=NO;
}
-(void)initFourControlNormalBtn{
NSArray*nomalarr=@[@"PFLight1_off.png",@"PFLight1_off.png",@"PFkilLlight_off.png",@"ChongLang_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=231+i;
self.button.frame=CGRectMake((wspace+i * (btnwidth+wspace)), (kScreenSize.height -180 + 10),btnwidth, btnwidth * 243/210);
[self.button setImage:[UIImage imageNamed:nomalarr[i]] forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(fourControlBtnCilck:) forControlEvents:UIControlEventTouchUpInside];
[self.tableView addSubview:self.button];
if (i==0) {
_light1Button = self.button;
}else if (i==1){
_light2Button = self.button;
}else if (i==2){
_light3Button = self.button;
}else if (i==3){
_pumpButton = self.button;
}
}
NSArray*arry=@[@"灯1",@"灯2",@"UV杀菌",@"冲浪/氧泵"];
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) {
self.light1Name = buttonNameLabel;
}else if (j==1){
self.light2Name = buttonNameLabel;
}else if (j==2){
self.uVName = buttonNameLabel;
}else if (j==3){
self.pumpName = buttonNameLabel;
}
}
//数据 赋初值
[self initCHNames];
}
-(void)fourControlBtnCilck:(UIButton*)btn{
FourControlbackMassge *backmodel=[[DataCenter defaultDtacenter]valueForKey:@"FourControlMassgeInfo"];
if (backmodel) {
}else{
[self.indicatorView stopAnimating];
[self deviceNotOnlineState];
[self showLableAction:@"设备已离线"];
}
manulswitchMSModel*manulModel=[[manulswitchMSModel alloc]init];
manulModel.sendmacId = self.currentDevice.macAddress;
manulModel.resavemacId = self.currentDevice.macAddress;
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 FOURCONTROLNORMAL_LIGR1TAG:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"PFLight1_on.png"] forState:UIControlStateSelected];
manulModel.lightNumber=@"01";// --灯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;
case FOURCONTROLNORMAL_LIGR2TAG:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"PFLight1_on.png"] forState:UIControlStateSelected];
manulModel.lightNumber=@"02";// --灯2
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 FOURCONTROLNORMAL_LIGR3TAG:
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"PFkilLlight_on.png"] forState:UIControlStateSelected];
manulModel.lightNumber=@"03";// --UV杀菌
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 FOURCONTROLNORMAL_PUMPTAG :
{
btn.userInteractionEnabled=YES;
[btn setImage:[UIImage imageNamed:@"ChongLang_on.png"] forState:UIControlStateSelected];
manulModel.lightNumber=@"04";// --冲浪/氧泵
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;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark 登陆成功后收到服务端的回执之后,调用的代理方法
-(void)socketDidGetBackmsgData:(NSData *)data onsoket:(AsyncSocket *)sock{
NSLog(@"收到服务器的回执--->%@",data);
NSString*string1=[dataContorl dataToHexString:data];
//self.offlineLabel.hidden=YES;
[self endRefreshing];
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 (_forControlSetVC.indicatorView) {
[_forControlSetVC.indicatorView stopAnimating];
}
NSLog(@"收到服务器的回执2--->%@",data);
self.dataLength=string1.length;
//存储
[FourControlDataUnity readSoketbackMassageOfFourControlData:data WithModel:_backmodel];
//按钮状态
dispatch_async(dispatch_get_main_queue(), ^{
[FourControlDataUnity resetFourControlButtonState:self.light1Button addlight2:self.light2Button addlight3:self.light3Button addpump:self.pumpButton withBackModel:_backmodel];
//setView
_forControlSetVC.timeArr = [FourControlDataUnity refreshFourControlTimerWithBackMassageModel:_backmodel];
[_forControlSetVC.tableView reloadData];
});
//温度label
[FourControlDataUnity setTemperaturelabel:self.temperature addbubleImge:self.bubble WithBackData:data addWithBackMassageModel:_backmodel addWithTableview:self.tableView];
}
-(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 = [[FourControlNameViewController 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{
FourControlName *name = [UserExtendataArchaver currentFourControlName:self.currentDevice.macAddress];
self.light1Name.text = name.light1;
self.light2Name.text = name.light2;
self.uVName.text = name.uvName;
self.pumpName.text = name.pumpName;
}
@end