diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/api/QueryStartChargeController.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/api/QueryStartChargeController.java index 7ecc7506..773f01bc 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/api/QueryStartChargeController.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/api/QueryStartChargeController.java @@ -69,12 +69,14 @@ public class QueryStartChargeController { startChargeResponse.setFailReason(0); //insert a gunStatusData to redis Map pushOrder = new HashMap<>(); - //1 means charging. + //1 means starting. pushOrder.put("startChargeSeqStat", 1); String orderNo = (String) res.getData(); pushOrder.put("internetSerialNumber", startChargeRequest.getStartChargeSeq()); pushOrder.put("connectorID", connectorID); pushOrder.put("startTime", etOrderData.get("startTime")); + //0 means needs to be notified. + pushOrder.put("startChargeNotificationStat", 0); REDIS.setCacheMap("pushOrder:".concat(orderNo), pushOrder); } resp.setRet(String.valueOf(res.getCode())); diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/AuthSecretTokenRepository.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/AuthSecretTokenRepository.java index f7e74610..a8044099 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/AuthSecretTokenRepository.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/jpa/AuthSecretTokenRepository.java @@ -33,4 +33,5 @@ public interface AuthSecretTokenRepository extends JpaRepository findBySecretTokenType(String secretTokenType); + } diff --git a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationStartChargeResultTask.java b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationStartChargeResultTask.java index 3a7056f7..97acaa0e 100644 --- a/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationStartChargeResultTask.java +++ b/evcs-modules/evcs-core/src/main/java/com/xhpc/evcs/notification/NotificationStartChargeResultTask.java @@ -1,12 +1,92 @@ package com.xhpc.evcs.notification; +import com.xhpc.evcs.domain.AuthSecretToken; +import com.xhpc.evcs.dto.CommonRequest; +import com.xhpc.evcs.dto.DTOJsonHelper; +import com.xhpc.evcs.dto.NotificationStartChargeResultRequestData; +import com.xhpc.evcs.dto.NotificationStartStopChargeResultResponse; +import com.xhpc.evcs.jpa.AuthSecretTokenRepository; +import com.xhpc.evcs.utils.JSONUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import java.io.IOException; +import java.util.Collection; +import java.util.Map; +import java.util.Optional; + +import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; +import static com.xhpc.evcs.domain.AuthSecretToken.SECRET_TOKEN_TYPE_OUT; + @Component public class NotificationStartChargeResultTask extends CoreDispatcher { - //todo invoke the pile service, and then waiting for its response to judge whether the - //todo pile is running actually. + @Autowired + private AuthSecretTokenRepository authSecretTokenRepository; + + private Logger logger = LoggerFactory.getLogger(NotificationStartChargeResultTask.class); + + /** + * Judging the 3rd whether it has got the start charging task. + */ + @Scheduled(fixedRate = 1000 * 20) + public void run() throws IOException { + + //Getting the charge orders which from 3rd. + Collection pushOrderKeys = REDIS.keys("pushOrder:*"); + for (String pushOrderKey : pushOrderKeys) { + Map pushOrder = REDIS.getCacheMap(pushOrderKey); + if (null != pushOrder) { + Integer startChargeNotificationStat = (Integer) pushOrder.get("startChargeNotificationStat"); + if (null != startChargeNotificationStat && 1 != startChargeNotificationStat && startChargeNotificationStat <= 20) { + String startChargeSeq = (String) pushOrder.get("internetSerialNumber"); + String operatorId = startChargeSeq.substring(0, 9); + Optional authSecretToken = authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenType(operatorId, SECRET_TOKEN_TYPE_OUT); + if (authSecretToken.isPresent()) { + NotificationStartChargeResultRequestData notificationStartChargeResultRequestData = new NotificationStartChargeResultRequestData(); + notificationStartChargeResultRequestData.setStartTime((String) pushOrder.get("startTime")); + notificationStartChargeResultRequestData.setStartChargeSeq(startChargeSeq); + notificationStartChargeResultRequestData.setConnectorId((String) pushOrder.get("connectorID")); + notificationStartChargeResultRequestData.setStartChargeSeqStat((Integer) pushOrder.get("startChargeSeqStat")); + notify(notificationStartChargeResultRequestData, authSecretToken.get(), pushOrderKey.substring(10)); + } + } + } + } + } + + //Notifying others platform. + public void notify(NotificationStartChargeResultRequestData notificationStartChargeResultRequestData, AuthSecretToken authSecretTokenOut, String orderNo) throws IOException { + + String operatorIdEvcs = "MA6DFCTD5"; + String data = JSONUtil.toJSONString(notificationStartChargeResultRequestData); + logger.debug(data); + CommonRequest commonRequest = new CommonRequest<>(); + commonRequest.setOperatorId(operatorIdEvcs); + commonRequest.setData(data); + String responseBody = ok(commonRequest, "/notification_start_charge_result", authSecretTokenOut, operatorIdEvcs); + NotificationStartStopChargeResultResponse notificationStartStopChargeResultResponse = DTOJsonHelper.parseResponseData(responseBody, + NotificationStartStopChargeResultResponse.class, authSecretTokenOut); + if (null != notificationStartStopChargeResultResponse) { + //Ensuring that only when startChargeNotificationStat equals 1 won't be notified. + if (notificationStartStopChargeResultResponse.getSuccStat() == 0) { + REDIS.setCacheMapValue("pushOrder:".concat(orderNo), "startChargeNotificationStat", 1); + } else { + Integer startChargeNotificationStat = REDIS.getCacheMapValue("pushOrder:".concat(orderNo), "startChargeNotificationStat"); + if (startChargeNotificationStat == 0) { + startChargeNotificationStat = 2; + } else { + ++startChargeNotificationStat; + } + REDIS.setCacheMapValue("pushOrder:".concat(orderNo), "startChargeNotificationStat", startChargeNotificationStat); + throw new RuntimeException(String.format("push CD start charge order status [%s] failed: %s", + notificationStartChargeResultRequestData.getStartChargeSeq(), responseBody)); + } + } + } }