From f929a6d305edeb7e66c5e45f73201a6260de6633 Mon Sep 17 00:00:00 2001 From: ZZ Date: Tue, 23 Nov 2021 12:04:29 +0800 Subject: [PATCH] =?UTF-8?q?WIP:=20=E5=81=9C=E6=AD=A2=E5=85=85=E7=94=B5?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../evcs/api/QueryStopChargeController.java | 93 ++++++------------- .../evcs/notification/CheckChargeOrders.java | 10 +- .../NotificationChargeOrderInfo.java | 10 +- .../NotificationChargeOrderInfoTask.java | 4 +- .../NotificationStopChargeResultTask.java | 15 +-- .../com/xhpc/pp/logic/OrderDataLogic.java | 7 +- .../com/xhpc/pp/logic/RealtimeDataLogic.java | 15 ++- 7 files changed, 60 insertions(+), 94 deletions(-) diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/api/QueryStopChargeController.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/api/QueryStopChargeController.java index 7096fd9c..8a71f633 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/api/QueryStopChargeController.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/api/QueryStopChargeController.java @@ -41,55 +41,36 @@ public class QueryStopChargeController { public CommonResponse queryStopCharge(@RequestBody CommonRequest commonRequest) throws IOException { QueryStopChargeRequest queryStopChargeRequest = JSONUtil.readParams(commonRequest.getData(), QueryStopChargeRequest.class); - // 获取充电订单号 充电订单号是一个27位的数字 + // evcs电订单号(27位) String startChargeSeq = queryStopChargeRequest.getStartChargeSeq(); - - //封装实体类数据 QueryStopChargeResponse queryStopChargeResponse = new QueryStopChargeResponse(); queryStopChargeResponse.setStartChargeSeq(startChargeSeq); - - //判断三方的订单号是否在我们的内部有对应的订单 + //判断三方的订单号是否存在 EtOrderMapping etOrderMapping = etOrderMappingRepo.findByEvcsOrderNo(startChargeSeq).orElse(null); if (etOrderMapping == null) { - - //充电订单状态 - queryStopChargeResponse.setStartChargeSeqStat(5); - //操作结果 - queryStopChargeResponse.setSuccStat(1); - //5 are custom value, means "error startChargeSeq" - queryStopChargeResponse.setFailReason(10); - - //转换为json对象 - String data = null; - try { - data = JSONUtil.toJSONString(queryStopChargeResponse); - } catch (JsonProcessingException e) { - e.printStackTrace(); - System.out.println("json转换出问题了"); - } - - CommonResponse commonResponse = new CommonResponse(); - commonResponse.setRet("1"); - commonResponse.setMsg("请求停止充电失败:错误的充电订单号"); - commonResponse.setData(data); - return commonResponse; + return failCommonResponse(queryStopChargeResponse); } String xhOrderNo = etOrderMapping.getXhOrderNo(); + String pushOrderkey = "pushOrder:".concat(xhOrderNo); + Map pushOrder = REDIS.getCacheMap(pushOrderkey); + if (pushOrder == null || (pushOrder.get("isStopNotified") != null && !(Boolean) pushOrder.get("isStopNotified"))) { + pushOrder.put("isStopNotified", false); + REDIS.setCacheMap(pushOrderkey, pushOrder); + return failCommonResponse(queryStopChargeResponse); + } //充电设备接口编码(枪编码) String connectorId = queryStopChargeRequest.getConnectorId(); - //获取桩编码 String pileNum = connectorId.substring(0, connectorId.length() - 2); //从数据库中查询对应的桩的版本信息 XhpcChargingPile pileInfo = XhpcChargingPileRepository.findBySerialNumber(pileNum).orElse(null); - String versionNum = pileInfo.getCommunicationProtocolVersion() != null ? pileInfo.getCommunicationProtocolVersion() : "0A"; + String versionNum = pileInfo.getCommunicationProtocolVersion() != null ? pileInfo.getCommunicationProtocolVersion() : + "0A"; //给对应的枪发送停止充电指令 R r = powerPileService.stopCharging(xhOrderNo, pileNum, connectorId, versionNum); // 判断停止指令发送是否成功 if (r.getCode() != 200) { //向redis中放入停止充电指令没有下发成功的标识 - Map pushOrder = REDIS.getCacheMap("pushOrder:".concat(xhOrderNo)); - pushOrder.put("stopCommandSent", false); - REDIS.setCacheMap("pushOrder:".concat(xhOrderNo), pushOrder); + REDIS.setCacheMap(pushOrderkey, pushOrder); //充电订单状态 queryStopChargeResponse.setStartChargeSeqStat(5); //操作结果 @@ -110,57 +91,41 @@ public class QueryStopChargeController { //5 means port is not charging queryStopChargeResponse.setFailReason(5); } - //充电订单状态 queryStopChargeResponse.setStartChargeSeqStat(4); - //操作结果 queryStopChargeResponse.setSuccStat(1); - - String data = null; - try { - data = JSONUtil.toJSONString(queryStopChargeResponse); - } catch (JsonProcessingException e) { - e.printStackTrace(); - System.out.println("json转换出问题了"); - } + String data = JSONUtil.toJSONString(queryStopChargeResponse); CommonResponse commonResponse = new CommonResponse(); commonResponse.setRet("1"); commonResponse.setMsg("请求停止充电失败"); commonResponse.setData(data); return commonResponse; } - //设置该订单已经被停止的标识 Map order = REDIS.getCacheMap("order:" + xhOrderNo); order.replace("status", "已结束"); - REDIS.setCacheMap("order:" + xhOrderNo, order); - - //设置推送停止充电订单所需要的数据 - Map pushOrder = REDIS.getCacheMap("pushOrder:".concat(xhOrderNo)); - pushOrder.put("stopCommandSent", false); - pushOrder.put("internetSerialNumber", startChargeSeq); - REDIS.setCacheMap("pushOrder:".concat(xhOrderNo), pushOrder); - - //充电订单状态 + REDIS.setCacheMap("order:".concat(xhOrderNo), order); + REDIS.setCacheMap(pushOrderkey, pushOrder); queryStopChargeResponse.setStartChargeSeqStat(3); - //操作结果 queryStopChargeResponse.setSuccStat(0); - //失败原因 queryStopChargeResponse.setFailReason(0); - - //转换为json对象 - String data = null; - try { - data = JSONUtil.toJSONString(queryStopChargeResponse); - } catch (JsonProcessingException e) { - e.printStackTrace(); - System.out.println("json转换出问题了"); - } - + String data = JSONUtil.toJSONString(queryStopChargeResponse); CommonResponse commonResponse = new CommonResponse(); commonResponse.setRet("0"); commonResponse.setMsg("请求停止充电成功"); commonResponse.setData(data); + return commonResponse; + } + private CommonResponse failCommonResponse(QueryStopChargeResponse queryStopChargeResponse) throws JsonProcessingException { + + queryStopChargeResponse.setStartChargeSeqStat(5); + queryStopChargeResponse.setSuccStat(1); + queryStopChargeResponse.setFailReason(0); + String data = JSONUtil.toJSONString(queryStopChargeResponse); + CommonResponse commonResponse = new CommonResponse(); + commonResponse.setRet("1"); + commonResponse.setMsg("请求停止充电失败:错误的充电订单号"); + commonResponse.setData(data); return commonResponse; } diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/CheckChargeOrders.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/CheckChargeOrders.java index f21b9e2c..e99e36fb 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/CheckChargeOrders.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/CheckChargeOrders.java @@ -46,8 +46,8 @@ public class CheckChargeOrders extends CoreDispatcher { for (String pushOrderKey : pushOrders) { Map pushOrder = REDIS.getCacheMap(pushOrderKey); - Boolean stopCommandSent = (Boolean) pushOrder.get("stopCommandSent"); - if (stopCommandSent == null || !stopCommandSent) { + Boolean isStopNotified = (Boolean) pushOrder.get("isStopNotified"); + if (isStopNotified != null && isStopNotified) { continue; } String orderNo = pushOrderKey.substring(10); @@ -56,7 +56,9 @@ public class CheckChargeOrders extends CoreDispatcher { if (operatorId3rdpty == null) { continue; } - AuthSecretToken authSecretTokenOut = authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenType(operatorId3rdpty, SECRET_TOKEN_TYPE_OUT).orElse(null); + AuthSecretToken authSecretTokenOut = + authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenType(operatorId3rdpty, + SECRET_TOKEN_TYPE_OUT).orElse(null); if (authSecretTokenOut == null) { continue; } @@ -64,7 +66,7 @@ public class CheckChargeOrders extends CoreDispatcher { //封装数据的实体类 CheckChargeOrderRequestData checkChargeOrder = new CheckChargeOrderRequestData(); //判断是否是结束了的充电订单 - if (stopCommandSent) { + if (isStopNotified) { //订单对账流水号(内部对应的第三方充电订单号) CacheOrderData orderData = (CacheOrderData) order.get("orderData"); String internetSerialNumber = (String) pushOrder.get("internetSerialNumber"); diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfo.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfo.java index e3c2192c..a9eaa36d 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfo.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfo.java @@ -52,8 +52,8 @@ public class NotificationChargeOrderInfo extends CoreDispatcher { for (String pushOrderKey : pushOrders) { Map pushOrder = REDIS.getCacheMap(pushOrderKey); - Boolean stopCommandSent = (Boolean) pushOrder.get("stopCommandSent"); - if (stopCommandSent == null || !stopCommandSent) { + Boolean isStopNotified = (Boolean) pushOrder.get("isStopNotified"); + if (isStopNotified) { continue; } String orderNo = pushOrderKey.substring(10); @@ -62,7 +62,9 @@ public class NotificationChargeOrderInfo extends CoreDispatcher { if (operatorId3rdpty == null) { continue; } - AuthSecretToken authSecretTokenOut = authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenType(operatorId3rdpty, SECRET_TOKEN_TYPE_OUT).orElse(null); + AuthSecretToken authSecretTokenOut = + authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenType(operatorId3rdpty, + SECRET_TOKEN_TYPE_OUT).orElse(null); if (authSecretTokenOut == null) { continue; } @@ -70,7 +72,7 @@ public class NotificationChargeOrderInfo extends CoreDispatcher { //封装数据的实体类 ChargeOrderInfo chargeOrderInfo = new ChargeOrderInfo(); //判断是否是结束了的充电订单 - if (stopCommandSent) { + if (isStopNotified) { //充电订单号(内部对应的第三方充电订单号) CacheOrderData orderData = (CacheOrderData) order.get("orderData"); String internetSerialNumber = (String) pushOrder.get("internetSerialNumber"); diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfoTask.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfoTask.java index 871f03e6..24f4f902 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfoTask.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationChargeOrderInfoTask.java @@ -18,6 +18,7 @@ import org.springframework.stereotype.Component; import java.util.List; +import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; import static com.xhpc.evcs.domain.AuthSecretToken.SECRET_TOKEN_TYPE_OUT; /** @@ -31,7 +32,7 @@ public class NotificationChargeOrderInfoTask extends CoreDispatcher { @Autowired private AuthSecretTokenRepository authSecretTokenRepository; - private Logger logger = LoggerFactory.getLogger(NotificationChargeOrderInfoTask.class); + private final Logger logger = LoggerFactory.getLogger(NotificationChargeOrderInfoTask.class); @Scheduled(fixedRate = 1000 * 15) public void run() throws JsonProcessingException { @@ -53,6 +54,7 @@ public class NotificationChargeOrderInfoTask extends CoreDispatcher { horder.setConfirmResult(pushResp.getConfirmResult()); logger.info("3rd part order {} push result: {}", horder.getSerialNumber(), pushResp.getConfirmResult()); xhpcHistoryOrderRepository.save(horder); + REDIS.setCacheMapValue("pushOrder:".concat(horder.getSerialNumber()), "horderpushed", true); } else { logger.error("3dp[{}] order[{}/{}] push fail", operatorId3rdptyEvcs, horder.getSerialNumber(), horder.getInternetSerialNumber()); diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationStopChargeResultTask.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationStopChargeResultTask.java index 4cd5fdda..7ebcd838 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationStopChargeResultTask.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationStopChargeResultTask.java @@ -52,8 +52,8 @@ public class NotificationStopChargeResultTask extends CoreDispatcher { CacheOrderData orderData = REDIS.getCacheMapValue(orderkey, "orderData"); if (orderData != null) { Map pushOrder = REDIS.getCacheMap(pushOrderKey); - Boolean stopCommandSent = (Boolean) pushOrder.get("stopCommandSent"); - if (stopCommandSent == null || !stopCommandSent) { + Boolean isStopNotified = (Boolean) pushOrder.get("isStopNotified"); + if (isStopNotified == null || !isStopNotified) { String internetSerialNumber = (String) pushOrder.get("internetSerialNumber"); String operatorId3rdpty = internetSerialNumber.substring(0, 9); AuthSecretToken authSecretTokenOut = @@ -75,15 +75,8 @@ public class NotificationStopChargeResultTask extends CoreDispatcher { String result = ok(commonRequest, "/notification_stop_charge_result", authSecretTokenOut); ChargeResultResponse chargeResultResponse = DTOJsonHelper.parseResponseData(result, ChargeResultResponse.class, authSecretTokenOut); - if (chargeResultResponse != null) { - succStat = chargeResultResponse.getSuccStat(); - //推送成功则排除这个已推送的订单,否则则继续推送该订单 - //将stopCommandSent置为null,表示推送成功之后该订单都不进行推送 - if (succStat == 0) { - pushOrder.replace("stopCommandSent", true); - REDIS.setCacheMap(pushOrderKey, pushOrder); - } - } + pushOrder.put("isStopNotified", true); + REDIS.setCacheMap(pushOrderKey, pushOrder); } } } diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/OrderDataLogic.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/OrderDataLogic.java index c276b8a1..38dd1f67 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/OrderDataLogic.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/OrderDataLogic.java @@ -29,7 +29,7 @@ import static com.xhpc.pp.utils.security.CacheDataUtils.reflectTranslate; @Component("OrderDataLogic") public class OrderDataLogic implements ServiceLogic { - private static Logger log = LoggerFactory.getLogger(OrderDataLogic.class); + private static final Logger log = LoggerFactory.getLogger(OrderDataLogic.class); @Autowired private PileOrderService pileOrderService; @@ -68,7 +68,10 @@ public class OrderDataLogic implements ServiceLogic { String resultStr = "6815".concat(req.get("seqhex").toString()).concat("0040").concat(orderNo).concat(ServiceResult.HEX_00); resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr)); - final R r = pileOrderService.pileEndOrder(orderNo); //todo 优化 通知第三方 + if (orderNo.equals("00000000000000000000000000000000")) { + return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK); + } + final R r = pileOrderService.pileEndOrder(orderNo); if (r.getCode() == 200) return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK); else diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RealtimeDataLogic.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RealtimeDataLogic.java index 0ad7aae2..080a4923 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RealtimeDataLogic.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RealtimeDataLogic.java @@ -48,7 +48,7 @@ public class RealtimeDataLogic implements ServiceLogic { @Autowired private ChargingController chargingController; - private static Logger log = LoggerFactory.getLogger(RealtimeDataLogic.class); + private static final Logger log = LoggerFactory.getLogger(RealtimeDataLogic.class); public static final String[] stable = {"离线", "故障", "空闲", "充电中"}; public static final String[] pvgstable = {"否", "是", "未知"}; @@ -159,7 +159,7 @@ public class RealtimeDataLogic implements ServiceLogic { HashMap paramMap = new HashMap<>(); paramMap.put("battery", stopSoc.toString()); paramMap.put("phone", tel); - paramMap.put("content", "【小华充电】尊敬的用户,你的车辆已充电达至设定的SOC(" + stopSoc.toString() + "%)" + + paramMap.put("content", "【小华充电】尊敬的用户,你的车辆已充电达至设定的SOC(" + stopSoc + "%)" + "已自动停止充电,请您尽快将车辆挪走以方便他人使用充电桩,谢谢合作。"); smsService.sendNotice(paramMap); cacheOrder.put("socalerted", "true"); @@ -188,14 +188,13 @@ public class RealtimeDataLogic implements ServiceLogic { if (errorfreecnt == 2) { CacheRealtimeData cacheRData = REDIS.getCacheObject(orderkey.concat(".lord")); R r = null; - final String stopResult = (String) cacheOrder.get("stopResult"); if (cacheRData == null) { - r = pileOrderService.abnormalOrder(orderNo); //todo 优化 通知第三方 - } else if ("01".equals(stopResult)) { + r = pileOrderService.abnormalOrder(orderNo); + } else { CacheOrderData lordAsOd = new CacheOrderData(cacheRData, (String) cacheOrder.get("orderstarttime"), (Integer) cacheOrder.get("startSoc"), (Integer) cacheOrder.get("stopSoc")); cacheOrder.put("orderData", lordAsOd); - r = pileOrderService.pileEndOrder(orderNo); //todo 优化 通知第三方 + r = pileOrderService.pileEndOrder(orderNo); REDIS.setCacheMap(orderkey, cacheOrder); } if (r == null || r.getCode() != 200) { @@ -215,13 +214,13 @@ public class RealtimeDataLogic implements ServiceLogic { idleCnt = idleCnt == null ? 0 : idleCnt; idleCnt++; if (idleCnt > 3) { - if (orderkey != null) { + if (orderkey != null && orderkey.length() > 0) { Map problematicOrder = REDIS.getCacheMap(orderkey); Object orderData = problematicOrder.get("orderData"); String status = (String) problematicOrder.get("status"); if (!isInteger(status) && !status.equals("充电中") && orderData == null) { String lorder = orderkey.replace("order:", ""); - pileOrderService.abnormalOrder(lorder); //todo 优化 通知第三方 + pileOrderService.abnormalOrder(lorder); log.error("abnormal.2 order[{}]", lorder); cacheGun.put("orderkey", null); idleCnt = 0;