|
@@ -0,0 +1,98 @@
|
|
|
+package com.huimv.environ.server;
|
|
|
+
|
|
|
+import io.netty.buffer.ByteBuf;
|
|
|
+import io.netty.buffer.Unpooled;
|
|
|
+import io.netty.channel.ChannelHandler;
|
|
|
+import io.netty.channel.ChannelHandlerContext;
|
|
|
+import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
+import io.netty.util.CharsetUtil;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv.shiwan
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2020-12-25
|
|
|
+ **/
|
|
|
+@ChannelHandler.Sharable
|
|
|
+@Component
|
|
|
+public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
+ private StringBuilder askTextSb = null;
|
|
|
+ private int num = 0;
|
|
|
+
|
|
|
+ //
|
|
|
+ public void appendClientAsk(String text) {
|
|
|
+ if (this.askTextSb == null) {
|
|
|
+ askTextSb = new StringBuilder();
|
|
|
+ }
|
|
|
+ askTextSb.append(text);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
+ ByteBuf data = (ByteBuf) msg;
|
|
|
+ String clientAskText = data.toString(CharsetUtil.UTF_8);
|
|
|
+// System.out.println((++num)+"次, 客户端消息clientAskText>>"+clientAskText);
|
|
|
+ System.out.println("############################### 客户端消-接收消息 clientAskText>>" + clientAskText);
|
|
|
+ //保存实例内的客户端请求
|
|
|
+// appendClientAsk(clientAskText);
|
|
|
+
|
|
|
+ // 处理客户端消息
|
|
|
+ handleClientAskMessage(clientAskText, ctx);
|
|
|
+
|
|
|
+ //临时写入耳标数据到文件
|
|
|
+// writeTxt(clientAskText,"all");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleClientAskMessage(String clientAskText, ChannelHandlerContext ctx) {
|
|
|
+ System.out.println("clientAskText=" + clientAskText);
|
|
|
+ String answerText = "";
|
|
|
+ if (clientAskText.length() == 12) {
|
|
|
+ answerText = "loginok";
|
|
|
+ } else {
|
|
|
+ answerText = "rok";
|
|
|
+ //添加数据入库
|
|
|
+ saveEnvDeviceData(clientAskText);
|
|
|
+ }
|
|
|
+ System.out.println("应答数据:"+answerText);
|
|
|
+ answerCmd(answerText, ctx);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveEnvDeviceData(String clientAskText) {
|
|
|
+ String[] askDataArray = clientAskText.split("\\,");
|
|
|
+ System.out.println("askDataArray.length=" + askDataArray.length);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 应答
|
|
|
+ public void answerCmd(String answerText, ChannelHandlerContext ctx)
|
|
|
+ {
|
|
|
+ ctx.writeAndFlush(Unpooled.copiedBuffer(answerText.getBytes()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
|
+// if (askTextSb.toString().indexOf("end") != -1) {
|
|
|
+// // 处理客户端消息
|
|
|
+// handleClientEartagMessage(askTextSb.toString(), ctx);
|
|
|
+// //清空重置;
|
|
|
+//// askTextSb.delete(0, askTextSb.length());
|
|
|
+// askTextSb = new StringBuilder();
|
|
|
+// num = 0;
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
+ if (cause.getMessage().indexOf("Connection reset") != -1) {
|
|
|
+ System.out.println("相关采集器设备正在重启:" + cause.toString());
|
|
|
+ }
|
|
|
+ ctx.close();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|