萤石云 iOSSDK,移植跨平台相关工程
JLChen
2021-03-02 d56c8422138cd82d2ea9bed8e2f4b0bc7766be4b
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
//
//  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 "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;
@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 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];
//                      }];
            [EZHttpUtil 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
    {
        [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)
                            {
                                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.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];
                    }];
 
}
 
- (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",@"添加失败")];
    }
}
 
@end