Parcourir la Source

修改北向接口缓存方式

konghl 4 ans auparavant
Parent
commit
cbbdfa675d

+ 4 - 3
location-rescue-service/src/main/java/com/ai/bss/location/rescue/controller/MapTagManageController.java

@ -145,11 +145,12 @@ public class MapTagManageController {
145 145
            return CommonResponse.fail("500","删除失败");
146 146
        }
147 147
148
        List<String> mapTagIdsList = null;
148
        List<String> mapTagIdsList = new ArrayList<String>();
149 149
        if (params.get("mapTagIds") instanceof List) {
150
            mapTagIdsList = (List<String>) params.get("mapTagIds");
150
            for (Object object : (List<Object>)params.get("mapTagIds")) {
151
                mapTagIdsList.add(object.toString())   ;
152
            }
151 153
        } else if (params.get("mapTagIds") instanceof String) {
152
            mapTagIdsList = new ArrayList<String>();
153 154
            mapTagIdsList.add((String) params.get("mapTagIds"));
154 155
        } else {
155 156
            return CommonResponse.fail("501","删除失败:标记类型错误");

+ 2 - 2
location-rescue-service/src/main/java/com/ai/bss/location/rescue/service/impl/MapTagManageServiceImpl.java

@ -89,7 +89,7 @@ public class MapTagManageServiceImpl implements MapTagManageService {
89 89
90 90
	@Override
91 91
	public CommonResponse<Void> addMapTagInfo(MapTagInfoDto mapTagInfoDto) throws Exception {
92
		mapTagInfoDto.setMapAreaSetId("0"); //TODO 固定值 所属地图围栏集合标识
92
		mapTagInfoDto.setMapAreaSetId(0L); //TODO 固定值 所属地图围栏集合标识
93 93
		mapTagInfoDto.setPriority("10");  //优先级
94 94
		mapTagInfoDto.setMapType(MapArea.MAP_TYPE_GEOMETRY);	//围栏形状,GEO多边形, RAD 圆形
95 95
@ -105,7 +105,7 @@ public class MapTagManageServiceImpl implements MapTagManageService {
105 105
106 106
	@Override
107 107
	public CommonResponse<Void> modifyMapTagInfo(MapTagInfoDto mapTagInfoDto) throws Exception {
108
		mapTagInfoDto.setMapAreaSetId("0"); //TODO 固定值 所属地图围栏集合标识
108
		mapTagInfoDto.setMapAreaSetId(0L); //TODO 固定值 所属地图围栏集合标识
109 109
		mapTagInfoDto.setPriority("10");  //优先级
110 110
		mapTagInfoDto.setMapType(MapArea.MAP_TYPE_GEOMETRY);	//围栏形状,GEO多边形, RAD 圆形
111 111

+ 41 - 0
location-rescue-service/src/main/java/com/ai/bss/location/rescue/util/CacheUtil.java

@ -0,0 +1,41 @@
1
package com.ai.bss.location.rescue.util;
2
3
import com.ai.ipu.cache.CacheFactory;
4
import com.ai.ipu.cache.ICache;
5
6
public class CacheUtil {
7
    //private static final Logger logger = LoggerFactory.getLogger(CacheUtil.class);
8
9
    private static final String CACHE_NAME = "ssn";
10
11
    public static boolean setCache(String key, Object value) throws Exception {
12
        if (value == null) getCache(key);
13
        if (key == null) new NullPointerException();
14
15
        ICache cache = CacheFactory.getCache(CacheFactory.CacheType.redis, CACHE_NAME);
16
        return cache.put(key, value);
17
    }
18
19
    public static Object getCache(String key) throws Exception {
20
        if (key == null) new NullPointerException();
21
22
        ICache cache = CacheFactory.getCache(CacheFactory.CacheType.redis, CACHE_NAME);
23
        return cache.get(key);
24
    }
25
26
    public static boolean removeCache(String key) throws Exception {
27
        if (key == null) new NullPointerException();
28
29
        ICache cache = CacheFactory.getCache(CacheFactory.CacheType.redis, CACHE_NAME);
30
        return cache.remove(key);
31
        //CacheFactory.close(CACHE_NAME, cache);
32
    }
33
34
    public static boolean keyExists(String value) throws Exception {
35
        if (value == null) new NullPointerException();
36
37
        ICache cache = CacheFactory.getCache(CacheFactory.CacheType.redis, CACHE_NAME);
38
        return cache.keyExists(value);
39
    }
40
41
}

+ 1 - 1
location-rescue-service/src/main/java/com/ai/bss/location/rescue/util/HttpServiceUtil.java

@ -186,7 +186,7 @@ public class HttpServiceUtil {
186 186
		return body;
187 187
	}
188 188
189
	public static String sendGet(String url, Charset encoding) {
189
	public static String sendGet(String url, Charset encoding,Map<String,String> headerMap) {
190 190
		HttpClient httpClient = HttpClients.createDefault();
191 191
		HttpGet httpGet = new HttpGet(url);
192 192
		httpGet.addHeader(HTTP.CONTENT_TYPE, HTTP_CONTENT_TYPE_JSON);

+ 237 - 249
location-rescue-service/src/main/java/com/ai/bss/location/rescue/util/NorthboundInterfaceUtil.java

@ -1,264 +1,252 @@
1 1
package com.ai.bss.location.rescue.util;
2 2
3
import java.nio.charset.Charset;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.Map;
7
import java.util.Set;
8
import java.util.concurrent.ConcurrentHashMap;
9
3
import com.alibaba.fastjson.JSON;
4
import com.alibaba.fastjson.JSONObject;
10 5
import org.slf4j.Logger;
11 6
import org.slf4j.LoggerFactory;
12
import org.springframework.beans.factory.annotation.Configurable;
13 7
import org.springframework.beans.factory.annotation.Value;
14 8
import org.springframework.context.annotation.Configuration;
15
import org.springframework.stereotype.Component;
16 9
17
import com.ai.bss.location.rescue.controller.MapTagManageController;
18
import com.alibaba.fastjson.JSON;
10
import java.nio.charset.Charset;
11
import java.util.HashMap;
12
import java.util.Map;
19 13
20 14
/**
21
 *北向接口统一入口
15
 * 北向接口统一入口
16
 *
22 17
 * @date 2010/09/24 23:42
23 18
 */
24 19
@Configuration
25 20
public class NorthboundInterfaceUtil {
26 21
27
	private static final Logger logger = LoggerFactory.getLogger(MapTagManageController.class);
28
	
29
	@Value("${aap.iot.userCode:IOT_ADMIN}")
30
	private String userCode;
31
32
	@Value("${aap.iot.passWord:123456}")
33
	private String passWord;
34
35
	@Value("${url.iot.login:http://47.105.130.83:8083/sso/login}")
36
	private String iotLoginUrl;
37
38
	@Value("${url.iot.service:http://47.105.130.83:8083/dmp/terminalNorthApi/}")
39
	private String iotServiceUrl;
40
41
	// 定义静态存储map空间存放sign与sessionId
42
	private volatile static Map<String, String> cacheMap = new ConcurrentHashMap<String, String>();// 缓存map
43
44
	// set
45
	public void setMapCache(Map<String, String> map) {
46
		Set<String> set = map.keySet();
47
		Iterator<String> it = set.iterator();
48
		while (it.hasNext()) {
49
			String key = it.next();
50
			cacheMap.put(key, map.get(key));
51
		}
52
	}
53
54
	// get
55
	public Map<String, String> getMapCache() {
56
57
		return cacheMap;
58
	}
59
60
	// 清除cache
61
	public void clear() {
62
		cacheMap.clear();
63
	}
64
65
	/**
66
	 * GET请求调用北向接口方法
67
	 * 
68
	 * @param url
69
	 * @param params
70
	 * @return
71
	 * @throws Exception
72
	 */
73
	public Map<String, Object> iotGetCallUtil(String url) throws Exception {
74
		// 调用北向服务的接口
75
		logger.debug("GET调用北向接口");
76
77
		boolean loginFlag = true;
78
79
		// 1.在缓存中获取sessionId与sign
80
		Map<String, String> mapCache = getMapCache();
81
		if (mapCache.isEmpty() || mapCache.get("sign") == null || mapCache.get("session_id") == null) {
82
			// 2.如果没有调用登录接口从新获取
83
			loginFlag = iotLogin();
84
		}
85
86
		if (!loginFlag) {
87
			logger.debug("调用北向接口登录失败");
88
			return new HashMap<String, Object>();
89
		}
90
91
		// 3.调用北向服务接口
92
		// (1)设置字符集
93
		Charset charset = Charset.forName("utf-8");
94
		// (2)调用接口
95
		String resultJson = HttpServiceUtil.sendGet(iotServiceUrl + url, charset);
96
		// (3)将参数转为Map<String,String>【将返回值统一为String】
97
		Map<String, Object> resultMap = JSON.parseObject(resultJson, Map.class);
98
99
		// 4.登录超时,需重新登录
100
		if ("登录超时".equals(resultMap.get("resultMsg"))) {
101
			logger.info("调用北向接口失败,需重新登录");
102
			// 4.调用不成功可能是登录过期
103
			// (1)清除缓存
104
			clear();
105
			// (2)重新登录
106
			loginFlag = iotLogin();
107
108
			if (!loginFlag) {
109
				logger.debug("再次调用北向接口登录失败");
110
				return new HashMap<String, Object>();
111
			}
112
113
			try {
114
				// (3)再次调用接口
115
				String fianlresultJson = HttpServiceUtil.sendGet(iotServiceUrl + url, charset);
116
				// (4)获取返回值
117
				resultMap = JSON.parseObject(fianlresultJson, Map.class);
118
			} catch (Exception e) {
119
				logger.error("再次调用北向接口失败: " + e.getMessage());
120
				return new HashMap<String, Object>();
121
			}
122
123
			if (resultMap == null) {
124
				logger.error("再次调用北向接口返回值为空");
125
				return new HashMap<String, Object>();
126
			}
127
		}
128
129
		// 5.判断是否调用成功
130
		if (NorthboundInterfaceConstant.resultCode_succeed.equals(resultMap.get("resultCode"))) {
131
			logger.info("调用北向接口成功");
132
		} else {
133
			logger.info("调用北向接口失败");
134
		}
135
136
		return resultMap;
137
	}
138
139
	/**
140
	 * POST请求调用北向接口方法
141
	 * 
142
	 * @param url
143
	 * @param paramsMap
144
	 * @return
145
	 * @throws Exception
146
	 */
147
	public Map<String, Object> iotPostCallUtil(String url, Map<String, Object> paramsMap) throws Exception {
148
		// 调用北向服务的接口
149
		logger.debug("POSt调用北向接口");
150
151
		boolean loginFlag = true;
152
153
		// 1.在缓存中获取sessionId与sign
154
		Map<String, String> mapCache = getMapCache();
155
		if (mapCache.isEmpty() || mapCache.get("sign") == null || mapCache.get("session_id") == null) {
156
			// 2.如果没有调用登录接口从新获取
157
			loginFlag = iotLogin();
158
		}
159
160
		if (!loginFlag) {
161
			logger.debug("调用北向接口登录失败");
162
			return new HashMap<String, Object>();
163
		}
164
165
		// 3.调用北向服务接口
166
		// (1)设置字符集
167
		Charset charset = Charset.forName("utf-8");
168
		// 返回值
169
		Map<String, Object> resultMap = null;
170
		try {
171
			// (2)调用接口
172
			String resultJson = HttpServiceUtil.sendPost(iotServiceUrl + url, paramsMap, charset, getMapCache());
173
			// (3)将参数转为Map<String,String>【将返回值统一为String】
174
			resultMap = JSON.parseObject(resultJson, Map.class);
175
		} catch (Exception e) {
176
			logger.error("调用北向接口失败: " + e.getMessage());
177
			return new HashMap<String, Object>();
178
		}
179
180
		if (resultMap == null) {
181
			logger.error("调用北向接口返回值为空");
182
			return new HashMap<String, Object>();
183
		}
184
185
		// 4.登录超时,需重新登录
186
		if ("登录超时".equals(resultMap.get("resultMsg"))) {
187
			logger.info("调用北向接口登录超时,需重新登录");
188
			// 4.调用不成功可能是登录过期
189
			// (1)清除缓存
190
			clear();
191
			// (2)重新登录
192
			loginFlag = iotLogin();
193
194
			if (!loginFlag) {
195
				logger.debug("再次调用北向接口登录失败");
196
				return new HashMap<String, Object>();
197
			}
198
199
			try {
200
				// (3)再次调用接口
201
				String fianlresultJson = HttpServiceUtil.sendPost(iotServiceUrl + url, paramsMap, charset,
202
						getMapCache());
203
				// (4)获取返回值
204
				resultMap = JSON.parseObject(fianlresultJson, Map.class);
205
			} catch (Exception e) {
206
				logger.error("再次调用北向接口失败: " + e.getMessage());
207
				return new HashMap<String, Object>();
208
			}
209
210
			if (resultMap == null) {
211
				logger.error("再次调用北向接口返回值为空");
212
				return new HashMap<String, Object>();
213
			}
214
		}
215
216
		// 5.判断是否调用成功
217
		if (NorthboundInterfaceConstant.resultCode_succeed.equals(resultMap.get("resultCode"))) {
218
			logger.info("调用北向接口成功");
219
		} else {
220
			logger.info("调用北向接口失败");
221
		}
222
223
		return resultMap;
224
	}
225
226
	/**
227
	 * 调用登录接口获取sessionId与sign
228
	 * 
229
	 * @return
230
	 */
231
	private boolean iotLogin() {
232
		logger.debug("登录北向接口");
233
234
		// 调用登录接口获取sessionId与sign
235
		HashMap<String, Object> loginParamMap = new HashMap<>();
236
		loginParamMap.put("userCode", userCode);
237
		loginParamMap.put("passWord", passWord);
238
239
		// 设置字符集
240
		Charset charset = Charset.forName("utf-8");
241
242
		Map loginResultMap = null;
243
		try {
244
			// 调用登录接口
245
			String loginResult = HttpServiceUtil.sendPost(iotLoginUrl, loginParamMap, charset);
246
			loginResultMap = JSON.parseObject(loginResult, Map.class);
247
		} catch (Exception e) {
248
			logger.error("登录北向接口失败: " + e.getMessage());
249
			return false;
250
		}
251
252
		if (loginResultMap != null && NorthboundInterfaceConstant.resultCode_succeed
253
				.equals(String.valueOf(loginResultMap.get("resultCode")))) {
254
			logger.info("登录北向接口成功");
255
			Map result = (Map) loginResultMap.get("result");
256
			// 将数据存到缓存中
257
			setMapCache(result);
258
			return true;
259
		} else {
260
			logger.info("登录北向接口失败");
261
			return false;
262
		}
263
	}
22
    private static final Logger logger = LoggerFactory.getLogger(NorthboundInterfaceUtil.class);
23
24
    @Value("${aap.iot.userCode:IOT_ADMIN}")
25
    private String userCode;
26
27
    @Value("${aap.iot.passWord:123456}")
28
    private String passWord;
29
30
    @Value("${url.iot.login:http://47.105.130.83:8083/sso/login}")
31
    private String iotLoginUrl;
32
33
    @Value("${url.iot.service:http://47.105.130.83:8083/dmp/terminalNorthApi/}")
34
    private String iotServiceUrl;
35
36
    /**
37
     * GET请求调用北向接口方法
38
     *
39
     * @param url
40
     * @return
41
     * @throws Exception
42
     */
43
    public Map<String, Object> iotGetCallUtil(String url) throws Exception {
44
        logger.debug("GET调用北向接口");
45
        Map<String, Object> resultMap = new HashMap<String, Object>();
46
        Map<String, String> signMap = new HashMap<String, String>();
47
48
        // 1.在缓存中获取sessionId与sign
49
        Object sign =CacheUtil.getCache("ebc_iot_north_sign");
50
        Object session_id = CacheUtil.getCache("ebc_iot_north_session_id");
51
52
        if (sign == null ) {
53
            // 2.如果没有调用登录接口从新获取
54
            signMap = iotLogin();
55
56
            if (signMap == null) {
57
                logger.debug("调用北向接口登录失败");
58
                return resultMap;
59
            }
60
61
        } else {
62
            signMap.put("ebc_iot_north_sign", String.valueOf(sign));
63
            signMap.put("ebc_iot_north_session_id", String.valueOf(session_id));
64
65
        }
66
67
        // 3.调用北向服务接口
68
        // (1)设置字符集
69
        Charset charset = Charset.forName("utf-8");
70
        try {
71
            // (2)调用接口
72
            String resultJson = HttpServiceUtil.sendGet(iotServiceUrl + url, charset, signMap);
73
            // (3)将参数转为Map<String,String>【将返回值统一为String】
74
            resultMap = JSON.parseObject(resultJson, Map.class);
75
        } catch (Exception e) {
76
            logger.error("调用北向接口失败: " + e.getMessage());
77
            return new HashMap<String, Object>();
78
        }
79
80
        if (resultMap == null) {
81
            logger.error("调用北向接口返回值为空");
82
            return new HashMap<String, Object>();
83
84
        } else if ("登录超时".equals(resultMap.get("resultMsg"))) {
85
            // 4.登录超时,需重新登录
86
            logger.info("调用北向接口失败,需重新登录");
87
            // (1)清除缓存
88
            CacheUtil.removeCache("ebc_iot_north_sign");
89
            CacheUtil.removeCache("ebc_iot_north_session_id");
90
            // (2)重新登录
91
            signMap = iotLogin();
92
93
            if (signMap == null) {
94
                logger.debug("再次调用北向接口登录失败");
95
                return resultMap;
96
            }
97
98
            try {
99
                // (3)再次调用接口
100
                String resultJson = HttpServiceUtil.sendGet(iotServiceUrl + url, charset, signMap);
101
                // (4)获取返回值
102
                resultMap = JSON.parseObject(resultJson, Map.class);
103
            } catch (Exception e) {
104
                logger.error("再次调用北向接口失败: " + e.getMessage());
105
                return new HashMap<String, Object>();
106
            }
107
108
            if (resultMap == null) {
109
                logger.error("再次调用北向接口返回值为空");
110
                return new HashMap<String, Object>();
111
            }
112
        }
113
114
        // 5.判断是否调用成功
115
        if (NorthboundInterfaceConstant.resultCode_succeed.equals(resultMap.get("resultCode"))) {
116
            logger.info("调用北向接口成功");
117
            return resultMap;
118
        } else {
119
            logger.info("调用北向接口失败");
120
            return new HashMap<String, Object>();
121
        }
122
    }
123
124
    /**
125
     * POST请求调用北向接口方法
126
     *
127
     * @param url
128
     * @param paramsMap
129
     * @return
130
     * @throws Exception
131
     */
132
    public Map<String, Object> iotPostCallUtil(String url, Map<String, Object> paramsMap) throws Exception {
133
        logger.debug("POSt调用北向接口");
134
        Map<String, Object> resultMap = new HashMap<String, Object>();
135
        Map<String, String> signMap = new HashMap<String, String>();
136
137
        // 1.在缓存中获取sessionId与sign
138
        Object sign =CacheUtil.getCache("ebc_iot_north_sign");
139
        Object session_id = CacheUtil.getCache("ebc_iot_north_session_id");
140
141
        if (sign == null ) {
142
            // 2.如果没有调用登录接口从新获取
143
            signMap = iotLogin();
144
145
            if (signMap == null) {
146
                logger.debug("调用北向接口登录失败");
147
                return resultMap;
148
            }
149
150
        } else {
151
            signMap.put("ebc_iot_north_sign", String.valueOf(sign));
152
            signMap.put("ebc_iot_north_session_id", String.valueOf(session_id));
153
154
        }
155
156
        // 3.调用北向服务接口
157
        // (1)设置字符集
158
        Charset charset = Charset.forName("utf-8");
159
        try {
160
            // (2)调用接口
161
            String resultJson = HttpServiceUtil.sendPost(iotServiceUrl + url, paramsMap, charset, signMap);
162
            // (3)将参数转为Map<String,String>【将返回值统一为String】
163
            resultMap = JSON.parseObject(resultJson, Map.class);
164
        } catch (Exception e) {
165
            logger.error("调用北向接口失败: " + e.getMessage());
166
            return new HashMap<String, Object>();
167
        }
168
169
        if (resultMap == null) {
170
            logger.error("调用北向接口返回值为空");
171
            return new HashMap<String, Object>();
172
        } else if ("登录超时".equals(resultMap.get("resultMsg"))) {
173
            // 4.登录超时,需重新登录
174
            logger.info("调用北向接口登录超时,需重新登录");
175
            // (1)清除缓存
176
            CacheUtil.removeCache("ebc_iot_north_sign");
177
            CacheUtil.removeCache("ebc_iot_north_session_id");
178
            // (2)重新登录
179
            signMap = iotLogin();
180
181
            if (signMap == null) {
182
                logger.debug("再次调用北向接口登录失败");
183
                return resultMap;
184
            }
185
186
            try {
187
                // (3)再次调用接口
188
                String resultJson = HttpServiceUtil.sendPost(iotServiceUrl + url, paramsMap, charset, signMap);
189
                // (4)获取返回值
190
                resultMap = JSON.parseObject(resultJson, Map.class);
191
            } catch (Exception e) {
192
                logger.error("再次调用北向接口失败: " + e.getMessage());
193
                return new HashMap<String, Object>();
194
            }
195
196
            if (resultMap == null) {
197
                logger.error("再次调用北向接口返回值为空");
198
                return new HashMap<String, Object>();
199
            }
200
        }
201
202
        // 5.判断是否调用成功
203
        if (NorthboundInterfaceConstant.resultCode_succeed.equals(resultMap.get("resultCode"))) {
204
            logger.info("调用北向接口成功");
205
            return resultMap;
206
        } else {
207
            logger.info("调用北向接口失败");
208
            return new HashMap<String, Object>();
209
        }
210
    }
211
212
    /**
213
     * 调用登录接口获取sessionId与sign
214
     *
215
     * @return
216
     */
217
    private Map<String, String> iotLogin() throws Exception {
218
        logger.debug("登录北向接口");
219
        HashMap<String, Object> loginParamMap = new HashMap<>();
220
221
        // 调用登录接口获取sessionId与sign
222
        loginParamMap.put("userCode", userCode);
223
        loginParamMap.put("passWord", passWord);
224
225
        // 设置字符集
226
        Charset charset = Charset.forName("utf-8");
227
228
        //Map<String, String> loginResultMap =new HashMap<String,String>();
229
        JSONObject loginResultJsonObject = null;
230
        try {
231
            // 调用登录接口
232
            String loginResult = HttpServiceUtil.sendPost(iotLoginUrl, loginParamMap, charset);
233
            loginResultJsonObject = JSON.parseObject(loginResult);
234
        } catch (Exception e) {
235
            logger.error("登录北向接口失败: " + e.getMessage());
236
            return null;
237
        }
238
239
        if (loginResultJsonObject != null && NorthboundInterfaceConstant.resultCode_succeed.equals(String.valueOf(loginResultJsonObject.get("resultCode")))) {
240
            logger.info("登录北向接口成功");
241
242
            Map<String, String> resultMap = (Map<String, String>) loginResultJsonObject.get("result");
243
            // 将数据存到缓存中
244
            CacheUtil.setCache("ebc_iot_north_sign", resultMap.get("sign"));
245
            CacheUtil.setCache("ebc_iot_north_session_id", resultMap.get("session_id"));
246
            return resultMap;
247
        } else {
248
            logger.info("登录北向接口失败");
249
            return null;
250
        }
251
    }
264 252
}