hxb
2022-11-22 b3513b1713bb979d0a69c5a8c4ddcd038f184e6e
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
package com.mm.android.deviceaddmodule.openapi;
 
import android.text.TextUtils;
 
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.BusinessException;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
import com.mm.android.deviceaddmodule.mobilecommon.utils.MD5Helper;
 
import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONObject;
 
import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
import javax.net.ssl.SSLException;
 
public class HttpSend {
 
    private static final String TAG = HttpSend.class.getSimpleName();
 
    public static JsonObject execute(Map<String, Object> paramsMap, String method,int timeOut) throws BusinessException {
        Map<String, Object> map = paramsInit(paramsMap);
        JsonObject jsonObj = null;
        JsonObject jsonResult = null;
        if (CONST.HOST.contains("hdlcontrol.com")) {
            // 返回json
            jsonObj = doPost(CONST.HOST + "/home-wisdom/imou/openapi/" + method, map, timeOut);
            LogUtil.debugLog(TAG, "url::" + method + "\n" + "response result:" + jsonObj.toString());
            if (jsonObj == null) {
                throw new BusinessException("openApi response is null");
            }
            jsonResult = jsonObj;
        } else {
            jsonObj = doPost(CONST.HOST + "/openapi/" + method, map, timeOut);
            LogUtil.debugLog(TAG, "url::" + method + "\n" + "response result:" + jsonObj.toString());
            if (jsonObj == null) {
                throw new BusinessException("openApi response is null");
            }
            jsonResult = jsonObj.getAsJsonObject("result");
        }
 
        if (jsonResult == null) {
            throw new BusinessException("openApi response is null");
        }
        String code = jsonResult.get("code").getAsString();
        if (!"0".equals(code)) {
            String msg = jsonResult.get("msg").getAsString();
            throw new BusinessException(code + msg);
        }
        try {
            JsonObject jsonData = jsonResult.getAsJsonObject("data");
            if (jsonData == null) {
                jsonData = new JsonObject();
            }
            return jsonData;
        } catch (Throwable e) {
            BusinessException businessException = new BusinessException(e);
            throw businessException;
        }
    }
 
    public static JsonObject execute(String json, String method,int timeOut) throws BusinessException {
        // 返回json
        LogUtil.debugLog(TAG, "request result:" + json.toString());
        JsonObject jsonResult=null;
        if (CONST.HOST.contains("hdlcontrol.com")) {
            JsonObject jsonObj = doPost(CONST.HOST + "/home-wisdom/imou/openapi/"  + method, json, timeOut);
            LogUtil.debugLog(TAG, "URL::"+method+"\n"+"response result:" + jsonObj.toString());
            if (jsonObj == null) {
                throw new BusinessException("openApi response is null");
            }
            jsonResult = jsonObj;
        }
        else {
            JsonObject jsonObj = doPost(CONST.HOST + "/openapi/" + method, json, timeOut);
            LogUtil.debugLog(TAG, "URL::"+method+"\n"+"response result:" + jsonObj.toString());
            if (jsonObj == null) {
                throw new BusinessException("openApi response is null");
            }
            jsonResult = jsonObj.getAsJsonObject("result");
        }
 
        if (jsonResult == null) {
            throw new BusinessException("openApi response is null");
        }
        String code = jsonResult.get("code").getAsString();
        if (!"0".equals(code)) {
            String msg = jsonResult.get("msg").getAsString();
            throw new BusinessException(code + msg);
        }
        try {
            JsonObject jsonData = jsonResult.getAsJsonObject("data");
            if (jsonData ==null){
                jsonData=new JsonObject();
            }
            return jsonData;
        } catch (Throwable e) {
            BusinessException businessException = new BusinessException(e);
            throw businessException;
        }
    }
 
    private static JsonObject doPost(String url, Map<String, Object> map, int timeOut) throws BusinessException {
        Gson gson = new Gson();
        map.put("homeId",1547099040465408002L);
        String json =null;
        JsonObject jsonObject = new JsonObject();
        if(url.contains("hdlcontrol.com")) {
            if (map.containsKey("params")) {
                Map<String, Object> tempMap = (Map<String, Object>) map.get("params");
                map.remove("params");
                map.remove("id");
                map.remove("system");
                for (Map.Entry<String, Object> temp : tempMap.entrySet()) {
                    if ("page".equals(temp.getKey())) {
                        map.put("pageNo", temp.getValue());
                    }
                    else {
                        if("token".equals(temp.getKey())){
                            continue;
                        }
                        map.put(temp.getKey(), temp.getValue());
                    }
                }
            }
 
            json = gson.toJson(HttpClient.encrypt(map));
        }else{
            json = gson.toJson(map);
        }
 
        try {
            LogUtil.debugLog(TAG, "reqest: " + url + " data:" + json);
            String openApi = HttpClient.post(url, json, "application/json", "OpenApi",timeOut);
            jsonObject =  new JsonParser().parse(openApi).getAsJsonObject();
        } catch (IOException e) {
            BusinessException b = new BusinessException(e);
            if (e instanceof ConnectTimeoutException || e instanceof SocketTimeoutException
                    || e instanceof UnknownHostException
                    || e instanceof UnknownServiceException || e instanceof SSLException
                    || e instanceof SocketException) {
                b.errorDescription = "操作失败,请检查网络";
            }
            throw b;
        } catch (Throwable e) {
            BusinessException b = new BusinessException(e);
            throw b;
        }
        return jsonObject;
    }
 
    private static JsonObject doPost(String url, String json, int timeOut) throws BusinessException {
        Gson gson = new Gson();
        JsonObject jsonObject = new JsonObject();
        try {
            LogUtil.debugLog(TAG, "reqest: " + url + " data:" + json.toString());
            String openApi = HttpClient.post(url, json, "application/json", "OpenApi",timeOut);
            jsonObject =  new JsonParser().parse(openApi).getAsJsonObject();
        } catch (IOException e) {
            BusinessException b = new BusinessException(e);
            if (e instanceof ConnectTimeoutException || e instanceof SocketTimeoutException
                    || e instanceof UnknownHostException
                    || e instanceof UnknownServiceException || e instanceof SSLException
                    || e instanceof SocketException) {
                b.errorDescription = "操作失败,请检查网络";
            }
            throw b;
        } catch (Throwable e) {
            BusinessException b = new BusinessException(e);
            throw b;
        }
        return jsonObject;
    }
 
    private static Map<String, Object> paramsInit(Map<String, Object> paramsMap) {
        long time = System.currentTimeMillis() / 1000;
        String nonce = UUID.randomUUID().toString();
        String id = UUID.randomUUID().toString();
        StringBuilder paramString = new StringBuilder();
        paramString.append("time:").append(time).append(",");
        paramString.append("nonce:").append(nonce).append(",");
        paramString.append("appSecret:").append(CONST.SECRET);
        String sign = "";
        // 计算MD5得值
        sign = MD5Helper.encodeToLowerCase(paramString.toString().trim());
        Map<String, Object> systemMap = new HashMap<String, Object>();
        systemMap.put("ver", "1.0");
        systemMap.put("sign", sign);
        systemMap.put("appId", CONST.APPID);
        systemMap.put("nonce", nonce);
        systemMap.put("time", time);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("system", systemMap);
        map.put("params", paramsMap);
        map.put("id", id);
        return map;
    }
 
 
    /**
     * 基本类型加密
     * <p>
     * 判断当前值是否需要参与签名,保持跟云端一致
     * 空字符串不参与
     * 数组,集合不参与
     *
     * @return ture 需要加密
     */
    private static boolean valueNeedSign(String valueStr) {
        try {
            if (TextUtils.isEmpty(valueStr)) return false;
            final char[] strChar = URLDecoder.decode(valueStr, "utf-8")
                    .substring(0, 1).toCharArray();
            final char firstChar = strChar[0];
            return firstChar != '{' && firstChar != '[';
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}