萤石云 iOSSDK,移植跨平台相关工程
JLChen
2021-01-29 2b77dde1f45abb7ef820ce91e9aedaabe8f7e20b
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
//
//  EZCameraTableViewController.m
//  EZOpenSDKDemo
//
//  Created by DeJohn Dong on 15/10/28.
//  Copyright © 2015年 Ezviz. All rights reserved.
//
 
#import "EZDeviceTableViewController.h"
 
#import "EZAccessToken.h"
#import "MJRefresh.h"
#import "DeviceListCell.h"
#import "DDKit.h"
#import "EZLivePlayViewController.h"
#import "EZPlaybackViewController.h"
#import "EZMessageListViewController.h"
#import "EZSettingViewController.h"
#import "EZCameraTableViewController.h"
#import "EZAreaInfo.h"
#import "EZUserInfo.h"
#import "Toast+UIView.h"
#import "EZVideoTalkViewcontroller.h"
#import "EZMultiChannelRealPlayVC.h"
#import "EZHubDebugViewController.h"
#import "GlobalKit.h"
 
 
#define EZDeviceListPageSize 10
 
@interface EZDeviceTableViewController ()
 
@property (nonatomic, strong) NSMutableArray *deviceList;
@property (nonatomic) NSInteger currentPageIndex;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *addButton;
@property (nonatomic, weak) IBOutlet UISegmentedControl *segmentedControl;
 
@property (weak, nonatomic) IBOutlet UIBarButtonItem *logoutBtn;
@property (nonatomic) BOOL isSharedDevice; //是否是分享设备的segmented档选中
@property (nonatomic) NSInteger go2Type;
 
@end
 
@implementation EZDeviceTableViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    self.navigationItem.title = NSLocalizedString(@"Devicelist", @"设备列表");
    //    self.title.
    
    self.logoutBtn.title = NSLocalizedString(@"Logout", @"注销");
    
    [self checkEZIsLogin];
}
 
 
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_needRefresh)
    {
        _needRefresh = NO;
        [self.tableView.header beginRefreshing];
    }
    [self.tableView reloadData];
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
 
/**
 checkEZIsLogin
 */
-(void)checkEZIsLogin{
    
    if(!_deviceList)
        _deviceList = [NSMutableArray new];
    
    
    //判断本地保存的accessToken,然后向SDK设置AccessToken。
    if ([GlobalKit shareKit].accessToken)
    {
        [EZOPENSDK setAccessToken:[GlobalKit shareKit].accessToken];
        [self addRefreshKit];
    }
    else
    {
        if (LanguageIsChinese) {
            [EZOPENSDK openLoginPage:^(EZAccessToken *accessToken) {
                [[GlobalKit shareKit] setAccessToken:accessToken.accessToken];
                [EZOPENSDK setAccessToken:accessToken.accessToken];
                [self addRefreshKit];
                //            self.navigationItem.rightBarButtonItem = self.addButton;
            }];
        }else{
            [EZOPENSDK getAreaList:^(NSArray *areaList, NSError *error) {
                EZAreaInfo *areaInfo = areaList.firstObject;
                [EZOPENSDK openLoginPage:[NSString stringWithFormat:@"%ld",(long)areaInfo.id]
                              completion:^(EZAccessToken *accessToken) {
                    [[GlobalKit shareKit] setAccessToken:accessToken.accessToken];
                    [EZOPENSDK setAccessToken:accessToken.accessToken];
                    [self addRefreshKit];
                    //                                            self.navigationItem.rightBarButtonItem = self.addButton;
                }];
            }];
            
        }
        
        
        return;
    }
}
 
#pragma mark - Table view data source
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_deviceList count];
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DeviceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EZDeviceCell" forIndexPath:indexPath];
    
    // Configure the cell...
    EZDeviceInfo *info = [_deviceList dd_objectAtIndex:indexPath.row];
    
    cell.isShared = _isSharedDevice;
    [cell setDeviceInfo:info];
    cell.parentViewController = self;
    
    return cell;
}
 
/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 }
 }
 */
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 73.0 * [UIScreen mainScreen].bounds.size.width/320.0f;
}
 
#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.
    EZDeviceInfo *deviceInfo = sender;
    if ([[segue destinationViewController] isKindOfClass:[EZLivePlayViewController class]]) {
        ((EZLivePlayViewController *)[segue destinationViewController]).deviceInfo = deviceInfo;
    } else if ([[segue destinationViewController] isKindOfClass:[EZPlaybackViewController class]]) {
        ((EZPlaybackViewController *)[segue destinationViewController]).deviceInfo = deviceInfo;
    } else if ([[segue destinationViewController] isKindOfClass:[EZMessageListViewController class]]) {
        ((EZMessageListViewController *)[segue destinationViewController]).deviceInfo = deviceInfo;
    } else if ([[segue destinationViewController] isKindOfClass:[EZSettingViewController class]]) {
        ((EZSettingViewController *)[segue destinationViewController]).deviceInfo = deviceInfo;
    } else if ([[segue destinationViewController] isKindOfClass:[EZCameraTableViewController class]]) {
        ((EZCameraTableViewController *)[segue destinationViewController]).deviceInfo = deviceInfo;
        ((EZCameraTableViewController *)[segue destinationViewController]).go2Type = _go2Type;
    }
}
 
- (IBAction)segmentControl:(id)sender {
    _isSharedDevice = NO;
    if (self.segmentedControl.selectedSegmentIndex == 1)
    {
        _isSharedDevice = YES;
    }
    [self addRefreshKit];
}
 
#pragma mark - Custom Methods
 
- (void)addRefreshKit
{
    __weak typeof(self) weakSelf = self;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.currentPageIndex = 0;
        
        if (!_isSharedDevice)
        {
            //            [GlobalKit shareKit].configDevSerial = @"E48220775";
            if ([GlobalKit shareKit].configDevSerial.length > 0) {
                
                //调试单设备
                [EZOPENSDK getDeviceInfo:[GlobalKit shareKit].configDevSerial completion:^(EZDeviceInfo *deviceInfo, NSError *error) {
                    
                    if (deviceInfo) {
                        
                        [weakSelf.deviceList removeAllObjects];
                        [weakSelf.deviceList addObject:deviceInfo];
                        [weakSelf.tableView reloadData];
                    }
                    else {
//                        [self.tableView makeToast:@"无此设备,请检查设备序列号" duration:2.0 position:@"center"];
                    }
                    [weakSelf.tableView.header endRefreshing];
                    [weakSelf.tableView.footer endRefreshingWithNoMoreData];
                }];
            }
            else {
                
                //获取设备列表接口
                [EZOPENSDK getDeviceList:weakSelf.currentPageIndex++
                                pageSize:EZDeviceListPageSize
                              completion:^(NSArray *deviceList, NSInteger totalCount, NSError *error) {
                    if(error)
                    {
//                        [weakSelf.view makeToast:error.description duration:2.0 position:@"bottom"];
                        [weakSelf.deviceList removeAllObjects];
                        [weakSelf.tableView reloadData];
                        [weakSelf.tableView.header endRefreshing];
                        return;
                    }
                    [weakSelf.deviceList removeAllObjects];
                    [weakSelf.deviceList addObjectsFromArray:deviceList];
                    [weakSelf.tableView reloadData];
                    [weakSelf.tableView.header endRefreshing];
                    if (weakSelf.deviceList.count == totalCount)
                    {
                        [weakSelf.tableView.footer endRefreshingWithNoMoreData];
                    }
                    else
                    {
                        [weakSelf addFooter];
                    }
                }];
            }
            
        } else {
            [EZOPENSDK getSharedDeviceList:weakSelf.currentPageIndex++
                                  pageSize:EZDeviceListPageSize
                                completion:^(NSArray *deviceList, NSInteger totalCount, NSError *error) {
//                if(error)
//                {
//                    [weakSelf.view makeToast:error.description duration:2.0 position:@"bottom"];
//                    return;
//                }
                [weakSelf.deviceList removeAllObjects];
                [weakSelf.deviceList addObjectsFromArray:deviceList];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.header endRefreshing];
                if (weakSelf.deviceList.count == totalCount)
                {
                    [weakSelf.tableView.footer endRefreshingWithNoMoreData];
                }
                else
                {
                    [weakSelf addFooter];
                }
            }];
        }
    }];
    self.tableView.header.automaticallyChangeAlpha = YES;
    [self.tableView.header beginRefreshing];
    
}
 
- (void)addFooter {
    __weak typeof(self) weakSelf = self;
    self.tableView.footer  = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (!_isSharedDevice)
        {
            //获取设备列表接口
            [EZOPENSDK getDeviceList:weakSelf.currentPageIndex++
                            pageSize:EZDeviceListPageSize
                          completion:^(NSArray *deviceList, NSInteger totalCount, NSError *error) {
//                if(error)
//                {
//                    [weakSelf.view makeToast:error.description duration:2.0 position:@"bottom"];
//                    return;
//                }
                [weakSelf.deviceList addObjectsFromArray:deviceList];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.footer endRefreshing];
                if (weakSelf.deviceList.count == totalCount)
                {
                    [weakSelf.tableView.footer endRefreshingWithNoMoreData];
                }
            }];
        } else {
            [EZOPENSDK getSharedDeviceList:weakSelf.currentPageIndex++
                                  pageSize:EZDeviceListPageSize
                                completion:^(NSArray *deviceList, NSInteger totalCount, NSError *error) {
//                if(error)
//                {
//                    [weakSelf.view makeToast:error.description duration:2.0 position:@"bottom"];
//                    return;
//                }
                [weakSelf.deviceList addObjectsFromArray:deviceList];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.footer endRefreshing];
                if (weakSelf.deviceList.count == totalCount)
                {
                    [weakSelf.tableView.footer endRefreshingWithNoMoreData];
                }
            }];
        }
    }];
}
 
- (IBAction)go2AddDevice:(id)sender {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        UIStoryboard *addDeviceStoryBoard = [UIStoryboard storyboardWithName:@"AddDevice" bundle:nil];
        UIViewController *rootViewController = [addDeviceStoryBoard instantiateViewControllerWithIdentifier:@"AddByQRCode"];
        UIBarButtonItem *returnButton = [[UIBarButtonItem alloc] init];
        returnButton.title = @"";
        self.navigationItem.backBarButtonItem = returnButton;
        [self.navigationController pushViewController:rootViewController animated:YES];
    } else {
//        [UIView dd_showMessage:NSLocalizedString(@"device_scan_function_tip", @"iOS 7.0以下扫码功能请自行实现")];
    }
}
 
- (void)go2Play:(EZDeviceInfo *)deviceInfo {
    _go2Type = 0;
    
    if ([deviceInfo.deviceType containsString:@"CAS"]) {
        [self go2HubDebugWith:1 deviceInfo:deviceInfo];
    }
    else if (deviceInfo.cameraNum == 1) {
        [self performSegueWithIdentifier:@"go2LivePlay" sender:deviceInfo];
    } else if(deviceInfo.cameraNum > 1) {
        [self performSegueWithIdentifier:@"go2CameraList" sender:deviceInfo];
    }
}
 
- (void)go2Record:(EZDeviceInfo *)deviceInfo
{
    _go2Type = 1;
    
    if ([deviceInfo.deviceType containsString:@"CAS"]) {
        [self go2HubDebugWith:2 deviceInfo:deviceInfo];
    }
    else if (deviceInfo.cameraNum == 1) {
        [self performSegueWithIdentifier:@"go2Playback" sender:deviceInfo];
    } else if(deviceInfo.cameraNum > 1) {
        [self performSegueWithIdentifier:@"go2CameraList" sender:deviceInfo];
    }
}
 
- (void)go2Setting:(EZDeviceInfo *)deviceInfo
{
    [self performSegueWithIdentifier:@"go2Setting" sender:deviceInfo];
}
 
- (void)go2Message:(EZDeviceInfo *)deviceInfo
{
    [self performSegueWithIdentifier:@"go2MessageList" sender:deviceInfo];
}
 
- (void) go2VideoTalk:(EZDeviceInfo *)deviceInfo {
    EZVideoTalkViewcontroller *vc = [EZVideoTalkViewcontroller videoTalkVC];
    vc.deviceInfo = deviceInfo;
    [self.navigationController pushViewController:vc animated:YES];
}
 
- (IBAction)go2MultiPlay:(id)sender {
    __weak typeof(self) weakSelf = self;
    [EZOPENSDK logout:^(NSError *error) {
        [[GlobalKit shareKit] clearSession];
        [weakSelf.deviceList removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf checkEZIsLogin];
    }];
    //
    //    EZMultiChannelRealPlayVC *vc = [[EZMultiChannelRealPlayVC alloc]initWithDevices:_deviceList];
    //    [self.navigationController pushViewController:vc animated:YES];
}
 
- (void) go2HubDebugWith:(int)playType deviceInfo:(EZDeviceInfo *)deviceInfo {
    
    EZHubDebugViewController *vc = [[UIStoryboard storyboardWithName:@"EZMain" bundle:nil] instantiateViewControllerWithIdentifier:@"EZHubDebugViewController"];
    vc.playType = playType;
    vc.deviceInfo = deviceInfo;
    [self.navigationController pushViewController:vc animated:YES];
}
 
@end