|
@@ -0,0 +1,262 @@
|
|
|
|
+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.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import io.netty.channel.ChannelHandlerContext;
|
|
|
|
+
|
|
|
|
+import java.net.InetSocketAddress;
|
|
|
|
+import java.text.ParseException;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+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 RegisterServerHandler 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);
|
|
|
|
+
|
|
|
|
+ InetSocketAddress ipSocket = (InetSocketAddress)ctx.channel().remoteAddress();
|
|
|
|
+ String clientIp = ipSocket.getAddress().getHostAddress();
|
|
|
|
+ log.info("客户端ip地址:{}",clientIp);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
+ if (askTextSb.toString().indexOf("end") != -1) {
|
|
|
|
+ // 处理客户端消息(业务入口)
|
|
|
|
+ handleBusinessMessage(askTextSb.toString(), ctx);
|
|
|
|
+ //清空重置;
|
|
|
|
+ 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) 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);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @Method : handleCommandText
|
|
|
|
+ * @Description :处理命令文本
|
|
|
|
+ * @Params : [askText, ctx]
|
|
|
|
+ * @Return : void
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Date : 2022/3/23
|
|
|
|
+ * @Time : 18:08
|
|
|
|
+ */
|
|
|
|
+ private void handleCommandLine(String askText, ChannelHandlerContext ctx) throws ParseException {
|
|
|
|
+ String[] dataArray = askText.split("\\+");
|
|
|
|
+ String cmdHeader = dataArray[0];
|
|
|
|
+ //芯片id/设备编码
|
|
|
|
+ String idCode = dataArray[1];
|
|
|
|
+ String cmd = dataArray[2];
|
|
|
|
+// Map map = new HashMap();
|
|
|
|
+// map.put("askText", 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){
|
|
|
|
+ String deviceCode = resultMap.get("deviceCode").toString();
|
|
|
|
+ // 发送设备已注册消息
|
|
|
|
+ producer.sendDeviceRegistered(idCode,deviceCode,askText);
|
|
|
|
+ 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,answer);
|
|
|
|
+ }else{
|
|
|
|
+ // 发送设备未注册消息
|
|
|
|
+ producer.sendDeviceUnregistered(askText);
|
|
|
|
+ }
|
|
|
|
+// } else if (cmd.trim().equalsIgnoreCase("1")) {
|
|
|
|
+// System.out.println("==>命令1请求 askText>>" + askText.trim());
|
|
|
|
+// //处理获取设备编码命令
|
|
|
|
+// //{读取设备编码}
|
|
|
|
+// String deviceCode = dataService.getDeviceCodeByChipId(idCode);
|
|
|
|
+// log.info("获取设备编码结果,芯片id>>" + idCode + " ,deviceCode>>" + deviceCode);
|
|
|
|
+// if (deviceCode != null) {
|
|
|
|
+// String answer = "hm+1+0+" + deviceCode + "+123+8+end";
|
|
|
|
+// log.info(">>命令1设备编码-应答数据>>" + answer);
|
|
|
|
+// ctx.writeAndFlush(Unpooled.copiedBuffer(answer.getBytes()));
|
|
|
|
+// } else {
|
|
|
|
+// log.error("检测到未注册的采集器设备,已舍弃请求.");
|
|
|
|
+// }
|
|
|
|
+// } else if (cmd.trim().equalsIgnoreCase("2")) {
|
|
|
|
+// System.out.println("==>命令2请求 askText>>" + askText);
|
|
|
|
+// if (dataService.isEffectiveDevice(idCode)) {
|
|
|
|
+// //处理服务器时间命令
|
|
|
|
+// String answer = "hm+2+" + dateUtil.getNowText() + "+4+end";
|
|
|
|
+// log.info(">>命令2服务器时间-应答数据>>" + answer);
|
|
|
|
+// ctx.writeAndFlush(Unpooled.copiedBuffer(answer.getBytes()));
|
|
|
|
+// }else {
|
|
|
|
+// System.out.println("##获取服务器时间-无效设备编码 idCode=" + idCode);
|
|
|
|
+// }
|
|
|
|
+// } else if (cmd.trim().equalsIgnoreCase("3")) {
|
|
|
|
+// System.out.println("==>命令3请求 askText>>" + askText);
|
|
|
|
+// //处理心跳包命令
|
|
|
|
+// //{设备心跳应答}
|
|
|
|
+// if (dataService.isEffectiveDevice(idCode)) {
|
|
|
|
+// String answer = "hm+3+6+end";
|
|
|
|
+// log.info(">>命令3心跳包-应答数据>>" + answer);
|
|
|
|
+// ctx.writeAndFlush(Unpooled.copiedBuffer(answer.getBytes()));
|
|
|
|
+//
|
|
|
|
+// //{发送到设备处理消息队列}
|
|
|
|
+// dataService.sendDeviceMQ(map);
|
|
|
|
+// log.info(">>发送设备心跳数据消息到MQ."+map);
|
|
|
|
+// } else {
|
|
|
|
+// System.out.println("##心跳包-无效设备编码(" + idCode+"),请求拒绝。");
|
|
|
|
+// }
|
|
|
|
+// } else if (cmd.trim().equalsIgnoreCase("4")) {
|
|
|
|
+// System.out.println("==>命令4请求 askText>>" + askText);
|
|
|
|
+// //处理设备环境温度命令
|
|
|
|
+// //{设备环境温度应答}
|
|
|
|
+// if (dataService.isEffectiveDevice(idCode)) {
|
|
|
|
+// String answer = "hm+4+7+end";
|
|
|
|
+// log.info(">>命令4环境温度-应答数据>>" + answer);
|
|
|
|
+// ctx.writeAndFlush(Unpooled.copiedBuffer(answer.getBytes()));
|
|
|
|
+//
|
|
|
|
+// //{发送到设备处理消息队列}
|
|
|
|
+// dataService.sendDeviceMQ(map);
|
|
|
|
+// log.info(">>发送设备环境温度数据消息到MQ."+map);
|
|
|
|
+// } else {
|
|
|
|
+// System.out.println("##环境温度-无效设备编码(" + idCode+"),请求拒绝。");
|
|
|
|
+// }
|
|
|
|
+ } else {
|
|
|
|
+ System.out.println("==>非法命令");
|
|
|
|
+ log.error(">>当前数据为非法数据>>" + askText);
|
|
|
|
+ }
|
|
|
|
+// } else if (cmdHeader.trim().equalsIgnoreCase("zj")) {
|
|
|
|
+// System.out.println("==>耳标命令请求 askText>>"+askText);
|
|
|
|
+// //耳标应答数据(不需要应答)
|
|
|
|
+// if (checkValidEarmark(cmd)) {
|
|
|
|
+// log.error(">>舍弃掉未配置耳标号的耳标," + askText + "");
|
|
|
|
+// } else {
|
|
|
|
+// //{检查设备是否在处于工作状态或待机状态}
|
|
|
|
+//// if (dataService.isWorkStatusDevice(idCode)) {
|
|
|
|
+// if (dataService.isEffectiveDevice(idCode)) {
|
|
|
|
+// //{发送到耳标处理消息队列}
|
|
|
|
+// dataService.sendEartagMQ(map);
|
|
|
|
+// log.info(">>发送耳标数据消息到MQ."+map);
|
|
|
|
+// }else{
|
|
|
|
+// System.out.println("无效设备编码 idCode="+idCode);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+ } 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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|