优化架构,获取设备注册时的设备相关信息
This commit is contained in:
@@ -13,6 +13,8 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -25,6 +27,12 @@ import java.util.List;
|
||||
public class Jt808Decoder extends ByteToMessageDecoder {
|
||||
private final static Logger log = LoggerFactory.getLogger(Jt808Decoder.class);
|
||||
|
||||
private ApplicationEventPublisher applicationEventPublisher = null;
|
||||
|
||||
public Jt808Decoder(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
Session session = ctx.channel().attr(Session.KEY).get();
|
||||
@@ -51,6 +59,10 @@ public class Jt808Decoder extends ByteToMessageDecoder {
|
||||
return;
|
||||
}
|
||||
Rs decode = handler.decode(buf, header, session);
|
||||
ApplicationEvent applicationEvent = handler.getEvent();
|
||||
if (applicationEvent != null) {
|
||||
applicationEventPublisher.publishEvent(applicationEvent);
|
||||
}
|
||||
if (decode != null) {
|
||||
out.add(decode);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import io.netty.handler.timeout.IdleStateHandler;
|
||||
import io.netty.util.concurrent.Future;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -35,11 +36,13 @@ public class TcpServer {
|
||||
private boolean isRunning = false;
|
||||
private EventLoopGroup bossGroup = null;
|
||||
private EventLoopGroup workerGroup = null;
|
||||
private ApplicationEventPublisher applicationEventPublisher = null;
|
||||
|
||||
private final ByteBuf DECODER_JT808 = Unpooled.wrappedBuffer(new byte[]{0x7e});
|
||||
|
||||
public TcpServer(Integer port) {
|
||||
public TcpServer(Integer port, ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.port = port;
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
private void startTcpServer() {
|
||||
@@ -60,7 +63,7 @@ public class TcpServer {
|
||||
channel.pipeline()
|
||||
.addLast(new IdleStateHandler(10, 0, 0, TimeUnit.MINUTES))
|
||||
.addLast(new DelimiterBasedFrameDecoder(1024 * 2, DECODER_JT808))
|
||||
.addLast(new Jt808Decoder())
|
||||
.addLast(new Jt808Decoder(applicationEventPublisher))
|
||||
.addLast(new Jt808Encoder())
|
||||
.addLast(new Jt808EncoderCmd())
|
||||
.addLast(new Jt808Handler());
|
||||
|
||||
Reference in New Issue
Block a user