Files
jsowell-charger-web/jsowell-admin/src/main/java/com/jsowell/api/thirdparty/NRController.java

288 lines
12 KiB
Java
Raw Normal View History

2024-05-10 15:30:20 +08:00
package com.jsowell.api.thirdparty;
2023-10-11 13:28:59 +08:00
2023-10-12 09:14:13 +08:00
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
2023-10-12 09:14:13 +08:00
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
2023-10-19 11:21:02 +08:00
import com.jsowell.common.core.domain.ykc.RealTimeMonitorData;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
2023-10-12 09:14:13 +08:00
import com.jsowell.common.response.RestApiResponse;
import com.jsowell.common.util.JWTUtils;
2023-10-19 11:21:02 +08:00
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.dto.PushStationInfoDTO;
2023-10-12 09:14:13 +08:00
import com.jsowell.pile.dto.QueryStationInfoDTO;
2023-10-19 11:21:02 +08:00
import com.jsowell.pile.dto.nanrui.GetTokenDTO;
2023-10-12 09:14:13 +08:00
import com.jsowell.pile.dto.nanrui.NRQueryOrderDTO;
2023-10-19 11:21:02 +08:00
import com.jsowell.pile.dto.nanrui.PushAlarmInfoDTO;
import com.jsowell.thirdparty.lianlian.common.CommonResult;
2024-03-27 11:46:11 +08:00
import com.jsowell.pile.thirdparty.CommonParamsDTO;
2024-03-27 16:13:43 +08:00
import com.jsowell.thirdparty.platform.util.Cryptos;
import com.jsowell.thirdparty.platform.util.Encodes;
2023-10-12 09:14:13 +08:00
import com.jsowell.thirdparty.nanrui.service.NRService;
import org.springframework.beans.factory.annotation.Autowired;
2023-10-19 11:21:02 +08:00
import org.springframework.web.bind.annotation.*;
2023-10-12 09:14:13 +08:00
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
2023-10-12 09:14:13 +08:00
import java.util.Map;
2023-10-11 13:28:59 +08:00
/**
2023-10-20 17:20:31 +08:00
* 江苏平台 controller
2023-10-11 13:28:59 +08:00
*
* @author Lemon
* @Date 2023/10/11 13:26
*/
2023-10-12 09:14:13 +08:00
@Anonymous
@RestController
2023-10-19 11:21:02 +08:00
@RequestMapping("/jiangsu")
2023-10-12 09:14:13 +08:00
public class NRController extends BaseController {
@Autowired
private NRService nrService;
/**
* 查询充电站信息
* @param dto
*/
@RequestMapping("/v1/query_stations_info")
public CommonResult<?> query_stations_info(HttpServletRequest request, @RequestBody CommonParamsDTO dto) {
2023-10-20 17:20:31 +08:00
logger.info("江苏平台查询充电站信息 params:{}", JSON.toJSONString(dto));
2023-10-12 09:14:13 +08:00
try {
// 校验令牌
String token = request.getHeader("Authorization");
if (!JWTUtils.checkThirdPartyToken(token)) {
// 校验失败
return CommonResult.failed("令牌校验错误");
}
// 校验签名
Map<String, String> resultMap = nrService.checkoutSign(dto);
if (resultMap == null) {
// 签名错误
return CommonResult.failed("签名校验错误");
}
String operatorSecret = resultMap.get("OperatorSecret");
String dataString = resultMap.get("Data");
String dataSecret = resultMap.get("DataSecret");
String dataSecretIV = resultMap.get("DataSecretIV");
// 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), dataSecret.getBytes(), dataSecretIV.getBytes());
String dataStr = new String(plainText, StandardCharsets.UTF_8);
// 转换成相应对象
QueryStationInfoDTO queryStationInfoDTO = JSONObject.parseObject(dataStr, QueryStationInfoDTO.class);
2024-01-08 16:05:27 +08:00
queryStationInfoDTO.setOperatorId(dto.getOperatorID());
Map<String, String> map = nrService.query_stations_info(queryStationInfoDTO);
2023-10-19 11:21:02 +08:00
return CommonResult.success(0, "查询充电站信息成功!", map.get("Data"), map.get("Sig"));
2023-10-12 09:14:13 +08:00
} catch (Exception e) {
2023-10-20 17:20:31 +08:00
logger.error("江苏平台查询充电站信息 error", e);
2023-10-12 09:14:13 +08:00
}
return CommonResult.failed("查询充电站信息异常");
2023-10-12 09:14:13 +08:00
}
/**
* 查询设备接口状态
* @param dto
*/
@RequestMapping("/v1/query_station_status")
public CommonResult<?> query_station_status(HttpServletRequest request, @RequestBody CommonParamsDTO dto) {
2023-10-20 17:20:31 +08:00
logger.info("江苏平台查询设备接口状态 params:{}", JSON.toJSONString(dto));
2023-10-12 09:14:13 +08:00
try {
// 校验令牌
String token = request.getHeader("Authorization");
if (!JWTUtils.checkThirdPartyToken(token)) {
// 校验失败
return CommonResult.failed("令牌校验错误");
}
// 校验签名
Map<String, String> resultMap = nrService.checkoutSign(dto);
if (resultMap == null) {
// 签名错误
return CommonResult.failed("签名校验错误");
}
String operatorSecret = resultMap.get("OperatorSecret");
String dataString = resultMap.get("Data");
String dataSecret = resultMap.get("DataSecret");
String dataSecretIV = resultMap.get("DataSecretIV");
// 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), dataSecret.getBytes(), dataSecretIV.getBytes());
String dataStr = new String(plainText, StandardCharsets.UTF_8);
// 转换成相应对象
QueryStationInfoDTO queryStationInfoDTO = JSONObject.parseObject(dataStr, QueryStationInfoDTO.class);
queryStationInfoDTO.setOperatorId(dto.getOperatorID());
Map<String, String> map = nrService.query_station_status(queryStationInfoDTO);
return CommonResult.success(0, "查询设备接口状态成功!", map.get("Data"), map.get("Sig"));
2023-10-12 09:14:13 +08:00
} catch (Exception e) {
2023-10-20 17:20:31 +08:00
logger.error("江苏平台查询设备接口状态 error", e);
e.printStackTrace();
2023-10-12 09:14:13 +08:00
}
return CommonResult.failed("查询设备接口状态异常");
2023-10-12 09:14:13 +08:00
}
/**
2023-10-12 09:14:57 +08:00
* 查询充电电量信息
2023-10-12 09:14:13 +08:00
* @param dto
*/
@RequestMapping("/v1/query_order_info")
public CommonResult<?> query_order_info(HttpServletRequest request, @RequestBody CommonParamsDTO dto) {
2023-10-20 17:20:31 +08:00
logger.info("江苏平台查询充电电量信息 params:{}", JSON.toJSONString(dto));
2023-10-12 09:14:13 +08:00
try {
// 校验令牌
String token = request.getHeader("Authorization");
if (!JWTUtils.checkThirdPartyToken(token)) {
// 校验失败
return CommonResult.failed("令牌校验错误");
}
// 校验签名
Map<String, String> resultMap = nrService.checkoutSign(dto);
if (resultMap == null) {
// 签名错误
return CommonResult.failed("签名校验错误");
}
String operatorSecret = resultMap.get("OperatorSecret");
String dataString = resultMap.get("Data");
String dataSecret = resultMap.get("DataSecret");
String dataSecretIV = resultMap.get("DataSecretIV");
// 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), dataSecret.getBytes(), dataSecretIV.getBytes());
String dataStr = new String(plainText, StandardCharsets.UTF_8);
// 转换成相应对象
NRQueryOrderDTO nrQueryOrderDTO = JSONObject.parseObject(dataStr, NRQueryOrderDTO.class);
nrQueryOrderDTO.setOperatorId(dto.getOperatorID());
Map<String, String> map = nrService.query_order_info(nrQueryOrderDTO);
return CommonResult.success(0, "查询充电电量信息成功!", map.get("Data"), map.get("Sig"));
2023-10-12 09:14:13 +08:00
} catch (Exception e) {
2023-10-20 17:20:31 +08:00
logger.error("江苏平台查询充电电量信息 error", e);
2023-10-12 09:14:13 +08:00
}
2023-10-19 11:21:02 +08:00
return CommonResult.failed("查询充电电量信息异常");
}
/**
* 获取token接口
* http://localhost:8080/nanrui/v1/query_token
*/
@PostMapping("/v1/query_token")
public CommonResult<?> queryToken(@RequestBody CommonParamsDTO dto) {
2024-03-19 16:22:40 +08:00
logger.info("江苏平台请求令牌 params:{}", JSON.toJSONString(dto));
try {
Map<String, String> map = nrService.generateToken(dto);
2024-03-19 16:22:40 +08:00
logger.info("江苏平台请求令牌 result:{}", JSON.toJSONString(map));
return CommonResult.success(0, "请求令牌成功!", map.get("Data"), map.get("Sig"));
} catch (UnsupportedEncodingException e) {
logger.error("获取token接口 异常");
return CommonResult.failed("获取token发生异常");
}
2023-10-12 09:14:13 +08:00
}
2023-10-19 11:21:02 +08:00
@RequestMapping("/v1/getToken")
public String getToken(@RequestBody GetTokenDTO dto) {
String token = nrService.getToken(dto.getUrlAddress(), dto.getOperatorId(), dto.getOperatorSecret(),
dto.getDataSecretIv(), dto.getSignSecret(), dto.getDataSecret());
return token;
}
/**
* 推送充电站信息
* @param dto
* @return
*/
@PostMapping("/v1/pushStationInfo")
public RestApiResponse<?> pushStationInfo(@RequestBody PushStationInfoDTO dto) {
2024-03-19 16:22:40 +08:00
logger.info("推送江苏平台充电站信息 params:{}", JSON.toJSONString(dto));
2023-10-19 11:21:02 +08:00
RestApiResponse<?> response = null;
try {
if (StringUtils.isBlank(String.valueOf(dto.getStationId()))) {
throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
}
2024-06-21 09:12:22 +08:00
// String s = nrService.pushStationInfo(dto);
String s = nrService.pushStationInfoV2(dto);
2023-10-19 11:21:02 +08:00
response = new RestApiResponse<>(s);
}catch (BusinessException e) {
2023-10-20 17:20:31 +08:00
logger.error("推送江苏平台充电站信息 error",e);
2023-10-19 11:21:02 +08:00
response = new RestApiResponse<>(e.getCode(), e.getMessage());
}catch (Exception e) {
2023-10-20 17:20:31 +08:00
logger.error("推送江苏平台充电站信息 error", e);
2023-10-19 11:21:02 +08:00
response = new RestApiResponse<>(e);
}
2023-10-20 17:20:31 +08:00
logger.info("推送江苏平台充电站信息 result:{}", response);
2023-10-19 11:21:02 +08:00
return response;
}
/**
* 推送告警信息
* @param dto
* @return
*/
@PostMapping("/v1/pushAlarmInfo")
public RestApiResponse<?> pushAlarmInfo(@RequestBody PushAlarmInfoDTO dto) {
2024-03-19 16:22:40 +08:00
logger.info("推送江苏平台告警信息 params:{}", JSON.toJSONString(dto));
2023-10-19 11:21:02 +08:00
RestApiResponse<?> response = null;
try {
if (StringUtils.isBlank(String.valueOf(dto.getStationId()))) {
throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
}
String s = nrService.pushAlarmInfo(dto);
response = new RestApiResponse<>(s);
}catch (BusinessException e) {
2023-10-20 17:20:31 +08:00
logger.error("推送江苏平台告警信息 error",e);
2023-10-19 11:21:02 +08:00
response = new RestApiResponse<>(e.getCode(), e.getMessage());
}catch (Exception e) {
2023-10-20 17:20:31 +08:00
logger.error("推送江苏平台告警信息 error", e);
2023-10-19 11:21:02 +08:00
response = new RestApiResponse<>(e);
}
2023-10-20 17:20:31 +08:00
logger.info("推送江苏平台告警信息 result:{}", response);
2023-10-19 11:21:02 +08:00
return response;
}
/**
* 设备状态变化推送
* @param data
* @return
*/
@PostMapping("/v1/pushPileStatus")
public RestApiResponse<?> pushPileStatus(@RequestBody RealTimeMonitorData data) {
2024-03-19 16:22:40 +08:00
logger.info("推送江苏平台设备状态变化推送 params:{}", JSON.toJSONString(data));
2023-10-19 11:21:02 +08:00
RestApiResponse<?> response = null;
try {
String s = nrService.pushPileStatus(data);
response = new RestApiResponse<>(s);
}catch (BusinessException e) {
2023-10-20 17:20:31 +08:00
logger.error("推送江苏平台设备状态变化推送 error",e);
2023-10-19 11:21:02 +08:00
response = new RestApiResponse<>(e.getCode(), e.getMessage());
}catch (Exception e) {
2023-10-20 17:20:31 +08:00
logger.error("推送江苏平台设备状态变化推送 error", e);
2023-10-19 11:21:02 +08:00
response = new RestApiResponse<>(e);
}
2023-10-20 17:20:31 +08:00
logger.info("推送江苏平台设备状态变化推送 result:{}", response);
2023-10-19 11:21:02 +08:00
return response;
}
/**
* 充电电量信息推送
* @param orderCode
* @return
*/
@GetMapping("/v1/pushOrderInfo/{orderCode}")
public RestApiResponse<?> pushOrderInfo(@PathVariable("orderCode") String orderCode) {
2023-10-20 17:20:31 +08:00
logger.info("江苏平台充电电量信息推送 params:{}", orderCode);
2023-10-19 11:21:02 +08:00
RestApiResponse<?> response = null;
try {
String s = nrService.pushOrderInfo(orderCode);
response = new RestApiResponse<>(s);
}catch (BusinessException e) {
2023-10-20 17:20:31 +08:00
logger.error("江苏平台充电电量信息推送 error",e);
2023-10-19 11:21:02 +08:00
response = new RestApiResponse<>(e.getCode(), e.getMessage());
}catch (Exception e) {
2023-10-20 17:20:31 +08:00
logger.error("江苏平台充电电量信息推送 error", e);
2023-10-19 11:21:02 +08:00
response = new RestApiResponse<>(e);
}
2023-10-20 17:20:31 +08:00
logger.info("江苏平台充电电量信息推送 result:{}", response);
2023-10-19 11:21:02 +08:00
return response;
}
}