|
@@ -57,35 +57,38 @@ public class ZHBDUtil {
|
|
|
* @return
|
|
* @return
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
- public Map<Integer, IotDeviceCarFuelVO> carFuels(String carIds, String startDate, String endDate) throws Exception {
|
|
|
|
|
- String zhbdtoken = redisTemplate.opsForValue().get("zhbdtoken");
|
|
|
|
|
- String clientId = redisTemplate.opsForValue().get("zhdbClientId");
|
|
|
|
|
- if (StrUtil.isBlank(zhbdtoken) || StrUtil.isBlank(clientId)) {
|
|
|
|
|
- Map<String, String> map = new HashMap<>();
|
|
|
|
|
- map.put("id", ID);
|
|
|
|
|
- map.put("secret", SECRET);
|
|
|
|
|
- JSONObject entries = restTemplate.postForObject(TOKEN_URL, map, JSONObject.class);
|
|
|
|
|
- if (Objects.nonNull(entries)&&entries.get("code") != null&&Integer.parseInt(String.valueOf(entries.get("code")))==200) {
|
|
|
|
|
- JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(entries.get("data")));
|
|
|
|
|
- zhbdtoken = jsonObject.get("token").toString();
|
|
|
|
|
- // 缓存 client_id
|
|
|
|
|
- clientId = jsonObject.get("client_id").toString();
|
|
|
|
|
- redisTemplate.opsForValue().set("zhbdtoken", zhbdtoken, 80000, TimeUnit.SECONDS);
|
|
|
|
|
- redisTemplate.opsForValue().set("zhdbClientId", clientId, 80000, TimeUnit.SECONDS);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // 构建请求JSON
|
|
|
|
|
- Map<String, Object> requestMap = new HashMap<>();
|
|
|
|
|
- requestMap.put("client_ids", clientId);
|
|
|
|
|
- requestMap.put("vehicle_ids", carIds);
|
|
|
|
|
- requestMap.put("begin_time", startDate);
|
|
|
|
|
- requestMap.put("end_time", endDate);
|
|
|
|
|
- requestMap.put("page_num", 1);
|
|
|
|
|
- requestMap.put("page_size", 10);
|
|
|
|
|
|
|
+ public Map<Integer, IotDeviceCarFuelVO> carFuels(String carIds, String startDate, String endDate) {
|
|
|
// key车辆carId value油耗值
|
|
// key车辆carId value油耗值
|
|
|
Map<Integer, IotDeviceCarFuelVO> carOilFuelPair = new HashMap<>();
|
|
Map<Integer, IotDeviceCarFuelVO> carOilFuelPair = new HashMap<>();
|
|
|
- if (StrUtil.isNotBlank(zhbdtoken)) {
|
|
|
|
|
- try {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ String zhbdtoken = redisTemplate.opsForValue().get("zhbdtoken");
|
|
|
|
|
+ String clientId = redisTemplate.opsForValue().get("zhdbClientId");
|
|
|
|
|
+ if (StrUtil.isBlank(zhbdtoken) || StrUtil.isBlank(clientId)) {
|
|
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
|
|
+ map.put("id", ID);
|
|
|
|
|
+ map.put("secret", SECRET);
|
|
|
|
|
+ JSONObject entries = restTemplate.postForObject(TOKEN_URL, map, JSONObject.class);
|
|
|
|
|
+ if (Objects.nonNull(entries)&&entries.get("code") != null&&Integer.parseInt(String.valueOf(entries.get("code")))==200) {
|
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(entries.get("data")));
|
|
|
|
|
+ zhbdtoken = jsonObject.get("token").toString();
|
|
|
|
|
+ // 缓存 client_id
|
|
|
|
|
+ clientId = jsonObject.get("client_id").toString();
|
|
|
|
|
+ redisTemplate.opsForValue().set("zhbdtoken", zhbdtoken, 80000, TimeUnit.SECONDS);
|
|
|
|
|
+ redisTemplate.opsForValue().set("zhdbClientId", clientId, 80000, TimeUnit.SECONDS);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (StrUtil.isNotBlank(zhbdtoken)) {
|
|
|
|
|
+ // 构建请求JSON
|
|
|
|
|
+ Map<String, Object> requestMap = new HashMap<>();
|
|
|
|
|
+ requestMap.put("client_ids", clientId);
|
|
|
|
|
+ requestMap.put("vehicle_ids", carIds);
|
|
|
|
|
+ requestMap.put("begin_time", startDate);
|
|
|
|
|
+ requestMap.put("end_time", endDate);
|
|
|
|
|
+ requestMap.put("page_num", 1);
|
|
|
|
|
+ requestMap.put("page_size", 10);
|
|
|
|
|
+
|
|
|
String jsonResponse = sendPostRequest(requestMap, zhbdtoken, DAILY_FUEL_STATISTICS_URL);
|
|
String jsonResponse = sendPostRequest(requestMap, zhbdtoken, DAILY_FUEL_STATISTICS_URL);
|
|
|
ApiOilResponse response = parseResponse(jsonResponse);
|
|
ApiOilResponse response = parseResponse(jsonResponse);
|
|
|
if (response != null && response.code == 200) {
|
|
if (response != null && response.code == 200) {
|
|
@@ -104,10 +107,12 @@ public class ZHBDUtil {
|
|
|
carOilFuelPair.put(carId, carFuel);
|
|
carOilFuelPair.put(carId, carFuel);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.info("获取油耗数据失败!");
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.info("获取油耗数据失败!");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return carOilFuelPair;
|
|
return carOilFuelPair;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -118,28 +123,28 @@ public class ZHBDUtil {
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
public Map<String, Integer> carLocations(String plates) throws Exception {
|
|
public Map<String, Integer> carLocations(String plates) throws Exception {
|
|
|
- String zhbdtoken = redisTemplate.opsForValue().get("zhbdtoken");
|
|
|
|
|
- if (StrUtil.isBlank(zhbdtoken)) {
|
|
|
|
|
- Map<String, String> map = new HashMap<>();
|
|
|
|
|
- map.put("id", ID);
|
|
|
|
|
- map.put("secret", SECRET);
|
|
|
|
|
- JSONObject entries = restTemplate.postForObject(TOKEN_URL, map, JSONObject.class);
|
|
|
|
|
- if (Objects.nonNull(entries)&&entries.get("code") != null&&Integer.parseInt(String.valueOf(entries.get("code")))==200) {
|
|
|
|
|
- JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(entries.get("data")));
|
|
|
|
|
- zhbdtoken = jsonObject.get("token").toString();
|
|
|
|
|
- redisTemplate.opsForValue().set("zhbdtoken", zhbdtoken, 80000, TimeUnit.SECONDS);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // 构建请求JSON
|
|
|
|
|
- Map<String, Object> requestMap = new HashMap<>();
|
|
|
|
|
- // 车牌号码 用逗号分隔,例如(示例车牌1,示例车牌2)
|
|
|
|
|
- requestMap.put("plates", plates);
|
|
|
|
|
- requestMap.put("time", null);
|
|
|
|
|
-
|
|
|
|
|
// key车牌号 value车辆carId
|
|
// key车牌号 value车辆carId
|
|
|
Map<String, Integer> plateCarIdPair = new HashMap<>();
|
|
Map<String, Integer> plateCarIdPair = new HashMap<>();
|
|
|
- if (StrUtil.isNotBlank(zhbdtoken)) {
|
|
|
|
|
- try {
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ String zhbdtoken = redisTemplate.opsForValue().get("zhbdtoken");
|
|
|
|
|
+ if (StrUtil.isBlank(zhbdtoken)) {
|
|
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
|
|
+ map.put("id", ID);
|
|
|
|
|
+ map.put("secret", SECRET);
|
|
|
|
|
+ JSONObject entries = restTemplate.postForObject(TOKEN_URL, map, JSONObject.class);
|
|
|
|
|
+ if (Objects.nonNull(entries)&&entries.get("code") != null&&Integer.parseInt(String.valueOf(entries.get("code")))==200) {
|
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(entries.get("data")));
|
|
|
|
|
+ zhbdtoken = jsonObject.get("token").toString();
|
|
|
|
|
+ redisTemplate.opsForValue().set("zhbdtoken", zhbdtoken, 80000, TimeUnit.SECONDS);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 构建请求JSON
|
|
|
|
|
+ Map<String, Object> requestMap = new HashMap<>();
|
|
|
|
|
+ // 车牌号码 用逗号分隔,例如(示例车牌1,示例车牌2)
|
|
|
|
|
+ requestMap.put("plates", plates);
|
|
|
|
|
+ requestMap.put("time", null);
|
|
|
|
|
+
|
|
|
|
|
+ if (StrUtil.isNotBlank(zhbdtoken)) {
|
|
|
String jsonResponse = sendPostRequest(requestMap, zhbdtoken, GET_LOCATION_USE_PLATES_URL);
|
|
String jsonResponse = sendPostRequest(requestMap, zhbdtoken, GET_LOCATION_USE_PLATES_URL);
|
|
|
ApiOilResponse response = parseResponse(jsonResponse);
|
|
ApiOilResponse response = parseResponse(jsonResponse);
|
|
|
if (response != null && response.code == 200) {
|
|
if (response != null && response.code == 200) {
|
|
@@ -154,36 +159,41 @@ public class ZHBDUtil {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.info("获取油耗数据失败!");
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.info("获取车辆位置数据失败!");
|
|
|
}
|
|
}
|
|
|
return plateCarIdPair;
|
|
return plateCarIdPair;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private String sendPostRequest(Map<String, Object> requestData, String token, String requestUrl) throws IOException {
|
|
|
|
|
|
|
+ private String sendPostRequest(Map<String, Object> requestData, String token, String requestUrl) {
|
|
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- String json = objectMapper.writeValueAsString(requestData);
|
|
|
|
|
- okhttp3.RequestBody body = okhttp3.RequestBody.create(json, okhttp3.MediaType.parse("application/json"));
|
|
|
|
|
-
|
|
|
|
|
- Request request = new Request.Builder()
|
|
|
|
|
- .url(requestUrl)
|
|
|
|
|
- .post(body)
|
|
|
|
|
- .addHeader("Content-Type", "application/json")
|
|
|
|
|
- .addHeader("Authorization", "Bearer " + token)// 关键头信息
|
|
|
|
|
- .build();
|
|
|
|
|
- String responseData = "";
|
|
|
|
|
- try (Response response = client.newCall(request).execute()) {
|
|
|
|
|
- if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
|
|
|
|
|
- responseData = response.body().string();
|
|
|
|
|
- System.out.println(responseData);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ String json = objectMapper.writeValueAsString(requestData);
|
|
|
|
|
+ okhttp3.RequestBody body = okhttp3.RequestBody.create(json, okhttp3.MediaType.parse("application/json"));
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(requestUrl)
|
|
|
|
|
+ .post(body)
|
|
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
|
|
+ .addHeader("Authorization", "Bearer " + token)
|
|
|
|
|
+ .build();
|
|
|
|
|
|
|
|
- return responseData;
|
|
|
|
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
|
|
+ logger.warn("第三方接口返回非200状态码: {}", response.code());
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ String responseData = response.body().string();
|
|
|
|
|
+ logger.debug("第三方接口返回数据: {}", responseData);
|
|
|
|
|
+ return responseData;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ // 任何网络/SSL/解析异常都被这里吞掉,返回 null
|
|
|
|
|
+ logger.error("调用第三方接口失败,URL: {}", requestUrl, e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private ApiOilResponse parseResponse(String json) throws IOException {
|
|
private ApiOilResponse parseResponse(String json) throws IOException {
|