update 电单车协议

This commit is contained in:
Guoqs
2024-08-29 15:10:41 +08:00
parent 45ff60f052
commit 896fa64e34
3 changed files with 39 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package com.jsowell.common.util;
import com.google.common.primitives.Bytes;
import com.jsowell.common.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
@@ -11,6 +12,7 @@ import java.time.LocalDateTime;
import java.util.Date;
import java.util.Stack;
@Slf4j
public class BytesUtil {
static final long fx = 0xffl;
@@ -234,6 +236,35 @@ 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.info("设备识别码:{}, 桩编号:{}", deviceId, deviceNumber);
return deviceNumber;
}
/**
* @函数功能: BCD码转为10进制串(阿拉伯数据) 小端模式
* @输入参数: BCD码