|
|
@@ -4,10 +4,10 @@ import cn.hutool.core.lang.UUID;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.zksy.api.domain.WarningThreshold;
|
|
|
import com.zksy.api.service.WarningThresholdService;
|
|
|
+import com.zksy.api.domain.AlarmData;
|
|
|
+import com.zksy.api.service.AlarmDataService;
|
|
|
import com.zksy.api.utils.SmsUtil;
|
|
|
import com.zksy.common.exception.InvalidMessageException;
|
|
|
-import com.zksy.gas.domain.Enum.GasDeviceCodeEnum;
|
|
|
-import com.zksy.gas.domain.Enum.GasWarningCodeEnum;
|
|
|
import com.zksy.gas.domain.GasMonitorData;
|
|
|
import com.zksy.gas.service.GasMonitorDataService;
|
|
|
import com.zksy.utils.DevicePhoneFetchUtil;
|
|
|
@@ -26,6 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -39,6 +40,8 @@ public class MessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
@Autowired
|
|
|
private WarningThresholdService warningThresholdService;
|
|
|
@Autowired
|
|
|
+ private AlarmDataService alarmDataService;
|
|
|
+ @Autowired
|
|
|
private SmsUtil smsUtil;
|
|
|
@Autowired
|
|
|
private DevicePhoneFetchUtil devicePhoneFetchUtil;
|
|
|
@@ -176,160 +179,187 @@ public class MessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
}
|
|
|
}
|
|
|
public void checkIfSmsAlertNeeded(GasMonitorData resultData){
|
|
|
-
|
|
|
//用MAC地址作为设备的设备编号
|
|
|
String deviceId = resultData.getMacAddress();
|
|
|
- String deviceName = "可燃气体设备";
|
|
|
- String deviceType = "flammable-gas";
|
|
|
|
|
|
//获取温度阈值
|
|
|
- String deviceWarningCode = GasDeviceCodeEnum.FIREFIGHTING_DEVICE.getCode();
|
|
|
- GasWarningCodeEnum temTargetWarningType=GasWarningCodeEnum.TEMPERATRUE;
|
|
|
- String temWarningCode = temTargetWarningType.getCode();
|
|
|
- String temWarningMsg = temTargetWarningType.getName();
|
|
|
- Double temWarningValue = checkTemAndHum(deviceWarningCode, temWarningCode);
|
|
|
+ String temWarningCode = "WARN-GAS-TEMPERATURE";
|
|
|
+ WarningThreshold temThreshold = checkThreshold(deviceId, temWarningCode);
|
|
|
+ Double temMinValue = temThreshold != null ? temThreshold.getMinValue() : null;
|
|
|
+ Double temMaxValue = temThreshold != null ? temThreshold.getMaxValue() : null;
|
|
|
+ String temWarningType = temThreshold != null ? temThreshold.getWarningType() : "可燃气体温度预警";
|
|
|
+ String temRemark = temThreshold != null ? temThreshold.getRemark() : null;
|
|
|
+
|
|
|
//获取湿度阈值
|
|
|
- GasWarningCodeEnum humTargetWarningType=GasWarningCodeEnum.HUMIDITY;
|
|
|
- String humWarningCode = humTargetWarningType.getCode();
|
|
|
- String humWarningMsg = humTargetWarningType.getName();
|
|
|
- Double humWarningValue = checkTemAndHum(deviceWarningCode, humWarningCode);
|
|
|
+ String humWarningCode = "WARN-GAS-HUMIDITY";
|
|
|
+ WarningThreshold humThreshold = checkThreshold(deviceId, humWarningCode);
|
|
|
+ Double humMinValue = humThreshold != null ? humThreshold.getMinValue() : null;
|
|
|
+ Double humMaxValue = humThreshold != null ? humThreshold.getMaxValue() : null;
|
|
|
+ String humWarningType = humThreshold != null ? humThreshold.getWarningType() : "湿度预警";
|
|
|
+ String humRemark = humThreshold != null ? humThreshold.getRemark() : null;
|
|
|
+
|
|
|
//获取气体浓度阈值
|
|
|
- GasWarningCodeEnum gasTargetWarningType=GasWarningCodeEnum.GASCONCENTRATION;
|
|
|
- String gasWarningCode = gasTargetWarningType.getCode();
|
|
|
- String gasWarningMsg = gasTargetWarningType.getName();
|
|
|
- Double gasWarningValue = checkTemAndHum(deviceWarningCode, gasWarningCode);
|
|
|
+ String gasWarningCode = "WARN-GAS-CONCENTRATION";
|
|
|
+ WarningThreshold gasThreshold = checkThreshold(deviceId, gasWarningCode);
|
|
|
+ Double gasMinValue = gasThreshold != null ? gasThreshold.getMinValue() : null;
|
|
|
+ Double gasMaxValue = gasThreshold != null ? gasThreshold.getMaxValue() : null;
|
|
|
+ String gasWarningType = gasThreshold != null ? gasThreshold.getWarningType() : "气体浓度预警";
|
|
|
+ String gasRemark = gasThreshold != null ? gasThreshold.getRemark() : null;
|
|
|
|
|
|
//判断温度
|
|
|
float temperature = resultData.getTemperature();
|
|
|
- boolean temperatureIsOverThreshold = temperature > temWarningValue;
|
|
|
+ boolean temperatureIsOverThreshold = false;
|
|
|
+ if (temMinValue != null && temperature <= temMinValue) {
|
|
|
+ temperatureIsOverThreshold = true;
|
|
|
+ }
|
|
|
+ if (temMaxValue != null && temperature >= temMaxValue) {
|
|
|
+ temperatureIsOverThreshold = true;
|
|
|
+ }
|
|
|
if(temperatureIsOverThreshold){
|
|
|
- sendMessage(deviceId,temWarningMsg,resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- temWarningMsg, temWarningCode, BigDecimal.valueOf(temWarningValue),
|
|
|
- BigDecimal.valueOf(temperature), "可燃气体温度报警");
|
|
|
+ //先保存告警数据
|
|
|
+ saveAlarmData(deviceId, temWarningType, temWarningCode,
|
|
|
+ temMinValue != null ? BigDecimal.valueOf(temMinValue) : null,
|
|
|
+ temMaxValue != null ? BigDecimal.valueOf(temMaxValue) : null,
|
|
|
+ BigDecimal.valueOf(temperature), temRemark != null ? temRemark : "可燃气体温度报警");
|
|
|
+ //再发送短信
|
|
|
+ sendMessage(deviceId, temWarningType, resultData.getLongitude(), resultData.getLatitude());
|
|
|
}
|
|
|
+
|
|
|
//判断湿度
|
|
|
float humidity = resultData.getHumidity();
|
|
|
- boolean humidityIsOverThreshold=humidity>humWarningValue;
|
|
|
+ boolean humidityIsOverThreshold = false;
|
|
|
+ if (humMinValue != null && humidity <= humMinValue) {
|
|
|
+ humidityIsOverThreshold = true;
|
|
|
+ }
|
|
|
+ if (humMaxValue != null && humidity >= humMaxValue) {
|
|
|
+ humidityIsOverThreshold = true;
|
|
|
+ }
|
|
|
if(humidityIsOverThreshold){
|
|
|
- sendMessage(deviceId,humWarningMsg,resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- humWarningMsg, humWarningCode, BigDecimal.valueOf(humWarningValue),
|
|
|
- BigDecimal.valueOf(humidity), "可燃气体湿度报警");
|
|
|
+ //先保存告警数据
|
|
|
+ saveAlarmData(deviceId, humWarningType, humWarningCode,
|
|
|
+ humMinValue != null ? BigDecimal.valueOf(humMinValue) : null,
|
|
|
+ humMaxValue != null ? BigDecimal.valueOf(humMaxValue) : null,
|
|
|
+ BigDecimal.valueOf(humidity), humRemark != null ? humRemark : "可燃气体湿度报警");
|
|
|
+ //再发送短信
|
|
|
+ sendMessage(deviceId, humWarningType, resultData.getLongitude(), resultData.getLatitude());
|
|
|
}
|
|
|
+
|
|
|
//判断气体浓度
|
|
|
BigDecimal gasConcentration = resultData.getGasConcentration();
|
|
|
- float actualGasConcentration=gasConcentration.floatValue();
|
|
|
- boolean concentrationIsOverThreshold=actualGasConcentration>gasWarningValue;
|
|
|
+ float actualGasConcentration = gasConcentration != null ? gasConcentration.floatValue() : 0f;
|
|
|
+ boolean concentrationIsOverThreshold = false;
|
|
|
+ if (gasMinValue != null && actualGasConcentration <= gasMinValue) {
|
|
|
+ concentrationIsOverThreshold = true;
|
|
|
+ }
|
|
|
+ if (gasMaxValue != null && actualGasConcentration >= gasMaxValue) {
|
|
|
+ concentrationIsOverThreshold = true;
|
|
|
+ }
|
|
|
if(concentrationIsOverThreshold){
|
|
|
- sendMessage(deviceId,gasWarningMsg,resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- gasWarningMsg, gasWarningCode, BigDecimal.valueOf(gasWarningValue),
|
|
|
- gasConcentration, "可燃气体浓度报警");
|
|
|
+ //先保存告警数据
|
|
|
+ saveAlarmData(deviceId, gasWarningType, gasWarningCode,
|
|
|
+ gasMinValue != null ? BigDecimal.valueOf(gasMinValue) : null,
|
|
|
+ gasMaxValue != null ? BigDecimal.valueOf(gasMaxValue) : null,
|
|
|
+ gasConcentration, gasRemark != null ? gasRemark : "可燃气体浓度报警");
|
|
|
+ //再发送短信
|
|
|
+ sendMessage(deviceId, gasWarningType, resultData.getLongitude(), resultData.getLatitude());
|
|
|
}
|
|
|
+
|
|
|
//获取到的数据逐一进行判断
|
|
|
Map<String, Boolean> alarmBits = resultData.parseAlarmBits();
|
|
|
if(alarmBits.get("unknownAlarm")){
|
|
|
+ saveAlarmData(deviceId, "未知报警", "WARN-UNKNOWN", null, null, null, "可燃气体未知报警");
|
|
|
sendMessage(deviceId,"未知报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "未知报警", "WARN-UNKNOWN", null,
|
|
|
- null, "可燃气体未知报警");
|
|
|
}
|
|
|
if(alarmBits.get("highAlarm")){
|
|
|
+ saveAlarmData(deviceId, "高报警", "WARN-HIGH", null, null, null, "可燃气体高报警");
|
|
|
sendMessage(deviceId,"高报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "高报警", "WARN-HIGH", null,
|
|
|
- null, "可燃气体高报警");
|
|
|
}
|
|
|
if(alarmBits.get("overRange")){
|
|
|
+ saveAlarmData(deviceId, "超量程", "WARN-OVER-RANGE", null, null, null, "可燃气体超量程报警");
|
|
|
sendMessage(deviceId,"超量程",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "超量程", "WARN-OVER-RANGE", null,
|
|
|
- null, "可燃气体超量程报警");
|
|
|
}
|
|
|
if(alarmBits.get("calibrationCycle")){
|
|
|
+ saveAlarmData(deviceId, "标定周期", "WARN-CALIBRATION-CYCLE", null, null, null, "可燃气体标定周期报警");
|
|
|
sendMessage(deviceId,"标定周期",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "标定周期", "WARN-CALIBRATION-CYCLE", null,
|
|
|
- null, "可燃气体标定周期报警");
|
|
|
}
|
|
|
if(alarmBits.get("overLife")){
|
|
|
+ saveAlarmData(deviceId, "超寿命", "WARN-OVER-LIFE", null, null, null, "可燃气体超寿命报警");
|
|
|
sendMessage(deviceId,"超寿命",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "超寿命", "WARN-OVER-LIFE", null,
|
|
|
- null, "可燃气体超寿命报警");
|
|
|
}
|
|
|
if(alarmBits.get("fallAlarm")){
|
|
|
+ saveAlarmData(deviceId, "跌倒报警", "WARN-FALL", null, null, null, "可燃气体跌倒报警");
|
|
|
sendMessage(deviceId,"跌倒报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "跌倒报警", "WARN-FALL", null,
|
|
|
- null, "可燃气体跌倒报警");
|
|
|
}
|
|
|
if(alarmBits.get("undervoltage")){
|
|
|
+ saveAlarmData(deviceId, "欠压报警", "WARN-UNDERVOLTAGE", null, null, null, "可燃气体欠压报警");
|
|
|
sendMessage(deviceId,"欠压报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "欠压报警", "WARN-UNDERVOLTAGE", null,
|
|
|
- null, "可燃气体欠压报警");
|
|
|
}
|
|
|
if(alarmBits.get("rangeAlarm")){
|
|
|
+ saveAlarmData(deviceId, "区间报警", "WARN-RANGE", null, null, null, "可燃气体区间报警");
|
|
|
sendMessage(deviceId,"区间报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "区间报警", "WARN-RANGE", null,
|
|
|
- null, "可燃气体区间报警");
|
|
|
}
|
|
|
if(alarmBits.get("keyAlarm")){
|
|
|
+ saveAlarmData(deviceId, "按键报警", "WARN-KEY", null, null, null, "可燃气体按键报警");
|
|
|
sendMessage(deviceId,"按键报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "按键报警", "WARN-KEY", null,
|
|
|
- null, "可燃气体按键报警");
|
|
|
}
|
|
|
if(alarmBits.get("vibrationAlarm")){
|
|
|
+ saveAlarmData(deviceId, "震动报警", "WARN-VIBRATION", null, null, null, "可燃气体震动报警");
|
|
|
sendMessage(deviceId,"震动报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "震动报警", "WARN-VIBRATION", null,
|
|
|
- null, "可燃气体震动报警");
|
|
|
}
|
|
|
if(alarmBits.get("waterLevelAlarm")){
|
|
|
+ saveAlarmData(deviceId, "水位报警", "WARN-WATER-LEVEL", null, null, null, "可燃气体水位报警");
|
|
|
sendMessage(deviceId,"水位报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "水位报警", "WARN-WATER-LEVEL", null,
|
|
|
- null, "可燃气体水位报警");
|
|
|
}
|
|
|
if(alarmBits.get("powerOffAlarm")){
|
|
|
+ saveAlarmData(deviceId, "断电报警", "WARN-POWER-OFF", null, null, null, "可燃气体断电报警");
|
|
|
sendMessage(deviceId,"断电报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "断电报警", "WARN-POWER-OFF", null,
|
|
|
- null, "可燃气体断电报警");
|
|
|
}
|
|
|
if(alarmBits.get("sensorFault")){
|
|
|
+ saveAlarmData(deviceId, "传感器故障", "WARN-SENSOR-FAULT", null, null, null, "可燃气体传感器故障");
|
|
|
sendMessage(deviceId,"传感器故障",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "传感器故障", "WARN-SENSOR-FAULT", null,
|
|
|
- null, "可燃气体传感器故障");
|
|
|
}
|
|
|
if(alarmBits.get("overHumidity")){
|
|
|
+ saveAlarmData(deviceId, "超湿报警", "WARN-OVER-HUMIDITY", null, null, null, "可燃气体超湿报警");
|
|
|
sendMessage(deviceId,"超湿报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "超湿报警", "WARN-OVER-HUMIDITY", null,
|
|
|
- null, "可燃气体超湿报警");
|
|
|
}
|
|
|
if(alarmBits.get("overTemperature")){
|
|
|
+ saveAlarmData(deviceId, "超温报警", "WARN-OVER-TEMPERATURE", null, null, null, "可燃气体超温报警");
|
|
|
sendMessage(deviceId,"超温报警",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "超温报警", "WARN-OVER-TEMPERATURE", null,
|
|
|
- null, "可燃气体超温报警");
|
|
|
}
|
|
|
if(alarmBits.get("systemFault")){
|
|
|
+ saveAlarmData(deviceId, "系统故障", "WARN-SYSTEM-FAULT", null, null, null, "可燃气体系统故障");
|
|
|
sendMessage(deviceId,"系统故障",resultData.getLongitude(),resultData.getLatitude());
|
|
|
- alarmUtil.saveAlarm(deviceName, deviceId, deviceType,
|
|
|
- "系统故障", "WARN-SYSTEM-FAULT", null,
|
|
|
- null, "可燃气体系统故障");
|
|
|
}
|
|
|
}
|
|
|
- public Double checkTemAndHum(String deviceWarningCode,String warningCode){
|
|
|
- //获取数据库中的阈值
|
|
|
- WarningThreshold threshold = warningThresholdService.getWarningThresholdByDeviceAndCode(deviceWarningCode, warningCode);
|
|
|
- return threshold.getWarningValue();
|
|
|
+
|
|
|
+ private WarningThreshold checkThreshold(String deviceCode, String warningCode) {
|
|
|
+ try {
|
|
|
+ return warningThresholdService.getWarningThresholdByDeviceAndCode(deviceCode, warningCode);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("查询预警阈值失败:deviceCode={}, warningCode={}", deviceCode, warningCode, e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveAlarmData(String deviceCode, String warningType, String warningCode,
|
|
|
+ BigDecimal minValue, BigDecimal maxValue,
|
|
|
+ BigDecimal actualValue, String remark) {
|
|
|
+ try {
|
|
|
+ AlarmData alarmData = new AlarmData();
|
|
|
+ alarmData.setDeviceCode(deviceCode);
|
|
|
+ alarmData.setWarningType(warningType);
|
|
|
+ alarmData.setWarningCode(warningCode);
|
|
|
+ alarmData.setMinValue(minValue);
|
|
|
+ alarmData.setMaxValue(maxValue);
|
|
|
+ alarmData.setActualValue(actualValue);
|
|
|
+ alarmData.setAlarmStatus(0);
|
|
|
+ alarmData.setAlarmTime(LocalDateTime.now());
|
|
|
+ alarmData.setRemark(remark);
|
|
|
+ alarmData.setCreateTime(LocalDateTime.now());
|
|
|
+ alarmDataService.saveAlarmData(alarmData);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("保存告警数据失败", e);
|
|
|
+ }
|
|
|
}
|
|
|
//构造发送短信参数&&发送短信
|
|
|
public void sendMessage(String deviceNo, String alarmType, BigDecimal lng, BigDecimal lat){
|