优化友电协议兼容逻辑

This commit is contained in:
Guoqs
2026-06-13 12:56:09 +08:00
parent 4e774d7d7d
commit 682f1b4b71
7 changed files with 195 additions and 48 deletions

View File

@@ -118,21 +118,23 @@ public class YouDianProtocolDecoder extends ByteToMessageDecoder {
ByteBuf frame = null;
try {
// 检查剩余数据是否足够
if (buffer.readableBytes() < HEADER_LENGTH_DNY + 1) {
if (buffer.readableBytes() < HEADER_LENGTH_DNY + 2) {
buffer.readerIndex(beginReader);
return;
}
// 获取消息长度
int length = buffer.getUnsignedByte(beginReader + HEADER_LENGTH_DNY);
// DNY协议长度域为2字节小端模式。长度不包含包头和长度域本身。
int length = buffer.getUnsignedByte(beginReader + HEADER_LENGTH_DNY)
| (buffer.getUnsignedByte(beginReader + HEADER_LENGTH_DNY + 1) << 8);
// log.info("获取消息长度, length:{}", length);
int frameLength = HEADER_LENGTH_DNY + 2 + length;
// 检查剩余数据是否足够
if (buffer.readableBytes() < HEADER_LENGTH_DNY + 1 + length) {
if (buffer.readableBytes() < frameLength) {
buffer.readerIndex(beginReader);
return;
}
// 读取 data 数据
frame = buffer.retainedSlice(beginReader, HEADER_LENGTH_DNY + length + 2);
buffer.readerIndex(beginReader + HEADER_LENGTH_DNY + length + 2);
frame = buffer.retainedSlice(beginReader, frameLength);
buffer.readerIndex(beginReader + frameLength);
out.add(frame);
} finally {
// if (frame != null) {

View File

@@ -1,8 +1,10 @@
package com.jsowell.netty.handler.electricbicycles;
import com.alibaba.fastjson2.JSON;
import com.jsowell.common.YouDianUtils;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ebike.EBikeDataProtocol;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.factory.EBikeOperateFactory;
import com.jsowell.pile.domain.ebike.EBikeCommandEnum;
@@ -39,12 +41,46 @@ public class RegistrationHandler extends AbstractEBikeHandler {
*/
@Override
public byte[] supplyProcess(EBikeDataProtocol dataProtocol, ChannelHandlerContext ctx) {
// 解析字节数组
EBikeMessageCmd20 message = new EBikeMessageCmd20(dataProtocol.getBytes());
EBikeMessageCmd20 message;
try {
// 解析字节数组
message = new EBikeMessageCmd20(dataProtocol.getBytes());
} catch (Exception e) {
handleRegistrationParseError(dataProtocol, ctx, e);
return getResult(dataProtocol, Constants.zeroByteArray);
}
// 保存时间
saveLastTimeAndCheckChannel(message.getPhysicalId() + "", ctx);
log.info("设备注册包:{}", JSON.toJSONString(message));
pileBasicInfoService.registrationEBikePile(message);
try {
pileBasicInfoService.registrationEBikePile(message);
} catch (Exception e) {
log.error("设备注册包自动建档失败, pileSn:{}, portNumber:{}, msg:{}",
message.getPhysicalId(), message.getPortNumber(), BytesUtil.binary(dataProtocol.getBytes(), 16), e);
}
return getResult(dataProtocol, Constants.zeroByteArray);
}
private void handleRegistrationParseError(EBikeDataProtocol dataProtocol, ChannelHandlerContext ctx, Exception e) {
String pileSn = null;
int portNumber = 0;
try {
pileSn = YouDianUtils.convertToPhysicalId(dataProtocol.getPhysicalId()) + "";
byte[] msgBody = dataProtocol.getMsgBody();
if (msgBody != null && msgBody.length >= 3) {
portNumber = BytesUtil.bytesToIntLittle(BytesUtil.copyBytes(msgBody, 2, 1));
}
saveLastTimeAndCheckChannel(pileSn, ctx);
log.error("设备注册包解析失败, pileSn:{}, portNumber:{}, msg:{}, msgBodyLength:{}",
pileSn, portNumber, BytesUtil.binary(dataProtocol.getBytes(), 16),
msgBody == null ? 0 : msgBody.length, e);
if (portNumber > 0) {
pileBasicInfoService.ensureEBikePileRegistered(pileSn, portNumber, "registration_0x20_parse_fallback");
}
} catch (Exception fallbackException) {
log.error("设备注册包解析失败后兜底处理失败, pileSn:{}, portNumber:{}, msg:{}",
pileSn, portNumber, BytesUtil.binary(dataProtocol.getBytes(), 16), fallbackException);
}
}
}