101captain
2021-09-09 a04fbfc66c3054c2714bad1796a676306fd2393a
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.panzhihua.service_property.netty;
 
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.common.utlis.StringUtils;
import com.panzhihua.service_property.dao.ComPropertyAlarmDao;
import com.panzhihua.service_property.dao.ComPropertyAlarmSettingDao;
import com.panzhihua.service_property.dao.ComPropertyEquipmentDao;
import com.panzhihua.service_property.entity.ComPropertyAlarm;
import com.panzhihua.service_property.entity.ComPropertyAlarmSetting;
import com.panzhihua.service_property.entity.ComPropertyEquipment;
import com.panzhihua.service_property.util.MyTools;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.Date;
 
@Slf4j
@Component
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    @Resource
    private ComPropertyAlarmDao comPropertyAlarmDao;
    @Resource
    private ComPropertyAlarmSettingDao comPropertyAlarmSettingDao;
    @Resource
    private ComPropertyEquipmentDao comPropertyEquipmentDao;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RabbitTemplate rabbitTemplate;
 
    private static NettyServerHandler nettyServerHandler;
    /**
     * 客户端连接会触发
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("Channel active......");
    }
 
    /**
     * 客户端发消息会触发
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        MyTools myTools=new MyTools();
        log.info("服务器收到消息: {}", msg.toString());
        if(msg.toString().startsWith("4A1802")){
            myTools.writeToClient("404A021823",ctx,"状态包");
        }
        if(msg.toString().startsWith("4A0C0134")){
            myTools.writeToClient("404A01"+ DateUtils.getDateFormatString(new Date(),"HHmmss")+"23",ctx,"心跳包");
        }
        if(msg.toString().startsWith("4A1803")){
            String serial=msg.toString().substring(14,24);
            myTools.writeToClient("404A03"+msg.toString().substring(msg.toString().length()-2)+"23",ctx,"事件包");
            ComPropertyAlarm comPropertyAlarm=new ComPropertyAlarm();
            comPropertyAlarm.setCreateTime(DateUtil.date());
            comPropertyAlarm.setSerialNo(serial);
            comPropertyAlarm.setType(ComPropertyAlarm.type.one);
            nettyServerHandler.comPropertyAlarmDao.insert(comPropertyAlarm);
        }
        ctx.flush();
    }
 
    /**
     * 发生异常触发
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
 
    @PostConstruct
    public void init() {
        nettyServerHandler=this;
        nettyServerHandler.comPropertyAlarmDao=this.comPropertyAlarmDao;
        nettyServerHandler.stringRedisTemplate=this.stringRedisTemplate;
        nettyServerHandler.rabbitTemplate=this.rabbitTemplate;
    }
 
    private void delayAlarm(String serial){
        int duration=0;
        if(StringUtils.isNotEmpty(serial)){
            if (nettyServerHandler.stringRedisTemplate.hasKey(serial)){
                ComPropertyEquipment comPropertyEquipment= JSONObject.parseObject(nettyServerHandler.stringRedisTemplate.boundValueOps(serial).get(),ComPropertyEquipment.class);
                if(nettyServerHandler.stringRedisTemplate.hasKey(comPropertyEquipment.getCommunityId().toString())){
                    duration=Integer.parseInt(nettyServerHandler.stringRedisTemplate.boundValueOps(comPropertyEquipment.getCommunityId().toString()).get());
                }else{
                    ComPropertyAlarmSetting comPropertyAlarmSetting=nettyServerHandler.comPropertyAlarmSettingDao.getByCommunityId(comPropertyEquipment.getCommunityId());
                    duration=comPropertyAlarmSetting.getTriggerTime();
                    nettyServerHandler.stringRedisTemplate.boundValueOps(comPropertyEquipment.getCommunityId().toString()).set(comPropertyAlarmSetting.getTriggerTime().toString());
                }
                nettyServerHandler.stringRedisTemplate.boundValueOps(serial).set(JSONObject.toJSONString(comPropertyEquipment), Duration.ofHours(duration));
            }
            else {
                ComPropertyEquipment comPropertyEquipment=nettyServerHandler.comPropertyEquipmentDao.selectOne(new QueryWrapper<ComPropertyEquipment>().eq("serial_no",serial));
                nettyServerHandler.stringRedisTemplate.boundValueOps(serial).set(JSONObject.toJSONString(comPropertyEquipment));
            }
            int finalDuration = duration;
            nettyServerHandler.rabbitTemplate.convertAndSend("delayed.queue","delayed.key", message -> {
                message.getMessageProperties().setDelay(finalDuration);
                return message;
            });
        }
    }
}