From 579b4eeb3892d95b308eeaaab368075480af4e4c Mon Sep 17 00:00:00 2001
From: 张天森 <1292933220@qq.com>
Date: 星期六, 12 十一月 2022 16:35:46 +0800
Subject: [PATCH] token获取物业id处理
---
springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServerHandler.java | 85 +++++++++++++++++++++++++-----------------
1 files changed, 51 insertions(+), 34 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 4839a7f..d9f7b8f 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
@@ -23,6 +23,7 @@
import javax.annotation.Resource;
import java.time.Duration;
import java.util.Date;
+import java.util.Objects;
@Slf4j
@Component
@@ -39,6 +40,7 @@
private RabbitTemplate rabbitTemplate;
private static NettyServerHandler nettyServerHandler;
+
/**
* 客户端连接会触发
*/
@@ -52,22 +54,28 @@
*/
@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("4A0C01")) {
+ 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);
+ 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("4A18031")) {
+ //正式处理
+ delayAlarm(serial);
+ //展会处理
+// ComPropertyAlarm comPropertyAlarm = new ComPropertyAlarm();
+// comPropertyAlarm.setCreateTime(DateUtil.date());
+// comPropertyAlarm.setSerialNo(serial);
+// comPropertyAlarm.setType(ComPropertyAlarm.type.one);
+// nettyServerHandler.comPropertyAlarmDao.insert(comPropertyAlarm);
+ }
+
}
ctx.flush();
}
@@ -83,35 +91,44 @@
@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)){
- 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());
- }
+ //报警事件包延迟处理方法
+ 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 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.queue","delayed.key", message -> {
- message.getMessageProperties().setDelay(finalDuration);
+ nettyServerHandler.rabbitTemplate.convertAndSend("huacheng.delayed.exchange", "huacheng.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