>
                }
177
            }
178
179
        } catch (MalformedURLException e) {
180
            e.printStackTrace();
181
        } catch (IOException e) {
182
            e.printStackTrace();
183
        } finally {
184
            //关闭连接
185
            if(br!=null){
186
                try {
187
                    br.close();
188
                } catch (IOException e) {
189
                    e.printStackTrace();
190
                }
191
            }
192
            if(os!=null){
193
                try {
194
                    os.close();
195
                } catch (IOException e) {
196
                    e.printStackTrace();
197
                }
198
            }
199
            if(is!=null){
200
                try {
201
                    is.close();
202
                } catch (IOException e) {
203
                    e.printStackTrace();
204
                }
205
            }
206
            //关闭连接
207
            connection.disconnect();
208
        }
209
        return result.toString();
210
    }
211
212
213
    /**
214
     * 调用登录接口获取sessionId与sign
215
     * @return
216
     */
217
    public static Map<String,String> iotLogin() {
218
        HashMap<String, String> paramHashMap = new HashMap<>();
219
        //调用登录接口获取sessionId与sign
220
        //设置登录接口参数
221
        String loginParam=" {\"userCode\": "+userCode+",\"passWord\":"+passWord +"}";
222
        String loginResult =doPost(UrlAddress.IOT_LOGIN, loginParam,paramHashMap);
223
        Map mapType = JSON.parseObject(loginResult,Map.class);
224
        Map result = (Map) mapType.get("result");
225
226
        if ((int)mapType.get("resultCode")==0){
227
            //将数据存到
228
            HttpURLConnectionUtil.setMapCache(result);
229
        }
230
        return cacheMap;
231
    }
232
    /**
233
     * 调用北向接口方法
234
     * @return
235
     */
236
    public static Map iotCallMothod(JMap params, String str) throws Exception {
237
        //调用北向服务的接口
238
        //1.在缓存中获取sessionId与sign
239
        Map<String, String> mapCache = HttpURLConnectionUtil.getMapCache();
240
        if(mapCache.isEmpty()||mapCache.get("sign")==null||mapCache.get("sessionId")==null){
241
            //2.如果没有调用登录接口从新获取
242
            HttpURLConnectionUtil.iotLogin();
243
        }
244
        //3.调用北向服务接口
245
        //(1)将参数转为json
246
        String  jmapParam= JSON.toJSONString(params);
247
        //(2)调用接口
248
        String resultJson = HttpURLConnectionUtil.doPost(str, jmapParam,mapCache);
249
        //(3)将参数转为json
250
        Map<String,String> resultMap = JSON.parseObject(resultJson, Map.class);
251
        //判断是否调用成功
252
        if("0".equals(resultMap.get("resultCode"))){
253
            //成功返回
254
            return resultMap;
255
        }else{
256
            //4.调用不成功过期,清除缓存,在调用登录接口
257
            HttpURLConnectionUtil.clear();
258
            HttpURLConnectionUtil.iotLogin();
259
            //5.调用北向服务接口
260
            //(2)调用接口
261
            String fianlresultJson = HttpURLConnectionUtil.doPost(str, jmapParam,mapCache);
262
            //(3)将json转为f返回值
263
            Map<String,String> fianlresultMap = JSON.parseObject(fianlresultJson, Map.class);
264
            //返回
265
            return fianlresultMap;
266
        }
267
    }
25
	private static final ILogger logger = IpuLoggerFactory.createLogger(HttpURLConnectionUtil.class);
26
27
	private static String userCode;
28
	private static String passWord;
29
30
	@Value("${aap.iot.userCode}")
31
	public void setUserCode(String userCode) {
32
		HttpURLConnectionUtil.userCode = userCode;
33
	}
34
35
	@Value("${aap.iot.passWord}")
36
	public void setPassWord(String passWord) {
37
		HttpURLConnectionUtil.passWord = passWord;
38
	}
39
40
	// 定义静态存储map空间存放sign与sessionId
41
	private volatile static Map<String, String> cacheMap = new ConcurrentHashMap<String, String>();// 缓存map
42
43
	// set
44
	public static void setMapCache(Map<String, String> map) {
45
		Set<String> set = map.keySet();
46
		Iterator<String> it = set.iterator();
47
		while (it.hasNext()) {
48
			String key = it.next();
49
			cacheMap.put(key, map.get(key));
50
		}
51
	}
52
53
	// get
54
	public static Map<String, String> getMapCache() {
55
56
		return cacheMap;
57
	}
58
59
	// 清除cache
60
	public static void clear() {
61
		cacheMap.clear();
62
	}
63
64
	/**
65
	 * 调用登录接口获取sessionId与sign
66
	 * 
67
	 * @return
68
	 */
69
	public static Map<String, String> iotLogin() {
70
		logger.debug("登录北向接口");
71
72
		// 调用登录接口获取sessionId与sign
73
		HashMap<String, String> loginParamMap = new HashMap<>();
74
		loginParamMap.put("userCode", userCode);
75
		loginParamMap.put("passWord", passWord);
76
77
		// 设置字符集
78
		Charset charset = Charset.forName("utf-8");
79
80
		// 调用登录接口
81
		String loginResult = HttpServiceUtil.sendPost(UrlAddress.IOT_LOGIN, loginParamMap, charset);
82
83
		Map mapType = JSON.parseObject(loginResult, Map.class);
84
		Map result = (Map) mapType.get("result");
85
86
		if ("0".equals(String.valueOf(mapType.get("resultCode")))) {
87
			logger.info("登录北向接口成功");
88
			// 将数据存到缓存中
89
			HttpURLConnectionUtil.setMapCache(result);
90
		} else {
91
			logger.info("登录北向接口失败");
92
		}
93
		return cacheMap;
94
	}
95
96
	/**
97
	 * 调用北向接口方法
98
	 * 
99
	 * @return
100
	 */
101
	public static Map<String, String> iotCallMothod(String url, Map<String, String> params) throws Exception {
102
		// 调用北向服务的接口
103
		logger.debug("调用北向接口");
104
105
		// 1.在缓存中获取sessionId与sign
106
		Map<String, String> mapCache = HttpURLConnectionUtil.getMapCache();
107
		if (mapCache.isEmpty() || mapCache.get("sign") == null || mapCache.get("sessionId") == null) {
108
			// 2.如果没有调用登录接口从新获取
109
			HttpURLConnectionUtil.iotLogin();
110
		}
111
112
		// 3.调用北向服务接口
113
		// 设置字符集
114
		Charset charset = Charset.forName("utf-8");
115
		// (1)调用接口
116
		String resultJson = HttpServiceUtil.sendPost(url, params, charset);
117
		// (2)将参数转为Map<String,String>【将返回值统一为String】
118
		Map<String, String> resultMap = JSON.parseObject(resultJson, Map.class);
119
120
		// 登录超时,需重新登录
121
		if ("登录超时".equals(resultMap.get("resultMsg"))) {
122
			logger.info("调用北向接口失败,需重新登录");
123
			// 4.调用不成功可能是登录过期
124
			// (1)清除缓存
125
			HttpURLConnectionUtil.clear();
126
			// (2)重新登录
127
			HttpURLConnectionUtil.iotLogin();
128
			// (3)再次调用接口
129
			String fianlresultJson = HttpServiceUtil.sendPost(url, params, charset);
130
			// (4)获取返回值
131
			resultMap = JSON.parseObject(fianlresultJson, Map.class);
132
		}
133
134
		// 判断是否调用成功
135
		if ("0".equals(resultMap.get("resultCode"))) {
136
			logger.info("调用北向接口成功");
137
		} else {
138
			logger.info("调用北向接口失败");
139
		}
140
141
		return resultMap;
142
	}
268 143
269 144
}

+ 76 - 0
ebc-sea-platform/src/main/java/com/ai/ipu/server/util/ProductEnum.java

@ -0,0 +1,76 @@
1
package com.ai.ipu.server.util;
2
3
import java.util.HashMap;
4
import java.util.Map;
5
6
/**
7
 * 设备类型枚举
8
 * 
9
 * @author konghl@asiainfo.com 
10
 * 2020-10-19
11
 */
12
public enum ProductEnum {
13
	// 船舶
14
	ship("001", "船舶", 1),
15
	// 风机
16
	fan("002", "风机", 2),
17
	// 升压站
18
	booster("003", "升压站", 2),
19
	// 测风塔
20
	anemometer("004", "测风塔", 2);
21
22
	private String productId;
23
	private String productName;
24
	private int productType;
25
26
	private ProductEnum(String productId, String productName, int productType) {
27
		this.productId = productId;
28
		this.productName = productName;
29
		this.productType = productType;
30
	}
31
32
	public String getProductId() {
33
		return productId;
34
	}
35
36
	public String getProductName() {
37
		return productName;
38
	}
39
40
	public int getProductType() {
41
		return productType;
42
	}
43
44
	/**
45
	 * 根据类型获取对应的产品类型列表
46
	 * 
47
	 * @param type 类型(1:绑定终端,2:不绑定终端)
48
	 * @return
49
	 */
50
	public static Map<String, Object> getProductByType(int type) {
51
		Map<String, Object> map = new HashMap<String, Object>();
52
53
		for (ProductEnum productEnum : ProductEnum.values()) {
54
			if (productEnum.getProductType() == type) {
55
				map.put("id", productEnum.getProductId());
56
				map.put("name", productEnum.getProductName());
57
			}
58
		}
59
		return map;
60
	}
61
62
	/**
63
	 * 获取所有的产品类型列表
64
	 * 
65
	 * @return
66
	 */
67
	public static Map<String, Object> getAllProduct() {
68
		Map<String, Object> map = new HashMap<String, Object>();
69
70
		for (ProductEnum productEnum : ProductEnum.values()) {
71
			map.put("id", productEnum.getProductId());
72
			map.put("name", productEnum.getProductName());
73
		}
74
		return map;
75
	}
76
}

登入 - Nuosi Git Service

登入