充电桩主动申请充电,平台生成订单号回复并启动充电

This commit is contained in:
Lemon
2023-03-18 10:59:02 +08:00
parent a2a5c90704
commit 52a6db253e
12 changed files with 249 additions and 44 deletions

View File

@@ -5,11 +5,19 @@ import com.google.common.primitives.Bytes;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.common.util.id.IdUtils;
import com.jsowell.netty.factory.YKCOperateFactory;
import com.jsowell.pile.domain.PileAuthCard;
import com.jsowell.pile.service.IMemberBasicInfoService;
import com.jsowell.pile.service.IMemberWalletInfoService;
import com.jsowell.pile.service.IPileAuthCardService;
import com.jsowell.pile.service.impl.MemberBasicInfoServiceImpl;
import com.jsowell.pile.service.impl.PileAuthCardServiceImpl;
import com.jsowell.pile.vo.uniapp.MemberVO;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
@@ -33,6 +41,9 @@ public class ConfirmStartChargingRequestHandler extends AbstractHandler{
@Autowired
private MemberBasicInfoServiceImpl memberBasicInfoService;
@Autowired
private IPileAuthCardService pileAuthCardService;
@Override
public void afterPropertiesSet() throws Exception {
YKCOperateFactory.register(type, this);
@@ -45,13 +56,11 @@ public class ConfirmStartChargingRequestHandler extends AbstractHandler{
byte[] msgBody = ykcDataProtocol.getMsgBody();
int startIndex = 0;
int length = 16;
int length = 7;
// 桩编码
startIndex += length;
length = 7;
byte[] pileSnByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String pileSn = BytesUtil.bcd2Str(pileSnByteArr);
String pileSn = BytesUtil.binary(pileSnByteArr, 16);
// 保存时间
saveLastTime(pileSn);
@@ -59,26 +68,28 @@ public class ConfirmStartChargingRequestHandler extends AbstractHandler{
// 枪号
startIndex += length;
length = 1;
byte[] pileConnectorNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String connectorCode = BytesUtil.bcd2Str(pileConnectorNumByteArr);
byte[] connectorNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String connectorCode = BytesUtil.bcd2Str(connectorNumByteArr);
// 启动方式
// 0x01 表示通过刷卡启动充电
// 0x02 表求通过帐号启动充电 (暂不支持)
// 0x03 表示vin码启动充电
startIndex += length;
length = 1;
byte[] startModeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String startMode = BytesUtil.bcd2Str(startModeByteArr);
// 是否需要密码 0x00 不需要 0x01 需要
startIndex += length;
byte[] needPasswordFlagByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String needPasswordFlag = BytesUtil.bcd2Str(needPasswordFlagByteArr);
// 物理卡号 不足 8 位补 0
startIndex += length;
length = 8;
byte[] cardNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String physicsCardNum = BytesUtil.bcd2Str(cardNumByteArr);
String physicsCard = BytesUtil.binary(cardNumByteArr, 16);
log.info("物理卡号:{}", physicsCard);
// 输入密码 对用户输入的密码进行16 位MD5 加密,采用小写上传
startIndex += length;
@@ -90,24 +101,59 @@ public class ConfirmStartChargingRequestHandler extends AbstractHandler{
length = 17;
byte[] vinCodeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String logicCard = "";
byte[] authenticationFlagByteArr = Constants.zeroByteArray;
byte[] accountBalanceByteArr = Constants.zeroByteArray;
try {
if (StringUtils.equals("01", startMode)) {
// 刷卡启动充电
// 根据传过来的物理卡号查询数据库中此卡信息
PileAuthCard pileAuthCard = PileAuthCard.builder()
.logicCard(physicsCard)
.build();
PileAuthCard pileAuthCardInfo = pileAuthCardService.selectPileAuthCardInfo(pileAuthCard);
if (pileAuthCardInfo == null) {
// 未查到此卡信息
throw new BusinessException(ReturnCodeEnum.CODE_THIS_CARD_HAS_NO_INFO);
}
if (StringUtils.isBlank(pileAuthCardInfo.getMemberId())) {
// 卡未绑定用户
throw new BusinessException(ReturnCodeEnum.CODE_THIS_CARD_NOT_BIND_USER);
}
// 卡状态
String cardStatus = pileAuthCardInfo.getStatus();
// 逻辑卡号
logicCard = pileAuthCardInfo.getLogicCard();
// 通过memberId获取账户余额
MemberVO memberVO = memberBasicInfoService.queryMemberInfoByMemberId(pileAuthCardInfo.getMemberId());
BigDecimal principalBalance = memberVO.getPrincipalBalance(); // 本金金额
double accountBalance = principalBalance.add(memberVO.getGiftBalance()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
accountBalanceByteArr = YKCUtils.getPriceByte(String.valueOf(accountBalance), 2);
// 鉴权成功标识 0x00 失败 0x01 成功
authenticationFlagByteArr = Constants.oneByteArray;
}
} catch (BusinessException e){
log.error("刷卡启动充电鉴权 error:{}, {}", e.getCode(), e.getMessage());
}catch (Exception e){
log.error("刷卡启动充电鉴权 error", e);
}
// 应答
// 交易流水号
String serialNum = IdUtils.generateTransactionCode(pileSn, connectorCode);
byte[] serialNumByteArr = BytesUtil.str2Bcd(serialNum);
String transactionCode = IdUtils.generateTransactionCode(pileSn, connectorCode);
byte[] serialNumByteArr = BytesUtil.str2Bcd(transactionCode);
// 逻辑卡号
String logicCardNum = "00000000";
byte[] logicCardNumByteArr = BytesUtil.str2Bcd(logicCardNum); // [0, 0, 0, 0]
// String logicCardNum = "00000000";
// 账户余额 保留两位小数
MemberVO memberVO = memberBasicInfoService.selectInfoByPhysicsCard(physicsCardNum);
BigDecimal principalBalance = memberVO.getPrincipalBalance(); // 本金金额
double accountBalance = principalBalance.add(memberVO.getGiftBalance()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
byte[] accountBalanceByteArr = BytesUtil.str2Bcd(String.valueOf(accountBalance));
// MemberVO memberVO = memberBasicInfoService.selectInfoByPhysicsCard(physicsCard);
// BigDecimal principalBalance = memberVO.getPrincipalBalance(); // 本金金额
// double accountBalance = principalBalance.add(memberVO.getGiftBalance()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
// byte[] accountBalanceByteArr = BytesUtil.str2Bcd(String.valueOf(accountBalance));
// 鉴权成功标识 0x00 失败 0x01 成功
byte[] authenticationFlagByteArr= Constants.oneByteArray;
// byte[] authenticationFlagByteArr = Constants.oneByteArray;
/**
* 失败原因
@@ -126,7 +172,7 @@ public class ConfirmStartChargingRequestHandler extends AbstractHandler{
byte[] defeatReasonByteArr = Constants.zeroByteArray;
// 拼装消息体
byte[] msgBodyByteArr = Bytes.concat(serialNumByteArr, logicCardNumByteArr, accountBalanceByteArr,
byte[] msgBodyByteArr = Bytes.concat(serialNumByteArr, pileSnByteArr, connectorNumByteArr, cardNumByteArr, accountBalanceByteArr,
authenticationFlagByteArr, defeatReasonByteArr);
return getResult(ykcDataProtocol, msgBodyByteArr);

View File

@@ -436,7 +436,7 @@ public class TransactionRecordsRequestHandler extends AbstractHandler {
length = 8;
byte[] cardNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
byte[] logicCardNum = BytesUtil.checkLengthAndBehindAppendZero(cardNumByteArr, 16);
String logicCard = BytesUtil.binary(logicCardNum, 10);
String logicCard = BytesUtil.binary(logicCardNum, 16);
log.info("[===交易记录===]交易流水号:{}, 桩编号:{}, 枪号:{}, 开始时间:{}, 结束时间:{}, 尖单价:{}, 尖电量:{}, 计损尖电量:{}, 尖金额:{}, " +
"峰单价:{}, 峰电量:{}, 计损峰电量:{}, 峰金额:{}, 平单价:{}, 平电量:{}, 计损平电量:{}, 平金额:{}, " +