萤石云 iOSSDK,移植跨平台相关工程
chenqiyang
2023-04-06 eeb0fad802f9558eb74c6964480b38b49e016c45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//
//  EZDeviceResultViewController.m
//  EZOpenSDKDemo
//
//  Created by DeJohn Dong on 15/10/28.
//  Copyright © 2015年 Ezviz. All rights reserved.
//
 
#import "EZDeviceResultViewController.h"
#import "UIImage+GIF.h"
 
#import <EZOpenSDKFramework/EZProbeDeviceInfo.h>
#import "DDKit.h"
#import "EZWifiConfigViewController.h"
#import "EZWifiTipsViewController.h"
#import "MBProgressHUD.h"
#import "EZSDK.h"
 
 
@interface EZDeviceResultViewController ()<UIAlertViewDelegate>
 
@property (nonatomic, weak) IBOutlet UIImageView *resultBackgroudImageView;
@property (nonatomic, weak) IBOutlet UIImageView *resultImageView;
@property (nonatomic, weak) IBOutlet UIImageView *noWifiImageView;
@property (nonatomic, weak) IBOutlet UIButton *actionButton;
@property (nonatomic, weak) IBOutlet UILabel *resultLabel;
@property (nonatomic, weak) IBOutlet UILabel *statusLabel;
 
@property (nonatomic,assign) BOOL supportApMode;
@property (nonatomic,assign) BOOL supportSmartMode;
@property (nonatomic,assign) BOOL supportSoundMode;
 
@property (nonatomic,strong) UIView *tipView ;//配网提示view
 
@property (nonatomic,strong) UIImageView *step1TipImgV ;//步骤1提示图view
@property (nonatomic,strong) UIImageView *step1ImgV ;//步骤1图view
@property (nonatomic,strong) UILabel *step1DescribeL ;//步骤1描述lable
 
@property (nonatomic,strong) UIImageView *step2TipImgV ;//步骤2提示图view
@property (nonatomic,strong) UIImageView *step2ImgV ;//步骤2图view
@property (nonatomic,strong) UILabel *step2DescribeL ;//步骤2描述lable
 
 
@property (nonatomic,strong) UIView *btnBackView ;//按钮背景view
@property (nonatomic,strong) UIButton *connectedBtn;//已连接按钮
 
@end
 
@implementation EZDeviceResultViewController
 
- (void)dealloc
{
    
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = NSLocalizedString(@"ad_result_title", @"结果");
    
    self.resultBackgroudImageView.hidden = YES;
    self.statusLabel.hidden = YES;
    self.actionButton.hidden = YES;
    
    [self doSearchDevice];
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([[segue destinationViewController] isKindOfClass:[EZWifiConfigViewController class]]) {
        ((EZWifiConfigViewController *)[segue destinationViewController]).isAddDeviceSuccessed = YES;
    }
    
    if ([[segue destinationViewController] isKindOfClass:[EZWifiTipsViewController class]]) {
        EZWifiTipsViewController *vc = (EZWifiTipsViewController *)[segue destinationViewController];
        vc.supportApMode = self.supportApMode;
        vc.supportSmartMode = self.supportSmartMode;
        vc.supportSoundMode = self.supportSoundMode;
    }
}
 
#pragma mark - UIAlertViewDelgate Methods
 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 0xaa && buttonIndex == 1)
    {
        [GlobalKit shareKit].deviceVerifyCode = [alertView textFieldAtIndex:0].text;
        __weak MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = NSLocalizedString(@"ad_adding_msg", @"正在添加,请稍候...");
        
//        [EZOPENSDK addDevice:[GlobalKit shareKit].deviceSerialNo
//                  verifyCode:[GlobalKit shareKit].deviceVerifyCode
//                  completion:^(NSError *error) {
//                      [hud hide:YES];
//                      [self handleTheError:error];
//                  }];
        
        [EZHttpUtil.sharedManager addDeviceByHDL:[GlobalKit shareKit].deviceSerialNo
                   verifyCode:[GlobalKit shareKit].deviceVerifyCode
                   completion:^(ResponseData *responseData) {
                       [hud hide:YES];
                       [self handleTheError:responseData];
                   }];
        
    }
    else if (alertView.tag == 0xbb && buttonIndex == 1)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"device_input_vierify_code", @"请输入设备验证码") message:@"" delegate:self cancelButtonTitle:NSLocalizedString(@"cancel",@"取消") otherButtonTitles:NSLocalizedString(@"done",@"确定"), nil];
        alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
        alertView.tag = 0xaa;
        [alertView show];
    }
}
 
#pragma mark - Action Methods
 
- (IBAction)nextAction:(id)sender
{
    if([self.actionButton.titleLabel.text isEqualToString:NSLocalizedString(@"ad_connect_net", @"连接网络")])
    {
        [self performSegueWithIdentifier:@"go2WifiTips" sender:nil];
    }
    else if([self.actionButton.titleLabel.text isEqualToString:NSLocalizedString(@"ad_add",@"添加")])
    {
        if([GlobalKit shareKit].deviceVerifyCode != nil)
        {
            __weak MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.labelText = NSLocalizedString(@"ad_adding_msg", @"正在添加,请稍候...");
//            [EZOPENSDK addDevice:[GlobalKit shareKit].deviceSerialNo
//                      verifyCode:[GlobalKit shareKit].deviceVerifyCode
//                      completion:^(NSError *error) {
//                          [hud hide:YES];
//                          [self handleTheError:error];
//                      }];
            if ([GlobalKit shareKit].deviceType==HDLEZDeviceType_Door) {//为门禁设备(需要验证门锁)
                [EZHttpUtil.sharedManager verifyLocalLockByHDL:[GlobalKit shareKit].deviceSerialNo completion:^(ResponseData * _Nonnull responseData) {
                    [hud hide:YES];
                    if (responseData.success) {
                        self.resultLabel.numberOfLines=2;
                        self.resultLabel.adjustsFontSizeToFitWidth=YES;
                        self.resultLabel.text = NSLocalizedString(@"ad_result_verify_local_lock",@"请在2分钟之内校验已添加的主用户指纹、密钥或感应卡,校验完成请点击已校验按钮");
                        
                        [self.actionButton setTitle:NSLocalizedString(@"ad_verified", @"已校验") forState:UIControlStateNormal];
                    }else{
                        [self handleTheError:responseData];
                    }
 
                    NSLog(@"调用验证接口返回:%@",responseData.data);
                }];
            }else{
                [EZHttpUtil.sharedManager addDeviceByHDL:[GlobalKit shareKit].deviceSerialNo
                           verifyCode:[GlobalKit shareKit].deviceVerifyCode
                           completion:^(ResponseData *responseData) {
                               [hud hide:YES];
                               [self handleTheError:responseData];
                           }];
            }
            
        }
        else
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"device_input_vierify_code", @"请输入设备验证码") message:@"" delegate:self cancelButtonTitle:NSLocalizedString(@"cancel",@"取消") otherButtonTitles:NSLocalizedString(@"done",@"确定"), nil];
            alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
            alertView.tag = 0xaa;
            [alertView show];
        }
    }
    else if([self.actionButton.titleLabel.text isEqualToString:NSLocalizedString(@"ad_verified",@"已校验")])
    {
        __weak MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = NSLocalizedString(@"ad_adding_msg", @"正在添加,请稍候...");
        [EZHttpUtil.sharedManager addDeviceByHDL:[GlobalKit shareKit].deviceSerialNo
                   verifyCode:[GlobalKit shareKit].deviceVerifyCode
                   completion:^(ResponseData *responseData) {
                       [hud hide:YES];
                if (!responseData.success){
                    [self.view makeToast:NSLocalizedString(@"wifi_add_fail", @"添加失败") duration:2.0 position:@"center"];
                }else{
                    [self handleTheError:responseData];
                }
 
        }];
    }
    else
    {
        [self doSearchDevice];
    }
}
 
- (void)doSearchDevice
{
    
    NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"query_loading" ofType:@"gif"];
    
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    self.resultImageView.image = [UIImage sd_animatedGIFWithData:imageData];
    
    self.resultBackgroudImageView.hidden = YES;
    self.resultImageView.hidden = NO;
    self.actionButton.hidden = YES;
    self.noWifiImageView.hidden = YES;
    
    self.resultLabel.text = NSLocalizedString(@"ad_get_device_info_msg",@"正在查询设备信息,请稍后...");
 
    [EZOPENSDK probeDeviceInfo:[GlobalKit shareKit].deviceSerialNo
                    deviceType:[GlobalKit shareKit].deviceModel
                    completion:^(EZProbeDeviceInfo *deviceInfo, NSError *error) {
                        NSLog(@"deviceInfo = %@, error = %@", deviceInfo, error);
                        self.resultBackgroudImageView.hidden = NO;
                        self.resultImageView.image = [UIImage imageNamed:@"device_default"];
                        self.resultLabel.text = [GlobalKit shareKit].deviceSerialNo;
 
                        if (error)
                        {
                            if (error.code == EZ_HTTPS_DEVICE_ADDED_MYSELF || error.code == EZ_HTTPS_DEVICE_ONLINE_ADDED)
                            {
                                self.statusLabel.textColor = [UIColor blackColor];
                                self.statusLabel.text = NSLocalizedString(@"ad_already_added",@"此设备已被添加");
                                self.statusLabel.hidden = NO;
                            }
                            else if (error.code == EZ_HTTPS_DEVICE_ONLINE_IS_ADDED || error.code == EZ_HTTPS_DEVICE_OFFLINE_IS_ADDED)
                            {
                                self.statusLabel.textColor = [UIColor blackColor];
                                self.statusLabel.text = NSLocalizedString(@"ad_added_by_others",@"此设备已被别人添加");
                                self.statusLabel.hidden = NO;
                            }
                            else if (error.code == EZ_HTTPS_DEVICE_OFFLINE_NOT_ADDED ||
                                     error.code == EZ_HTTPS_DEVICE_NOT_EXISTS ||
                                     error.code == EZ_HTTPS_DEVICE_OFFLINE_IS_ADDED_MYSELF)
                            {
                                if ([[EZSDK sharedInstance].connectTipModels containsObject:deviceInfo.model]||[self checkIfShouldTipWithDeviceModel:deviceInfo.model]) {//为需要提示配网的类型
                                    [self showConnectWifiTipView];
                                    return;
                                }
                                self.statusLabel.hidden = NO;
                                self.actionButton.hidden = NO;
                                if (deviceInfo)
                                {
                                    self.supportApMode = YES;
                                    self.supportSmartMode = deviceInfo.supportWifi == 3;
                                    self.supportSoundMode = deviceInfo.supportSoundWave == 1;
                                }
                                else
                                {
                                    //查不到能力级则根据设备灯来判断配网模式
                                    self.supportApMode = YES;
                                    self.supportSmartMode = YES;
                                    self.supportSoundMode = YES;
                                }
                                
                                self.statusLabel.text =HDLEZLocallizedString(@"wifi_config_not_connected");
 
                                [self.actionButton setTitle:NSLocalizedString(@"ad_connect_net", @"连接网络") forState:UIControlStateNormal];
                            }
                            else
                            {
                                self.resultLabel.text = NSLocalizedString(@"get_info_fail", @"查询失败,网络不给力");
                                self.noWifiImageView.hidden = NO;
                                self.resultBackgroudImageView.hidden = YES;
                                self.resultImageView.hidden = YES;
                                self.actionButton.hidden = NO;
                                [self.actionButton setTitle:NSLocalizedString(@"retry", @"重试") forState:UIControlStateNormal];
                            }
                            
                            return;
                        }
                        
 
                        self.actionButton.hidden = NO;
                        [self.actionButton setTitle:NSLocalizedString(@"ad_add",@"添加")
                                           forState:UIControlStateNormal];
                    }];
 
}
 
/*检测model是否包含需要提示的类型
 */
-(BOOL)checkIfShouldTipWithDeviceModel:(NSString*)deviceModel{
    for (NSString *model in [EZSDK sharedInstance].connectTipModels) {
        if ([deviceModel containsString:[NSString stringWithFormat:@"-%@-",model]]) {
            return YES;
            break;
        }
    }
    return NO;
}
 
/*提示view
 */
-(UIView*)tipView{
    if (!_tipView) {
        //提示view
        _tipView=[[UIView alloc] init];
        [self.view addSubview:_tipView];
        [_tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(HDLEZ_APP_TOP_BAR_HEIGHT);
            make.left.bottom.right.equalTo(self.view);
        }];
        _tipView.backgroundColor=HDLEZ_COLOR_VIEW_BACKGROUND;
        
        //步骤1指示图
        _step1TipImgV=[[UIImageView alloc] init];
        [_tipView addSubview:_step1TipImgV];
        [_step1TipImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_tipView).offset(33);
            make.left.equalTo(_tipView).offset(HDLEZGetRealWidth(33));
            make.width.mas_equalTo(130);
            make.height.mas_equalTo(120);
        }];
        _step1TipImgV.image=[UIImage imageNamed:@"hdl_ez_wifi_connect_step1"];
        
        //步骤1图
        _step1ImgV=[[UIImageView alloc] init];
        [_tipView addSubview:_step1ImgV];
        [_step1ImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_step1TipImgV.mas_bottom).offset(13);
            make.left.equalTo(_step1TipImgV).offset(7);
            make.width.mas_equalTo(19);
            make.height.mas_equalTo(22);
        }];
        _step1ImgV.image=[UIImage imageNamed:@"hdl_ez_wifi_connect_step_one"];
        
        //步骤1描述lable
        _step1DescribeL=[[UILabel alloc] init];
        [_tipView addSubview:_step1DescribeL];
        [_step1DescribeL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_step1ImgV.mas_bottom).offset(7);
            make.left.equalTo(_step1ImgV);
            make.right.equalTo(_step1TipImgV);
            make.height.mas_equalTo(22);
        }];
        _step1DescribeL.font=HDLEZ_Get_FontBoldWithSize(HDLEZ_FontSize_16);
        _step1DescribeL.textColor=HDLEZ_COLOR_TEXT_TITLE_GRAY;
        _step1DescribeL.numberOfLines=2;
        _step1DescribeL.adjustsFontSizeToFitWidth=YES;
        _step1DescribeL.text=HDLEZLocallizedString(@"wifi_config_select_set_param");
        
        //步骤2指示图
        _step2TipImgV=[[UIImageView alloc] init];
        [_tipView addSubview:_step2TipImgV];
        [_step2TipImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_tipView).offset(33);
            make.right.equalTo(_tipView).offset(HDLEZGetRealWidth(-33));
            make.width.mas_equalTo(130);
            make.height.mas_equalTo(120);
        }];
        _step2TipImgV.image=[UIImage imageNamed:@"hdl_ez_wifi_connect_step2"];
        
        //步骤2图
        _step2ImgV=[[UIImageView alloc] init];
        [_tipView addSubview:_step2ImgV];
        [_step2ImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_step2TipImgV.mas_bottom).offset(13);
            make.left.equalTo(_step2TipImgV).offset(7);
            make.width.mas_equalTo(19);
            make.height.mas_equalTo(22);
        }];
        _step2ImgV.image=[UIImage imageNamed:@"hdl_ez_wifi_connect_step_two"];
        
        //步骤2描述lable
        _step2DescribeL=[[UILabel alloc] init];
        [_tipView addSubview:_step2DescribeL];
        [_step2DescribeL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_step2ImgV.mas_bottom).offset(7);
            make.left.equalTo(_step2ImgV);
            make.right.equalTo(_step2TipImgV);
            make.height.mas_equalTo(22);
        }];
        _step2DescribeL.font=HDLEZ_Get_FontBoldWithSize(HDLEZ_FontSize_16);
        _step2DescribeL.textColor=HDLEZ_COLOR_TEXT_TITLE_GRAY;
        _step2DescribeL.numberOfLines=2;
        _step2DescribeL.adjustsFontSizeToFitWidth=YES;
        _step2DescribeL.text=HDLEZLocallizedString(@"wifi_config_select_wifi");
        
        //已连接按钮背景view
        _btnBackView=[[UIView alloc] init];
        [_tipView addSubview:_btnBackView];
        [_btnBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(_tipView);
            make.height.mas_equalTo(76);
        }];
        _btnBackView.backgroundColor=HDLEZ_COLOR_VIEW_FOREGROUND;
        _btnBackView.layer.cornerRadius = 16;
        _btnBackView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
 
        
        //已连接按钮
        _connectedBtn=[[UIButton alloc] init];
        [_btnBackView addSubview:_connectedBtn];
        [_connectedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_tipView).offset(78);
            make.right.equalTo(_tipView).offset(-78);
            make.top.equalTo(_btnBackView).offset(12);
            make.height.mas_equalTo(44);
        }];
        _connectedBtn.layer.cornerRadius = 22;
        [_connectedBtn setBackgroundColor:HDLEZ_COLOR_BACKGROUND_BUTTON];
        [_connectedBtn setTitle:HDLEZLocallizedString(@"wifi_config_connected") forState:UIControlStateNormal];
        _connectedBtn.titleLabel.font=HDLEZ_Get_FontBoldWithSize(HDLEZ_FontSize_16);
        [_connectedBtn setTitleColor:HDLEZ_COLOR_TEXT_WHITE forState:UIControlStateNormal];
        [_connectedBtn addTarget:self action:@selector(Connected) forControlEvents:UIControlEventTouchUpInside];
    }
    return _tipView;
}
 
/*展示引导view(萤石部分设备不能通过该流程进行配网)
 */
-(void)showConnectWifiTipView{
    self.title = HDLEZLocallizedString(@"wifi_config_net_work");
    [self.tipView setHidden:NO];
    
}
 
/*已连接
 */
-(void)Connected{
    self.title = HDLEZLocallizedString(@"ad_result_title");
 
    //隐藏指示view
    [self.tipView setHidden:YES];
    
    [self doSearchDevice];
}
 
- (void)handleTheError:(ResponseData *)responseData
{
    if (responseData.success)
    {
        [self performSegueWithIdentifier:@"go2WifiResult" sender:nil];
        return;
    }
    if ([responseData.code isEqual: @"20010"])
    {
        UIAlertView *retryAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"device_verify_code_wrong", @"验证码错误") message:nil delegate:self cancelButtonTitle:NSLocalizedString(@"cancel", @"取消") otherButtonTitles:NSLocalizedString(@"retry", @"重试"), nil];
        retryAlertView.tag = 0xbb;
        [retryAlertView show];
    }
    else if ([responseData.code isEqual: @"20017"])
    {
        [UIView dd_showMessage:NSLocalizedString(@"ad_already_added",@"您已添加过此设备")];
    }
    else if ([responseData.code isEqual: @"20013"])
    {
        [UIView dd_showMessage:NSLocalizedString(@"ad_added_by_others",@"此设备已被别人添加")];
    }
    else
    {
//        [UIView dd_showMessage:NSLocalizedString(@"wifi_add_fail",@"添加失败")];
        [UIView dd_showMessage:responseData.message];
    }
}
 
@end