| | |
| | | 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 |
| | |
| | | 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; |
| | | /** |
| | |
| | | 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; |
| | | }); |
| | | } |
| | | } |
| | | } |