|
@@ -1,5 +1,6 @@
|
|
|
package vip.xiaonuo.coldchain.core.alarm.service.check;
|
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
@@ -12,9 +13,11 @@ import vip.xiaonuo.coldchain.core.alarm.service.threshold.SensorThresholdService
|
|
|
import vip.xiaonuo.coldchain.core.bean.influxdb.SensorData;
|
|
|
import vip.xiaonuo.coldchain.core.event.SensorAlarmEvent;
|
|
|
import vip.xiaonuo.coldchain.core.util.DateFormatter;
|
|
|
+import vip.xiaonuo.coldchain.modular.monitortargetregion.entity.MonitorTargetRegion;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@Component
|
|
|
@RequiredArgsConstructor
|
|
@@ -37,18 +40,17 @@ public class DefaultSensorAlarmChecker implements SensorAlarmChecker {
|
|
|
log.warn("没有找到设备 {} 的阈值配置", sensorData.getDeviceId());
|
|
|
return false;
|
|
|
}
|
|
|
- final String deviceName = threshold.getMonitorTargetRegion().getName();
|
|
|
- final String deviceId = threshold.getMonitorTargetRegion().getMonitorTargetId();
|
|
|
+ MonitorTargetRegion monitorTargetRegion = threshold.getMonitorTargetRegion();
|
|
|
// 检查传感器数据并触发报警
|
|
|
boolean alarmTriggered = false;
|
|
|
if (isNull(sensorData.getTemperature())) {
|
|
|
- alarmTriggered = checkThreshold(sensorData.getTemperature(), threshold.getTemperatureUp(), threshold.getTemperatureDown(), "温度", deviceName, deviceId);
|
|
|
+ alarmTriggered = checkThreshold(sensorData.getTemperature(), threshold.getTemperatureUp(), threshold.getTemperatureDown(), "温度", monitorTargetRegion);
|
|
|
}
|
|
|
if (isNull(sensorData.getHumidity())) {
|
|
|
- alarmTriggered |= checkThreshold(sensorData.getHumidity(), threshold.getHumidityUp(), threshold.getHumidityDown(), "湿度", deviceName, deviceId);
|
|
|
+ alarmTriggered |= checkThreshold(sensorData.getHumidity(), threshold.getHumidityUp(), threshold.getHumidityDown(), "湿度", monitorTargetRegion);
|
|
|
}
|
|
|
if (isNull(sensorData.getCo2())) {
|
|
|
- alarmTriggered |= checkThreshold(sensorData.getCo2(), threshold.getCo2Up(), threshold.getCo2Down(), "二氧化碳", deviceName, deviceId);
|
|
|
+ alarmTriggered |= checkThreshold(sensorData.getCo2(), threshold.getCo2Up(), threshold.getCo2Down(), "二氧化碳", monitorTargetRegion);
|
|
|
}
|
|
|
return alarmTriggered;
|
|
|
}
|
|
@@ -60,20 +62,19 @@ public class DefaultSensorAlarmChecker implements SensorAlarmChecker {
|
|
|
* @param upperThreshold 阈值上限
|
|
|
* @param lowerThreshold 阈值下限
|
|
|
* @param type 数据类型(温度、湿度、二氧化碳)
|
|
|
- * @param deviceName 设备名称
|
|
|
* @return 是否触发报警
|
|
|
*/
|
|
|
- private boolean checkThreshold(Float value, Float upperThreshold, Float lowerThreshold, String type, String deviceName, String deviceId) {
|
|
|
+ private boolean checkThreshold(Float value, Float upperThreshold, Float lowerThreshold, String type, MonitorTargetRegion monitorTargetRegion) {
|
|
|
boolean alarmTriggered = false;
|
|
|
String time = DATE_FORMAT.format(new Date()); // 获取当前时间
|
|
|
String unit = getUnit(type);
|
|
|
if (value > upperThreshold) {
|
|
|
// 超过上限,触发超标报警
|
|
|
- publishAlarm(type + "超标", value, unit, deviceName, time, upperThreshold, deviceId);
|
|
|
+ publishAlarm(type + "超标", value, unit, time, upperThreshold, monitorTargetRegion);
|
|
|
alarmTriggered = true;
|
|
|
} else if (value < lowerThreshold) {
|
|
|
// 低于下限,触发低于阈值报警
|
|
|
- publishAlarm(type + "过低", value, unit, deviceName, time, lowerThreshold, deviceId);
|
|
|
+ publishAlarm(type + "过低", value, unit, time, lowerThreshold, monitorTargetRegion);
|
|
|
alarmTriggered = true;
|
|
|
}
|
|
|
return alarmTriggered;
|
|
@@ -83,16 +84,17 @@ public class DefaultSensorAlarmChecker implements SensorAlarmChecker {
|
|
|
/**
|
|
|
* 发布报警事件
|
|
|
*
|
|
|
- * @param alarmType 报警类型(例如 温度超标、湿度过低等)
|
|
|
- * @param value 传感器数据值
|
|
|
- * @param unit 传感器数据单位
|
|
|
- * @param deviceName 设备名称
|
|
|
- * @param time 时间戳
|
|
|
+ * @param alarmType 报警类型(例如 温度超标、湿度过低等)
|
|
|
+ * @param value 传感器数据值
|
|
|
+ * @param unit 传感器数据单位
|
|
|
+ * @param time 时间戳
|
|
|
*/
|
|
|
- private void publishAlarm(String alarmType, Float value, String unit, String deviceName, String time, float threshold, String deviceId) {
|
|
|
- // 获取对应的报警消息模板
|
|
|
+ private void publishAlarm(String alarmType, Float value, String unit, String time, float threshold, MonitorTargetRegion monitorTargetRegion) {
|
|
|
+ String monitorTargetId = monitorTargetRegion.getMonitorTargetId();
|
|
|
+ Integer sensorRoute = monitorTargetRegion.getSensorRoute();
|
|
|
+ String deviceName = monitorTargetRegion.getName();
|
|
|
+ String deviceId = monitorTargetRegion.getId();
|
|
|
String alarmMessage = getAlarmMessage(alarmType, value, unit, deviceName, time, threshold);
|
|
|
- // 构建 SensorAlarm 对象
|
|
|
SensorAlarm sensorAlarm = new SensorAlarm();
|
|
|
sensorAlarm.setAlarmType(alarmType); // 设置报警类型(例如 温度超标、湿度过低等)
|
|
|
sensorAlarm.setValue(value); // 设置传感器值
|
|
@@ -105,14 +107,24 @@ public class DefaultSensorAlarmChecker implements SensorAlarmChecker {
|
|
|
sensorAlarm.setAlarmTime(time); // 设置报警时间
|
|
|
sensorAlarm.setMessage(alarmMessage); // 设置报警消息
|
|
|
sensorAlarm.setThreshold(threshold); // 设置预警值
|
|
|
- sensorAlarm.getAlarmUsers().add(new SensorAlarmUser(null, "oltp26cDdkiAAsualbzKMyiZbJrU"));
|
|
|
- sensorAlarm.getAlarmUsers().add(new SensorAlarmUser(null, "oltp26fXeljOKUxYGhAx_dRqVAak"));
|
|
|
+ sensorAlarm.setMonitorTargetId(monitorTargetId);
|
|
|
+ sensorAlarm.setSensorRoute(sensorRoute);
|
|
|
+ List<SensorAlarmUser> alarmUsers = monitorTargetRegion.getAlarmUsers();
|
|
|
+ sensorAlarm.setAlarmUsers(alarmUsers);
|
|
|
+ sensorAlarm.setThreshold(threshold);
|
|
|
log.warn("触发报警: 类型: {},详细报警内容 : {}", alarmType, alarmMessage);
|
|
|
// 发布报警事件
|
|
|
applicationEventPublisher.publishEvent(new SensorAlarmEvent(this, sensorAlarm));
|
|
|
}
|
|
|
|
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
+ SensorAlarm sensorAlarm = new SensorAlarm();
|
|
|
+ sensorAlarm.getAlarmUsers().add(new SensorAlarmUser(null, "oltp26cDdkiAAsualbzKMyiZbJrU"));
|
|
|
+ sensorAlarm.getAlarmUsers().add(new SensorAlarmUser(null, "oltp26fXeljOKUxYGhAx_dRqVAak"));
|
|
|
+ System.out.println(JSONUtil.toJsonStr(sensorAlarm.getAlarmUsers()));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取报警消息模板并替换占位符
|
|
|
*
|