From 055a55cda1c828d64f5b6071f0509a40aa577750 Mon Sep 17 00:00:00 2001 From: "autumn.g@foxmail.com" Date: Tue, 20 Feb 2024 16:05:54 +0800 Subject: [PATCH] update --- .../jsowell/common/constant/Constants.java | 3 ++ .../core/domain/ykc/YKCFrameTypeCode.java | 10 ++-- .../com/jsowell/common/util/BytesUtil.java | 17 +++--- .../com/jsowell/common/util/YKCUtils.java | 52 ++++++++++++------- .../netty/handler/AbstractHandler.java | 2 +- .../ConfirmStartChargingRequestHandler.java | 12 ----- .../yunkuaichong/NettyServerHandler.java | 11 ++-- 7 files changed, 60 insertions(+), 47 deletions(-) diff --git a/jsowell-common/src/main/java/com/jsowell/common/constant/Constants.java b/jsowell-common/src/main/java/com/jsowell/common/constant/Constants.java index 8d34ccddd..28babdc02 100644 --- a/jsowell-common/src/main/java/com/jsowell/common/constant/Constants.java +++ b/jsowell-common/src/main/java/com/jsowell/common/constant/Constants.java @@ -11,6 +11,9 @@ import java.math.BigDecimal; */ public class Constants { + // 十六进制前缀 + public static final String HEX_PREFIX = "0x"; + // 白名单默认金额 public static final BigDecimal whitelistDefaultAmount = new BigDecimal(Constants.FIVE_HUNDRED); diff --git a/jsowell-common/src/main/java/com/jsowell/common/core/domain/ykc/YKCFrameTypeCode.java b/jsowell-common/src/main/java/com/jsowell/common/core/domain/ykc/YKCFrameTypeCode.java index ff6e7aa80..be8eed69c 100644 --- a/jsowell-common/src/main/java/com/jsowell/common/core/domain/ykc/YKCFrameTypeCode.java +++ b/jsowell-common/src/main/java/com/jsowell/common/core/domain/ykc/YKCFrameTypeCode.java @@ -193,8 +193,8 @@ public enum YKCFrameTypeCode { this.responseFrameType = responseFrameType; } - // 根据请求帧类型 获取应答帧类型 - public static int getResponseFrameType(int requestFrameType) { + // 根据请求帧类型 获取应答帧类型 int类型 + public static int getResponseFrameTypeByRequestFrameType(int requestFrameType) { for (ResponseRelation responseRelation : ResponseRelation.values()) { if (responseRelation.getRequestFrameType() == requestFrameType) { return responseRelation.getResponseFrameType(); @@ -203,10 +203,12 @@ public enum YKCFrameTypeCode { return 0; } - public static byte[] getResponseFrameType(byte[] requestFrameType) { + // 根据请求帧类型 获取应答帧类型 byte[]类型 + public static byte[] getResponseFrameTypeBytes(byte[] requestFrameType) { int frameType = BytesUtil.bytesToInt(requestFrameType); - return BytesUtil.intToBytes(getResponseFrameType(frameType), 1); + return BytesUtil.intToBytes(getResponseFrameTypeByRequestFrameType(frameType), 1); } + } } diff --git a/jsowell-common/src/main/java/com/jsowell/common/util/BytesUtil.java b/jsowell-common/src/main/java/com/jsowell/common/util/BytesUtil.java index 9fda0a9a5..a8d43eb6f 100644 --- a/jsowell-common/src/main/java/com/jsowell/common/util/BytesUtil.java +++ b/jsowell-common/src/main/java/com/jsowell/common/util/BytesUtil.java @@ -1,9 +1,9 @@ package com.jsowell.common.util; import com.google.common.primitives.Bytes; +import com.jsowell.common.constant.Constants; import java.io.UnsupportedEncodingException; -import java.math.BigDecimal; import java.math.BigInteger; import java.time.LocalDateTime; import java.util.Date; @@ -563,6 +563,9 @@ public class BytesUtil { * @Description 16进制字符串转字节数组 */ public static byte[] hexString2Bytes(String src) { + if (src.startsWith(Constants.HEX_PREFIX)) { + src = StringUtils.replace(src, Constants.HEX_PREFIX, ""); + } int l = src.length() / 2; byte[] ret = new byte[l]; for (int i = 0; i < l; i++) { @@ -631,12 +634,12 @@ public class BytesUtil { * @param bigDecimal * @return */ - public static byte[] bigDecimal2Bcd(BigDecimal bigDecimal) { - int i = Float.floatToIntBits(bigDecimal.floatValue()); - String hexString = Integer.toHexString(i); - return hexString2Bytes(hexString); - - } + // public static byte[] bigDecimal2Bcd(BigDecimal bigDecimal) { + // int i = Float.floatToIntBits(bigDecimal.floatValue()); + // String hexString = Integer.toHexString(i); + // return hexString2Bytes(hexString); + // + // } /** * int 转 byte[] diff --git a/jsowell-common/src/main/java/com/jsowell/common/util/YKCUtils.java b/jsowell-common/src/main/java/com/jsowell/common/util/YKCUtils.java index 9245b0d57..76d6f303d 100644 --- a/jsowell-common/src/main/java/com/jsowell/common/util/YKCUtils.java +++ b/jsowell-common/src/main/java/com/jsowell/common/util/YKCUtils.java @@ -1,6 +1,7 @@ package com.jsowell.common.util; import com.google.common.primitives.Bytes; +import com.jsowell.common.constant.Constants; import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode; import lombok.extern.slf4j.Slf4j; @@ -112,16 +113,27 @@ public class YKCUtils { } /** - * byte转帧类型字符串 + * byte转帧类型字符串 如:"01"--> "0x01" * @param bytes * @return */ public static String frameType2Str(byte[] bytes) { String s = BytesUtil.bin2HexStr(bytes); - return "0x" + s; + return Constants.HEX_PREFIX + s; + } + + public static byte[] frameTypeStr2Bytes(String frameTypeStr) { + byte[] bytes = BytesUtil.hexString2Bytes(frameTypeStr); + return bytes; } public static void main(String[] args) { + String frameTypeStr = "0x01"; + byte[] bytes = frameTypeStr2Bytes(frameTypeStr); + System.out.println("转为byte数组:" + Arrays.toString(bytes)); + String frameType2Str = frameType2Str(bytes); + System.out.println("转为Str:" + frameType2Str); + // String hexString = "681E0000003388000000000027012302081602434533880000000000270101008361"; // byte[] byteArray = new byte[hexString.length() / 2]; // for (int i = 0; i < byteArray.length; i++) { @@ -135,24 +147,24 @@ public class YKCUtils { // System.out.println(binary); // System.out.println(aaa); - String targetCRC = "0abb"; - String substring = StringUtils.substring(targetCRC, 0, 2); - String substring1 = StringUtils.substring(targetCRC, 2, 4); - String crc = substring1 + substring; - - String hexString = "4f"; - byte[] bytes = new byte[]{0x4f}; - - String s = transitionTemperature(bytes); - System.out.println(s); - - byte[] bytess = new byte[]{(byte) 0x80, (byte) 0x1A, 0x06, 0x00}; - String s1 = convertDecimalPoint(bytess, 5); - System.out.println(s1); - - String amount = "1000"; - byte[] priceByte = getPriceByte(amount, 2); - System.out.println(BytesUtil.bin2HexStr(priceByte)); + // String targetCRC = "0abb"; + // String substring = StringUtils.substring(targetCRC, 0, 2); + // String substring1 = StringUtils.substring(targetCRC, 2, 4); + // String crc = substring1 + substring; + // + // String hexString = "4f"; + // byte[] bytes = new byte[]{0x4f}; + // + // String s = transitionTemperature(bytes); + // System.out.println(s); + // + // byte[] bytess = new byte[]{(byte) 0x80, (byte) 0x1A, 0x06, 0x00}; + // String s1 = convertDecimalPoint(bytess, 5); + // System.out.println(s1); + // + // String amount = "1000"; + // byte[] priceByte = getPriceByte(amount, 2); + // System.out.println(BytesUtil.bin2HexStr(priceByte)); } /** diff --git a/jsowell-netty/src/main/java/com/jsowell/netty/handler/AbstractHandler.java b/jsowell-netty/src/main/java/com/jsowell/netty/handler/AbstractHandler.java index a3eac19cc..3e560ea9a 100644 --- a/jsowell-netty/src/main/java/com/jsowell/netty/handler/AbstractHandler.java +++ b/jsowell-netty/src/main/java/com/jsowell/netty/handler/AbstractHandler.java @@ -53,7 +53,7 @@ public abstract class AbstractHandler implements InitializingBean { // 请求帧类型 byte[] requestFrameType = ykcDataProtocol.getFrameType(); // 应答帧类型 - byte[] responseFrameType = YKCFrameTypeCode.ResponseRelation.getResponseFrameType(requestFrameType); + byte[] responseFrameType = YKCFrameTypeCode.ResponseRelation.getResponseFrameTypeBytes(requestFrameType); // 数据域 值为“序列号域+加密标志+帧类型标志+消息体”字节数之和 byte[] dataFields = Bytes.concat(serialNumber, encryptFlag, responseFrameType, messageBody); diff --git a/jsowell-netty/src/main/java/com/jsowell/netty/handler/ConfirmStartChargingRequestHandler.java b/jsowell-netty/src/main/java/com/jsowell/netty/handler/ConfirmStartChargingRequestHandler.java index 0444fb520..d8824e60a 100644 --- a/jsowell-netty/src/main/java/com/jsowell/netty/handler/ConfirmStartChargingRequestHandler.java +++ b/jsowell-netty/src/main/java/com/jsowell/netty/handler/ConfirmStartChargingRequestHandler.java @@ -231,18 +231,6 @@ public class ConfirmStartChargingRequestHandler extends AbstractHandler{ // String transactionCode = IdUtils.generateTransactionCode(pileSn, connectorCode); byte[] serialNumByteArr = BytesUtil.str2Bcd(transactionCode); - // 逻辑卡号 - // String logicCardNum = "00000000"; - - // 账户余额 保留两位小数 - // 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; - /** * 失败原因 * 0x01 账户不存在 diff --git a/jsowell-netty/src/main/java/com/jsowell/netty/server/yunkuaichong/NettyServerHandler.java b/jsowell-netty/src/main/java/com/jsowell/netty/server/yunkuaichong/NettyServerHandler.java index b7bf5f535..c490eca9e 100644 --- a/jsowell-netty/src/main/java/com/jsowell/netty/server/yunkuaichong/NettyServerHandler.java +++ b/jsowell-netty/src/main/java/com/jsowell/netty/server/yunkuaichong/NettyServerHandler.java @@ -72,7 +72,8 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { byte[] msg = (byte[]) message; // 获取帧类型 - String frameType = YKCUtils.frameType2Str(BytesUtil.copyBytes(msg, 5, 1)); + byte[] frameTypeBytes = BytesUtil.copyBytes(msg, 5, 1); + String frameType = YKCUtils.frameType2Str(frameTypeBytes); // 获取序列号域 int serialNumber = BytesUtil.bytesToIntLittle(BytesUtil.copyBytes(msg, 2, 2)); // 获取channel @@ -95,8 +96,12 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { ByteBuf buffer = ctx.alloc().buffer().writeBytes(response); this.channelWrite(channel.id(), buffer); if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) { - log.info("【>>>>>平台响应消息>>>>>】channel:{}, 帧类型:{}, 帧名称:{}, 序列号域:{}, response:{}", - channel.id(), frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber, + // 应答帧类型 + byte[] responseFrameTypeBytes = YKCFrameTypeCode.ResponseRelation.getResponseFrameTypeBytes(frameTypeBytes); + String responseFrameType = YKCUtils.frameType2Str(responseFrameTypeBytes); + log.info("【>>>>>平台响应消息>>>>>】channel:{}, 响应帧类型:{}, 响应帧名称:{}, 原帧类型:{}, 原帧名称:{}, 序列号域:{}, response:{}", + channel.id(), responseFrameType, YKCFrameTypeCode.getFrameTypeStr(responseFrameType), + frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber, BytesUtil.binary(response, 16)); } }