|
@@ -5,11 +5,13 @@ import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import vip.xiaonuo.coldchain.core.alarm.bean.SensorAlarm;
|
|
import vip.xiaonuo.coldchain.core.alarm.bean.SensorAlarm;
|
|
|
|
+import vip.xiaonuo.coldchain.core.alarm.bean.SensorAlarmUser;
|
|
import vip.xiaonuo.coldchain.core.alarm.bean.SensorThreshold;
|
|
import vip.xiaonuo.coldchain.core.alarm.bean.SensorThreshold;
|
|
import vip.xiaonuo.coldchain.core.alarm.config.ColdChainAlarmMessageProperties;
|
|
import vip.xiaonuo.coldchain.core.alarm.config.ColdChainAlarmMessageProperties;
|
|
import vip.xiaonuo.coldchain.core.alarm.service.threshold.SensorThresholdService;
|
|
import vip.xiaonuo.coldchain.core.alarm.service.threshold.SensorThresholdService;
|
|
import vip.xiaonuo.coldchain.core.bean.influxdb.SensorData;
|
|
import vip.xiaonuo.coldchain.core.bean.influxdb.SensorData;
|
|
import vip.xiaonuo.coldchain.core.event.SensorAlarmEvent;
|
|
import vip.xiaonuo.coldchain.core.event.SensorAlarmEvent;
|
|
|
|
+import vip.xiaonuo.coldchain.core.util.DateFormatter;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
@@ -35,7 +37,7 @@ public class DefaultSensorAlarmChecker implements SensorAlarmChecker {
|
|
log.warn("没有找到设备 {} 的阈值配置", sensorData.getDeviceId());
|
|
log.warn("没有找到设备 {} 的阈值配置", sensorData.getDeviceId());
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
- final String deviceName = threshold.getMonitorTargetRegion().getDeviceName();
|
|
|
|
|
|
+ final String deviceName = threshold.getMonitorTargetRegion().getDeviceName();
|
|
// 检查传感器数据并触发报警
|
|
// 检查传感器数据并触发报警
|
|
boolean alarmTriggered = false;
|
|
boolean alarmTriggered = false;
|
|
if (sensorData.getTemperature() != null) {
|
|
if (sensorData.getTemperature() != null) {
|
|
@@ -66,16 +68,17 @@ public class DefaultSensorAlarmChecker implements SensorAlarmChecker {
|
|
String unit = getUnit(type);
|
|
String unit = getUnit(type);
|
|
if (value > upperThreshold) {
|
|
if (value > upperThreshold) {
|
|
// 超过上限,触发超标报警
|
|
// 超过上限,触发超标报警
|
|
- publishAlarm(type + "超标", value, unit, deviceName, time);
|
|
|
|
|
|
+ publishAlarm(type + "超标", value, unit, deviceName, time, upperThreshold);
|
|
alarmTriggered = true;
|
|
alarmTriggered = true;
|
|
} else if (value < lowerThreshold) {
|
|
} else if (value < lowerThreshold) {
|
|
// 低于下限,触发低于阈值报警
|
|
// 低于下限,触发低于阈值报警
|
|
- publishAlarm(type + "过低", value, unit, deviceName, time);
|
|
|
|
|
|
+ publishAlarm(type + "过低", value, unit, deviceName, time, lowerThreshold);
|
|
alarmTriggered = true;
|
|
alarmTriggered = true;
|
|
}
|
|
}
|
|
return alarmTriggered;
|
|
return alarmTriggered;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 发布报警事件
|
|
* 发布报警事件
|
|
*
|
|
*
|
|
@@ -85,15 +88,30 @@ public class DefaultSensorAlarmChecker implements SensorAlarmChecker {
|
|
* @param deviceName 设备名称
|
|
* @param deviceName 设备名称
|
|
* @param time 时间戳
|
|
* @param time 时间戳
|
|
*/
|
|
*/
|
|
- private void publishAlarm(String alarmType, Float value, String unit, String deviceName, String time) {
|
|
|
|
|
|
+ private void publishAlarm(String alarmType, Float value, String unit, String deviceName, String time, float threshold) {
|
|
// 获取对应的报警消息模板
|
|
// 获取对应的报警消息模板
|
|
String alarmMessage = getAlarmMessage(alarmType, value, unit, deviceName, time);
|
|
String alarmMessage = getAlarmMessage(alarmType, value, unit, deviceName, time);
|
|
|
|
+ // 构建 SensorAlarm 对象
|
|
SensorAlarm sensorAlarm = new SensorAlarm();
|
|
SensorAlarm sensorAlarm = new SensorAlarm();
|
|
- sensorAlarm.setAlarmType(alarmType);
|
|
|
|
- // 发布报警事件(可以替换成实际的报警处理逻辑)
|
|
|
|
|
|
+ sensorAlarm.setAlarmType(alarmType); // 设置报警类型(例如 温度超标、湿度过低等)
|
|
|
|
+ sensorAlarm.setValue(value); // 设置传感器值
|
|
|
|
+ sensorAlarm.setAlarmTime(DateFormatter.now(new Date())); // 设置单位(例如 °C, %, ppm)
|
|
|
|
+ sensorAlarm.setSource(deviceName); // 设置设备名称
|
|
|
|
+ sensorAlarm.setDeviceName(deviceName); // 设置设备名称
|
|
|
|
+ sensorAlarm.setPriority("高"); // 设置设备名称
|
|
|
|
+ sensorAlarm.setStatus("未处理"); // 设置设备名称
|
|
|
|
+ sensorAlarm.setAlarmTime(time); // 设置报警时间
|
|
|
|
+ sensorAlarm.setMessage(alarmMessage); // 设置报警消息
|
|
|
|
+ sensorAlarm.setThreshold(threshold); // 设置预警值
|
|
|
|
+ sensorAlarm.getAlarmUsers().add(new SensorAlarmUser(null,"oltp26cDdkiAAsualbzKMyiZbJrU"));
|
|
|
|
+ sensorAlarm.getAlarmUsers().add(new SensorAlarmUser(null,"oltp26fXeljOKUxYGhAx_dRqVAak"));
|
|
|
|
+ // 打印日志(可选)
|
|
|
|
+ log.warn("触发报警: 类型: {}, 设备: {}, 值: {} {}, 时间: {}", alarmType, deviceName, value, unit, time);
|
|
|
|
+ // 发布报警事件
|
|
applicationEventPublisher.publishEvent(new SensorAlarmEvent(this, sensorAlarm));
|
|
applicationEventPublisher.publishEvent(new SensorAlarmEvent(this, sensorAlarm));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取报警消息模板并替换占位符
|
|
* 获取报警消息模板并替换占位符
|
|
*
|
|
*
|