From cb3cbeba7b17deb777e9866b4718bb41527c3b37 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@63.com>
Date: 星期六, 30 七月 2022 17:07:26 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/netty/IMessageBody.java | 11 +++
src/main/java/com/zy/core/netty/handle/ProtectorHandler.java | 4
src/main/java/com/zy/core/netty/constant/FireDataType.java | 30 ++++++++++
src/main/java/com/zy/core/netty/handle/PackageServerHandler.java | 13 ++++
src/main/java/com/zy/core/netty/domain/fire/Fire_3030.java | 76 +++++++++++++++++++++++++
5 files changed, 131 insertions(+), 3 deletions(-)
diff --git a/src/main/java/com/zy/core/netty/IMessageBody.java b/src/main/java/com/zy/core/netty/IMessageBody.java
new file mode 100644
index 0000000..d41e030
--- /dev/null
+++ b/src/main/java/com/zy/core/netty/IMessageBody.java
@@ -0,0 +1,11 @@
+package com.zy.core.netty;
+
+public interface IMessageBody {
+
+ int DEFAULT_LEN = 256;
+
+ byte[] writeToBytes();
+
+ IMessageBody readFromBytes(byte[] bytes);
+
+}
diff --git a/src/main/java/com/zy/core/netty/constant/FireDataType.java b/src/main/java/com/zy/core/netty/constant/FireDataType.java
new file mode 100644
index 0000000..d9f1a20
--- /dev/null
+++ b/src/main/java/com/zy/core/netty/constant/FireDataType.java
@@ -0,0 +1,30 @@
+package com.zy.core.netty.constant;
+
+public enum FireDataType {
+
+ HEARTBEAT(0x30, 0x30, "蹇冭烦淇℃伅"),
+
+ FIRE_ALARM(0x38, 0x30, "鐏"),
+
+ ;
+
+ public int byte1;
+ public int byte2;
+ public String des;
+
+ FireDataType(int byte1, int byte2, String des) {
+ this.byte1 = byte1;
+ this.byte2 = byte2;
+ this.des = des;
+ }
+
+ public static FireDataType get(int byte1, int byte2) {
+ for (FireDataType value : FireDataType.values()) {
+ if (byte1 == value.byte1 && byte2 == value.byte2) {
+ return value;
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/src/main/java/com/zy/core/netty/domain/fire/Fire_3030.java b/src/main/java/com/zy/core/netty/domain/fire/Fire_3030.java
new file mode 100644
index 0000000..0c9a03e
--- /dev/null
+++ b/src/main/java/com/zy/core/netty/domain/fire/Fire_3030.java
@@ -0,0 +1,76 @@
+package com.zy.core.netty.domain.fire;
+
+import com.zy.core.netty.IMessageBody;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.buffer.ByteBufUtil;
+import lombok.Data;
+
+import java.nio.charset.StandardCharsets;
+
+/**
+ * 蹇冭烦 pac
+ * 8230303234303030303030313130353138303E303F3131383083
+ * Created by vincent on 2022/7/30
+ */
+@Data
+public class Fire_3030 implements IMessageBody {
+
+ private Integer no;
+
+ private Integer year;
+
+ private Integer month;
+
+ private Integer day;
+
+ private Integer hour;
+
+ private Integer minute;
+
+ private Integer seconds;
+
+ private Integer validCode;
+
+ @Override
+ public byte[] writeToBytes() {
+ ByteBuf byteBuf = ByteBufAllocator.DEFAULT.heapBuffer(DEFAULT_LEN);
+ byteBuf.writeShort(no);
+ // todo
+ return ByteBufUtil.getBytes(byteBuf);
+ }
+
+ @Override
+ public Fire_3030 readFromBytes(byte[] bytes) {
+// ByteBuf byteBuf = ByteBufAllocator.DEFAULT.heapBuffer(bytes.length > DEFAULT_LEN ? bytes.length + 100 : DEFAULT_LEN);
+// byteBuf.writeBytes(bytes);
+// byteBuf.resetReaderIndex();
+// byteBuf.skipBytes(3);
+// this.no = byteBuf.readShort();
+// byteBuf.skipBytes(6);
+// this.year = byteBuf.readShort();
+// this.month = byteBuf.readShort();
+// this.day = byteBuf.readShort();
+// this.hour = byteBuf.readShort();
+// this.minute = byteBuf.readShort();
+// this.seconds = byteBuf.readShort();
+// this.validCode = byteBuf.readShort();
+// byteBuf.skipBytes(1);
+// byteBuf.release();
+// System.out.println(byteBuf.refCnt());
+ this.no = analyze(new byte[]{bytes[3], bytes[4]});
+ this.year = analyze(new byte[]{bytes[11], bytes[12]});
+ this.month = analyze(new byte[]{bytes[13], bytes[14]});
+ this.day = analyze(new byte[]{bytes[15], bytes[16]});
+ this.hour = analyze(new byte[]{bytes[17], bytes[18]});
+ this.minute = analyze(new byte[]{bytes[19], bytes[20]});
+ this.seconds = analyze(new byte[]{bytes[21], bytes[22]});
+ this.validCode = analyze(new byte[]{bytes[23], bytes[24]});
+ return this;
+ }
+
+ public static Integer analyze(byte[] bytes) {
+ return Integer.parseInt(new String(bytes, StandardCharsets.US_ASCII), 16);
+ }
+
+}
diff --git a/src/main/java/com/zy/core/netty/handle/PackageServerHandler.java b/src/main/java/com/zy/core/netty/handle/PackageServerHandler.java
index d91b1f1..3ebba0a 100644
--- a/src/main/java/com/zy/core/netty/handle/PackageServerHandler.java
+++ b/src/main/java/com/zy/core/netty/handle/PackageServerHandler.java
@@ -2,7 +2,9 @@
import com.zy.core.netty.AbstractInboundHandler;
import com.zy.core.netty.cache.ChannelCache;
+import com.zy.core.netty.constant.FireDataType;
import com.zy.core.netty.domain.ChPackage;
+import com.zy.core.netty.domain.fire.Fire_3030;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
@@ -21,7 +23,16 @@
@Override
protected boolean channelRead0(ChannelHandlerContext ctx, ChPackage pac) {
- log.info("{}", pac.getBytes().length);
+ FireDataType dataType = FireDataType.get(pac.getBytes()[1], pac.getBytes()[2]);
+ switch (dataType) {
+ case HEARTBEAT:
+ Fire_3030 fire3030 = new Fire_3030().readFromBytes(pac.getBytes());
+ break;
+ case FIRE_ALARM:
+ break;
+ default:
+ break;
+ }
return true;
}
diff --git a/src/main/java/com/zy/core/netty/handle/ProtectorHandler.java b/src/main/java/com/zy/core/netty/handle/ProtectorHandler.java
index ec0b43c..add63dd 100644
--- a/src/main/java/com/zy/core/netty/handle/ProtectorHandler.java
+++ b/src/main/java/com/zy/core/netty/handle/ProtectorHandler.java
@@ -62,8 +62,8 @@
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
- ChannelCache.removeChannel(ctx.channel());
- ctx.close();
+// ChannelCache.removeChannel(ctx.channel());
+// ctx.close();
}
}
--
Gitblit v1.9.1