南昌市物联网技防平台-后台
zengh
2021-03-10 fa9ffa9012d7b1df7692bb99e71be6592f48cd4b
blade-service/blade-jfpts/src/main/java/org/springblade/jfpt/nettyTcpServer/TcpServerHandler.java
@@ -4,10 +4,12 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.ReferenceCountUtil;
import org.springblade.jfpt.alarm.service.IAlarmService;
import org.springblade.jfpt.equipment.service.IEquipmentService;
import org.springblade.jfpt.nettyServer.NettyConfig;
import org.springblade.jfpt.nettyUdpServer.service.AnimalHeatService;
import org.springblade.jfpt.animalheat.service.AnimalHeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@@ -15,26 +17,6 @@
@Component
public class TcpServerHandler extends ChannelInboundHandlerAdapter {
   private  String reg_LA ="LA[d]{8}[d|A-F]{12}[d|A-F]{8}[d|A-Z]{2}[d|A-F]{4}[x2A][d|A-F]{6}[#@]";
   private String reg_LB = "LB[\\d|A-F]{12}[\\x2A].*[#@]";
   private String reg_LB2 = "LB[\\d|A-F]{6}[\\x2A].*[#@]";
   private String reg_LD = "LD[d]{8}[d|A-F]{12}:[A-Z]{4}[\\x2A].*[#@]";
   private ConcurrentHashMap<String, Channel> sessionChannelMap = new ConcurrentHashMap<String, Channel>();
   @Autowired
   private org.springblade.jfpt.catalog.service.catalogService catalogService;
   @Autowired
   private org.springblade.jfpt.Netty.service.nettyService nettyService;
   @Autowired
   private IEquipmentService equipmentService;
   @Autowired
   private IAlarmService alarmService;
   private static TcpServerHandler tcpServerHandler;
@@ -52,9 +34,6 @@
   public void channelActive(ChannelHandlerContext ctx) throws Exception {
      System.out.println("CTX:"+ctx.channel());
      System.out.println("客户端与服务端连接开始...");
//      ChannelId id = ctx.channel().id();
//      String s = id.toString();
      //NettyConfig.group.add(ctx.channel());
   }
   /**
@@ -93,12 +72,21 @@
      ByteBuf buf = (ByteBuf) info;
      byte[] req = new byte[buf.readableBytes()];
      buf.readBytes(req);
      //手动释放内存
      ReferenceCountUtil.release(info);
      String body = new String(req, "UTF-8");
      String content = body;
      System.out.println("接收客户端数据:" + body);
      //插入数据
      tcpServerHandler.animalHeatService.save(body);
      System.out.println("content.length() = " + content.length());
      //获取字符串的长度
      int length = content.length();
      //当字符串长度大于200时,采集体温数据,小于则采集报警数据
      //只有数据长度大于300时,采集体温数据  200-300 之间为心跳数据,当前不采集
      if (length>250){
         //插入数据
         tcpServerHandler.animalHeatService.save(body);
      }
   }