JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
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
//
//  Copyright © 2019 dahua. All rights reserved.
//
 
#import "LCDeviceManagerInterface.h"
#import "LCNetworkRequestManager.h"
#import "LCApplicationDataManager.h"
#import "TextDefine.h"
 
@implementation LCDeviceManagerInterface
 
+ (void)unBindDeviceWithDevice:(NSString *)deviceId success:(void (^)(void))success failure:(void (^)(LCError *_Nonnull))failure {
    [[LCNetworkRequestManager manager] lc_POST:@"/unBindDevice" parameters:@{ KEY_TOKEN: [LCApplicationDataManager token], KEY_DEVICE_ID: deviceId } success:^(id _Nonnull objc) {
        if (success) {
            success();
        }
    } failure:^(LCError *_Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
 
+ (void)shareDeviceListFrom:(NSInteger)startIndex To:(NSInteger)endIndex success:(void (^)(NSMutableArray<LCShareDeviceInfo *> *_Nonnull))success failure:(void (^)(LCError *_Nonnull))failure {
    NSString *range = [NSString stringWithFormat:@"%ld-%ld", startIndex, endIndex];
    [[LCNetworkRequestManager manager] lc_POST:@"/shareDeviceList" parameters:@{ KEY_TOKEN: [LCApplicationDataManager token], KEY_QUERYRANGE: range } success:^(id _Nonnull objc) {
        NSDictionary *dic = (NSDictionary *)objc;
 
        NSMutableArray <LCShareDeviceInfo *> *infos = [LCShareDeviceInfo mj_objectArrayWithKeyValuesArray:dic[@"devices"]];
        if (success) {
            success(infos);
        }
    } failure:^(LCError *_Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
 
+ (void)modifyDeviceForDevice:(NSString *)deviceId Channel:(NSString *)channelId NewName:(NSString *)name success:(void (^)(void))success failure:(void (^)(LCError *_Nonnull))failure {
    [[LCNetworkRequestManager manager] lc_POST:@"/modifyDeviceName" parameters:@{ KEY_TOKEN: [LCApplicationDataManager token], KEY_DEVICE_ID: deviceId, KEY_CHANNEL_ID: channelId ? channelId : @"", KEY_NAME: name } success:^(id _Nonnull objc) {
        if (success) {
            success();
        }
    } failure:^(LCError *_Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
 
+ (void)bindDeviceChannelInfoWithDevice:(NSString *)deviceId ChannelId:(NSString *)channelId success:(void (^)(LCBindDeviceChannelInfo *_Nonnull))success failure:(void (^)(LCError *_Nonnull))failure {
    [[LCNetworkRequestManager manager] lc_POST:@"/bindDeviceChannelInfo" parameters:@{ KEY_TOKEN: [LCApplicationDataManager token], KEY_DEVICE_ID: deviceId, KEY_CHANNEL_ID: channelId } success:^(id _Nonnull objc) {
        LCBindDeviceChannelInfo *info = [LCBindDeviceChannelInfo mj_objectWithKeyValues:objc];
        if (success) {
            success(info);
        }
    } failure:^(LCError *_Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
 
+ (void)deviceVersionForDevices:(NSArray *)devices success:(void (^)(NSMutableArray<LCDeviceVersionInfo *> *_Nonnull))success failure:(void (^)(LCError *_Nonnull))failure {
    NSString * str = @"";
    for (NSString * device in devices) {
        str = [str stringByAppendingString:device];
    }
    [[LCNetworkRequestManager manager] lc_POST:@"/deviceVersionList" parameters:@{ KEY_TOKEN: [LCApplicationDataManager token], KEY_DEVICES: str } success:^(id _Nonnull objc) {
        NSMutableArray<LCDeviceVersionInfo *> *infos = [LCDeviceVersionInfo mj_objectArrayWithKeyValuesArray:[objc objectForKey:@"deviceVersionList"]];
//        LCDeviceVersionInfo * infoTest = [LCDeviceVersionInfo new];
//        [infoTest testInfo];
//        NSMutableArray * aaa = [NSMutableArray array];
//        [aaa addObject:infoTest];
        if (success) {
            success(infos);
        }
    } failure:^(LCError *_Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
 
+ (void)deviceDetailListFromLeChangeWith:(NSInteger)bindId Limit:(int)limit Type:(NSString *)type NeedApInfo:(BOOL)needApInfo success:(void (^)(NSMutableArray<LCDeviceInfo *> *_Nonnull))success failure:(void (^)(LCError *_Nonnull))failure {
    //首先获取基本信息
    [[LCNetworkRequestManager manager] lc_POST:@"/deviceBaseList" parameters:@{ KEY_TOKEN: [LCApplicationDataManager token], KEY_BINDID: @(bindId), KEY_LIMIT: @(limit), KEY_TYPE: type, KEY_NEEDAPINFO: @(needApInfo) } success:^(id _Nonnull objc) {
        NSMutableArray <LCDeviceInfo *> *infos = [LCDeviceInfo mj_objectArrayWithKeyValuesArray:[objc objectForKey:KEY_DEVICE_LIST]];
        [LCDeviceManagerInterface deviceBaseDetailListFromLeChangeWithSimpleList:infos success:success failure:failure];
    } failure:^(LCError *_Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
 
//获取乐橙详细信息
+ (void)deviceBaseDetailListFromLeChangeWithSimpleList:(NSMutableArray <LCDeviceInfo *>*)infos  success:(void (^)(NSMutableArray<LCDeviceInfo *> *_Nonnull))success failure:(void (^)(LCError *_Nonnull))failure{
    
    //获取所有设备信息
    int index = 0;
    NSMutableArray * requestList = [NSMutableArray array];
    while (index<infos.count) {
        LCDeviceInfo * info = [infos objectAtIndex:index];
        NSString * channels = @"";
        for (LCChannelInfo * channel in info.channels) {
            channels = [channels stringByAppendingString:[NSString stringWithFormat:@"%@,",channel.channelId]];
        }
        //如果不需要AP也要传空数组
        [requestList addObject:@{KEY_DEVICE_ID:info.deviceId,KEY_CHANNELS:channels,KEY_APLIST:@""}];
        index++;
    }
    if (requestList.count==0) {
        if (success) {
            success([NSMutableArray array]);
        }
        return;
    }
    [[LCNetworkRequestManager manager] lc_POST:@"/deviceBaseDetailList" parameters:@{KEY_TOKEN:[LCApplicationDataManager token],KEY_DEVICE_LIST:requestList} success:^(id  _Nonnull objc) {
        NSMutableArray <LCDeviceInfo *> *tempInfos = [LCDeviceInfo mj_objectArrayWithKeyValuesArray:[objc objectForKey:KEY_DEVICE_LIST]];
        for (LCDeviceInfo * oldInfo in infos) {
            for (LCDeviceInfo * newInfo in tempInfos) {
                if ([oldInfo.deviceId isEqualToString:newInfo.deviceId]) {
                    newInfo.bindId = oldInfo.bindId;
                }
            }
        }
        if (success) {
            success(tempInfos);
        }
    } failure:^(LCError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
 
+(void)deviceDetailListFromOpenPlatformWith:(NSInteger)bindId Limit:(int)limit Type:(NSString *)type NeedApInfo:(BOOL)needApInfo success:(void (^)(NSMutableArray<LCDeviceInfo *> * _Nonnull))success failure:(void (^)(LCError * _Nonnull))failure{
    //首先获取基本信息
    [[LCNetworkRequestManager manager] lc_POST:@"/deviceOpenList" parameters:@{ KEY_TOKEN: [LCApplicationDataManager token], KEY_BINDID: @(bindId), KEY_LIMIT: @(limit), KEY_TYPE: type, KEY_NEEDAPINFO: @(needApInfo) } success:^(id _Nonnull objc) {
        NSMutableArray <LCDeviceInfo *> *infos = [LCDeviceInfo mj_objectArrayWithKeyValuesArray:[objc objectForKey:@"deviceList"]];
        [LCDeviceManagerInterface deviceOpenDetailListFromLeChangeWithSimpleList:infos success:success failure:failure];
    } failure:^(LCError *_Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
 
//获取开放平台详细信息
+ (void)deviceOpenDetailListFromLeChangeWithSimpleList:(NSMutableArray <LCDeviceInfo *>*)infos  success:(void (^)(NSMutableArray<LCDeviceInfo *> *_Nonnull))success failure:(void (^)(LCError *_Nonnull))failure{
    
    //获取所有设备信息
    int index = 0;
    NSMutableArray * requestList = [NSMutableArray array];
    while (index<infos.count) {
        LCDeviceInfo * info = [infos objectAtIndex:index];
        NSString * channels = @"";
        for (LCChannelInfo * channel in info.channels) {
            channels = [channels stringByAppendingString:[NSString stringWithFormat:@"%@,",channel.channelId]];
        }
        [requestList addObject:@{KEY_DEVICE_ID:info.deviceId,KEY_CHANNELS:channels,KEY_APLIST:@""}];
        index++;
    }
    if (requestList.count==0) {
        if (success) {
            success([NSMutableArray array]);
        }
        return;
    }
    
    [[LCNetworkRequestManager manager] lc_POST:@"/deviceOpenDetailList" parameters:@{KEY_TOKEN:[LCApplicationDataManager token],KEY_DEVICE_LIST:requestList} success:^(id  _Nonnull objc) {
        NSMutableArray <LCDeviceInfo *> *tempInfos = [LCDeviceInfo mj_objectArrayWithKeyValuesArray:[objc objectForKey:KEY_DEVICE_LIST]];
        for (LCDeviceInfo * oldInfo in infos) {
            for (LCDeviceInfo * newInfo in tempInfos) {
                if ([oldInfo.deviceId isEqualToString:newInfo.deviceId]) {
                    newInfo.bindId = oldInfo.bindId;
                }
            }
        }
        
        if (success) {
            success(tempInfos);
        }
    } failure:^(LCError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
 
@end