diff --git a/zc-business/src/main/java/com/zc/business/constant/RedisKeyConstants.java b/zc-business/src/main/java/com/zc/business/constant/RedisKeyConstants.java index cddabc1c..cd29ee5e 100644 --- a/zc-business/src/main/java/com/zc/business/constant/RedisKeyConstants.java +++ b/zc-business/src/main/java/com/zc/business/constant/RedisKeyConstants.java @@ -45,4 +45,8 @@ public class RedisKeyConstants * 企业微信access_token */ public static final String QYWX_ACCESS_TOKEN = "qywx:accessToken"; + /** + * 交通流 + */ + public static final String TRAFFIC_FLOW = "trafficFlow"; } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java index f487742f..44374b7c 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java @@ -1,19 +1,21 @@ package com.zc.business.service.impl; import cn.hutool.core.bean.BeanUtil; -import cn.hutool.core.date.DateUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.zc.business.domain.*; -import com.zc.business.enums.ChannelCongestionLevelEnum; +import com.ruoyi.common.core.redis.RedisCache; +import com.ruoyi.common.utils.spring.SpringUtils; +import com.zc.business.constant.RedisKeyConstants; +import com.zc.business.domain.DcCongestionSection; +import com.zc.business.domain.DcFacility; +import com.zc.business.domain.DcRoadSection; +import com.zc.business.domain.DcRoadSectionCongestion; import com.zc.business.enums.StakeMarkRange; -import com.zc.business.enums.TrafficCompositionRateEnum; import com.zc.business.service.IDcFacilityService; import com.zc.business.service.IDcRoadSectionService; import com.zc.business.service.IDcTrafficStatisticsService; -import com.zc.business.statistics.cache.metrics.DailyGantryMetricsStatisticsCache; import com.zc.business.utils.StakeMarkUtils; import com.zc.common.core.httpclient.OkHttp; import com.zc.common.core.httpclient.exception.HttpException; @@ -24,7 +26,6 @@ import okhttp3.Response; import okhttp3.ResponseBody; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; @@ -32,6 +33,7 @@ import javax.annotation.Resource; import java.io.IOException; import java.time.LocalTime; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -42,7 +44,6 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi protected final Logger logger = LoggerFactory.getLogger(this.getClass()); // 智慧高速平台token - private JSONObject token = null; private final String sysid = "sdgs_it_hs_jihe"; private final String baseUrl = "http://10.166.139.16:8080"; @@ -50,6 +51,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi @Resource private IDcFacilityService facilityService; + @Resource private IDcRoadSectionService roadSectionService; @@ -70,7 +72,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi * 2. 使用POST方法向指定URL发送请求,以获取新的访问令牌。 * 3. 在请求成功时,解析响应体中的令牌信息并更新本地存储的令牌。 */ - @Scheduled(cron = "0 0 */5 * * ?") + //@Scheduled(cron = "0 0 */5 * * ?") public void refreshAccessToken() { OkHttp okHttp = new OkHttp(); try { @@ -79,7 +81,8 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi .post(new Callback() { // 请求失败时的回调处理,此处未实现具体逻辑 @Override - public void onFailure(Call call, IOException e) {} + public void onFailure(Call call, IOException e) { + } // 请求成功时的回调处理 @Override @@ -87,7 +90,11 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi try { // 判断响应体是否非空,非空则解析令牌信息 if (response.body() != null) { - token = JSON.parseObject(response.body().string()); + + RedisCache redisCache = SpringUtils.getBean(RedisCache.class); + JSONObject token = JSON.parseObject(response.body().string()); + redisCache.setCacheObject(RedisKeyConstants.TRAFFIC_FLOW, token.getString("token_type") + " " + token.getString("access_token"), token.getInteger("expires_in"), TimeUnit.SECONDS); + } } catch (IOException e) { // 解析异常转为运行时异常抛出 @@ -111,14 +118,19 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi * * @return 如果存在有效的token,则返回构建的访问令牌字符串(包括token类型和访问令牌)。如果不存在有效的token,则返回null。 */ + public String getAccessToken() { + RedisCache redisCache = SpringUtils.getBean(RedisCache.class); + + String token = redisCache.getCacheObject(RedisKeyConstants.TRAFFIC_FLOW); // 检查token是否存在 - if (token != null) { + if (token == null) { // 组合并返回token类型和访问令牌 - return token.getString("token_type") + " " + token.getString("access_token"); + refreshAccessToken(); + token = redisCache.getCacheObject(RedisKeyConstants.TRAFFIC_FLOW); } // 如果token不存在,返回null - return null; + return token; } /** @@ -267,13 +279,13 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi * * @return List 当前路段拥堵情况列表 * @throws HttpException 抛出HttpException异常,当HTTP请求发生错误时。 - * @throws IOException 抛出IOException异常,当发生输入/输出错误时。 + * @throws IOException 抛出IOException异常,当发生输入/输出错误时。 */ @Override public List currentSectionCongested() throws HttpException, IOException { // 调用方法获取当前拥堵事件信息 - JSONArray currentEventCongested = currentEventCongested(); + JSONArray currentEventCongested = currentEventCongested(); // 如果没有拥堵事件,则直接返回空列表 if (currentEventCongested == null || currentEventCongested.isEmpty()) { @@ -297,7 +309,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi dcRoadSectionCongestion.setDownCongestionSections(new ArrayList<>()); // 遍历当前拥堵事件,计算每个路段的拥堵情况 - for (Object object :currentEventCongested) { + for (Object object : currentEventCongested) { if (object instanceof JSONObject) { JSONObject jsonObject = (JSONObject) object; calculateSectionCongestion(jsonObject, dcRoadSectionCongestion); @@ -315,10 +327,10 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi /** * 根据给定的JSON对象计算路段的拥堵情况。 * - * @param jsonObject 包含拥堵信息的JSON对象,包括拥堵开始桩号、结束桩号和拥堵距离。 + * @param jsonObject 包含拥堵信息的JSON对象,包括拥堵开始桩号、结束桩号和拥堵距离。 * @param dcRoadSectionCongestion 包含路段基本信息和拥堵情况的对象,需要根据计算结果更新拥堵信息。 */ - private void calculateSectionCongestion(JSONObject jsonObject, DcRoadSectionCongestion dcRoadSectionCongestion){ + private void calculateSectionCongestion(JSONObject jsonObject, DcRoadSectionCongestion dcRoadSectionCongestion) { // 根据方向,计算对应方向的路段拥堵情况 String dirCode = jsonObject.getString("dir_code"); @@ -408,7 +420,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi // 路段包含结束拥堵桩号不包含开始拥堵桩号 if (endPileNo >= startStakeMark && endPileNo <= endStakeMark && startPileNo <= startStakeMark) { congestionSection.setCongestionStartStakeMark(startStakeMark); - congestionSection.setCongestionEndStakeMark((int)endPileNo); + congestionSection.setCongestionEndStakeMark((int) endPileNo); congestionSection.setCongestionDistance((endStakeMark - startStakeMark)); congestionSections.add(congestionSection); @@ -510,7 +522,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi * 获取门架指标数据 * * @param startTime 开始时间 - * @param endTime 结束时间 + * @param endTime 结束时间 */ @Override public JSONArray gantryMetrics(String startTime, String endTime) throws HttpException, IOException { @@ -607,6 +619,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi requestParams.put("parameters", parameters.toJSONString()); Map headers = new HashMap<>(); + headers.put("Authorization", getAccessToken()); Response response // 请求响应 @@ -692,7 +705,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi } @Override - public List> realTimeTrafficFlowHour(String startDate,Long direction) throws HttpException, IOException { + public List> realTimeTrafficFlowHour(String startDate, Long direction) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(); @@ -719,6 +732,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi .post(); // 请求方法 ResponseBody body = response.body(); if (body != null) { + JSONArray jsonArray = JSON.parseArray(body.string()); // 获取当前时间 LocalTime now = LocalTime.now(); @@ -792,6 +806,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi return "N/A"; } } + public static String getDescriptionByIdentification(int identification) { for (StakeMarkRange range : StakeMarkRange.values()) { if (range.getIdentification() == identification) {