update 电单车协议

This commit is contained in:
Guoqs
2024-08-29 15:24:29 +08:00
parent 430a03ba7e
commit d60d50ed8a
4 changed files with 30 additions and 44 deletions

View File

@@ -10,21 +10,35 @@ import java.util.Arrays;
*/
@Slf4j
public class YouDianUtils {
public static void main(String[] args) {
String s = "44 4e 59 0a 00 3b 37 ab 04 01 00 21 00 38 02";
byte[] bytes = BytesUtil.hexStringToByteArray(s);
String s2 = BytesUtil.printHexBinary(bytes);
System.out.println(s2);
byte[] bytes2 = BytesUtil.hexStringToByteArray(s2);
System.out.println(bytes2);
boolean b = validateChecksum(bytes);
//
String s3 = "44 4e 59 0a 00 3b 37 ab 04 01 00 21 00";
byte[] bytes3 = BytesUtil.hexStringToByteArray(s3);
int i = calculateCheckField(bytes3);
BytesUtil.intToBytesLittle(i);
/**
* 将byte数组转换为物理ID
* @param bytes 输入的byte数组
* @return 物理ID对象包含设备识别码和二维码下方的编号
*/
public static int convertToPhysicalId(byte[] bytes) {
// 检查输入是否合法
if (bytes == null || bytes.length != 4) {
throw new IllegalArgumentException("Input byte array must be of length 4.");
}
// 小端模式转大端模式
byte[] bigEndianBytes = new byte[4];
bigEndianBytes[0] = bytes[3];
bigEndianBytes[1] = bytes[2];
bigEndianBytes[2] = bytes[1];
bigEndianBytes[3] = bytes[0];
// 提取设备识别码
byte deviceId = bigEndianBytes[0];
// 剩余的三个字节转换为十进制
int deviceNumber = ((bigEndianBytes[1] & 0xFF) << 16) |
((bigEndianBytes[2] & 0xFF) << 8) |
(bigEndianBytes[3] & 0xFF);
log.debug("设备识别码:{}, 桩编号:{}", deviceId, deviceNumber);
return deviceNumber;
}
/**

View File

@@ -236,35 +236,6 @@ public class BytesUtil {
return new String(temp);
}
/**
* 将byte数组转换为物理ID
* @param bytes 输入的byte数组
* @return 物理ID对象包含设备识别码和二维码下方的编号
*/
public static int convertToPhysicalId(byte[] bytes) {
// 检查输入是否合法
if (bytes == null || bytes.length != 4) {
throw new IllegalArgumentException("Input byte array must be of length 4.");
}
// 小端模式转大端模式
byte[] bigEndianBytes = new byte[4];
bigEndianBytes[0] = bytes[3];
bigEndianBytes[1] = bytes[2];
bigEndianBytes[2] = bytes[1];
bigEndianBytes[3] = bytes[0];
// 提取设备识别码
byte deviceId = bigEndianBytes[0];
// 剩余的三个字节转换为十进制
int deviceNumber = ((bigEndianBytes[1] & 0xFF) << 16) |
((bigEndianBytes[2] & 0xFF) << 8) |
(bigEndianBytes[3] & 0xFF);
log.debug("设备识别码:{}, 桩编号:{}", deviceId, deviceNumber);
return deviceNumber;
}
/**
* @函数功能: BCD码转为10进制串(阿拉伯数据) 小端模式
* @输入参数: BCD码

View File

@@ -64,7 +64,7 @@ public class ElectricBicyclesServerHandler extends ChannelInboundHandlerAdapter
// 处理数据
byte[] response = eBikeService.process(msg, ctx);
if (Objects.nonNull(response)) {
log.info("响应数据:{}", BytesUtil.binary(response, 16));
log.info("[响应数据]:{}", BytesUtil.binary(response, 16));
// 响应客户端
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
// this.channelWrite(channel.id(), buffer);

View File

@@ -1,5 +1,6 @@
package com.jsowell.pile.domain.ebike;
import com.jsowell.common.YouDianUtils;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.pile.domain.ebike.deviceupload.*;
import lombok.Getter;
@@ -73,7 +74,7 @@ public abstract class AbsEBikeMessage {
// 读取物理ID
byte[] physicalIdBytes = Arrays.copyOfRange(messageBytes, 5, 9);
int physicalId = BytesUtil.convertToPhysicalId(physicalIdBytes);
int physicalId = YouDianUtils.convertToPhysicalId(physicalIdBytes);
// 读取消息ID
byte[] messageIdBytes = Arrays.copyOfRange(messageBytes, 9, 11);