|
@@ -0,0 +1,188 @@
|
|
|
+package com.huimv.center.server;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.huimv.center.producer.Producer;
|
|
|
+import com.huimv.center.service.BizDeviceRegisterService;
|
|
|
+import com.huimv.center.utils.RegexUtil;
|
|
|
+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 lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.net.InetSocketAddress;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv.shiwan
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2020-12-25
|
|
|
+ **/
|
|
|
+@ChannelHandler.Sharable
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class RegisterServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
+ @Autowired
|
|
|
+ private RegexUtil regexUtil;
|
|
|
+ @Autowired
|
|
|
+ private BizDeviceRegisterService bizDeviceRegisterService;
|
|
|
+ @Autowired
|
|
|
+ private Producer producer;
|
|
|
+ //
|
|
|
+ private StringBuilder askTextSb = null;
|
|
|
+
|
|
|
+ //
|
|
|
+ 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);
|
|
|
+ //保存实例内的客户端请求
|
|
|
+ appendClientAsk(clientAskText);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
|
+ if (askTextSb.toString().indexOf("end") != -1) {
|
|
|
+ InetSocketAddress ipSocket = (InetSocketAddress)ctx.channel().remoteAddress();
|
|
|
+ String clientIp = ipSocket.getAddress().getHostAddress();
|
|
|
+ log.info("客户端ip地址:{}",clientIp);
|
|
|
+ // 处理客户端消息(业务入口)
|
|
|
+ handleBusinessMessage(askTextSb.toString(), ctx,clientIp);
|
|
|
+ //清空重置;
|
|
|
+ askTextSb.delete(0, askTextSb.length());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
+ if (cause.getMessage().indexOf("Connection reset") != -1) {
|
|
|
+ log.info("相关采集器设备正在重启:" + cause.toString());
|
|
|
+ }
|
|
|
+// cause.printStackTrace();
|
|
|
+ ctx.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : handleBusinessMessage
|
|
|
+ * @Description : 处理业务消息
|
|
|
+ * @Params : [clientAskText, ctx]
|
|
|
+ * @Return : void
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/28
|
|
|
+ * @Time : 17:36
|
|
|
+ */
|
|
|
+ private void handleBusinessMessage(String clientAskText, ChannelHandlerContext ctx, String clientIp) throws ParseException {
|
|
|
+ clientAskText = clientAskText.replaceAll("\r", "").replaceAll("\n", "");
|
|
|
+ System.out.println("服务端开始接收数据 >>" + clientAskText);
|
|
|
+ // 计算+号数量
|
|
|
+ int countPlus = regexUtil.countPlus(clientAskText);
|
|
|
+ if (countPlus < 4) {
|
|
|
+ System.out.println("当前数据为不完整数据,故丢弃.>>" + clientAskText);
|
|
|
+ } else {
|
|
|
+ //{拆分粘包数据}
|
|
|
+ JSONArray askJa = getPerData(clientAskText);
|
|
|
+ for (int a = 0; a < askJa.size(); a++) {
|
|
|
+ String askText = askJa.getString(a);
|
|
|
+ //
|
|
|
+ handleCommandLine(askText, ctx,clientIp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : handleCommandText
|
|
|
+ * @Description :处理命令文本
|
|
|
+ * @Params : [askText, ctx]
|
|
|
+ * @Return : void
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/23
|
|
|
+ * @Time : 18:08
|
|
|
+ */
|
|
|
+ private void handleCommandLine(String askText, ChannelHandlerContext ctx, String clientIp) throws ParseException {
|
|
|
+ String[] dataArray = askText.split("\\+");
|
|
|
+ String cmdHeader = dataArray[0];
|
|
|
+ //芯片id/设备编码
|
|
|
+ String idCode = dataArray[1];
|
|
|
+ String cmd = dataArray[2];
|
|
|
+ // 保存请求命令
|
|
|
+ producer.sendAskCmdRawData(askText);
|
|
|
+ if (cmdHeader.trim().equalsIgnoreCase("hm")) {
|
|
|
+ //采集器应答数据
|
|
|
+ if (cmd.trim().equalsIgnoreCase("0")) {
|
|
|
+ //不需要处理
|
|
|
+ System.out.println("==>命令0");
|
|
|
+ Map resultMap = bizDeviceRegisterService.getServerAndIpByChipId(idCode);
|
|
|
+ System.out.println("获取服务器IP>>"+resultMap);
|
|
|
+ if(resultMap.size()>0){
|
|
|
+ // 发送设备已注册消息
|
|
|
+ producer.sendDeviceRegistered(idCode,resultMap,askText,clientIp);
|
|
|
+ String ip = resultMap.get("ip").toString();
|
|
|
+ String port = resultMap.get("port").toString();
|
|
|
+ String answer = "hm+0+0+"+ip+"+"+port+"+8+end";
|
|
|
+ log.info(">>命令0设备编码-应答数据>>" + answer);
|
|
|
+ ctx.writeAndFlush(Unpooled.copiedBuffer(answer.getBytes()));
|
|
|
+ // 发送设备应答消息
|
|
|
+ producer.sendDeviceAnswer(idCode,resultMap,answer);
|
|
|
+ }else{
|
|
|
+ // 发送设备未注册消息
|
|
|
+ producer.sendDeviceUnregistered(idCode,askText);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("==>非法命令");
|
|
|
+ log.error(">>当前数据为非法数据>>" + askText);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("==>非法命令");
|
|
|
+ log.error("##当前请求数据为非法数据>>" + askText);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //检查无效耳标
|
|
|
+ public boolean checkValidEarmark(String earmark) {
|
|
|
+ if (earmark.trim().equalsIgnoreCase("ffffffffffffffff") || earmark.trim().equalsIgnoreCase("0000000000000000")) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //拆分粘包数据
|
|
|
+ public JSONArray getPerData(String text) {
|
|
|
+ String key = "end";
|
|
|
+ Pattern pattern = Pattern.compile(key);
|
|
|
+ Matcher matcher = pattern.matcher(text);
|
|
|
+ int count = 0;
|
|
|
+ while (matcher.find()) {
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ JSONArray dataJa = new JSONArray();
|
|
|
+ if (count == 1) {
|
|
|
+ dataJa.add(text);
|
|
|
+ } else {
|
|
|
+ for (int a = 0; a < count; a++) {
|
|
|
+ int p1 = text.indexOf("end");
|
|
|
+ dataJa.add(text.substring(0, p1 + 3));
|
|
|
+ text = text.substring(p1 + 3, text.length());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dataJa;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|