101captain
2021-09-22 1775bb71f952106c58657cf02891cbe2a286c8f8
springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServerHandler.java
@@ -40,6 +40,7 @@
    private RabbitTemplate rabbitTemplate;
    private static NettyServerHandler nettyServerHandler;
    /**
     * 客户端连接会触发
     */
@@ -53,24 +54,31 @@
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        MyTools myTools=new MyTools();
        MyTools myTools = new MyTools();
        log.info("服务器收到消息: {}", msg.toString());
        if(msg.toString().startsWith("4A1802")){
            myTools.writeToClient("404A021823",ctx,"状态包");
        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("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,"事件包");
        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);
            if(msg.toString().startsWith("4A18031")){
                delayAlarm(serial);
            if (msg.toString().startsWith("4A18031")) {
                //正式处理
                // delayAlarm(serial);
                //展会处理
                ComPropertyAlarm comPropertyAlarm = new ComPropertyAlarm();
                comPropertyAlarm.setCreateTime(DateUtil.date());
                comPropertyAlarm.setSerialNo(serial);
                comPropertyAlarm.setType(ComPropertyAlarm.type.one);
                nettyServerHandler.comPropertyAlarmDao.insert(comPropertyAlarm);
            }
        }
@@ -88,30 +96,30 @@
    @PostConstruct
    public void init() {
        nettyServerHandler=this;
        nettyServerHandler.comPropertyAlarmDao=this.comPropertyAlarmDao;
        nettyServerHandler.stringRedisTemplate=this.stringRedisTemplate;
        nettyServerHandler.rabbitTemplate=this.rabbitTemplate;
        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)){
            ComPropertyEquipment comPropertyEquipment=new ComPropertyEquipment();
            if (nettyServerHandler.stringRedisTemplate.hasKey(serial)){
                comPropertyEquipment= JSONObject.parseObject(nettyServerHandler.stringRedisTemplate.boundValueOps(serial).get(),ComPropertyEquipment.class);
    //报警事件包延迟处理方法
    private void delayAlarm(String serial) {
        int duration = 0;
        if (StringUtils.isNotEmpty(serial)) {
            ComPropertyEquipment comPropertyEquipment = new ComPropertyEquipment();
            if (nettyServerHandler.stringRedisTemplate.hasKey(serial)) {
                comPropertyEquipment = JSONObject.parseObject(nettyServerHandler.stringRedisTemplate.boundValueOps(serial).get(), ComPropertyEquipment.class);
                duration = getDuration(Objects.requireNonNull(comPropertyEquipment));
                nettyServerHandler.stringRedisTemplate.boundValueOps(serial).set(JSONObject.toJSONString(comPropertyEquipment), Duration.ofHours(duration));
            }
            else {
                comPropertyEquipment=nettyServerHandler.comPropertyEquipmentDao.selectOne(new QueryWrapper<ComPropertyEquipment>().eq("serial_no",serial));
            } else {
                comPropertyEquipment = nettyServerHandler.comPropertyEquipmentDao.selectOne(new QueryWrapper<ComPropertyEquipment>().eq("serial_no", serial));
                duration = getDuration(comPropertyEquipment);
                nettyServerHandler.stringRedisTemplate.boundValueOps(serial).set(JSONObject.toJSONString(comPropertyEquipment));
            }
            int finalDuration = duration;
            nettyServerHandler.rabbitTemplate.convertAndSend("delayed.exchange","delayed.key",comPropertyEquipment, message -> {
                message.getMessageProperties().setHeader("x-delay", finalDuration*1000*3601);
            nettyServerHandler.rabbitTemplate.convertAndSend("delayed.exchange", "delayed.key", comPropertyEquipment, message -> {
                message.getMessageProperties().setHeader("x-delay", finalDuration * 1000 * 3601);
                return message;
            });
        }
@@ -119,11 +127,11 @@
    private int getDuration(ComPropertyEquipment comPropertyEquipment) {
        int duration;
        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();
        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());
        }
        return duration;