| | |
| | | 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.common.core.utils.HttpUtils; |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.integration.drainage.model.*; |
| | | import com.ruoyi.integration.drainage.model.enu.InterfaceUrlEnum; |
| | | import com.ruoyi.integration.drainage.util.AesEncryption; |
| | | import com.ruoyi.integration.drainage.util.HMacMD5Util; |
| | | import com.ruoyi.integration.drainage.util.SequenceGenerator; |
| | | import com.ruoyi.other.api.domain.Operator; |
| | | import com.ruoyi.other.api.feignClient.OperatorClient; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneOffset; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import static com.ruoyi.integration.drainage.TCECUtil.getToken; |
| | | |
| | | /** |
| | | * 中电联TCEC标准 |
| | |
| | | * @Date 2025/1/21 11:48 |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class TCECSuperviseUtil { |
| | | |
| | | private static OperatorClient operatorClient = SpringUtils.getBean(OperatorClient.class); |
| | | // 测试环境 |
| | | private static final String OperatorID = "MA01H3BQ2"; |
| | | private static final String OperatorSecret = "f1331ef0b37c2d1b"; |
| | | private static final String SigSecret = "a6fedf0e1b27d6f7"; |
| | | private static final String DataSecret = "50a61b93919c9604"; |
| | | private static final String DataSecretIV = "7c8ac6861661d584"; |
| | | |
| | | @Autowired |
| | | private RedisService redisService; |
| | | public static final String TOKEN_KEY = "charge_token:"; |
| | | private final static String url = "https://dev-gov-hlht-sc.unievbj.com/evcs/v1.0.0/"; |
| | | /** |
| | | * 获取token |
| | | */ |
| | | private final static String query_token = "/query_token"; |
| | | /** |
| | | * 推送充电设备接口状态信息 |
| | | */ |
| | |
| | | /** |
| | | * 获取token |
| | | */ |
| | | public static String queryToken(Operator operator){ |
| | | 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"); |
| | | 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()); |
| | | return null; |
| | | public String queryToken(Operator operator){ |
| | | String token = redisService.getCacheObject(TOKEN_KEY); |
| | | if(StringUtils.hasLength(token)){ |
| | | return token; |
| | | } |
| | | 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 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(tokenAvailableTime); |
| | | operatorClient.editOperator(operator); |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("OperatorID", OperatorID); |
| | | jsonObject.put("OperatorSecret", OperatorSecret); |
| | | String params = jsonObject.toJSONString(); |
| | | // 参数加密 |
| | | String data = AesEncryption.encrypt(DataSecret, DataSecretIV, params); |
| | | // 获取签名 |
| | | String timeStamp = System.currentTimeMillis() + ""; |
| | | SequenceGenerator generator = new SequenceGenerator(); |
| | | String nextSequence = generator.getNextSequence(); |
| | | String hmacMD5 = HMacMD5Util.getHMacMD5(OperatorID,timeStamp, data,nextSequence,SigSecret); |
| | | jsonObject = new JSONObject(); |
| | | jsonObject.put("OperatorID", OperatorID); |
| | | jsonObject.put("Data", data); |
| | | jsonObject.put("TimeStamp", timeStamp); |
| | | jsonObject.put("Seq", nextSequence); |
| | | jsonObject.put("Sig", hmacMD5); |
| | | String result = HttpUtils.sendPost(url+query_token, jsonObject.toJSONString()); |
| | | String string = JSONObject.parseObject(result).getString("Data"); |
| | | String decrypt = AesEncryption.decrypt(DataSecret, DataSecretIV, string); |
| | | JSONObject tokenResult = JSONObject.parseObject(decrypt); |
| | | token = tokenResult.getString("AccessToken"); |
| | | Integer TokenAvailableTime = tokenResult.getInteger("TokenAvailableTime"); |
| | | redisService.setCacheObject(TOKEN_KEY,token, (long) (TokenAvailableTime - 60), TimeUnit.SECONDS); |
| | | return token; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取token |
| | | * @return |
| | | */ |
| | | public static String getToken(Operator operator){ |
| | | 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) { |
| | | public NotificationStationStatusResult notificationStationStatus(Operator operator, ConnectorStatusInfo info) { |
| | | HttpRequest post = HttpUtil.createPost(url+supervise_notification_station_status); |
| | | buildBody(post, info, operator); |
| | | HttpResponse execute = post.execute(); |
| | |
| | | * @param info |
| | | * @return |
| | | */ |
| | | public static NotificationEquipChargeStatusResult notificationSupEquipChargeStatus(Operator operator, SupEquipChargeStatus info){ |
| | | public NotificationEquipChargeStatusResult notificationSupEquipChargeStatus(Operator operator, SupEquipChargeStatus info){ |
| | | HttpRequest post = HttpUtil.createPost(url+supervise_notification_equip_charge_status); |
| | | buildBody(post, info, operator); |
| | | HttpResponse execute = post.execute(); |
| | |
| | | * @param info |
| | | * @return |
| | | */ |
| | | public static NotificationChargeOrderInfoResult notificationChargeOrderInfo(Operator operator, SupChargeOrderInfo info){ |
| | | public NotificationChargeOrderInfoResult notificationChargeOrderInfo(Operator operator, SupChargeOrderInfo info){ |
| | | HttpRequest post = HttpUtil.createPost(url+supervise_notification_charge_order_info); |
| | | buildBody(post, info, operator); |
| | | HttpResponse execute = post.execute(); |
| | |
| | | * @param post |
| | | * @param o |
| | | */ |
| | | public static void buildBody(HttpRequest post, Object o, Operator operator){ |
| | | public void buildBody(HttpRequest post, Object o, Operator operator){ |
| | | Long timeStamp = Long.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); |
| | | post.contentType("application/json;charset=utf-8"); |
| | | post.header("Authorization", "Bearer " + getToken(operator)); |
| | |
| | | baseRequest.setTimeStamp(timeStamp); |
| | | baseRequest.setSeq("0001"); |
| | | String jsonString = JacksonUtils.toJson(o); |
| | | String encrypt = AESUtil.encrypt(jsonString, operator.getDataSecret(), operator.getDataSecretIv()); |
| | | String encrypt = AesEncryption.encrypt(jsonString, operator.getDataSecret(), operator.getDataSecretIv()); |
| | | baseRequest.setData(encrypt); |
| | | baseRequest.setOperator(operator); |
| | | // baseRequest.setSig(buildSign(baseRequest)); |