|
@@ -0,0 +1,134 @@
|
|
|
+package com.persagy.kafka;
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.date.TimeInterval;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.persagy.cache.AlarmInfoCache;
|
|
|
+import com.persagy.constant.CommonConst;
|
|
|
+import com.persagy.entity.AlarmDefine;
|
|
|
+import com.persagy.entity.NettyMessage;
|
|
|
+import com.persagy.entity.ZktAlarmRecordDO;
|
|
|
+import com.persagy.netty.client.NettyClient;
|
|
|
+import com.persagy.repository.AlarmRecordRepository;
|
|
|
+import com.persagy.service.AlarmDefineService;
|
|
|
+import com.persagy.utils.LockUtil;
|
|
|
+import io.netty.channel.ChannelHandlerContext;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.kafka.clients.consumer.ConsumerRecord;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.kafka.annotation.KafkaListener;
|
|
|
+import org.springframework.kafka.support.Acknowledgment;
|
|
|
+import org.springframework.kafka.support.KafkaHeaders;
|
|
|
+import org.springframework.messaging.handler.annotation.Header;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/**
|
|
|
+ * kafak消费
|
|
|
+ * @author 易涛
|
|
|
+ * @version 1.0
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@Slf4j
|
|
|
+@ConditionalOnProperty(prefix = "spring.kafka",name = "enable",havingValue = "true")
|
|
|
+public class KafkaConsumer {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlarmDefineService alarmDefineService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlarmRecordRepository alarmRecordRepository;
|
|
|
+
|
|
|
+ @KafkaListener(topics = {"#{'${topicName}'.split(',')}"}, containerFactory = "listenerContainerFactory")
|
|
|
+ public void topicCloudAlarmConsumer(List<ConsumerRecord<?,String>> record, Acknowledgment ack) {
|
|
|
+ Iterator<ConsumerRecord<?, String>> it = record.iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ ConsumerRecord<?, String> consumerRecords = it.next();
|
|
|
+ Optional<String> message = Optional.ofNullable(consumerRecords.value());
|
|
|
+ if (message.isPresent()) {
|
|
|
+ NettyMessage msg = JSONObject.parseObject(message.get(),NettyMessage.class);
|
|
|
+ try {
|
|
|
+ if (Objects.equals(msg.getProjectId(), CommonConst.projectId)) {
|
|
|
+ handlerMsg(msg);
|
|
|
+ ack.acknowledge();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理kafka消息失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handlerMsg(NettyMessage msg) {
|
|
|
+ if (msg.getOpCode() == 7) {
|
|
|
+ log.info("--报警定义新增或更新--{}",msg);
|
|
|
+ NettyMessage<AlarmDefine> alarmDefineMessage = JSONObject.parseObject(msg.toString(), new TypeReference<NettyMessage<AlarmDefine>>() {
|
|
|
+ });
|
|
|
+ List<AlarmDefine> definesList = alarmDefineMessage.getContent();
|
|
|
+ if (CollectionUtil.isNotEmpty(definesList)) {
|
|
|
+ alarmDefineService.listSomeAlarmDefine(definesList);
|
|
|
+ }
|
|
|
+ } else if (msg.getOpCode() == 8) {
|
|
|
+ log.info("-----报警记录id推送----[{}]", msg);
|
|
|
+ List content = msg.getContent();
|
|
|
+ if (CollectionUtil.isNotEmpty(content)) {
|
|
|
+ JSONObject parseObject = JSONObject.parseObject(JSONObject.toJSONString(content.get(0)));
|
|
|
+ String defineId = AlarmInfoCache.getAlarmDefineId(parseObject);
|
|
|
+ ZktAlarmRecordDO zktAlarmRecordDO = alarmRecordRepository.findById(defineId).orElse(new ZktAlarmRecordDO());
|
|
|
+ zktAlarmRecordDO.setDefinitionId(defineId);
|
|
|
+ zktAlarmRecordDO.setObjId(parseObject.getString("objId"));
|
|
|
+ zktAlarmRecordDO.setItemId(parseObject.getString("itemId"));
|
|
|
+ zktAlarmRecordDO.setAlarmId(parseObject.getString("id"));
|
|
|
+ alarmRecordRepository.save(zktAlarmRecordDO);
|
|
|
+ }
|
|
|
+ } else if (msg.getOpCode() == 9) {
|
|
|
+ NettyMessage<AlarmDefine> alarmDefineMessage = JSONObject.parseObject(msg.toString(), new TypeReference<NettyMessage<AlarmDefine>>() {
|
|
|
+ });
|
|
|
+ List<AlarmDefine> definesList = alarmDefineMessage.getContent();
|
|
|
+ if (CollectionUtil.isNotEmpty(definesList)) {
|
|
|
+ try {
|
|
|
+ LockUtil.getInstance().lock.lock();
|
|
|
+ LockUtil.getInstance().setExecute(false);
|
|
|
+ //加个等待,保证正在执行的逻辑执行成功
|
|
|
+ Thread.sleep(4000);
|
|
|
+ alarmDefineService.listAllAlarmDefine(definesList);
|
|
|
+ LockUtil.getInstance().setExecute(true);
|
|
|
+ LockUtil.getInstance().condition.signalAll();
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ LockUtil.getInstance().lock.unlock();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (msg.getOpCode() == 10) {
|
|
|
+ NettyMessage<AlarmDefine> alarmDefineMessage = JSONObject.parseObject(msg.toString(), new TypeReference<NettyMessage<AlarmDefine>>() {
|
|
|
+ });
|
|
|
+ List<AlarmDefine> definesList = alarmDefineMessage.getContent();
|
|
|
+ if (CollectionUtil.isNotEmpty(definesList)) {
|
|
|
+ alarmDefineService.deleteAlarmDefine(definesList);
|
|
|
+ }
|
|
|
+ } else if (msg.getOpCode() == 11) {
|
|
|
+ //更新隔离的系统对象
|
|
|
+ NettyMessage<String> alarmDefineMessage = JSONObject.parseObject(msg.toString(), new TypeReference<NettyMessage<String>>() {
|
|
|
+ });
|
|
|
+ List<String> isolationSystemList = alarmDefineMessage.getContent();
|
|
|
+ if (CollectionUtil.isNotEmpty(isolationSystemList)) {
|
|
|
+ AlarmInfoCache.isolationSystemList = isolationSystemList;
|
|
|
+ }
|
|
|
+ } else if (msg.getOpCode() == 12) {
|
|
|
+ //云端更新报警记录状态
|
|
|
+ NettyMessage<JSONObject> alarmDefineMessage = JSONObject.parseObject(msg.toString(), new TypeReference<NettyMessage<JSONObject>>() {});
|
|
|
+ List<JSONObject> stateList = alarmDefineMessage.getContent();
|
|
|
+ alarmDefineService.updateAlarmDefine(stateList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|