ifish/Ifish/controllers/IfishTabControllers/设备/IfishSelectCardViewControll...

376 lines
12 KiB
Objective-C

//
// IfishSelectCardViewController.m
// ShotViewAnimation
//
// Created by imac on 16/10/25.
// Copyright © 2016年 xiang. All rights reserved.
//
#import "IfishSelectCardViewController.h"
#import "IfishCardScrollView.h"
#import "Reachability.h"
/**
* color config
*/
#define GCUIColorFromRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 \
green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 \
blue:((float)(rgbValue & 0xFF)) / 255.0 \
alpha:1.0]
#define kGCCardRatio 0.8
#define kGCCardWidth CGRectGetWidth(self.view.frame)*kGCCardRatio
#define kGCCardHeight kGCCardWidth/kGCCardRatio
#import "SecondConnectWifiController.h"
#import "UDPManager.h"
#import "UDManager.h"
#import "LoginResult.h"
#import "P2PClient.h"
#import "AFNetworking.h"
#import "YooseeNextConnectViewController.h"
@interface IfishSelectCardViewController ()<CardScrollViewDelegate,CardScrollViewDataSource>
@property (nonatomic, strong) IfishCardScrollView *cardScrollView;
@property (nonatomic, strong) NSMutableArray *cards;
@property (nonatomic, strong) NSArray *cardsImg;
@property(nonatomic)BOOL islogInGewell;
@end
extern BOOL isfromCameraView;
@implementation IfishSelectCardViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 添加鱼缸设备不是来自摄像头界面
isfromCameraView = NO;
[self addTitleViewWithTitle:@"选择设备"];
self.view.backgroundColor = COLOR_MIAN;
self.cardScrollView = [[IfishCardScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
self.cardScrollView.cardDelegate = self;
self.cardScrollView.cardDataSource = self;
[self.view addSubview:self.cardScrollView];
self.cards = [NSMutableArray array];
for (NSInteger i = 0; i < 2; i++) {
[self.cards addObject:@(i)];
}
self.cardsImg = @[@"equipment_background",@"equipment_background_camera"];
self.automaticallyAdjustsScrollViewInsets = NO;
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self loginGewell];
if( _islogInGewell){
NSLog(@"p2pConnect success.成功,你可以操作设备了");
}else{//new added
[self loginGewell];
NSLog(@"p2pConnect failure.失败,你将不能操作设备");
return;
}
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//CGPoint curentpoint= [self.cardScrollView contentOffsetWithIndex:[self.cardScrollView currentCard]];
//[self.cardScrollView.scrollView setContentOffset:curentpoint animated:NO];
[self loginGewell];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self.cardScrollView loadCard];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
}
-(void)goBackAction{
//充血返回事件 防止在选择水族箱 时 返回卡顿
[self.cardScrollView.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - CardScrollViewDelegate
- (void)updateCard:(UIView *)card withProgress:(CGFloat)progress direction:(CardMoveDirection)direction {
if (direction == CardMoveDirectionNone) {
if (card.tag != [self.cardScrollView currentCard]) {
CGFloat scale = 1 - 0.1 * progress;
card.layer.transform = CATransform3DMakeScale(scale, scale, 1.0);
card.layer.opacity = 1 - 0.2*progress;
} else {
card.layer.transform = CATransform3DIdentity;
card.layer.opacity = 1;
}
} else {
NSInteger transCardTag = direction == CardMoveDirectionLeft ? [self.cardScrollView currentCard] + 1 : [self.cardScrollView currentCard] - 1;
if (card.tag != [self.cardScrollView currentCard] && card.tag == transCardTag) {
card.layer.transform = CATransform3DMakeScale(0.9 + 0.1*progress, 0.9 + 0.1*progress, 1.0);
card.layer.opacity = 0.8 + 0.2*progress;
} else if (card.tag == [self.cardScrollView currentCard]) {
card.layer.transform = CATransform3DMakeScale(1 - 0.1 * progress, 1 - 0.1 * progress, 1.0);
card.layer.opacity = 1 - 0.2*progress;
}
}
}
#pragma mark - CardScrollViewDataSource
- (NSInteger)numberOfCards {
return self.cards.count;
}
- (UIView *)cardReuseView:(UIView *)reuseView atIndex:(NSInteger)index {
if (reuseView) {
// you can set new style
return reuseView;
}
// UIView *card = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kGCCardWidth * 0.9, kGCCardHeight)];
// card.layer.backgroundColor = [UIColor whiteColor].CGColor;
// card.layer.cornerRadius = 4;
// card.layer.masksToBounds = YES;
UIView *card = [[UIView alloc] initWithFrame:CGRectMake(0, 0,kGCCardWidth * 0.9, kGCCardWidth * 0.9 *1.55)];
card.layer.backgroundColor = [UIColor whiteColor].CGColor;
card.layer.cornerRadius = 4;
card.layer.masksToBounds = YES;
UIImageView *cardImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0, kGCCardWidth * 0.9, kGCCardWidth * 0.9 *1.55)];
NSString *imagename=self.cardsImg[index];
cardImgView.image = [UIImage imageNamed:imagename];
cardImgView.userInteractionEnabled = YES;
[card addSubview:cardImgView];
//前往绑定按钮
CGFloat qianWangBtnW = kGCCardWidth * 0.9 - 20*2;
CGFloat qianWangBtnY = kGCCardWidth * 0.9 *1.55 - 20 -qianWangBtnW *0.18;
UIButton *qianWangBtn=[UIButton buttonWithType:UIButtonTypeCustom];
qianWangBtn.frame = CGRectMake(CGRectGetMaxX(cardImgView.frame)/2 - qianWangBtnW/2, qianWangBtnY, qianWangBtnW , qianWangBtnW *0.18);
[qianWangBtn setTitle:@"前往绑定" forState:UIControlStateNormal];
[qianWangBtn addTarget:self action:@selector(qianWangBtnAction:) forControlEvents:UIControlEventTouchUpInside];
qianWangBtn.tag = index *100;
// qianWangBtn.layer.masksToBounds = YES;
// qianWangBtn.layer.cornerRadius = 5;
[qianWangBtn setBackgroundImage:[UIImage imageNamed:@"equipment_button"] forState:UIControlStateNormal];
[cardImgView addSubview:qianWangBtn];
return card;
}
-(void)qianWangBtnAction:(UIButton *)btn{
[self testwifiWithBtn:btn];
}
/**
* 检测是否wifi环境
*/
-(void)testwifiWithBtn:(UIButton *)btn{
Reachability *currReach = [Reachability reachabilityForLocalWiFi];
NetworkStatus status = [currReach currentReachabilityStatus];
NSLog(@"NetworkStatus%ld", (long)status);
if (status==AFNetworkReachabilityStatusReachableViaWiFi) {
//wifi环境下转跳下一界面
switch (btn.tag) {
case 0:
{
//水族箱
NSArray * deviceArr =[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"];
if ([deviceArr count]>=5) {
[self.view makeToast:@"设备已达上线限"];
return;
}
self.hidesBottomBarWhenPushed = YES;
SecondConnectWifiController*wifivc=[[SecondConnectWifiController alloc]init];
[self.navigationController pushViewController:wifivc animated:YES];
}
break;
case 100:
{
//摄像头
NSArray * cameraArr =[[DataCenter defaultDtacenter]valueForKey:@"cameraArr"];
if ([cameraArr count]>=5) {
[self.view makeToast:@"设备已达上线限"];
return;
}
if ( self.islogInGewell) {
self.hidesBottomBarWhenPushed = YES;
YooseeNextConnectViewController *nextvc=[[YooseeNextConnectViewController alloc] init];
[self.navigationController pushViewController:nextvc animated:YES];
nextvc.conectType = 0;
}else{
UserModel *model=[dataContorl getUserInfo];
[self loginGewellWith:model];
}
}
break;
default:
break;
}
}else{
[self.view makeToast:@"请切换到WiFi环境下绑定"];
}
}
-(void)loginGewellWith:(UserModel *)model;
{
NSString *userIDName=[NSString stringWithFormat:@"%d",(int)[model.gwellUserID integerValue]&0x7fffffff];
if ([userIDName isEqualToString:@"0"]) {
[self.view makeToast:@"暂未检测到设备"];
//请求GewellCodes
//[self getGewellCodes];
return;
}
if ([model.P2PVerifyCode1 isEqualToString:@""]) {
[self.view makeToast:@"暂未检测到设备"];
return;
}
if ([model.P2PVerifyCode2 isEqualToString:@""]) {
[self.view makeToast:@"暂未检测到设备"];
return;
}
self.islogInGewell = [[P2PClient sharedClient] p2pConnectWithId:userIDName codeStr1:model.P2PVerifyCode1 codeStr2:model.P2PVerifyCode2];
//loginResult.contactId 用户ID号
NSLog(@"%@%@%@",userIDName,model.P2PVerifyCode1,model.P2PVerifyCode2);
}
#pragma mark - loginGewell
-(void)loginGewell{
[[UDPManager sharedDefault] ScanLanDevice];
_islogInGewell = NO;
// LoginResult *loginResult = [UDManager getLoginInfo];
// NSString * P2PVerifyCode1 = @"1528438234";
// NSString * P2PVerifyCode2 = @"992805759";
// NSString *UserID = @"-2144354932";
UserModel *model=[dataContorl getUserInfo];
NSString *userIDName=[NSString stringWithFormat:@"%d",(int)[model.gwellUserID integerValue]&0x7fffffff];
NSLog(@"%@%@%@",userIDName,model.P2PVerifyCode1,model.P2PVerifyCode2);
if ([userIDName isEqualToString:@"0"]) {
//
[self.view makeToast:@"摄像头用户名空错误码10001"];
return;
}
if ([model.P2PVerifyCode1 isEqualToString:@""]) {
[self.view makeToast:@"摄像头P2PVerifyCode1空错误码10002"];
return;
}
if ([model.P2PVerifyCode2 isEqualToString:@""]) {
[self.view makeToast:@"摄像头P2PVerifyCode2空错误码10003"];
return;
}
_islogInGewell = [[P2PClient sharedClient] p2pConnectWithId:userIDName codeStr1:model.P2PVerifyCode1 codeStr2:model.P2PVerifyCode2];
}
-(void)getGewellCodes{
[self.view makeToast:@"连接服务器中。。"];
UserModel *model= [dataContorl getUserInfo];
NSString *userid = [NSString stringWithFormat:@"%@",model.userId];
[AFHttpTool getGwellCodes:userid success:^(id response) {
NSDictionary *reDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSDictionary *data=reDic[@"data"];
if ([reDic[@"result"] isEqualToString:@"100"]) {
model.P2PVerifyCode1 =data[@"P2PVerifyCode1"];
model.P2PVerifyCode2 =data[@"P2PVerifyCode2"];
model.gwellUserID =data[@"gwellUserID"];
[dataContorl resetUser:model];
[self loginGewellWith:model];
[self.view makeToast:@"连接服务器成功请继续"];
}else{
}
} failure:^(NSError *err) {
NSLog(@"错误err%@",err);
}];
}
//- (void)deleteCardWithIndex:(NSInteger)index {
// [self.cards removeObjectAtIndex:index];
//}
@end