JLChen
2021-11-04 d3713a9e02760ac9f5c0551ca72be0bdda3ba91c
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
//
//  Copyright © 2019 dahua. All rights reserved.
//
 
#import "LCDeviceListPresenter.h"
#import "LCDeviceVideoManager.h"
#import <LCOpenSDKDynamic/LCOpenSDK/LCOpenSDK_Api.h>
#import "LCDeviceListCell.h"
 
@interface LCDeviceListPresenter ()
 
@end
 
@implementation LCDeviceListPresenter
 
//MARK: - Public Methods
 
- (void)initSDK {
    NSLog(@"%@,@%ld", [LCApplicationDataManager SDKHost], [LCApplicationDataManager SDKPort]);
    LCOpenSDK_ApiParam *param = [LCOpenSDK_ApiParam new];
    param.procotol = [[LCApplicationDataManager hostApi] containsString:@"https"] ? PROCOTOL_TYPE_HTTPS : PROCOTOL_TYPE_HTTP;
    param.addr = [LCApplicationDataManager SDKHost];
    param.port = [LCApplicationDataManager SDKPort];
    param.token = [LCApplicationDataManager token];
    [[LCOpenSDK_Api shareMyInstance] initOpenApi:param];
//    [[LCOpenSDK_Api shareMyInstance] initOpenApi:[[LCApplicationDataManager hostApi] containsString:@"https"] ? PROCOTOL_TYPE_HTTPS : PROCOTOL_TYPE_HTTPS addr:[LCApplicationDataManager SDKHost] port:[LCApplicationDataManager SDKPort] CA_PATH:@""];
 
    
}
 
- (void)initSDKLog {
    LCOpenSDK_LogInfo *info = [LCOpenSDK_LogInfo new];
    info.levelType = LogLevelTypeDebug;
    [[LCOpenSDK_Log shareInstance] setLogInfo:info];
}
 
- (void)refreshData:(LCRefreshHeader *)header {
    // TODO: 判断是否在上拉 yes:return
    weakSelf(self);
    if (self.isRefreshing) {
        [header endRefreshing];
        return;
    }
    self.isRefreshing = YES;
    if (![LCApplicationDataManager isManagerMode]) {
        [LCDeviceManagerInterface deviceDetailListFromOpenPlatformWith:-1 Limit:8 Type:@"bind" NeedApInfo:NO success:^(NSMutableArray<LCDeviceInfo *> *_Nonnull devices) {
            [self.lcDevices removeAllObjects];
            [self.openDevices removeAllObjects];
            [self.infos removeAllObjects];
      
            //只显示NVR与通道数大于等于1
            NSMutableArray *tempArr = [NSMutableArray arrayWithArray:[self.openDevices arrayByAddingObjectsFromArray:devices]];
            [tempArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj isKindOfClass:[LCDeviceInfo class]]) {
                    if (((LCDeviceInfo *)obj).channels.count == 0 && ![((LCDeviceInfo *)obj).catalog isEqualToString:@"NVR"]) {
                        [tempArr removeObject:obj];
                    }
                }
                *stop = NO;
            }];
            self.openDevices = tempArr;
            
            [LCProgressHUD hideAllHuds:nil];
            [header endRefreshing];
            self.isRefreshing = NO;
        } failure:^(LCError *_Nonnull error) {
            [LCProgressHUD hideAllHuds:nil];
            [LCProgressHUD showMsg:error.errorMessage];
            [header endRefreshing];
            self.isRefreshing = NO;
        }];
    } else {
        [LCDeviceManagerInterface deviceDetailListFromLeChangeWith:-1 Limit:8 Type:@"bind" NeedApInfo:NO success:^(NSMutableArray<LCDeviceInfo *> *_Nonnull devices) {
            //如果获取数量小于8表示乐橙获取完,再从开放平台获取
            [self.lcDevices removeAllObjects];
            [self.openDevices removeAllObjects];
            [self.infos removeAllObjects];
            
            //只显示NVR与通道数大于等于1,此处存在lcdevice=opendevice异常
            NSMutableArray *tempArr = [NSMutableArray arrayWithArray:[self.openDevices arrayByAddingObjectsFromArray:devices]];
            [tempArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj isKindOfClass:[LCDeviceInfo class]]) {
                    if (((LCDeviceInfo *)obj).channels.count == 0 && ![((LCDeviceInfo *)obj).catalog isEqualToString:@"NVR"]) {
                        [tempArr removeObject:obj];
                    }
                }
                *stop = NO;
            }];
            self.lcDevices = tempArr;
            
            if (devices.count < 8) {
                [LCDeviceManagerInterface deviceDetailListFromOpenPlatformWith:-1 Limit:8 Type:@"bind" NeedApInfo:NO success:^(NSMutableArray<LCDeviceInfo *> *_Nonnull devices) {
                
                    //只显示NVR与通道数大于等于1
                    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:[self.openDevices arrayByAddingObjectsFromArray:devices]];
                    [tempArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([obj isKindOfClass:[LCDeviceInfo class]]) {
                            if (((LCDeviceInfo *)obj).channels.count == 0 && ![((LCDeviceInfo *)obj).catalog isEqualToString:@"NVR"]) {
                                [tempArr removeObject:obj];
                            }
                        }
                        *stop = NO;
                    }];
                    self.openDevices = tempArr;
                    
                    [LCProgressHUD hideAllHuds:nil];
                    [header endRefreshing];
                    self.isRefreshing = NO;
                } failure:^(LCError *_Nonnull error) {
                    [LCProgressHUD hideAllHuds:nil];
                    [LCProgressHUD showMsg:error.errorMessage];
                    [header endRefreshing];
                    self.isRefreshing = NO;
                }];
            } else {
                [header endRefreshing];
                self.isRefreshing = NO;
                [LCProgressHUD hideAllHuds:nil];
            }
        } failure:^(LCError *_Nonnull error) {
            [LCProgressHUD hideAllHuds:nil];
            [header endRefreshing];
            self.isRefreshing = NO;
            [LCProgressHUD showMsg:error.errorMessage];
        }];
    }
}
 
- (void)loadMoreData:(LCRefreshFooter *)footer {
    weakSelf(self);
    if (self.isRefreshing) {
        [footer endRefreshing];
        return;
    }
    self.isRefreshing = YES;
    if ([LCApplicationDataManager isManagerMode] && self.lcDevices.count > 0) {
        //应该继续从乐橙获取
        [LCDeviceManagerInterface deviceDetailListFromLeChangeWith:self.lcDevices.lastObject.bindId Limit:8 Type:@"bind" NeedApInfo:NO success:^(NSMutableArray<LCDeviceInfo *> *_Nonnull devices) {
            
            NSMutableArray *tempArr = [NSMutableArray arrayWithArray:[self.lcDevices arrayByAddingObjectsFromArray:devices]];
            [tempArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj isKindOfClass:[LCDeviceInfo class]]) {
                    if (((LCDeviceInfo *)obj).channels.count == 0 && ![((LCDeviceInfo *)obj).catalog isEqualToString:@"NVR"]) {
                        [tempArr removeObject:obj];
                    }
                }
                *stop = NO;
            }];
            self.lcDevices = tempArr;
            
            //如果获取数量小于8表示乐橙获取完,再从开放平台获取
            if (devices.count < 8 && weakself.openDevices.count > 0) {
                [LCDeviceManagerInterface deviceDetailListFromOpenPlatformWith:weakself.openDevices.lastObject.bindId Limit:8 Type:@"bind" NeedApInfo:NO success:^(NSMutableArray<LCDeviceInfo *> *_Nonnull devices) {
        
                    //只显示NVR与通道数大于0
                    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:[self.openDevices arrayByAddingObjectsFromArray:devices]];
                    [tempArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([obj isKindOfClass:[LCDeviceInfo class]]) {
                            if (((LCDeviceInfo *)obj).channels.count == 0 && ![((LCDeviceInfo *)obj).catalog isEqualToString:@"NVR"]) {
                                [tempArr removeObject:obj];
                            }
                        }
                        *stop = NO;
                    }];
                    self.openDevices = tempArr;
                    
                    [LCProgressHUD hideAllHuds:nil];
                    weakself.isRefreshing = NO;
                    [footer endRefreshing];
                    if (devices.count < 8) {
                        [footer setState:MJRefreshStateNoMoreData];
                    }
                } failure:^(LCError *_Nonnull error) {
                    [LCProgressHUD hideAllHuds:nil];
                    weakself.isRefreshing = NO;
                    [footer endRefreshing];
                    [LCProgressHUD showMsg:error.errorMessage];
                }];
            } else {
                weakself.isRefreshing = NO;
                [footer endRefreshing];
                [LCProgressHUD hideAllHuds:nil];
            }
        } failure:^(LCError *_Nonnull error) {
            [LCProgressHUD hideAllHuds:nil];
            weakself.isRefreshing = NO;
            [footer endRefreshing];
            [LCProgressHUD showMsg:error.errorMessage];
        }];
    } else {
        //直接从开放平台获取
        if (self.openDevices.count > 0) {
            [LCDeviceManagerInterface deviceDetailListFromOpenPlatformWith:self.openDevices.lastObject.bindId Limit:8 Type:@"bind" NeedApInfo:NO success:^(NSMutableArray<LCDeviceInfo *> *_Nonnull devices) {
                
                //只显示NVR与通道数大于等于1
                NSMutableArray *tempArr = [NSMutableArray arrayWithArray:[self.openDevices arrayByAddingObjectsFromArray:devices]];
                [tempArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([obj isKindOfClass:[LCDeviceInfo class]]) {
                        if (((LCDeviceInfo *)obj).channels.count == 0 && ![((LCDeviceInfo *)obj).catalog isEqualToString:@"NVR"]) {
                            [tempArr removeObject:obj];
                        }
                    }
                    *stop = NO;
                }];
                self.openDevices = tempArr;
                
                [footer endRefreshing];
                weakself.isRefreshing = NO;
                [LCProgressHUD hideAllHuds:nil];
            } failure:^(LCError *_Nonnull error) {
                [LCProgressHUD hideAllHuds:nil];
                [footer endRefreshing];
                weakself.isRefreshing = NO;
                [LCProgressHUD showMsg:error.errorMessage];
            }];
        }else{
             weakself.isRefreshing = NO;
        }
    }
}
 
//MARK: - Private Methods
 
- (NSMutableArray<LCDeviceInfo *> *)lcDevices {
    if (!_lcDevices) {
        _lcDevices = [NSMutableArray array];
    }
    return _lcDevices;
}
 
- (NSMutableArray<LCDeviceInfo *> *)openDevices {
    if (!_openDevices) {
        _openDevices = [NSMutableArray array];
    }
    return _openDevices;
}
 
- (NSMutableArray<LCDeviceInfo *> *)infos {
    NSMutableArray *infos = [NSMutableArray array];
    [infos addObjectsFromArray:self.lcDevices];
    [infos addObjectsFromArray:self.openDevices];
    _infos = infos;
    return _infos;
}
 
//MARK: - UITableviewDataSource
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infos.count;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCDeviceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LCDeviceListCell" forIndexPath:indexPath];
    LCDeviceInfo *info = [self.infos objectAtIndex:indexPath.row];
    cell.deviceInfo = info;
    cell.presenter = self;
    weakSelf(self);
    cell.resultBlock = ^(LCDeviceInfo *_Nonnull info, NSInteger channelIndex, NSInteger index) {
        [LCDeviceVideoManager manager].isbindFromLeChange = [self.lcDevices containsObject:info];
        [LCDeviceVideoManager manager].currentDevice = info;
        [LCDeviceVideoManager manager].currentChannelIndex = -1;
        if (index == 0) {
            [LCDeviceVideoManager manager].currentChannelIndex = channelIndex;
            if ([info.catalog isEqualToString:@"NVR"]&&![[LCDeviceVideoManager manager].currentChannelInfo.status isEqualToString:@"online"]) {
                return;
            }
            if ([info.catalog isEqualToString:@"IPC"]&&![info.status isEqualToString:@"online"]) {
                return;
            }
            [weakself.listContainer.navigationController pushToLivePreview];
        } else if (index == 1) {
            [weakself.listContainer.navigationController pushToDeviceSettingPage];
        } else {
            [weakself.listContainer.navigationController pushToCloudService];
        }
    };
 
    return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCDeviceInfo *info = self.infos[indexPath.row];
    return [self getTableViewCellHeight:info];
}
 
- (CGFloat)getTableViewCellHeight:(LCDeviceInfo *)info {
    if (info.channels.count > 1) {
        //多通道设备
        return 204.f;
    } else {
        //单通道设备
        return 257.f;
    }
}
 
@end