From 5e0561f866dd22e8c56e8db30fe84c2bcb4147ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com>
Date: Thu, 11 May 2023 09:48:51 +0800
Subject: [PATCH] 'commit'
---
pom.xml | 6 +
src/main/java/UnitTest/HexDataSender.java | 111 +++++++++++++++++++
target/classes/ExcelExportTemplate/exam.json | 18 +--
3 files changed, 126 insertions(+), 9 deletions(-)
create mode 100644 src/main/java/UnitTest/HexDataSender.java
diff --git a/pom.xml b/pom.xml
index bc3619a..cbd8781 100644
--- a/pom.xml
+++ b/pom.xml
@@ -285,6 +285,12 @@
kafka-clients
3.4.0
+
+
+ io.netty
+ netty-all
+ 4.1.32.Final
+
diff --git a/src/main/java/UnitTest/HexDataSender.java b/src/main/java/UnitTest/HexDataSender.java
new file mode 100644
index 0000000..f717434
--- /dev/null
+++ b/src/main/java/UnitTest/HexDataSender.java
@@ -0,0 +1,111 @@
+package UnitTest;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.*;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.handler.codec.MessageToByteEncoder;
+
+public class HexDataSender {
+ private final String host;
+ private final int port;
+
+ public HexDataSender(String host, int port) {
+ this.host = host;
+ this.port = port;
+ }
+
+ public void sendHexData(String hexData) throws Exception {
+ EventLoopGroup workerGroup = new NioEventLoopGroup();
+
+ try {
+ Bootstrap b = new Bootstrap();
+ b.group(workerGroup);
+ b.channel(NioSocketChannel.class);
+ b.option(ChannelOption.SO_KEEPALIVE, true);
+ b.handler(new ChannelInitializer() {
+ @Override
+ public void initChannel(SocketChannel ch) {
+ ChannelPipeline p = ch.pipeline();
+ p.addLast(new HexDataEncoder());
+ p.addLast(new HexDataSenderHandler(hexData));
+ }
+ });
+
+ // Start the client.
+ ChannelFuture f = b.connect(host, port).sync();
+
+ // Wait until the connection is closed.
+ f.channel().closeFuture().sync();
+ } finally {
+ workerGroup.shutdownGracefully();
+ }
+ }
+
+ public static String IP = "10.10.21.18";
+ public static int PORT = 8001;
+
+ public static void Open(String sb_id) throws Exception {
+ //开
+ String hexData = "16 00 34 F5 41 11 FE 82 0D 02 ? 00 00 00 00 00 00 01 00 00 01".replace("?", sb_id).replace(" ", "");
+ HexDataSender sender = new HexDataSender(IP, PORT);
+ sender.sendHexData(hexData);
+ }
+
+ public static void Close(String sb_id) throws Exception {
+ //关
+ String hexData = "16 00 34 F5 41 11 FE 82 0D 02 ? 00 00 00 00 00 00 01 00 00 00".replace("?", sb_id).replace(" ", "");
+ HexDataSender sender = new HexDataSender(IP, PORT);
+ sender.sendHexData(hexData);
+ }
+
+ public static void main(String[] args) throws Exception {
+ Close("6D FF");
+ //Thread.sleep(3000);
+ //Open("6D FF");
+ }
+}
+
+class HexDataSenderHandler extends ChannelInboundHandlerAdapter {
+ private final String hexData;
+
+ public HexDataSenderHandler(String hexData) {
+ this.hexData = hexData;
+ }
+
+ @Override
+ public void channelActive(ChannelHandlerContext ctx) {
+ byte[] bytes = hexStringToByteArray(hexData);
+ ByteBuf buffer = Unpooled.buffer(bytes.length);
+ buffer.writeBytes(bytes);
+ ctx.writeAndFlush(buffer);
+ }
+
+ @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
+ cause.printStackTrace();
+ ctx.close();
+ }
+
+ private byte[] hexStringToByteArray(String hexData) {
+ int len = hexData.length();
+ byte[] data = new byte[len / 2];
+ for (int i = 0; i < len; i += 2) {
+ data[i / 2] = (byte) ((Character.digit(hexData.charAt(i), 16) << 4) +
+ Character.digit(hexData.charAt(i + 1), 16));
+ }
+ return data;
+ }
+}
+
+class HexDataEncoder extends MessageToByteEncoder {
+ @Override
+ protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
+ byte[] bytes = new byte[msg.readableBytes()];
+ msg.getBytes(msg.readerIndex(), bytes);
+ out.writeBytes(bytes);
+ }
+}
diff --git a/target/classes/ExcelExportTemplate/exam.json b/target/classes/ExcelExportTemplate/exam.json
index 5793061..9830166 100644
--- a/target/classes/ExcelExportTemplate/exam.json
+++ b/target/classes/ExcelExportTemplate/exam.json
@@ -1,6 +1,6 @@
{
- "title": "党建知识问答排名结果",
- "sheetName": "结果表",
+ "title": "长春市教育局学习贯彻习近平新时代中国特色社会主义思想知识答题",
+ "sheetName": "结果",
"titleHeight": 30,
"rowHeight": 30,
"showNumber": true,
@@ -8,37 +8,37 @@
{
"show_column_name": "姓名",
"list_column_name": "person_name",
- "width": 40
+ "width": 20
},
{
"show_column_name": "处室",
"list_column_name": "ks",
- "width": 40
+ "width": 30
},
{
"show_column_name": "电话",
"list_column_name": "tel",
- "width": 40
+ "width": 30
},
{
"show_column_name": "得分",
"list_column_name": "score",
- "width": 40
+ "width": 20
},
{
"show_column_name": "用时",
"list_column_name": "ys",
- "width": 40
+ "width": 20
},
{
"show_column_name": "开始时间",
"list_column_name": "start_time",
- "width": 40
+ "width": 32
},
{
"show_column_name": "交卷时间",
"list_column_name": "end_time",
- "width": 40
+ "width": 32
}
]
}
\ No newline at end of file