update 电单车协议

This commit is contained in:
Guoqs
2024-08-26 14:45:19 +08:00
parent f27773ba05
commit c091b6dea7
10 changed files with 144 additions and 83 deletions

View File

@@ -1,11 +1,10 @@
package com.jsowell.netty.server.electricbicycles;
import com.jsowell.netty.decoder.MessageDecode;
import com.jsowell.netty.decoder.MessageEncode;
import com.jsowell.netty.decoder.StartAndLengthFieldFrameDecoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;
@@ -22,8 +21,10 @@ public class ElectricBicyclesServerChannelInitializer extends ChannelInitializer
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast("frameDecoder", new StartAndLengthFieldFrameDecoder());
pipeline.addLast("decoder", new MessageDecode());
pipeline.addLast("encoder", new MessageEncode());
// pipeline.addLast("decoder", new MessageDecode());
// pipeline.addLast("encoder", new MessageEncode());
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayDecoder());
//读超时时间设置为10s0表示不监控
pipeline.addLast(new IdleStateHandler(60, 0, 0, TimeUnit.SECONDS));
pipeline.addLast("handler", chargingPileHandler);

View File

@@ -1,8 +1,9 @@
package com.jsowell.netty.server.electricbicycles;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.jsowell.netty.service.electricbicycles.EBikeBusinessService;
import com.jsowell.pile.domain.ebike.AbsEBikeMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
@@ -11,7 +12,9 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -19,16 +22,38 @@ import java.util.concurrent.TimeUnit;
@ChannelHandler.Sharable
@Slf4j
@Component
public class ElectricBicyclesServerHandler extends SimpleChannelInboundHandler<AbsEBikeMessage> {
public class ElectricBicyclesServerHandler extends SimpleChannelInboundHandler<Object> {
private final Map<String, ResponseFuture> responseFutureMap = new ConcurrentHashMap<>();
private final List<String> notPrintFrameTypeList = Lists.newArrayList("0x03");
@Autowired
private EBikeBusinessService eBikeService;
@Override
protected void channelRead0(ChannelHandlerContext ctx, AbsEBikeMessage msg) throws Exception {
log.info("收到消息, channelId:{}, msg:{}", ctx.channel().id().toString(), JSON.toJSONString(msg));
protected void channelRead0(ChannelHandlerContext ctx, Object message) throws Exception {
Channel channel = ctx.channel();
log.info("收到消息, channelId:{}, msg:{}", channel.id().toString(), JSON.toJSONString(message));
byte[] msg = (byte[]) message;
// 处理数据
byte[] response = eBikeService.process(msg, ctx);
if (Objects.nonNull(response)) {
// 响应客户端
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
// this.channelWrite(channel.id(), buffer);
ctx.writeAndFlush(buffer);
// if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
// // 应答帧类型
// byte[] responseFrameTypeBytes = YKCFrameTypeCode.PlatformAnswersRelation.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));
// }
}
}
public String sendCommandAndWaitForResponse(Channel channel, String command, long timeout) throws InterruptedException {