From 6958d89bfec79450782f8352d5716d6603549b98 Mon Sep 17 00:00:00 2001 From: Pu Zhibing <393733352@qq.com> Date: 星期日, 26 一月 2025 17:25:56 +0800 Subject: [PATCH] 新增加互联互通扩展接口 --- ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/drainage/TCECUtil.java | 185 ++++++++++++++++++++++++++++++--------------- 1 files changed, 122 insertions(+), 63 deletions(-) diff --git a/ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/drainage/TCECUtil.java b/ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/drainage/TCECUtil.java index 0cadb76..397691b 100644 --- a/ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/drainage/TCECUtil.java +++ b/ruoyi-service/ruoyi-integration/src/main/java/com/ruoyi/integration/drainage/TCECUtil.java @@ -3,14 +3,23 @@ import cn.hutool.http.*; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.ruoyi.common.core.utils.SpringUtils; +import com.ruoyi.integration.drainage.kuaidian.model.NotificationStationChangeResult; +import com.ruoyi.integration.drainage.kuaidian.model.StationChange; import com.ruoyi.integration.drainage.model.*; +import com.ruoyi.integration.drainage.model.enu.InterfaceUrlEnum; +import com.ruoyi.other.api.domain.Operator; +import com.ruoyi.other.api.feignClient.OperatorClient; import lombok.extern.slf4j.Slf4j; +import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * 中电联TCEC标准 @@ -23,18 +32,8 @@ * 运营商标识 */ private static final String OperatorID = ""; - /** - * 运营商秘钥 - */ - private static final String OperatorSecret = ""; - /** - * tokne - */ - private static String token = ""; - /** - * token过期时间,秒 - */ - private static Long TokenAvailableTime = 0L; + + private static OperatorClient operatorClient = SpringUtils.getBean(OperatorClient.class); private static Map<Long, Integer> map = new HashMap<>(); @@ -42,26 +41,31 @@ /** * 获取token */ - public static void queryToken(){ + public static String queryToken(Operator operator){ HttpRequest post = HttpUtil.createPost(""); post.contentType("application/json;charset=utf-8"); JSONObject body = new JSONObject(); body.put("OperatorID", OperatorID); - body.put("OperatorSecret", OperatorSecret); + body.put("OperatorSecret", operator.getOurOperatorSecret()); post.body(body.toJSONString()); HttpResponse execute = post.execute(); if(200 != execute.getStatus()){ - log.error("获取token失败:" + execute.body()); - return; + log.error("获取三方token失败:" + execute.body()); + return null; } JSONObject jsonObject = JSON.parseObject(execute.body()); Integer succStat = jsonObject.getInteger("SuccStat"); if(0 != succStat){ - log.error("获取token失败:" + jsonObject.getString("FailReason")); - return; + log.error("获取三方token失败:" + jsonObject.getString("FailReason")); + return null; } - token = jsonObject.getString("AccessToken"); - TokenAvailableTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) + jsonObject.getLong("TokenAvailableTime"); + String token = jsonObject.getString("AccessToken"); + Long tokenAvailableTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) + jsonObject.getLong("TokenAvailableTime"); + LocalDateTime localDateTime = Instant.ofEpochSecond(tokenAvailableTime).atOffset(ZoneOffset.UTC).toLocalDateTime(); + operator.setAccessToken(token); + operator.setTokenAvailableTime(localDateTime); + operatorClient.editOperator(operator); + return token; } @@ -69,12 +73,12 @@ * 获取token * @return */ - public static String getToken(){ - long second = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC); - if(second >= TokenAvailableTime){ - queryToken(); + public static String getToken(Operator operator){ + if(null != operator.getTokenAvailableTime() && operator.getTokenAvailableTime().isBefore(LocalDateTime.now())){ + return operator.getAccessToken(); + }else{ + return queryToken(operator); } - return token; } @@ -83,12 +87,12 @@ * 设备状态变化推送 * @param info */ - public static NotificationStationStatusResult notificationStationStatus(ConnectorStatusInfo info) { - HttpRequest post = HttpUtil.createPost(""); - buildBody(post, info); + public static NotificationStationStatusResult notificationStationStatus(Operator operator, ConnectorStatusInfo info) { + HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_STATION_STATUS.getUrl()); + buildBody(post, info, operator); HttpResponse execute = post.execute(); if(200 != execute.getStatus()){ - log.error("设备状态变化推送失败:" + execute.body()); + log.error("推送三方设备状态变化失败:" + execute.body()); return null; } return JSON.parseObject(execute.body(), NotificationStationStatusResult.class); @@ -96,22 +100,48 @@ /** + * 站点变更通知 + * @param operator + * @param type + * @param siteIds + * @return + */ + public static NotificationStationChangeResult notificationStationChange(Operator operator, Integer type, List<Integer> siteIds) { + StationChange stationChange = new StationChange(); + stationChange.setOperatorId(OperatorID); + stationChange.setType(type); + List<String> collect = siteIds.stream().map(String::valueOf).collect(Collectors.toList()); + stationChange.setStationIds(collect); + HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_STATION_CHANGE.getUrl()); + buildBody(post, stationChange, operator); + HttpResponse execute = post.execute(); + if(200 != execute.getStatus()){ + log.error("推送站点变更通知失败:" + execute.body()); + return null; + } + return JSON.parseObject(execute.body(), NotificationStationChangeResult.class); + } + + + + + /** * 推送启动充电结果 * @param info * @return */ - public static NotificationStartChargeResult notificationStartChargeResult(NotificationStartCharge info){ - HttpRequest post = HttpUtil.createPost(""); - buildBody(post, info); + public static NotificationStartChargeResult notificationStartChargeResult(Operator operator, NotificationStartCharge info){ + HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_START_CHARGE_RESULT.getUrl()); + buildBody(post, info, operator); HttpResponse execute = post.execute(); if(200 != execute.getStatus()){ - log.error("启动充电结果推送失败:" + execute.body()); + log.error("推送三方启动充电结果失败:" + execute.body()); return null; } JSONObject jsonObject = JSON.parseObject(execute.body()); Integer ret = jsonObject.getInteger("Ret"); if(0 != ret){ - log.error("启动充电结果推送失败:" + execute.body()); + log.error("推送三方启动充电结果失败:" + execute.body()); return null; } return jsonObject.getObject("Data", NotificationStartChargeResult.class); @@ -128,18 +158,18 @@ * @param info * @return */ - public static NotificationEquipChargeStatusResult notificationEquipChargeStatus(QueryEquipChargeStatusResult info){ - HttpRequest post = HttpUtil.createPost(""); - buildBody(post, info); + public static NotificationEquipChargeStatusResult notificationEquipChargeStatus(Operator operator, QueryEquipChargeStatusResult info){ + HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_EQUIP_CHARGE_STATUS.getUrl()); + buildBody(post, info, operator); HttpResponse execute = post.execute(); if(200 != execute.getStatus()){ - log.error("推送充电状态失败:" + execute.body()); + log.error("推送三方充电状态失败:" + execute.body()); return null; } JSONObject jsonObject = JSON.parseObject(execute.body()); Integer ret = jsonObject.getInteger("Ret"); if(0 != ret){ - log.error("推送充电状态失败:" + execute.body()); + log.error("推送三方充电状态失败:" + execute.body()); return null; } return jsonObject.getObject("Data", NotificationEquipChargeStatusResult.class); @@ -151,18 +181,18 @@ * @param info * @return */ - public static NotificationStopCharge notificationStopChargeResult(NotificationStopChargeResult info){ - HttpRequest post = HttpUtil.createPost(""); - buildBody(post, info); + public static NotificationStopCharge notificationStopChargeResult(Operator operator, NotificationStopChargeResult info){ + HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_STOP_CHARGE_RESULT.getUrl()); + buildBody(post, info, operator); HttpResponse execute = post.execute(); if(200 != execute.getStatus()){ - log.error("推送停止充电结果失败:" + execute.body()); + log.error("推送三方停止充电结果失败:" + execute.body()); return null; } JSONObject jsonObject = JSON.parseObject(execute.body()); Integer ret = jsonObject.getInteger("Ret"); if(0 != ret){ - log.error("推送停止充电结果失败:" + execute.body()); + log.error("推送三方停止充电结果失败:" + execute.body()); return null; } return jsonObject.getObject("Data", NotificationStopCharge.class); @@ -176,18 +206,18 @@ * @param info * @return */ - public static NotificationChargeOrderInfoResult notificationChargeOrderInfo(NotificationChargeOrderInfo info){ - HttpRequest post = HttpUtil.createPost(""); - buildBody(post, info); + public static NotificationChargeOrderInfoResult notificationChargeOrderInfo(Operator operator, NotificationChargeOrderInfo info){ + HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_CHARGE_ORDER_INFO.getUrl()); + buildBody(post, info, operator); HttpResponse execute = post.execute(); if(200 != execute.getStatus()){ - log.error("推送充电订单信息失败:" + execute.body()); + log.error("推送三方充电订单信息失败:" + execute.body()); return null; } JSONObject jsonObject = JSON.parseObject(execute.body()); Integer ret = jsonObject.getInteger("Ret"); if(0 != ret){ - log.error("推送充电订单信息失败:" + execute.body()); + log.error("推送三方充电订单信息失败:" + execute.body()); return null; } return jsonObject.getObject("Data", NotificationChargeOrderInfoResult.class); @@ -199,30 +229,59 @@ * @param post * @param o */ - public static void buildBody(HttpRequest post, Object o){ - Long key = Long.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); - Integer integer = map.get(key); + public static void buildBody(HttpRequest post, Object o, Operator operator){ + Long timeStamp = Long.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); + Integer integer = map.get(timeStamp); if(null == integer){ integer = 1; }else{ integer++; } - map.put(key, integer); + map.put(timeStamp, integer); post.contentType("application/json;charset=utf-8"); - post.header("Authorization", "Bearer " + getToken()); - JSONObject body = new JSONObject(); - body.put("OperatorID", OperatorID); - body.put("Data", AESUtil.encrypt(JSON.toJSONString(o))); - body.put("TimeStamp", key); - body.put("Seq", String.format("%04d", integer)); - body.put("Sig", ""); - post.body(body.toJSONString()); - + post.header("Authorization", "Bearer " + getToken(operator)); + BaseRequest baseRequest = new BaseRequest(); + baseRequest.setOperatorID(OperatorID); + baseRequest.setTimeStamp(timeStamp); + baseRequest.setSeq(String.format("%04d", integer)); + String jsonString = JSON.toJSONString(o); + String encrypt = AESUtil.encrypt(jsonString, operator.getDataSecret(), operator.getDataSecretIv()); + baseRequest.setData(encrypt); + baseRequest.setOperator(operator); + baseRequest.setSig(buildSign(baseRequest)); + post.body(JSON.toJSONString(baseRequest)); //清空小于当前时间的map中的无效数据 for (Long k : map.keySet()) { - if(k <= (key - 10)){ + if(k <= (timeStamp - 10)){ map.remove(k); } } } + + + /** + * 构建签名字符串 + * @param model + * @return + */ + public static String buildSign(BaseModel model){ + Operator operator = model.getOperator(); + //签名秘钥SigSecret + String key = operator.getSigSecret(); + String sign = ""; + switch (operator.getName()){ + case "XinDianTu": + //进行字符串拼接、计算 + String m = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString(); + byte[] hmacMd5 = SignUtil.getHMacMD5Bytes(key.getBytes(), m.getBytes()); + // 打印计算得到的签名Sig + sign = SignUtil.bytesToHexString(hmacMd5); + break; + case "KuaiDian": + sign = SignUtil.hmacSign(model.getData(), key); + break; + } + return sign; + } + } -- Gitblit v1.7.1