Accomplishing the notification_start_charge_result.

This commit is contained in:
little-cat-sweet 2021-11-01 15:54:09 +08:00
parent 9cb93c094c
commit 90da0ef85d
3 changed files with 86 additions and 3 deletions

View File

@ -69,12 +69,14 @@ public class QueryStartChargeController {
startChargeResponse.setFailReason(0);
//insert a gunStatusData to redis
Map<String, Object> 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()));

View File

@ -33,4 +33,5 @@ public interface AuthSecretTokenRepository extends JpaRepository<AuthSecretToken
List<AuthSecretToken> findBySecretTokenType(String secretTokenType);
}

View File

@ -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<String> pushOrderKeys = REDIS.keys("pushOrder:*");
for (String pushOrderKey : pushOrderKeys) {
Map<String, Object> 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> 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<NotificationStartChargeResultRequestData> 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));
}
}
}
}