ifish/Ifish/controllers/FishTinkController/maincontroller/CenterontrolControllers/XuTo/Xuanduo2fController.m

464 lines
16 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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 "XuToSetViewController.h"
#import "CreatErWeiMaController.h"
#import "SongBaoViewController.h"
#import "XuToControlName.h"
#import "UserExtendataArchaver.h"
#import "UIButton+WebCache.h"
#import "NSString+Add.h"
#import "Xuanduo2DataUtility.h"
#define kBtnTag 100
@interface Xuanduo2fController ()<LxPopViewDelegate,IfishCommuniteDelegate>
@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;
@end
@implementation Xuanduo2fController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName :[UIColor blackColor]};
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setup];
[self connect];
// Do any additional setup after loading the view from its nib.
}
- (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 initCHNames];
[Socketsingleton sharedInstance].communiteDelegate = self;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tempSwitch)];
[self.tempLabel addGestureRecognizer:tap];
}
- (void)connect {
if ([self.currentDevice.isBlacklist isEqualToString:@"0"]) {
[self InitSocket];//允许请求数据
}
}
#pragma mark 连接socket
-(void)InitSocket{
NSLog(@"******%d",[Socketsingleton sharedInstance].clientSocket.isConnected);
if (![Socketsingleton sharedInstance].clientSocket.isConnected) {
//[[Socketsingleton sharedInstance];
}
// 切换设备 先断开然后重连
[[Socketsingleton sharedInstance] socketConnectHost];
[Socketsingleton sharedInstance].macAddress = self.currentDevice.macAddress;
}
#pragma mark 通信监听communiteDelegate
//当已经和服务端断开连接后调用该代理方法
//断线重连功能需要在这里进行
-(void)ifishSocketDidDisconnect:(AsyncSocket *)sock{
}
-(void)ifishSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err{
// NSLog(@"soket错误断开");
// NSLog(@"存留数据%@",[sock unreadData]);
}
-(BOOL)ifishSocketWillConnect:(AsyncSocket *)sock{
// NSLog(@"将要链接");
return YES;
}
#pragma mark 当已经与服务端建立连接后调用的代理方法
-(void)ifishSocket:(AsyncSocket *)sock ifishSocketdidConnectToHost:(NSString *)host port:(UInt16)port{
//已建立连接 登陆指令同意在soket单例中
}
//发送完成处理
-(void)ifishSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{
}
#pragma mark 登陆成功后收到服务端的回执之后,调用的代理方法
//父类里面不执行此方法 接收服务器返回信息 在子类中做处理
//communiteDelegate
-(void)socketDidGetBackmsgData:(NSData *)data onsoket:(AsyncSocket *)sock{
[Xuanduo2DataUtility readSocketDataWithBackMsgModel:self.dataModel addWithBackData:data];
if ([self.tipLabel.text isEqualToString:@"鱼缸温度"]) {
[self setTemperature];
}
[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];
}
-(void)ifishDeviceLogInFail{
}
-(void)ifishDeviceLogInSuccees{
}
-(void)ifishSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
}
#pragma mark -- actions --
- (IBAction)btnActions:(UIButton *)sender {
sender.selected = !sender.selected;
switch (sender.tag) {
case kBtnTag: //杀菌灯
break;
case kBtnTag + 1:
default:
break;
}
}
- (IBAction)brandClick:(id)sender {
SongBaoViewController *brand = [[SongBaoViewController alloc]init];
brand.brandIntroduce = self.currentDevice.brandIntroduce;
[self.navigationController pushViewController:brand animated:YES];
self.navigationController.navigationBarHidden=NO;
}
- (void)tempSwitch {
if ([self.tipLabel.text isEqualToString:@"当月电量"]) {
}else {
self.tipLabel.text = @"当月电量";
UInt64 mac = [dataContorl hexToTen:self.dataModel.electricity];
NSString *stringTemp = [NSString stringWithFormat:@"%llukwh",mac];
[self.tempLabel setAttributedText:[NSString ls_changeFontAndColor:[UIFont systemFontOfSize:15] Color:nil TotalString:stringTemp SubStringArray:@[@"kwh"]]];
if (!_switchTimer) {
_switchTimer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(changeTemp) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_switchTimer forMode:NSRunLoopCommonModes];
}
}
}
- (void)changeTemp {
self.tipLabel.text = @"鱼缸温度";
[self setTemperature];
[_switchTimer invalidate];
_switchTimer = nil;
}
- (void)goBackAction{
[[Socketsingleton sharedInstance] cutOffSocket];
[[IfishDataUnity shareDataInstance] setAppTabRoot];
}
- (void)presentPopView{
UIImage *image = [UIImage imageNamed:@"popover_background_image_notrangle"];
NSArray *titleImgs = nil;
NSArray *titles=nil;
LXPopView *pop=[[LXPopView alloc] init];
CGFloat popRowH=160*(image.size.height/image.size.width);
//主控
pop.frame = CGRectMake(0, 0, 160, 160*(image.size.height/image.size.width) + (popRowH -12)/2);
titleImgs = @[@"ifishdropview_edite_device",@"ifishdropview_delect_device",@"Ifish_share_device",@"ifishdropview_set_device"];
titles=@[@"修改名称",@"删除设备",@"分享设备",@"鱼缸设置"];
pop.delegate = self;
pop.backgroundImage =image
;
pop.titles = titles;
pop.titleImgs =titleImgs;
DXPopover *popover = [DXPopover popover];
self.popover = popover;
popover.arrowSize = CGSizeMake(0.0,0.0);
[popover showAtView:_positionView withContentView:pop];
NSLog(@"设备右侧pop");
}
#pragma mark - PopViewDelagate
-(void)disSelectedPopViewIndex:(NSIndexPath *)Index{
[self.popover dismiss];
if (Index.row == 0) {
//修改名称
[self shezhiMingCheng];
}else if (Index.row == 1){
//删除设备
[self deletDevice];
}else if (Index.row == 2){
//分享设备
[self fenXiangSheBei];
}else if (Index.row == 3){
[self setting];
}
}
#pragma mark - 修改设备名
- (void)setTemperature {
UInt64 mac = [dataContorl hexToTen:self.dataModel.waterTemperature];
NSLog(@"%llu",mac);
//float TPlabel=mac/10+(mac%10)*0.1;
float temp = mac/10;
NSString *stringTemp = [NSString stringWithFormat:@"%.1f℃",temp];
[self.tempLabel setAttributedText:[NSString ls_changeFontAndColor:[UIFont systemFontOfSize:15] Color:nil TotalString:stringTemp SubStringArray:@[@""]]];
}
-(void)shezhiMingCheng{
XuToControlNameViewController *_changeNameVC = [[XuToControlNameViewController alloc] init];
_changeNameVC.isNewdevice = YES;
_changeNameVC.name = self.title;
_changeNameVC.device = self.currentDevice;
__weak typeof (self)wself = self;
_changeNameVC.NameWithBlock = ^(NSString *nameStr){
wself.title = nameStr;
[wself initCHNames];
};
[ wself.navigationController pushViewController: _changeNameVC animated:YES];
}
/// 各种控制旋钮标题显示
-(void)initCHNames{
XuToControlName *name = [UserExtendataArchaver currentNewXuToControlName:self.currentDevice.macAddress];
self.shajunLight.text = name.shajunLight;
self.zengyangLabel.text = name.zengyangPump;
self.light1Label.text = name.light1;
self.light2Label.text = name.light2;
self.zaolangLabel.text = name.zaolangPump;
self.huliLabel.text = name.huLiDeng;
self.xunhuanLabel.text = name.xunhuanPump;
self.jiareLabel.text = name.jiaRe;
}
#pragma mark - 删除设备
-(void)deletDevice{
UIAlertController*ac=[UIAlertController alertControllerWithTitle:@"提示" message:@"确认删除设备?" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:ac animated:YES completion:nil];
[ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction*action){
}]];
[ac addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action){
//先判断此设备是否有关联 摄像头
//DeviceModel*deviceModel=[_deviceArr objectAtIndex:indexpath.row];
//要删除的设设备id
//存在则 解除关系 删除对应的cameraId
NSMutableArray *guanxiiArr= [[DataCenter defaultDtacenter] valueForKey:@"devicamerArr"];
for (DeviceCameraModel *model in guanxiiArr) {
if (self.currentDevice.deviceId == model.deviceId ) {
[guanxiiArr removeObject:model];
//保存新关系数组
[[DataCenter defaultDtacenter] setValue:guanxiiArr forKey:@"devicamerArr"];
}
}
[self deleteDeviceRequset];
}]];
}
-(void)deleteDeviceRequset{
AFHTTPRequestOperationManager*mannager=[AFHTTPRequestOperationManager manager];
mannager.responseSerializer=[AFHTTPResponseSerializer serializer];
NSMutableDictionary * para = [NSMutableDictionary dictionary];
NSString *priId = self.currentDevice.deviceId;
;
NSString *userId = self.currentDevice.userId;
[para setValue:priId forKey:@"priId.deviceId"];
[para setValue:userId forKey:@"priId.userId"];
// __weak typeof (self)weakSelf=self;
[mannager POST:kDeleteDeviceUser parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject) {
NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"result:%@",resultDic[@"result"]);
if ([resultDic[@"result"] isEqualToString:@"100"]) {
[self showTitle:@"" messsage:@"删除成功"];
//重置数据库
NSArray*deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"];
NSMutableArray *newArr = [NSMutableArray arrayWithArray:deviceArry];
for (DeviceModel *model in deviceArry) {
if ([model.deviceId isEqual:self.currentDevice.deviceId]) {
[newArr removeObject:model];
}
}
[[DataCenter defaultDtacenter] setValue: newArr forKey:@"deviceInfo"];
if ([newArr count]==0) {
NSArray *cameraArr= [[DataCenter defaultDtacenter] valueForKey:@"cameraArr"];
if ([cameraArr count]!=0) {
[[Socketsingleton sharedInstance] cutOffSocket];
}else{
//两种设备都
[[Socketsingleton sharedInstance] cutOffSocket];
}
}
[[IfishDataUnity shareDataInstance] setAppTabRoot];
}else if ([resultDic[@"result"] isEqualToString:@"101"]){
[self showTitle:@"" messsage:@"删除失败"];
}else if ([resultDic[@"result"] isEqualToString:@"301"]){
[self showTitle:@"" messsage:@"请求验证失败,请重新登陆"];
}else if ([resultDic[@"result"] isEqualToString:@"302"]){
[self showTitle:@"" messsage:@"请求被舍弃,未执行"];
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self showTitle:@"" messsage:@"请求异常"];
}];
}
#pragma mark - 分享设备
-(void)fenXiangSheBei{
CreatErWeiMaController*vc=[[CreatErWeiMaController alloc]init];
vc.isNewdevice = YES;
vc.erdevicemodel=self.currentDevice;
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -设置页面 -
- (void)setting {
XuToSetViewController * _xuToSetVC=[[XuToSetViewController alloc] init];
_xuToSetVC.currentdevice=self.currentDevice;
_xuToSetVC.isNewDevice = YES;
[self.navigationController pushViewController:_xuToSetVC animated:YES];
}
-(void)showTitle:(NSString*)title messsage:(NSString*)message{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
}
- (Xuanduo2Model *)dataModel {
if (!_dataModel) {
_dataModel = [Xuanduo2Model new];
}
return _dataModel;
}
- (void)dealloc {
}
@end