2024-01-19 16:56:36 +08:00
|
|
|
package com.jsowell.thirdparty.huawei;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
import com.jsowell.common.constant.Constants;
|
|
|
|
|
import com.jsowell.common.core.redis.RedisCache;
|
|
|
|
|
import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum;
|
|
|
|
|
import com.jsowell.common.util.DateUtils;
|
|
|
|
|
import com.jsowell.common.util.StringUtils;
|
2024-01-20 16:57:44 +08:00
|
|
|
import com.jsowell.pile.domain.PileBasicInfo;
|
|
|
|
|
import com.jsowell.pile.domain.PileConnectorInfo;
|
2024-01-19 16:56:36 +08:00
|
|
|
import com.jsowell.pile.domain.ThirdPartySettingInfo;
|
2024-01-20 16:57:44 +08:00
|
|
|
import com.jsowell.pile.domain.huawei.HWStationInfo;
|
|
|
|
|
import com.jsowell.pile.dto.PushStationInfoDTO;
|
|
|
|
|
import com.jsowell.pile.service.PileBasicInfoService;
|
|
|
|
|
import com.jsowell.pile.service.PileConnectorInfoService;
|
|
|
|
|
import com.jsowell.pile.service.PileStationInfoService;
|
2024-01-19 16:56:36 +08:00
|
|
|
import com.jsowell.pile.service.ThirdPartySettingInfoService;
|
2024-01-20 16:57:44 +08:00
|
|
|
import com.jsowell.pile.vo.web.PileStationVO;
|
2024-01-19 16:56:36 +08:00
|
|
|
import com.jsowell.thirdparty.lianlian.common.CommonResult;
|
2024-01-20 16:57:44 +08:00
|
|
|
import com.jsowell.thirdparty.lianlian.domain.ConnectorStatusInfo;
|
|
|
|
|
import com.jsowell.thirdparty.lianlian.domain.StationStatusInfo;
|
2024-01-19 16:56:36 +08:00
|
|
|
import com.jsowell.thirdparty.lianlian.dto.CommonParamsDTO;
|
|
|
|
|
import com.jsowell.thirdparty.lianlian.util.Cryptos;
|
|
|
|
|
import com.jsowell.thirdparty.lianlian.util.Encodes;
|
|
|
|
|
import com.jsowell.thirdparty.lianlian.util.GBSignUtils;
|
2024-01-20 16:57:44 +08:00
|
|
|
import com.jsowell.thirdparty.lianlian.util.HttpRequestUtil;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
2024-01-19 16:56:36 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2024-01-20 16:57:44 +08:00
|
|
|
import java.util.*;
|
2024-01-19 16:56:36 +08:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 华为Service
|
|
|
|
|
*
|
|
|
|
|
* @author Lemon
|
|
|
|
|
* @Date 2024/1/19 14:10:02
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
2024-01-20 16:57:44 +08:00
|
|
|
@Slf4j
|
2024-01-19 16:56:36 +08:00
|
|
|
public class HuaweiServiceV2 {
|
|
|
|
|
|
2024-01-20 16:57:44 +08:00
|
|
|
@Autowired
|
|
|
|
|
private PileStationInfoService pileStationInfoService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private PileConnectorInfoService pileConnectorInfoService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private PileBasicInfoService pileBasicInfoService;
|
|
|
|
|
|
2024-01-19 16:56:36 +08:00
|
|
|
@Autowired
|
|
|
|
|
private ThirdPartySettingInfoService thirdPartySettingInfoService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取华为 Token
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public String getHuaWeiToken() {
|
|
|
|
|
String operatorId = Constants.OPERATORID_JIANG_SU;
|
|
|
|
|
String redisKey = "huawei_get_token:" + operatorId;
|
|
|
|
|
// 先查缓存
|
|
|
|
|
String cacheToken = redisCache.getCacheObject(redisKey);
|
|
|
|
|
if (StringUtils.isNotBlank(cacheToken)) {
|
|
|
|
|
return cacheToken;
|
|
|
|
|
}
|
|
|
|
|
// 通过华为的type查询出密钥配置
|
|
|
|
|
ThirdPartySettingInfo info = new ThirdPartySettingInfo();
|
|
|
|
|
info.setType(ThirdPlatformTypeEnum.HUA_WEI.getCode());
|
|
|
|
|
ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.selectSettingInfo(info);
|
|
|
|
|
|
|
|
|
|
String operatorSecret = settingInfo.getOperatorSecret();
|
|
|
|
|
String dataSecret = settingInfo.getDataSecret();
|
|
|
|
|
String dataSecretIv = settingInfo.getDataSecretIv();
|
|
|
|
|
String signSecret = settingInfo.getSignSecret();
|
|
|
|
|
String urlAddress = settingInfo.getUrlAddress();
|
|
|
|
|
|
|
|
|
|
String requestUrl = urlAddress + "query_token";
|
|
|
|
|
|
|
|
|
|
// 拼装参数
|
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
|
jsonObject.put("OperatorID", operatorId);
|
|
|
|
|
jsonObject.put("OperatorSecret", operatorSecret);
|
|
|
|
|
|
|
|
|
|
// 加密
|
|
|
|
|
byte[] encryptText = Cryptos.aesEncrypt(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8),
|
|
|
|
|
dataSecret.getBytes(), dataSecretIv.getBytes());
|
|
|
|
|
String strData = Encodes.encodeBase64(encryptText);
|
|
|
|
|
|
2024-01-20 16:57:44 +08:00
|
|
|
// 向华为发送请求
|
|
|
|
|
String response = sendRequest2HuaWei(strData, signSecret, requestUrl);
|
2024-01-19 16:56:36 +08:00
|
|
|
|
|
|
|
|
CommonResult<?> commonResult = JSONObject.parseObject(response, CommonResult.class);
|
|
|
|
|
if (commonResult.getRet() != 0) {
|
|
|
|
|
return commonResult.getMsg();
|
|
|
|
|
}
|
|
|
|
|
// 解密data
|
|
|
|
|
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64((String) commonResult.getData()),
|
|
|
|
|
dataSecret.getBytes(), dataSecretIv.getBytes());
|
|
|
|
|
String dataStr = new String(plainText, StandardCharsets.UTF_8);
|
|
|
|
|
Map<String, String> resultMap = (Map<String, String>) JSON.parse(dataStr);
|
|
|
|
|
int succStat = Integer.parseInt(resultMap.get("SuccStat"));
|
|
|
|
|
if (succStat != 0) {
|
|
|
|
|
return resultMap.get("FailReason");
|
|
|
|
|
}
|
|
|
|
|
String token = resultMap.get("AccessToken");
|
|
|
|
|
int tokenAvailableTime = Integer.parseInt(resultMap.get("TokenAvailableTime")); // 凭证有效期,单位秒
|
|
|
|
|
// 存入缓存
|
|
|
|
|
redisCache.setCacheObject(redisKey, token, tokenAvailableTime, TimeUnit.SECONDS);
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-20 16:57:44 +08:00
|
|
|
/**
|
|
|
|
|
* 平台充电设备编码同步
|
|
|
|
|
*
|
|
|
|
|
* notification_operation_system_info
|
|
|
|
|
*
|
|
|
|
|
* @param dto
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public String notificationOperationSystemInfo(PushStationInfoDTO dto) {
|
|
|
|
|
Long stationId = dto.getStationId();
|
|
|
|
|
String type = dto.getThirdPartyType();
|
|
|
|
|
|
|
|
|
|
// 通过第三方配置类型查询相关配置信息
|
|
|
|
|
ThirdPartySettingInfo settingInfo = new ThirdPartySettingInfo();
|
|
|
|
|
settingInfo.setType(type);
|
|
|
|
|
ThirdPartySettingInfo thirdPartySettingInfo = thirdPartySettingInfoService.selectSettingInfo(settingInfo);
|
|
|
|
|
String operatorId = thirdPartySettingInfo.getOperatorId();
|
|
|
|
|
String operatorSecret = thirdPartySettingInfo.getOperatorSecret();
|
|
|
|
|
String signSecret = thirdPartySettingInfo.getSignSecret();
|
|
|
|
|
String dataSecret = thirdPartySettingInfo.getDataSecret();
|
|
|
|
|
String dataSecretIv = thirdPartySettingInfo.getDataSecretIv();
|
|
|
|
|
String urlAddress = thirdPartySettingInfo.getUrlAddress();
|
|
|
|
|
|
|
|
|
|
List<HWStationInfo
|
|
|
|
|
.EquipmentLogicInfo> equipmentLogicInfos = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
// 通过站点id查询站点信息
|
|
|
|
|
PileStationVO stationInfo = pileStationInfoService.getStationInfo(String.valueOf(stationId));
|
|
|
|
|
if (stationInfo == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
HWStationInfo hwStationInfo = HWStationInfo.builder()
|
|
|
|
|
.stationId(stationInfo.getId())
|
|
|
|
|
.stationName(stationInfo.getStationName())
|
|
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
// 查询桩列表
|
|
|
|
|
equipmentLogicInfos = getPileList(String.valueOf(stationId));
|
|
|
|
|
|
|
|
|
|
hwStationInfo.setEquipmentInfoNum(equipmentLogicInfos.size());
|
|
|
|
|
hwStationInfo.setEquipmentLogicInfos(equipmentLogicInfos);
|
|
|
|
|
|
|
|
|
|
String jsonString = JSONObject.toJSONString(hwStationInfo);
|
|
|
|
|
|
|
|
|
|
String url = urlAddress + "notification_operation_system_info";
|
|
|
|
|
// 获取令牌
|
|
|
|
|
String token = getHuaWeiToken();
|
|
|
|
|
// 发送请求
|
|
|
|
|
String result = HttpRequestUtil.sendPost(token, jsonString, url, dataSecret, dataSecretIv, operatorId, signSecret);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设备接口状态查询
|
|
|
|
|
*
|
|
|
|
|
* query_station_status
|
|
|
|
|
*
|
|
|
|
|
* @param stationIds
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public List<StationStatusInfo> queryStationStatus(List<String> stationIds) {
|
|
|
|
|
// 通过华为的type查询出密钥配置
|
|
|
|
|
ThirdPartySettingInfo info = new ThirdPartySettingInfo();
|
|
|
|
|
info.setType(ThirdPlatformTypeEnum.HUA_WEI.getCode());
|
|
|
|
|
ThirdPartySettingInfo settingInfo = thirdPartySettingInfoService.selectSettingInfo(info);
|
|
|
|
|
|
|
|
|
|
String operatorSecret = settingInfo.getOperatorSecret();
|
|
|
|
|
String dataSecret = settingInfo.getDataSecret();
|
|
|
|
|
String dataSecretIv = settingInfo.getDataSecretIv();
|
|
|
|
|
String signSecret = settingInfo.getSignSecret();
|
|
|
|
|
String urlAddress = settingInfo.getUrlAddress();
|
2024-01-19 16:56:36 +08:00
|
|
|
|
2024-01-20 16:57:44 +08:00
|
|
|
String requestUrl = urlAddress + "query_station_status";
|
2024-01-19 16:56:36 +08:00
|
|
|
|
2024-01-20 16:57:44 +08:00
|
|
|
// 拼装参数
|
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
|
jsonObject.put("StationIDs", stationIds);
|
2024-01-19 16:56:36 +08:00
|
|
|
|
2024-01-20 16:57:44 +08:00
|
|
|
// 加密
|
|
|
|
|
byte[] encryptText = Cryptos.aesEncrypt(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8),
|
|
|
|
|
dataSecret.getBytes(), dataSecretIv.getBytes());
|
|
|
|
|
String strData = Encodes.encodeBase64(encryptText);
|
|
|
|
|
|
|
|
|
|
// 向华为发送请求
|
|
|
|
|
String response = sendRequest2HuaWei(strData, signSecret, requestUrl);
|
|
|
|
|
|
|
|
|
|
CommonResult<?> commonResult = JSONObject.parseObject(response, CommonResult.class);
|
|
|
|
|
if (commonResult.getRet() != 0) {
|
|
|
|
|
log.error("查询华为设备接口状态 error:{}, ", commonResult.getMsg());
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
// 解密data
|
|
|
|
|
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64((String) commonResult.getData()),
|
|
|
|
|
dataSecret.getBytes(), dataSecretIv.getBytes());
|
|
|
|
|
String dataStr = new String(plainText, StandardCharsets.UTF_8);
|
|
|
|
|
|
|
|
|
|
// 转换成 StationStatus 对象
|
|
|
|
|
List<StationStatusInfo> list = JSON.parseArray(dataStr, StationStatusInfo.class);
|
|
|
|
|
return list;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Map<String, String> receiveNotificationStationStatus(ConnectorStatusInfo connectorStatusInfo) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 向华为发送请求
|
|
|
|
|
* @param strData 加密后的Data
|
|
|
|
|
* @param signSecret 签名密钥
|
|
|
|
|
* @param requestUrl 请求url
|
|
|
|
|
* @return 请求结果
|
|
|
|
|
*/
|
|
|
|
|
private String sendRequest2HuaWei(String strData, String signSecret, String requestUrl) {
|
|
|
|
|
Map<String, String> request = new LinkedHashMap<>();
|
|
|
|
|
request.put("OperatorID", Constants.OPERATORID_JIANG_SU);
|
|
|
|
|
request.put("Data", strData);
|
|
|
|
|
request.put("TimeStamp", DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date()));
|
|
|
|
|
request.put("Seq", "0001");
|
|
|
|
|
// 生成签名
|
|
|
|
|
String sig = GBSignUtils.sign(request, signSecret);
|
|
|
|
|
request.put("Sig", sig);
|
|
|
|
|
|
|
|
|
|
String tokenRequest = JSONObject.toJSONString(request);
|
|
|
|
|
String response = HttpUtil.post(requestUrl, tokenRequest);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<HWStationInfo.EquipmentLogicInfo> getPileList(String stationId) {
|
|
|
|
|
List<HWStationInfo
|
|
|
|
|
.EquipmentLogicInfo> equipmentLogicInfos = new ArrayList<>();
|
|
|
|
|
HWStationInfo
|
|
|
|
|
.EquipmentLogicInfo equipmentLogicInfo = null;
|
|
|
|
|
|
|
|
|
|
List<PileBasicInfo> pileBasicInfos = pileBasicInfoService.getPileListByStationId(stationId);
|
|
|
|
|
if (CollectionUtils.isEmpty(pileBasicInfos)) {
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
for (PileBasicInfo pileBasicInfo : pileBasicInfos) {
|
|
|
|
|
String pileSn = pileBasicInfo.getSn();
|
|
|
|
|
equipmentLogicInfo = HWStationInfo.EquipmentLogicInfo.builder()
|
|
|
|
|
.equipmentId(pileSn)
|
|
|
|
|
// .equipmentType()
|
|
|
|
|
.chargeHostESN(pileSn)
|
|
|
|
|
.build();
|
|
|
|
|
// 获取枪口信息
|
|
|
|
|
List<HWStationInfo.EquipmentLogicInfo
|
|
|
|
|
.ConnectorInfo> connectorInfoList = getConnectorInfoList(pileSn);
|
|
|
|
|
if (CollectionUtils.isEmpty(connectorInfoList)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
equipmentLogicInfo.setConnectorInfoNum(connectorInfoList.size());
|
|
|
|
|
equipmentLogicInfo.setConnectorInfos(connectorInfoList);
|
|
|
|
|
|
|
|
|
|
equipmentLogicInfos.add(equipmentLogicInfo);
|
|
|
|
|
}
|
|
|
|
|
return equipmentLogicInfos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<HWStationInfo.EquipmentLogicInfo.ConnectorInfo> getConnectorInfoList(String pileSn) {
|
|
|
|
|
List<HWStationInfo.EquipmentLogicInfo
|
|
|
|
|
.ConnectorInfo> connectorInfoList = new ArrayList<>();
|
|
|
|
|
HWStationInfo.EquipmentLogicInfo
|
|
|
|
|
.ConnectorInfo connectorInfo = null;
|
|
|
|
|
|
|
|
|
|
List<PileConnectorInfo> pileConnectorInfos = pileConnectorInfoService.selectPileConnectorInfoList(pileSn);
|
|
|
|
|
if (CollectionUtils.isEmpty(pileConnectorInfos)) {
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
for (PileConnectorInfo pileConnectorInfo : pileConnectorInfos) {
|
|
|
|
|
connectorInfo = HWStationInfo.EquipmentLogicInfo.ConnectorInfo.builder()
|
|
|
|
|
.connectorID(pileConnectorInfo.getPileConnectorCode())
|
|
|
|
|
.connectorNumber(pileConnectorInfo.getId())
|
|
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
connectorInfoList.add(connectorInfo);
|
|
|
|
|
}
|
|
|
|
|
return connectorInfoList;
|
|
|
|
|
}
|
2024-01-19 16:56:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|