From 853db78efb186052c2a24068a3c3208e33bdd4cd Mon Sep 17 00:00:00 2001 From: Guoqs <123@jsowell.com> Date: Thu, 23 May 2024 16:41:20 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E8=BF=9C=E7=A8=8B=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jsowell/common/util/BytesUtil.java | 86 ++++++++++++------- .../impl/YKCPushCommandServiceImpl.java | 16 ++-- 2 files changed, 60 insertions(+), 42 deletions(-) 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 6420b28d1..65a2f56b6 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 @@ -516,37 +516,6 @@ public class BytesUtil { return result; } - public static void main(String[] args) { - // byte[] a = new byte[] {0x0C, 0x00, 0x00, 0x00, 0x02, 0x20, 0x22, 0x12, 0x14, 0x00, 0x00, 0x01, 0x00}; - // byte[] bytes = intToBytes(CRC16Util.calcCrc16(a)); - // String binary = binary(bytes, 16); - // System.out.println(binary); - - // String a = "10"; - // byte[] bytes = bigDecimal2Bcd(new BigDecimal(a)); - // - // System.out.println(bytes); - - // byte[] startTimeByteArr = new byte[] {(byte) 0x98, (byte) 0xB7, 0x0E, 0x11, 0x10, 0x03, 0x14}; - // String binary = binary(startTimeByteArr, 16); - // String s = DateUtils.decodeCP56Time2a(binary); - // System.out.println(s); - - byte[] a = new byte[] {0x33, 0x37, 0x31, 0x31, 0x30, 0x30, 0x41, 0x4E, 0x38, 0x42, 0x53, 0x33, 0x43, 0x47, 0x38, 0x36, 0x4C}; - String s = ascii2Str(a); - System.out.println(s); - String s1 = ascii2StrLittle(a); - System.out.println(s1); - - // BigDecimal chargeAmount = new BigDecimal("10.5").setScale(2, BigDecimal.ROUND_HALF_UP); - // byte[] accountBalanceByteArr = BytesUtil.getFloatBytes(chargeAmount.floatValue()); - // - // float aFloat = BytesUtil.getFloat(accountBalanceByteArr); - // - // System.out.println(aFloat); - - } - /** * @param hexString * @return 将十六进制转换为二进制字节数组 16-2 @@ -601,7 +570,7 @@ public class BytesUtil { */ public static byte[] checkLengthAndBehindAppendZero(byte[] msg, int length) { String s = BytesUtil.binary(msg, 16); - int msgLen = msg.length; + int msgLen = s.length(); if (msgLen < length) { while (msgLen < length) { StringBuffer sb = new StringBuffer(); @@ -609,7 +578,7 @@ public class BytesUtil { sb.append(s).append("0"); s = sb.toString(); msgLen = s.length(); - } + } } else { return msg; } @@ -639,6 +608,57 @@ public class BytesUtil { return BytesUtil.str2Bcd(s); } + /** + * 确保byte数组长度至少为指定长度,不足则在末尾补充0。 + * + * @param bytes 原始byte数组 + * @param length 指定的最小长度 + * @return 补充0后确保长度至少为指定长度的byte数组 + */ + public static byte[] ensureLength(byte[] bytes, int length) { + if (bytes.length < length) { + // 创建一个新的byte数组,长度为指定长度,前面填充原数组的元素,后面补充0 + byte[] newArray = new byte[length]; + System.arraycopy(bytes, 0, newArray, 0, bytes.length); + return newArray; + } + // 如果原数组长度已经达到或超过指定长度,则直接返回原数组 + return bytes; + } + + public static void main(String[] args) { + byte[] originalBytes = {1, 2, 3}; + int desiredLength = 5; + byte[] result = ensureLengthPrependZero(originalBytes, desiredLength); + + System.out.println("Original array: "); + for (byte b : originalBytes) { + System.out.print(b + " "); + } + System.out.println("\nResulting array with padding at the beginning: "); + for (byte b : result) { + System.out.print(b + " "); + } + } + + /** + * 确保byte数组长度至少为指定长度,不足则在前面补充0。 + * + * @param bytes 原始byte数组 + * @param length 指定的最小长度 + * @return 补充0后确保长度至少为指定长度的byte数组,不足部分在前面补充 + */ + public static byte[] ensureLengthPrependZero(byte[] bytes, int length) { + if (bytes.length < length) { + // 创建一个新的byte数组,长度为指定长度,前面补充0,后面放置原数组的元素 + byte[] newArray = new byte[length]; + System.arraycopy(bytes, 0, newArray, length - bytes.length, bytes.length); + return newArray; + } + // 如果原数组长度已经达到或超过指定长度,则直接返回原数组 + return bytes; + } + /** * 将金额转换成BCD数组 * diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/YKCPushCommandServiceImpl.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/YKCPushCommandServiceImpl.java index 93f91d074..68c2a3955 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/YKCPushCommandServiceImpl.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/YKCPushCommandServiceImpl.java @@ -72,7 +72,7 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService { String value = ((YKCFrameTypeCode) frameTypeCode).getValue(); if (Objects.isNull(channel)) { log.error("push命令[{}]失败, 桩号:{}无法获取到长连接, 请检查充电桩连接状态!", value, pileSn); - return false; + // return false; } /** * 拼接报文 @@ -326,24 +326,22 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService { int i = Integer.parseInt(ratedPower); // byte[] ratedPowerByteArr = Base64.getDecoder().decode(ratedPower); - byte[] ratedPowerByteArr = BytesUtil.checkLengthAndBehindAppendZero(Constants.zeroByteArray, 4); + byte[] ratedPowerByteArr = BytesUtil.ensureLength(Constants.zeroByteArray, 2); // 升级服务器地址 - // byte[] updateServerAddressByteArr = BytesUtil.checkLengthAndBehindAppendZero(BytesUtil.str2Asc(Constants.updateServerIP), 32); - byte[] updateServerAddressByteArr = BytesUtil.checkLengthAndBehindAppendZero(BytesUtil.str2Asc(command.getServerAddress()), 32); + byte[] updateServerAddressByteArr = BytesUtil.ensureLength(BytesUtil.str2Asc(command.getServerAddress()), 16); // 升级服务器端口 - // byte[] updateServerPortByteArr = BytesUtil.checkLengthAndBehindAppendZero(Constants.updateServerPort, 4); - byte[] updateServerPortByteArr = BytesUtil.checkLengthAndBehindAppendZero(BytesUtil.hexString2Bytes(Integer.toHexString(command.getPort())), 4); + byte[] updateServerPortByteArr = BytesUtil.ensureLength(BytesUtil.hexString2Bytes(Integer.toHexString(command.getPort())), 2); // 用户名 - byte[] userNameByteArr = BytesUtil.checkLengthAndBehindAppendZero(BytesUtil.str2Asc(command.getUsername()), 32); + byte[] userNameByteArr = BytesUtil.ensureLength(BytesUtil.str2Asc(command.getUsername()), 16); // 密码 - byte[] passwordByteArr = BytesUtil.checkLengthAndBehindAppendZero(BytesUtil.str2Asc(command.getPassword()), 32); + byte[] passwordByteArr = BytesUtil.ensureLength(BytesUtil.str2Asc(command.getPassword()), 16); // 文件路径 - byte[] filePathByteArr = BytesUtil.checkLengthAndBehindAppendZero(BytesUtil.str2Asc(command.getFilePath()), 64); + byte[] filePathByteArr = BytesUtil.ensureLength(BytesUtil.str2Asc(command.getFilePath()), 64); // 执行控制 01:立即执行 02:空闲执行 byte[] performTypeByteArr = Constants.oneByteArray;