//
|
// EZAPConfigResultViewController.m
|
// EZOpenSDKDemo
|
//
|
// Created by linyong on 2018/6/5.
|
// Copyright © 2018年 Ezviz. All rights reserved.
|
//
|
|
#import "EZAPConfigResultViewController.h"
|
#import "GlobalKit.h"
|
#import <EZOpenSDKFramework/EZOpenSDK.h>
|
#import <EZOpenSDKFramework/EZProbeDeviceInfo.h>
|
#import "Toast+UIView.h"
|
#import "EZDeviceTableViewController.h"
|
|
#import "EZAddByQRCodeViewController.h"
|
|
#define MAX_COUNT (20)
|
|
@interface EZAPConfigResultViewController ()
|
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *loadingIndicator;
|
@property (weak, nonatomic) IBOutlet UIImageView *successImageView;
|
@property (weak, nonatomic) IBOutlet UIButton *doneBtn;
|
@property (weak, nonatomic) IBOutlet UIButton *retryBtn;
|
@property (weak, nonatomic) IBOutlet UILabel *msgLabel;
|
@property (nonatomic,strong) NSTimer *timer;
|
@property (nonatomic,assign) NSInteger addCount;
|
@property (nonatomic,strong) UIButton *verifiedBtn;//已校验按钮
|
|
@end
|
|
@implementation EZAPConfigResultViewController
|
|
- (void)viewDidLoad
|
{
|
[super viewDidLoad];
|
|
self.title = NSLocalizedString(@"wifi_ap_add_device_title", @"添加设备");
|
|
self.msgLabel.numberOfLines=2;
|
self.msgLabel.adjustsFontSizeToFitWidth=YES;
|
|
[self initSubviews];
|
|
[self startTimer];
|
}
|
|
- (void) viewWillDisappear:(BOOL)animated
|
{
|
[super viewWillDisappear:animated];
|
|
[self stopTimer];
|
}
|
|
|
#pragma mark - views
|
|
- (void) initSubviews
|
{
|
[self.loadingIndicator startAnimating];
|
|
//添加已验证按钮
|
CGFloat screenHeight = self.view.frame.size.height;
|
CGFloat screenWidth = self.view.frame.size.width;
|
self.verifiedBtn=[[UIButton alloc] initWithFrame:CGRectMake((screenWidth-120)/2, screenHeight-150, 120, 30)];
|
self.verifiedBtn.titleLabel.textColor=[UIColor whiteColor];
|
[self.verifiedBtn setBackgroundColor:[UIColor orangeColor]];
|
[self.verifiedBtn setTitle:NSLocalizedString(@"ad_verified", @"已校验") forState:UIControlStateNormal];
|
[self.verifiedBtn addTarget:self action:@selector(verified) forControlEvents:UIControlEventTouchUpInside];
|
[self.view addSubview:self.verifiedBtn];
|
[self.verifiedBtn setHidden:YES];
|
|
//国际化语言
|
[self.doneBtn setTitle:HDLEZLocallizedString(@"complete") forState:UIControlStateNormal];
|
[self.retryBtn setTitle:HDLEZLocallizedString(@"retry") forState:UIControlStateNormal];
|
self.msgLabel.text=HDLEZLocallizedString(@"ad_adding_msg");
|
}
|
|
#pragma mark - actions
|
|
- (IBAction)doneBtnClick:(id)sender
|
{
|
NSArray *viewControllers = self.navigationController.viewControllers;
|
for (UIViewController *vc in viewControllers)
|
{
|
if ([vc isKindOfClass:[EZDeviceTableViewController class]])
|
{
|
((EZDeviceTableViewController *)vc).needRefresh = YES;
|
[self.navigationController popToViewController:vc animated:YES];
|
break;
|
}
|
|
if ([vc isKindOfClass:[EZAddByQRCodeViewController class]])
|
{//跳转到扫码页
|
// ((EZAddByQRCodeViewController *)vc).shouldPop = YES;
|
[self.navigationController popToViewController:((EZAddByQRCodeViewController *)vc).prevController animated:YES];
|
break;
|
}
|
|
}
|
}
|
|
- (IBAction)retryBtnClick:(id)sender
|
{
|
self.msgLabel.hidden = NO;
|
self.loadingIndicator.hidden = NO;
|
[self.loadingIndicator startAnimating];
|
self.retryBtn.hidden = YES;
|
|
[self startTimer];
|
}
|
|
/*已校验
|
*/
|
-(void)verified{
|
self.loadingIndicator.hidden = NO;
|
[self.loadingIndicator startAnimating];
|
[EZHttpUtil.sharedManager addDeviceByHDL:[GlobalKit shareKit].deviceSerialNo
|
verifyCode:[GlobalKit shareKit].deviceVerifyCode
|
completion:^(ResponseData *responseData) {
|
|
if (!responseData.success){
|
// [self.view makeToast:NSLocalizedString(@"wifi_add_fail", @"添加失败") duration:2.0 position:@"center"];
|
[self.view makeToast:responseData.message duration:2.0 position:@"center"];
|
}
|
else
|
{
|
[self.verifiedBtn setHidden:YES];
|
[self successProcess];
|
}
|
|
}];
|
}
|
|
#pragma mark - callback
|
|
- (void) timerCallback
|
{
|
self.addCount ++;
|
|
if (self.addCount > MAX_COUNT)
|
{
|
[self timeoutProcess];
|
return;
|
}
|
|
[self addProcess];
|
}
|
|
#pragma mark - supoort
|
|
- (void) startTimer
|
{
|
[self stopTimer];
|
|
self.addCount = 0;
|
|
self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0
|
target:self
|
selector:@selector(timerCallback)
|
userInfo:nil
|
repeats:YES];
|
}
|
|
- (void) stopTimer
|
{
|
if (!self.timer)
|
{
|
return;
|
}
|
|
if ([self.timer isValid])
|
{
|
[self.timer invalidate];
|
}
|
|
self.timer = nil;
|
}
|
|
- (void) addProcess
|
{
|
[EZOPENSDK probeDeviceInfo:[GlobalKit shareKit].deviceSerialNo
|
deviceType:[GlobalKit shareKit].deviceModel
|
completion:^(EZProbeDeviceInfo *deviceInfo, NSError *error) {
|
if (error)
|
{
|
[self probeErrorProcessWithError:error];
|
}
|
else
|
{
|
[self stopTimer];
|
// [EZOpenSDK addDevice:[GlobalKit shareKit].deviceSerialNo
|
// verifyCode:[GlobalKit shareKit].deviceVerifyCode
|
// completion:^(NSError *error) {
|
// if (error)
|
// {
|
// [self addErrorProcessWithError:error];
|
// }
|
// else
|
// {
|
// [self successProcess];
|
// }
|
// }];
|
if ([GlobalKit shareKit].deviceType==HDLEZDeviceType_Door) {//为门锁
|
[EZHttpUtil.sharedManager verifyLocalLockByHDL:[GlobalKit shareKit].deviceSerialNo completion:^(ResponseData * _Nonnull responseData) {
|
if (responseData.success) {
|
self.loadingIndicator.hidden = YES;
|
[self.loadingIndicator stopAnimating];
|
self.msgLabel.text = NSLocalizedString(@"ad_result_verify_local_lock",@"请在2分钟之内校验已添加的主用户指纹、密钥或感应卡,校验完成请点击已校验按钮");
|
[self.verifiedBtn setHidden:NO];
|
[self stopTimer];
|
}else{
|
[self addErrorProcessWithError:responseData];
|
}
|
}];
|
}else{
|
[EZHttpUtil.sharedManager addDeviceByHDL:[GlobalKit shareKit].deviceSerialNo
|
verifyCode:[GlobalKit shareKit].deviceVerifyCode
|
completion:^(ResponseData *responseData) {
|
|
if (!responseData.success)
|
{
|
[self addErrorProcessWithError:responseData];
|
}
|
else
|
{
|
[self successProcess];
|
}
|
|
}];
|
}
|
|
|
}
|
}];
|
}
|
|
- (void) probeErrorProcessWithError:(NSError *) error
|
{
|
NSString *msg = nil;
|
if (error.code == EZ_HTTPS_DEVICE_ADDED_MYSELF || error.code == EZ_HTTPS_DEVICE_ONLINE_ADDED)
|
{
|
msg = NSLocalizedString(@"ad_already_added",@"您已添加过此设备");
|
|
self.loadingIndicator.hidden = YES;
|
[self.loadingIndicator stopAnimating];
|
|
self.successImageView.hidden = NO;
|
self.doneBtn.hidden = NO;
|
}
|
else if (error.code == EZ_HTTPS_DEVICE_ONLINE_IS_ADDED ||
|
error.code == EZ_HTTPS_DEVICE_OFFLINE_IS_ADDED)
|
{
|
msg = NSLocalizedString(@"ad_added_by_others",@"此设备已被别人添加");
|
}
|
else
|
{
|
//continue
|
return;
|
}
|
|
self.msgLabel.hidden = YES;
|
[self stopTimer];
|
[self.view makeToast:msg duration:2.0 position:@"center"];
|
}
|
|
//- (void) addErrorProcessWithError:(NSError *) error
|
//{
|
// NSString *msg = nil;
|
// if (error.code == 120010)
|
// {
|
// msg = NSLocalizedString(@"device_verify_code_wrong", @"验证码错误");
|
// }
|
// else if (error.code == 120020)
|
// {
|
// msg = NSLocalizedString(@"ad_already_added", @"您已添加过此设备");
|
// }
|
// else if (error.code == 120022)
|
// {
|
// msg = NSLocalizedString(@"ad_added_by_others", @"此设备已被别人添加");
|
// }
|
// else
|
// {
|
// msg = NSLocalizedString(@"wifi_add_fail", @"添加失败");
|
// }
|
//
|
// [self.view makeToast:msg duration:2.0 position:@"center"];
|
// self.msgLabel.hidden = YES;
|
//}
|
|
- (void) addErrorProcessWithError:(ResponseData *)responseData
|
{
|
NSString *msg = nil;
|
if ([responseData.code isEqual: @"20010"])
|
{
|
msg = NSLocalizedString(@"device_verify_code_wrong", @"验证码错误");
|
}
|
else if ([responseData.code isEqual: @"20017"])
|
{
|
msg = NSLocalizedString(@"ad_already_added", @"您已添加过此设备");
|
}
|
else if ([responseData.code isEqual: @"20013"])
|
{
|
msg = NSLocalizedString(@"ad_added_by_others", @"此设备已被别人添加");
|
}
|
else
|
{
|
msg = NSLocalizedString(@"wifi_add_fail", @"添加失败");
|
}
|
|
[self.view makeToast:msg duration:2.0 position:@"center"];
|
self.msgLabel.hidden = YES;
|
}
|
|
- (void) timeoutProcess
|
{
|
[self stopTimer];
|
|
[self.view makeToast:@"timeout,add device fail." duration:2.0 position:@"center"];
|
self.retryBtn.hidden = NO;
|
self.msgLabel.hidden = YES;
|
}
|
|
- (void) successProcess
|
{
|
[self stopTimer];
|
self.loadingIndicator.hidden = YES;
|
[self.loadingIndicator stopAnimating];
|
|
self.successImageView.hidden = NO;
|
self.doneBtn.hidden = NO;
|
self.msgLabel.hidden = YES;
|
|
[self.verifiedBtn setHidden:YES];
|
|
}
|
|
|
@end
|