From 814b7dfb73f8ae763023a4fd2835bbead8c66a93 Mon Sep 17 00:00:00 2001
From: 101captain <237651143@qq.com>
Date: 星期五, 17 九月 2021 17:53:06 +0800
Subject: [PATCH] 一键报警,物业平台相关功能修改

---
 springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServerHandler.java |   70 ++++++++++++++++++++++++++++++++--
 1 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServerHandler.java b/springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServerHandler.java
index 2d6e071..2dcc215 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServerHandler.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServerHandler.java
@@ -1,24 +1,43 @@
 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;
+import java.util.Objects;
 
 @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;
     /**
@@ -45,11 +64,15 @@
         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);
+//            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);
+            }
+
         }
         ctx.flush();
     }
@@ -67,5 +90,42 @@
     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)){
+
+            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));
+                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);
+                return message;
+            });
+        }
+    }
+
+    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();
+            nettyServerHandler.stringRedisTemplate.boundValueOps(comPropertyEquipment.getCommunityId().toString()).set(comPropertyAlarmSetting.getTriggerTime().toString());
+        }
+        return duration;
     }
 }

--
Gitblit v1.7.1