南昌市物联网技防平台-公安版
zengh
2021-06-04 c926acaadc3d98fd8ba8926466b842f1edb3aee3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.springblade.jfpt.nettyTcpServer;
 
import io.netty.buffer.ByteBuf;
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.animalheat.service.AnimalHeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.concurrent.ConcurrentHashMap;
 
@Component
public class TcpServerHandler extends ChannelInboundHandlerAdapter {
 
    private static TcpServerHandler tcpServerHandler;
 
    @Autowired
    private AnimalHeatService animalHeatService;
 
    @PostConstruct
    public void init() {
        tcpServerHandler = this;
    }
    /**
     * 客户端与服务端创建连接的时候调用
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("CTX:"+ctx.channel());
        System.out.println("客户端与服务端连接开始...");
    }
 
    /**
     * 客户端与服务端断开连接时调用
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("客户端与服务端连接关闭...");
        NettyConfig.group.remove(ctx.channel());
    }
 
    /**
     * 服务端接收客户端发送过来的数据结束之后调用
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
        System.out.println("信息接收完毕...");
    }
 
    /**
     * 工程出现异常的时候调用
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
 
    /**
     * 服务端处理客户端websocket请求的核心方法,这里接收了客户端发来的信息
     */
    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object info) throws Exception {
        System.out.println("接收到了:"+info);
        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);
        //获取字符串的长度
        int length = content.length();
        //当字符串长度大于200时,采集体温数据
        //只有数据长度大于300时,采集体温数据  200-300 之间为心跳数据,当前不采集
        if (length>250){
            //插入数据
            tcpServerHandler.animalHeatService.save(body);
        }
    }
 
 
}