| | |
| | | import cn.hutool.http.*; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.nacos.common.utils.JacksonUtils; |
| | | 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.other.api.domain.Operator; |
| | | import com.ruoyi.other.api.feignClient.OperatorClient; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | |
| | | import java.time.Instant; |
| | | import java.time.LocalDateTime; |
| | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | @Slf4j |
| | | public class TCECUtil { |
| | | |
| | | // private static OperatorClient operatorClient = SpringUtils.getBean(OperatorClient.class); |
| | | |
| | | public static Map<Long, Integer> map = new HashMap<>(); |
| | | |
| | | private static OperatorClient operatorClient = SpringUtils.getBean(OperatorClient.class); |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取token |
| | | */ |
| | | public static String queryToken(Operator operator){ |
| | | HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.QUERY_TOKEN.getUrl()); |
| | | HttpRequest post = HttpUtil.createPost(operator.getUrl() + InterfaceUrlEnum.QUERY_TOKEN.getUrl()); |
| | | JSONObject info = new JSONObject(); |
| | | info.put("OperatorID", operator.getOurOperatorId()); |
| | | info.put("OperatorSecret", operator.getOperatorSecret()); |
| | | Long timeStamp = Long.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); |
| | | post.contentType("application/json;charset=utf-8"); |
| | | JSONObject body = new JSONObject(); |
| | | body.put("OperatorID", operator.getOurOperatorId()); |
| | | body.put("OperatorSecret", operator.getOurOperatorSecret()); |
| | | post.body(body.toJSONString()); |
| | | BaseRequest baseRequest = new BaseRequest(); |
| | | baseRequest.setOperatorID(operator.getOurOperatorId()); |
| | | baseRequest.setTimeStamp(timeStamp); |
| | | baseRequest.setSeq("0001"); |
| | | String jsonString = JacksonUtils.toJson(info); |
| | | String encrypt = AESUtil.encrypt(jsonString, operator.getDataSecret(), operator.getDataSecretIv()); |
| | | baseRequest.setData(encrypt); |
| | | baseRequest.setOperator(operator); |
| | | baseRequest.setSig(buildSign(baseRequest)); |
| | | String request_json = JacksonUtils.toJson(baseRequest); |
| | | log.info("获取三方平台授权token请求地址:" + post.getUrl()); |
| | | log.info("获取三方平台授权token请求参数:" + request_json); |
| | | log.info("获取三方平台授权token请求Data:" + jsonString); |
| | | post.body(request_json); |
| | | HttpResponse execute = post.execute(); |
| | | if(200 != execute.getStatus()){ |
| | | log.error("获取三方token失败:" + execute.body()); |
| | | 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")); |
| | | log.info("获取三方平台授权token响应参数:" + execute.body()); |
| | | BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class); |
| | | Integer Ret = baseResult.getRet(); |
| | | if(0 != Ret){ |
| | | log.error("获取三方平台授权token失败:" + baseResult.getMsg()); |
| | | return null; |
| | | } |
| | | String token = jsonObject.getString("AccessToken"); |
| | | Long tokenAvailableTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) + jsonObject.getLong("TokenAvailableTime"); |
| | | LocalDateTime localDateTime = Instant.ofEpochSecond(tokenAvailableTime).atOffset(ZoneOffset.UTC).toLocalDateTime(); |
| | | //解密参数 |
| | | String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv()); |
| | | log.info("获取三方平台授权token响应Data:" + decrypt); |
| | | QueryTokenResult queryTokenResult = JSON.parseObject(decrypt, QueryTokenResult.class); |
| | | String token = queryTokenResult.getAccessToken(); |
| | | Long tokenAvailableTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) + queryTokenResult.getTokenAvailableTime(); |
| | | operator.setAccessToken(token); |
| | | operator.setTokenAvailableTime(localDateTime); |
| | | // operatorClient.editOperator(operator); |
| | | operator.setTokenAvailableTime(tokenAvailableTime); |
| | | operatorClient.editOperator(operator); |
| | | return token; |
| | | } |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public static String getToken(Operator operator){ |
| | | if(null != operator.getTokenAvailableTime() && operator.getTokenAvailableTime().isBefore(LocalDateTime.now())){ |
| | | if(null != operator.getTokenAvailableTime() && operator.getTokenAvailableTime() > LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)){ |
| | | return operator.getAccessToken(); |
| | | }else{ |
| | | return queryToken(operator); |
| | |
| | | * @param info |
| | | */ |
| | | public static NotificationStationStatusResult notificationStationStatus(Operator operator, ConnectorStatusInfo info) { |
| | | HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_STATION_STATUS.getUrl()); |
| | | HttpRequest post = HttpUtil.createPost(operator.getUrl() + 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); |
| | | log.info("推送三方平台设备状态响应:" + execute.body()); |
| | | BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class); |
| | | Integer Ret = baseResult.getRet(); |
| | | if(0 != Ret){ |
| | | log.error("推送三方平台设备状态失败:" + baseResult.getMsg()); |
| | | return null; |
| | | } |
| | | //解密参数 |
| | | String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv()); |
| | | log.info("推送三方平台设备状态Data:" + decrypt); |
| | | NotificationStationStatusResult notificationStationStatusResult = JSON.parseObject(decrypt, NotificationStationStatusResult.class); |
| | | return notificationStationStatusResult; |
| | | } |
| | | |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public static NotificationStartChargeResult notificationStartChargeResult(Operator operator, NotificationStartCharge info){ |
| | | HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_START_CHARGE_RESULT.getUrl()); |
| | | HttpRequest post = HttpUtil.createPost(operator.getUrl() + 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.info("推送三方平台启动充电结果响应:" + execute.body()); |
| | | BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class); |
| | | Integer Ret = baseResult.getRet(); |
| | | if(0 != Ret){ |
| | | log.error("推送三方平台启动充电结果失败:" + baseResult.getMsg()); |
| | | return null; |
| | | } |
| | | return jsonObject.getObject("Data", NotificationStartChargeResult.class); |
| | | //解密参数 |
| | | String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv()); |
| | | log.info("推送三方平台启动充电结果Data:" + decrypt); |
| | | NotificationStartChargeResult notificationStartChargeResult = JSON.parseObject(decrypt, NotificationStartChargeResult.class); |
| | | return notificationStartChargeResult; |
| | | } |
| | | |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public static NotificationEquipChargeStatusResult notificationEquipChargeStatus(Operator operator, QueryEquipChargeStatusResult info){ |
| | | HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_EQUIP_CHARGE_STATUS.getUrl()); |
| | | HttpRequest post = HttpUtil.createPost(operator.getUrl() + 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.info("推送三方平台充电状态响应:" + execute.body()); |
| | | BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class); |
| | | Integer Ret = baseResult.getRet(); |
| | | if(0 != Ret){ |
| | | log.error("推送三方平台充电状态失败:" + baseResult.getMsg()); |
| | | return null; |
| | | } |
| | | return jsonObject.getObject("Data", NotificationEquipChargeStatusResult.class); |
| | | //解密参数 |
| | | String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv()); |
| | | log.info("推送三方平台充电状态Data:" + decrypt); |
| | | NotificationEquipChargeStatusResult notificationEquipChargeStatusResult = JSON.parseObject(decrypt, NotificationEquipChargeStatusResult.class); |
| | | return notificationEquipChargeStatusResult; |
| | | } |
| | | |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public static NotificationStopCharge notificationStopChargeResult(Operator operator, NotificationStopChargeResult info){ |
| | | HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_STOP_CHARGE_RESULT.getUrl()); |
| | | HttpRequest post = HttpUtil.createPost(operator.getUrl() + 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.info("推送三方平台停止充电结果响应:" + execute.body()); |
| | | BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class); |
| | | Integer Ret = baseResult.getRet(); |
| | | if(0 != Ret){ |
| | | log.error("推送三方平台停止充电结果失败:" + baseResult.getMsg()); |
| | | return null; |
| | | } |
| | | return jsonObject.getObject("Data", NotificationStopCharge.class); |
| | | //解密参数 |
| | | String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv()); |
| | | log.info("推送三方平台停止充电结果Data:" + decrypt); |
| | | NotificationStopCharge notificationStopCharge = JSON.parseObject(decrypt, NotificationStopCharge.class); |
| | | return notificationStopCharge; |
| | | } |
| | | |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public static NotificationChargeOrderInfoResult notificationChargeOrderInfo(Operator operator, NotificationChargeOrderInfo info){ |
| | | HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_CHARGE_ORDER_INFO.getUrl()); |
| | | HttpRequest post = HttpUtil.createPost(operator.getUrl() + 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.info("推送三方平台充电订单信息响应:" + execute.body()); |
| | | BaseResult baseResult = JSON.parseObject(execute.body(), BaseResult.class); |
| | | Integer Ret = baseResult.getRet(); |
| | | if(0 != Ret){ |
| | | log.error("推送三方平台充电订单信息失败:" + baseResult.getMsg()); |
| | | return null; |
| | | } |
| | | return jsonObject.getObject("Data", NotificationChargeOrderInfoResult.class); |
| | | //解密参数 |
| | | String decrypt = AESUtil.decrypt(baseResult.getData(), operator.getDataSecret(), operator.getDataSecretIv()); |
| | | log.info("推送三方平台充电订单信息Data:" + decrypt); |
| | | NotificationChargeOrderInfoResult notificationChargeOrderInfoResult = JSON.parseObject(decrypt, NotificationChargeOrderInfoResult.class); |
| | | return notificationChargeOrderInfoResult; |
| | | } |
| | | |
| | | |
| | |
| | | */ |
| | | 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(timeStamp, integer); |
| | | post.contentType("application/json;charset=utf-8"); |
| | | post.header("Authorization", "Bearer " + getToken(operator)); |
| | | BaseRequest baseRequest = new BaseRequest(); |
| | | baseRequest.setOperatorID(operator.getOurOperatorId()); |
| | | baseRequest.setTimeStamp(timeStamp); |
| | | baseRequest.setSeq(String.format("%04d", integer)); |
| | | String jsonString = JSON.toJSONString(o); |
| | | String encrypt = AESUtil.encrypt(jsonString, operator.getOurDataSecret(), operator.getOurDataSecretIv()); |
| | | baseRequest.setSeq("0001"); |
| | | String jsonString = JacksonUtils.toJson(o); |
| | | String encrypt = AESUtil.encrypt(jsonString, operator.getDataSecret(), operator.getDataSecretIv()); |
| | | baseRequest.setData(encrypt); |
| | | baseRequest.setOperator(operator); |
| | | baseRequest.setSig(ourBuildSign(baseRequest)); |
| | | post.body(JSON.toJSONString(baseRequest)); |
| | | //清空小于当前时间的map中的无效数据 |
| | | for (Long k : map.keySet()) { |
| | | if(k <= (timeStamp - 10)){ |
| | | map.remove(k); |
| | | } |
| | | } |
| | | baseRequest.setSig(buildSign(baseRequest)); |
| | | String request_json = JacksonUtils.toJson(baseRequest); |
| | | post.body(request_json); |
| | | log.info("推送三方平台请求地址:" + post.getUrl()); |
| | | log.info("推送三方平台请求参数:" + request_json); |
| | | log.info("推送三方平台请求Data:" + jsonString); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 构建签名字符串 |
| | |
| | | */ |
| | | public static String buildSign(BaseModel model){ |
| | | Operator operator = model.getOperator(); |
| | | if(null == operator){ |
| | | return ""; |
| | | } |
| | | //签名秘钥SigSecret |
| | | String key = operator.getSigSecret(); |
| | | String sign = ""; |
| | |
| | | sign = SignUtil.bytesToHexString(hmacMd5); |
| | | break; |
| | | case "KuaiDian": |
| | | sign = SignUtil.hmacSign(model.getData(), key); |
| | | //进行字符串拼接、计算 |
| | | String m1 = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString(); |
| | | // 打印计算得到的签名Sig |
| | | sign = SignUtil.hmacSign(m1, key); |
| | | break; |
| | | } |
| | | return sign; |
| | |
| | | */ |
| | | public static String ourBuildSign(BaseModel model){ |
| | | Operator operator = model.getOperator(); |
| | | if(null == operator){ |
| | | return ""; |
| | | } |
| | | //签名秘钥SigSecret |
| | | String key = operator.getOurSigSecret(); |
| | | String sign = ""; |
| | | switch (operator.getName()){ |
| | | case "XinDianTu": |
| | | //进行字符串拼接、计算 |
| | | String m = new StringBuilder(operator.getOurOperatorId()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString(); |
| | | 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); |
| | | //进行字符串拼接、计算 |
| | | String m1 = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString(); |
| | | sign = SignUtil.hmacSign(m1, key); |
| | | break; |
| | | } |
| | | return sign; |
| | | } |
| | | public static String ourBuildSignJianGuan(BaseModel model){ |
| | | Operator operator = model.getOperator(); |
| | | if(null == operator){ |
| | | return ""; |
| | | } |
| | | //签名秘钥SigSecret |
| | | String key = operator.getOurSigSecret(); |
| | | String sign = ""; |
| | | //进行字符串拼接、计算 |
| | | String m1 = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString(); |
| | | sign = SignUtil.hmacSign(m1, key); |
| | | return sign; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | BaseModel model = new BaseModel(); |
| | | model.setOperatorID("MA25CNM38"); |
| | | model.setData("lkQUXmphG3h5Wf0UIxSfzNoCdqDLYkmDGceCbofJuWFqKBwCYulTUHSn9bJpVMVFNji/4xtSq3o1U5jSZZwIhA=="); |
| | | model.setData("AoArdDDcmHcmOMkCLHodTpY1xLtt9yhLqxvKPyfdlmEOBj1LJnQM+Z4JOZllt3Pj9rubfgxJ51zMAfzquQegJzHGAT9Y7JrKFzFe6jGtXo0="); |
| | | model.setTimeStamp(20250205120800L); |
| | | model.setSeq("0001"); |
| | | Operator operator = new Operator(); |
| | |
| | | operator.setSigSecret("S94xUpTpOIlLJBk8"); |
| | | model.setOperator(operator); |
| | | String key = operator.getSigSecret(); |
| | | |
| | | |
| | | 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 |
| | | String s = SignUtil.bytesToHexString(hmacMd5); |
| | | System.err.println(s); |
| | | |
| | | |
| | | // BaseModel model = new BaseModel(); |
| | | // model.setOperatorID("MA25CNM38"); |
| | | // model.setData("xQYYEPiwoc4JENnQsF50qP6Tmnj7uU/AshWn3QbIgrMIk8zvFpy6fWLIiLzQHcLPFCihqH1uzoNhGVNcS8Wvf5gIx9+cWJvYtjwtsjAGMEwNQ6y+rF1jjKnqCNcQbHbhqJ2l76sD54QCFKyenKxymrXri19bDV8hizn3nBw+jcHBtqKHWzjdSHwPDWPIw4rXnrbWLya2PXVgYtVg93WPtliw+0xsqW0BxJamE70/Ilbg4wB5fOAOvXmbLFI+v9jRjpcA7ImhprqPMYkXYAfa2QlICrJEBK7DfpJvRMzKzeT90kOAITqIqFvbfV0ti+greRqToLP6ni4o6w3ID42UmKasIpLTnx+qtR4bVTBTRuDzXvNXDA92RqHaJO21xbSen1eHN/yGfrchBIM0gmav7EpaYAerfU4ubnmJ6xujR6okYepJwBD5DiSIrObfS+GWVPRekvnjvm7BC02NXTrGKA=="); |
| | | // model.setTimeStamp(20250205161426L); |
| | | // model.setSeq("0001"); |
| | | // Operator operator = new Operator(); |
| | | // operator.setOurOperatorId("2921700136"); |
| | | // operator.setOurSigSecret("KBm5J2fbGzyhX023"); |
| | | // model.setOperator(operator); |
| | | // String key = operator.getOurSigSecret(); |
| | | // |
| | | // String m = new StringBuilder(operator.getOurOperatorId()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString(); |
| | | // byte[] hmacMd5 = SignUtil.getHMacMD5Bytes(key.getBytes(), m.getBytes()); |
| | | // // 打印计算得到的签名Sig |
| | | // String s = SignUtil.bytesToHexString(hmacMd5); |
| | | // System.err.println(s); |
| | | } |
| | | } |