NoiseLeakAlarmService.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.zksy.audio.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.zksy.api.domain.AlarmData;
  4. import com.zksy.api.domain.WarningThreshold;
  5. import com.zksy.api.service.AlarmDataService;
  6. import com.zksy.api.service.EarlyWarningAutoService;
  7. import com.zksy.api.service.WarningThresholdService;
  8. import com.zksy.audio.domain.NoiseInfo;
  9. import com.zksy.audio.mapper.NoiseInfoMapper;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Service;
  14. import java.math.BigDecimal;
  15. import java.time.LocalDateTime;
  16. import java.time.format.DateTimeFormatter;
  17. import java.util.Objects;
  18. import java.util.Set;
  19. import java.util.concurrent.ConcurrentHashMap;
  20. /**
  21. * 漏失监测告警服务
  22. * <p>
  23. * 采用"时间持续性 + 漏水特征频段过滤"策略(漏失噪音是持续的,瞬时环境噪音不应触发):
  24. * <ul>
  25. * <li>rms 越限阈值(max_value)、恢复阈值(min_value) 均从 {@code warning_threshold} 表按设备编码获取,与系统现有逻辑一致</li>
  26. * <li>出现单次 rms≥max 且在漏水特征频段 → <b>预警</b>(疑似漏失)</li>
  27. * <li>最近窗口内累计 N 次 rms≥max 且在漏水特征频段 → <b>告警</b>(确认漏失),并解除预警</li>
  28. * <li>rms 回落≤min(或不在特征频段)→ 解除预警,重置确认态</li>
  29. * </ul>
  30. */
  31. @Slf4j
  32. @Service
  33. public class NoiseLeakAlarmService {
  34. //rms声音响度
  35. private static final String LEAK_WARNING_CODE = "WARN-LEAK";
  36. private static final String LEAK_FREQ_CODE = "WARN-LEAK-FREQ";
  37. private static final String LEAK_WARNING_TYPE = "漏失预警";
  38. private static final DateTimeFormatter TS_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  39. /** 已确认漏失的设备编码,用于避免同一漏失事件内重复告警 */
  40. private final Set<String> confirmedLeakEncode = ConcurrentHashMap.newKeySet();
  41. @Value("${wlds.leak.freq-min:200.0}")
  42. private double freqMin;
  43. @Value("${wlds.leak.freq-max:2000.0}")
  44. private double freqMax;
  45. @Value("${wlds.leak.window-minutes:30}")
  46. private int windowMinutes;
  47. @Value("${wlds.leak.alarm-count:3}")
  48. private int alarmCount;
  49. @Autowired
  50. private NoiseInfoMapper noiseInfoMapper;
  51. @Autowired
  52. private AlarmDataService alarmDataService;
  53. @Autowired
  54. private EarlyWarningAutoService earlyWarningAutoService;
  55. @Autowired
  56. private WarningThresholdService warningThresholdService;
  57. /**
  58. * 对一条新入库的噪音数据进行漏失预警/告警评估
  59. *
  60. * @param noise 当前噪音数据
  61. */
  62. public void evaluate(NoiseInfo noise) {
  63. if (noise == null || noise.getEncode() == null || noise.getEncode().trim().isEmpty()) {
  64. log.warn("漏失评估跳过:噪音数据缺少设备编码");
  65. return;
  66. }
  67. String encode = noise.getEncode();
  68. // 从阈值表获取该设备的漏失阈值(max=越限,min=恢复)
  69. WarningThreshold threshold = queryThreshold(encode, LEAK_WARNING_CODE);
  70. Double maxValue = threshold != null ? threshold.getMaxValue() : null;
  71. Double minValue = threshold != null ? threshold.getMinValue() : null;
  72. if (maxValue == null) {
  73. log.warn("漏失评估跳过:设备未配置漏失阈值(max_value),encode={}", encode);
  74. return;
  75. }
  76. // 特征频段:优先取阈值表(WARN-LEAK-FREQ min=下限 max=上限),未配则回退应用配置
  77. WarningThreshold freqThreshold = queryThreshold(encode, LEAK_FREQ_CODE);
  78. double lowerFreq = freqMin;
  79. double upperFreq = freqMax;
  80. if (freqThreshold != null && freqThreshold.getMinValue() != null && freqThreshold.getMaxValue() != null
  81. && freqThreshold.getMinValue() < freqThreshold.getMaxValue()) {
  82. lowerFreq = freqThreshold.getMinValue();
  83. upperFreq = freqThreshold.getMaxValue();
  84. }
  85. Integer rms = noise.getRms();
  86. if (rms == null) {
  87. return;
  88. }
  89. boolean freqInBand = noise.getCenterFrequency() != null
  90. && noise.getCenterFrequency() >= lowerFreq && noise.getCenterFrequency() <= upperFreq;
  91. // 1) 特征频段消失:漏水特征不再成立,彻底解除
  92. if (!freqInBand) {
  93. confirmedLeakEncode.remove(encode);
  94. earlyWarningAutoService.closeAutoWarning(encode, LEAK_WARNING_CODE, "漏失特征频段消失,解除预警");
  95. return;
  96. }
  97. // 2) rms 回落至恢复阈值(≤min):解除预警并重置确认态
  98. if (minValue != null && rms <= minValue) {
  99. confirmedLeakEncode.remove(encode);
  100. earlyWarningAutoService.closeAutoWarning(encode, LEAK_WARNING_CODE, "rms回落至恢复阈值,解除预警");
  101. return;
  102. }
  103. // 3) 中间带(min < rms < max):保持现状,不做级别变更,避免边界抖动
  104. if (rms < maxValue) {
  105. return;
  106. }
  107. // 4) rms >= max:疑似→确认(时间持续性)
  108. long count = countQualified(encode, maxValue, lowerFreq, upperFreq);
  109. if (count >= alarmCount) {
  110. if (confirmedLeakEncode.add(encode)) {
  111. saveLeakAlarm(encode, rms, count, lowerFreq, upperFreq);
  112. log.info("确认漏失告警: encode={}, 窗口累计{}次", encode, count);
  113. }
  114. earlyWarningAutoService.closeAutoWarning(encode, LEAK_WARNING_CODE, "已确认漏失,转告警处理");
  115. } else {
  116. confirmedLeakEncode.remove(encode);
  117. earlyWarningAutoService.recordWarning(encode, LEAK_WARNING_TYPE, LEAK_WARNING_CODE, 3,
  118. buildWarningContent(encode, count, maxValue, lowerFreq, upperFreq));
  119. }
  120. }
  121. /**
  122. * 按设备编码与预警编码查询阈值规则
  123. */
  124. private WarningThreshold queryThreshold(String encode, String warningCode) {
  125. try {
  126. return warningThresholdService.getWarningThresholdByDeviceAndCode(encode, warningCode);
  127. } catch (Exception e) {
  128. log.error("查询阈值失败:encode={}, warningCode={}", encode, warningCode, e);
  129. return null;
  130. }
  131. }
  132. /**
  133. * 统计指定设备在最近时间窗口内,rms 超过越限阈值且中心频率落在漏失频段的采样次数
  134. */
  135. private long countQualified(String encode, Double maxValue, double lowerFreq, double upperFreq) {
  136. String start = LocalDateTime.now().minusMinutes(windowMinutes).format(TS_FORMATTER);
  137. return noiseInfoMapper.selectCount(new LambdaQueryWrapper<NoiseInfo>()
  138. .eq(NoiseInfo::getEncode, encode)
  139. .ge(NoiseInfo::getTs, start)
  140. .ge(NoiseInfo::getRms, maxValue)
  141. .ge(NoiseInfo::getCenterFrequency, lowerFreq)
  142. .le(NoiseInfo::getCenterFrequency, upperFreq));
  143. }
  144. private void saveLeakAlarm(String encode, Integer rms, long count, double lowerFreq, double upperFreq) {
  145. AlarmData alarm = new AlarmData();
  146. alarm.setDeviceCode(encode);
  147. alarm.setWarningType("漏失报警");
  148. alarm.setWarningCode(LEAK_WARNING_CODE);
  149. if (rms != null) {
  150. alarm.setActualValue(BigDecimal.valueOf(rms));
  151. }
  152. alarm.setAlarmLevel(1);
  153. alarm.setAlarmStatus(0);
  154. alarm.setAlarmTime(LocalDateTime.now());
  155. alarm.setCreateTime(LocalDateTime.now());
  156. alarm.setRemark(String.format("漏失监测确认漏失:最近%d分钟内%d次采样rms≥阈值且在漏失频段[%.0f,%.0f]Hz",
  157. windowMinutes, count, lowerFreq, upperFreq));
  158. try {
  159. alarmDataService.saveAlarmData(alarm);
  160. log.info("漏失告警入库成功: encode={}, count={}", encode, count);
  161. } catch (Exception e) {
  162. log.error("漏失告警入库失败: encode={}", encode, e);
  163. }
  164. }
  165. private String buildWarningContent(String encode, long count, Double maxValue, double lowerFreq, double upperFreq) {
  166. return String.format("【系统自动预警】设备[%s]疑似漏失:最近%d分钟内%d次采样rms≥%s且中心频率落在漏失频段[%.0f,%.0f]Hz,待确认",
  167. encode, windowMinutes, count, Objects.toString(maxValue, "-"), lowerFreq, upperFreq);
  168. }
  169. }