111
hxb
2022-11-24 0a3e07f10937484145f33c7560607b4b2353cb81
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
package com.mm.android.deviceaddmodule.openapi;
 
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.mm.android.deviceaddmodule.LCDeviceEngine;
import com.mm.android.deviceaddmodule.device_wifi.CurWifiInfo;
import com.mm.android.deviceaddmodule.device_wifi.WifiConfig;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.BusinessException;
import com.mm.android.deviceaddmodule.openapi.data.AddDevicePolicyData;
import com.mm.android.deviceaddmodule.openapi.data.BindDeviceData;
import com.mm.android.deviceaddmodule.openapi.data.DeviceDetailListData;
import com.mm.android.deviceaddmodule.openapi.data.DeviceInfoBeforeBindData;
import com.mm.android.deviceaddmodule.openapi.data.DeviceLeadingInfoData;
import com.mm.android.deviceaddmodule.openapi.data.DeviceModelOrLeadingInfoCheckData;
import com.mm.android.deviceaddmodule.openapi.data.ModifyDeviceNameData;
import com.mm.android.deviceaddmodule.openapi.data.PolicyData;
 
import org.json.JSONObject;
 
import java.util.HashMap;
import java.util.Map;
 
public class DeviceAddOpenApiManager {
    private static int TIME_OUT = 10 * 1000;
    private static int TOKEN_TIME_OUT = 4 * 1000;
    private static int DMS_TIME_OUT = 45 * 1000;
 
    /**
     * 获取token
     *
     * @return
     * @throws BusinessException
     */
    public static String getToken() throws BusinessException {
        // 获取管理员token
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        JsonObject jsonData = HttpSend.execute(paramsMap, CONST.METHOD_ACCESSTOKEN,TOKEN_TIME_OUT);
        String token = jsonData.get("accessToken").getAsString();
        return token;
    }
 
    /**
     * 设备未绑定状态
     *
     * @param deviceInfoBeforeBindData
     * @return
     * @throws BusinessException
     */
    public static DeviceInfoBeforeBindData.Response deviceInfoBeforeBind(DeviceInfoBeforeBindData deviceInfoBeforeBindData) throws BusinessException {
        // 未绑定设备信息获取
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", deviceInfoBeforeBindData.data.token);
        paramsMap.put("deviceId", deviceInfoBeforeBindData.data.deviceId);
        paramsMap.put("deviceCodeModel", deviceInfoBeforeBindData.data.deviceCodeModel);
        paramsMap.put("deviceModelName", deviceInfoBeforeBindData.data.deviceModelName);
        paramsMap.put("ncCode", deviceInfoBeforeBindData.data.ncCode);
        JsonObject json = HttpSend.execute(paramsMap, CONST.METHOD_UNBINDDEVICEINFO,TIME_OUT);
        DeviceInfoBeforeBindData.Response response = new DeviceInfoBeforeBindData.Response();
        response.parseData(json);
        return response;
    }
 
    /**
     * 根据设备市场型号获取设备添加流程引导页配置信息
     *
     * @param deviceLeadingInfoData
     * @return
     * @throws BusinessException
     */
    public static DeviceLeadingInfoData.Response deviceLeadingInfo(DeviceLeadingInfoData deviceLeadingInfoData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", deviceLeadingInfoData.data.token);
        paramsMap.put("deviceModelName", deviceLeadingInfoData.data.deviceModelName);
        JsonObject json = HttpSend.execute(paramsMap, CONST.METHOD_GUIDEINFOGET,TIME_OUT);
        DeviceLeadingInfoData.Response response = new DeviceLeadingInfoData.Response();
        response.parseData(json);
        return response;
    }
 
    /**
     * 校验设备添加流程引导页配置信息是否需更新
     *
     * @param deviceModelOrLeadingInfoCheckData
     * @return
     * @throws BusinessException
     */
    public static DeviceModelOrLeadingInfoCheckData.Response deviceModelOrLeadingInfoCheck(DeviceModelOrLeadingInfoCheckData deviceModelOrLeadingInfoCheckData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", deviceModelOrLeadingInfoCheckData.data.token);
        paramsMap.put("deviceModelName", deviceModelOrLeadingInfoCheckData.data.deviceModelName);
        paramsMap.put("updateTime", deviceModelOrLeadingInfoCheckData.data.updateTime);
        JsonObject json = HttpSend.execute(paramsMap, CONST.METHOD_GUIDEINFOCHECK,TIME_OUT);
        DeviceModelOrLeadingInfoCheckData.Response response = new DeviceModelOrLeadingInfoCheckData.Response();
        response.parseData(json);
        return response;
    }
 
    /**
     * 绑定设备
     *
     * @param bindDeviceData
     * @return
     * @throws BusinessException
     */
    public static BindDeviceData.Response userDeviceBind(BindDeviceData bindDeviceData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", bindDeviceData.data.token);
        paramsMap.put("deviceId", bindDeviceData.data.deviceId);
        paramsMap.put("code", bindDeviceData.data.code);
        JsonObject json = HttpSend.execute(paramsMap, CONST.METHOD_BINDDEVICE,DMS_TIME_OUT);
        BindDeviceData.Response response = new BindDeviceData.Response();
        response.parseData(json);
        return response;
    }
 
    /**
     * 绑定设备
     *
     * @param bindDeviceData
     * @return
     * @throws BusinessException
     */
    public static BindDeviceData.Response hdlUserDeviceBind(BindDeviceData bindDeviceData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("deviceId", bindDeviceData.data.deviceId);
        paramsMap.put("spk","security.ipcam.imou");
        paramsMap.put("code", bindDeviceData.data.code);
        JsonObject json = HttpSend.execute(paramsMap, CONST.METHOD_BINDDEVICE,DMS_TIME_OUT);
        BindDeviceData.Response response = new BindDeviceData.Response();
        response.parseData(json);
        return response;
    }
 
    /**
     * 修改设备或通道名称
     *
     * @param bindDeviceData
     * @return
     * @throws BusinessException
     */
    public static ModifyDeviceNameData.Response modifyDeviceName(ModifyDeviceNameData bindDeviceData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", bindDeviceData.data.token);
        paramsMap.put("deviceId", bindDeviceData.data.deviceId);
        paramsMap.put("channelId", bindDeviceData.data.channelId);
        paramsMap.put("name", bindDeviceData.data.name);
        JsonObject json = HttpSend.execute(paramsMap, CONST.METHOD_MODIFYDEVICENAME,TIME_OUT);
        ModifyDeviceNameData.Response response = new ModifyDeviceNameData.Response();
        response.parseData(json);
        return response;
    }
 
    /**
     * 设备当前连接热点信息
     *
     * @param deviceId
     * @return
     * @throws BusinessException
     */
    public static CurWifiInfo currentDeviceWifi(String deviceId) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceId",deviceId);
        JsonObject json = HttpSend.execute(paramsMap, CONST.METHOD_CURRENT_DEVICE_WIFI,DMS_TIME_OUT);
        CurWifiInfo.Response response = new CurWifiInfo.Response();
        response.parseData(json);
        return response.data;
    }
 
    /**
     * 设备周边WIFI信息
     *
     * @param token
     * @param deviceId
     * @return
     * @throws BusinessException
     */
    public static WifiConfig wifiAround(String token, String deviceId) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", token);
        paramsMap.put("deviceId", deviceId);
        JsonObject json = HttpSend.execute(paramsMap, CONST.METHOD_WIFI_AROUND,DMS_TIME_OUT);
        WifiConfig.Response response = new WifiConfig.Response();
        response.parseData(json);
        return response.data;
    }
 
    /**
     * 控制设备连接热点
     *
     * @param token
     * @param deviceId
     * @return
     * @throws BusinessException
     */
    public static boolean controlDeviceWifi(String token, String deviceId, String ssid, String bssid, boolean linkEnable, String password) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", token);
        paramsMap.put("deviceId", deviceId);
        paramsMap.put("ssid", ssid);
        paramsMap.put("bssid", bssid);
        paramsMap.put("linkEnable", linkEnable);
        paramsMap.put("password", password);
         HttpSend.execute(paramsMap, CONST.METHOD_CONTROL_DEVICE_WIFI,DMS_TIME_OUT);
        return true;
    }
 
    /**
     * 批量根据设备序列号、通道号列表和配件号列表,获取设备的详细信息
     *
     * @param deviceDetailListData
     * @return
     * @throws BusinessException
     */
    public static DeviceDetailListData.Response deviceOpenDetailList(DeviceDetailListData deviceDetailListData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().accessToken);
        paramsMap.put("deviceList", deviceDetailListData.data.deviceList);
        JsonObject json = HttpSend.execute(paramsMap, CONST.METHOD_DEVICE_OPEN_DETAIL_LIST,TIME_OUT);
        DeviceDetailListData.Response response = new DeviceDetailListData.Response();
        response.parseData(json);
        return response;
    }
 
    public static DeviceDetailListData.Response subAccountDeviceInfo(DeviceDetailListData deviceDetailListData) throws BusinessException{
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceList", deviceDetailListData.data.deviceList);
        JsonObject json = HttpSend.execute(paramsMap, CONST.SUB_ACCOUNT_DEVICE_INFO,TIME_OUT);
        DeviceDetailListData.Response response = new DeviceDetailListData.Response();
        response.parseData(json);
        return response;
    }
 
 
    public static String getUserOpenIdByAccout(String userName)throws  BusinessException{
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().accessToken);
        paramsMap.put("account", userName);
        JsonObject json = HttpSend.execute(paramsMap, CONST.GET_OPEN_ID_AY_ACCOUNT,TIME_OUT);
        JsonElement openid = json.get("openid");
        String oid =  openid.getAsString();
        return oid;
    }
 
    public static String getSubAccountToken(String openId)throws BusinessException{
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().accessToken);
        paramsMap.put("openid", openId);
        JsonObject json = HttpSend.execute(paramsMap, CONST.SUB_ACCOUNT_TOKEN,TIME_OUT);
        return json.get("accessToken").getAsString();
    }
 
    public static String createSubAccountToken(String userName)throws BusinessException{
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().accessToken);
        paramsMap.put("account", userName);
        JsonObject json = HttpSend.execute(paramsMap, CONST.CREATE_SUB_ACCOUNT,TIME_OUT);
        return  json.get("openid").getAsString();
    }
 
    public static boolean addPolicy(AddDevicePolicyData req)throws BusinessException {
//        String jsonParam = new Gson().toJson(req);
//        HttpSend.execute(jsonParam, CONST.ADD_POLICY,TIME_OUT);
//        return  true;
 
        final Map<String, Object> map = new Gson().fromJson(new Gson().toJson(req), new TypeToken<Map<String, String>>() {
        }.getType());
 
        JsonObject json = HttpSend.execute(map, CONST.CREATE_SUB_ACCOUNT, TIME_OUT);
        return true;
    }
}