This commit is contained in:
2024-02-20 16:05:54 +08:00
parent fd51d1a357
commit 055a55cda1
7 changed files with 60 additions and 47 deletions

View File

@@ -11,6 +11,9 @@ import java.math.BigDecimal;
*/ */
public class Constants { public class Constants {
// 十六进制前缀
public static final String HEX_PREFIX = "0x";
// 白名单默认金额 // 白名单默认金额
public static final BigDecimal whitelistDefaultAmount = new BigDecimal(Constants.FIVE_HUNDRED); public static final BigDecimal whitelistDefaultAmount = new BigDecimal(Constants.FIVE_HUNDRED);

View File

@@ -193,8 +193,8 @@ public enum YKCFrameTypeCode {
this.responseFrameType = responseFrameType; this.responseFrameType = responseFrameType;
} }
// 根据请求帧类型 获取应答帧类型 // 根据请求帧类型 获取应答帧类型 int类型
public static int getResponseFrameType(int requestFrameType) { public static int getResponseFrameTypeByRequestFrameType(int requestFrameType) {
for (ResponseRelation responseRelation : ResponseRelation.values()) { for (ResponseRelation responseRelation : ResponseRelation.values()) {
if (responseRelation.getRequestFrameType() == requestFrameType) { if (responseRelation.getRequestFrameType() == requestFrameType) {
return responseRelation.getResponseFrameType(); return responseRelation.getResponseFrameType();
@@ -203,10 +203,12 @@ public enum YKCFrameTypeCode {
return 0; return 0;
} }
public static byte[] getResponseFrameType(byte[] requestFrameType) { // 根据请求帧类型 获取应答帧类型 byte[]类型
public static byte[] getResponseFrameTypeBytes(byte[] requestFrameType) {
int frameType = BytesUtil.bytesToInt(requestFrameType); int frameType = BytesUtil.bytesToInt(requestFrameType);
return BytesUtil.intToBytes(getResponseFrameType(frameType), 1); return BytesUtil.intToBytes(getResponseFrameTypeByRequestFrameType(frameType), 1);
} }
} }
} }

View File

@@ -1,9 +1,9 @@
package com.jsowell.common.util; package com.jsowell.common.util;
import com.google.common.primitives.Bytes; import com.google.common.primitives.Bytes;
import com.jsowell.common.constant.Constants;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
@@ -563,6 +563,9 @@ public class BytesUtil {
* @Description 16进制字符串转字节数组 * @Description 16进制字符串转字节数组
*/ */
public static byte[] hexString2Bytes(String src) { public static byte[] hexString2Bytes(String src) {
if (src.startsWith(Constants.HEX_PREFIX)) {
src = StringUtils.replace(src, Constants.HEX_PREFIX, "");
}
int l = src.length() / 2; int l = src.length() / 2;
byte[] ret = new byte[l]; byte[] ret = new byte[l];
for (int i = 0; i < l; i++) { for (int i = 0; i < l; i++) {
@@ -631,12 +634,12 @@ public class BytesUtil {
* @param bigDecimal * @param bigDecimal
* @return * @return
*/ */
public static byte[] bigDecimal2Bcd(BigDecimal bigDecimal) { // public static byte[] bigDecimal2Bcd(BigDecimal bigDecimal) {
int i = Float.floatToIntBits(bigDecimal.floatValue()); // int i = Float.floatToIntBits(bigDecimal.floatValue());
String hexString = Integer.toHexString(i); // String hexString = Integer.toHexString(i);
return hexString2Bytes(hexString); // return hexString2Bytes(hexString);
//
} // }
/** /**
* int 转 byte[] * int 转 byte[]

View File

@@ -1,6 +1,7 @@
package com.jsowell.common.util; package com.jsowell.common.util;
import com.google.common.primitives.Bytes; import com.google.common.primitives.Bytes;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode; import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -112,16 +113,27 @@ public class YKCUtils {
} }
/** /**
* byte转帧类型字符串 * byte转帧类型字符串 如:"01"--> "0x01"
* @param bytes * @param bytes
* @return * @return
*/ */
public static String frameType2Str(byte[] bytes) { public static String frameType2Str(byte[] bytes) {
String s = BytesUtil.bin2HexStr(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) { 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"; // String hexString = "681E0000003388000000000027012302081602434533880000000000270101008361";
// byte[] byteArray = new byte[hexString.length() / 2]; // byte[] byteArray = new byte[hexString.length() / 2];
// for (int i = 0; i < byteArray.length; i++) { // for (int i = 0; i < byteArray.length; i++) {
@@ -135,24 +147,24 @@ public class YKCUtils {
// System.out.println(binary); // System.out.println(binary);
// System.out.println(aaa); // System.out.println(aaa);
String targetCRC = "0abb"; // String targetCRC = "0abb";
String substring = StringUtils.substring(targetCRC, 0, 2); // String substring = StringUtils.substring(targetCRC, 0, 2);
String substring1 = StringUtils.substring(targetCRC, 2, 4); // String substring1 = StringUtils.substring(targetCRC, 2, 4);
String crc = substring1 + substring; // String crc = substring1 + substring;
//
String hexString = "4f"; // String hexString = "4f";
byte[] bytes = new byte[]{0x4f}; // byte[] bytes = new byte[]{0x4f};
//
String s = transitionTemperature(bytes); // String s = transitionTemperature(bytes);
System.out.println(s); // System.out.println(s);
//
byte[] bytess = new byte[]{(byte) 0x80, (byte) 0x1A, 0x06, 0x00}; // byte[] bytess = new byte[]{(byte) 0x80, (byte) 0x1A, 0x06, 0x00};
String s1 = convertDecimalPoint(bytess, 5); // String s1 = convertDecimalPoint(bytess, 5);
System.out.println(s1); // System.out.println(s1);
//
String amount = "1000"; // String amount = "1000";
byte[] priceByte = getPriceByte(amount, 2); // byte[] priceByte = getPriceByte(amount, 2);
System.out.println(BytesUtil.bin2HexStr(priceByte)); // System.out.println(BytesUtil.bin2HexStr(priceByte));
} }
/** /**

View File

@@ -53,7 +53,7 @@ public abstract class AbstractHandler implements InitializingBean {
// 请求帧类型 // 请求帧类型
byte[] requestFrameType = ykcDataProtocol.getFrameType(); byte[] requestFrameType = ykcDataProtocol.getFrameType();
// 应答帧类型 // 应答帧类型
byte[] responseFrameType = YKCFrameTypeCode.ResponseRelation.getResponseFrameType(requestFrameType); byte[] responseFrameType = YKCFrameTypeCode.ResponseRelation.getResponseFrameTypeBytes(requestFrameType);
// 数据域 值为“序列号域+加密标志+帧类型标志+消息体”字节数之和 // 数据域 值为“序列号域+加密标志+帧类型标志+消息体”字节数之和
byte[] dataFields = Bytes.concat(serialNumber, encryptFlag, responseFrameType, messageBody); byte[] dataFields = Bytes.concat(serialNumber, encryptFlag, responseFrameType, messageBody);

View File

@@ -231,18 +231,6 @@ public class ConfirmStartChargingRequestHandler extends AbstractHandler{
// String transactionCode = IdUtils.generateTransactionCode(pileSn, connectorCode); // String transactionCode = IdUtils.generateTransactionCode(pileSn, connectorCode);
byte[] serialNumByteArr = BytesUtil.str2Bcd(transactionCode); 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 账户不存在 * 0x01 账户不存在

View File

@@ -72,7 +72,8 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
byte[] msg = (byte[]) message; 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)); int serialNumber = BytesUtil.bytesToIntLittle(BytesUtil.copyBytes(msg, 2, 2));
// 获取channel // 获取channel
@@ -95,8 +96,12 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response); ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
this.channelWrite(channel.id(), buffer); this.channelWrite(channel.id(), buffer);
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) { 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)); BytesUtil.binary(response, 16));
} }
} }