|
|
@@ -8,6 +8,7 @@ import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictEnum;
|
|
|
import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceTypeLv1Enum;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotDevicefactor;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotWarnconfig;
|
|
|
import com.yunfeiyun.agmp.iot.common.enums.EnumWarnRuleOp;
|
|
|
import com.yunfeiyun.agmp.iots.service.IIotDevicefactorService;
|
|
|
import com.yunfeiyun.agmp.iots.warn.model.WarnConfigInfo;
|
|
|
@@ -15,6 +16,7 @@ import com.yunfeiyun.agmp.iots.warn.model.WarnResult;
|
|
|
import com.yunfeiyun.agmp.iots.warn.util.CompareUtil;
|
|
|
import com.yunfeiyun.agmp.iots.warn.util.WarnMessageBuilderUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -62,8 +64,8 @@ public class WarnService {
|
|
|
/**
|
|
|
* 统一处理上报数据
|
|
|
*
|
|
|
- * @param devId 设备id
|
|
|
* @param data 上报的数据对象
|
|
|
+ * param ext 上报的原始数据
|
|
|
*/
|
|
|
public void processWarningReportData(IotDevice iotDevice, JSONObject data) {
|
|
|
//转异步处理
|
|
|
@@ -74,16 +76,15 @@ public class WarnService {
|
|
|
/**
|
|
|
* 统一处理上报数据:异步处理
|
|
|
*
|
|
|
- * @param devId 设备id
|
|
|
* @param data 上报的数据对象
|
|
|
*/
|
|
|
private void processWarningReportDataSyn(IotDevice iotDevice, JSONObject data) {
|
|
|
- String devId = iotDevice.getDevBid();
|
|
|
+ String devBid = iotDevice.getDevBid();
|
|
|
String devtypeBid = iotDevice.getDevtypeBid();
|
|
|
String devClass = IotDeviceDictEnum.getLv1CodeByCode(devtypeBid);
|
|
|
IotDeviceTypeLv1Enum iotDeviceTypeLv1Enum = IotDeviceTypeLv1Enum.findEnumByCode(devClass);
|
|
|
if (iotDeviceTypeLv1Enum == null) {
|
|
|
- log.error("[设备告警] 设备大类不存在,devId:{}, devtypeBid:{}", devId, devtypeBid);
|
|
|
+ log.error("[设备告警] 设备大类不存在,devBid:{}, devtypeBid:{}", devBid, devtypeBid);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -91,55 +92,38 @@ public class WarnService {
|
|
|
// 获取该设备有哪些告警配置
|
|
|
WarnConfigInfo warnConfigInfo = new WarnConfigInfo();
|
|
|
warnConfigInfo.setTid(iotDevice.getTid());
|
|
|
- warnConfigInfo.setDevBid(devId);
|
|
|
+ warnConfigInfo.setDevBid(devBid);
|
|
|
|
|
|
Map<String, List<WarnConfigInfo>> configMap = iotWarnBussinessService.selectIotWarnConfigInfoMap(warnConfigInfo);
|
|
|
//配置一个个检查
|
|
|
for(Map.Entry<String, List<WarnConfigInfo>> entry : configMap.entrySet()) {
|
|
|
List<WarnConfigInfo> configList = entry.getValue();
|
|
|
WarnResult warnResult = null;
|
|
|
- switch (iotDeviceTypeLv1Enum) {
|
|
|
- case QXZ: {
|
|
|
- warnResult = comparableQxzReportData(iotDevice, configList, data);
|
|
|
- break;
|
|
|
- }
|
|
|
+ try{
|
|
|
+ switch (iotDeviceTypeLv1Enum) {
|
|
|
+ case QXZ: {
|
|
|
+ warnResult = comparableQxzReportData(iotDevice, configList, data);
|
|
|
+ break;
|
|
|
+ }
|
|
|
// case SQZ: {
|
|
|
-// warnResult = comparableSqzReportData(devId, config, data);
|
|
|
+// warnResult = comparableSqzReportData(devBid, config, data);
|
|
|
// break;
|
|
|
// }
|
|
|
// case "病虫害": {
|
|
|
-// warnResult = comparableBchReportData(devId, config, data);
|
|
|
+// warnResult = comparableBchReportData(devBid, config, data);
|
|
|
// break;
|
|
|
// }
|
|
|
- default:
|
|
|
- break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("[设备告警] 设备上报数据 异常,devBid:{}, config:{}, data:{}", devBid, configList, data, e);
|
|
|
}
|
|
|
+
|
|
|
if (warnResult != null) {
|
|
|
handleWarnRecord(warnResult);
|
|
|
}
|
|
|
}
|
|
|
-// for (Object config : objects) {
|
|
|
-// WarnResult warnResult = null;
|
|
|
-// switch (iotDeviceTypeLv1Enum) {
|
|
|
-// case QXZ: {
|
|
|
-// warnResult = comparableQxzReportData(devId, config, data);
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// case SQZ: {
|
|
|
-// warnResult = comparableSqzReportData(devId, config, data);
|
|
|
-// break;
|
|
|
-// }
|
|
|
-//// case "病虫害": {
|
|
|
-//// warnResult = comparableBchReportData(devId, config, data);
|
|
|
-//// break;
|
|
|
-//// }
|
|
|
-// default:
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// if (warnResult != null) {
|
|
|
-// handleWarnRecord(warnResult);
|
|
|
-// }
|
|
|
-// }
|
|
|
}, threadPoolTaskExecutor);
|
|
|
}
|
|
|
|
|
|
@@ -156,160 +140,156 @@ public class WarnService {
|
|
|
/**
|
|
|
* 根据设备id获取策略,可能有多个
|
|
|
*
|
|
|
- * @param devId
|
|
|
+ * @param devBid
|
|
|
* @return
|
|
|
*/
|
|
|
- List<Object> getConfigByDevId(String devId) {
|
|
|
+ List<Object> getConfigByDevId(String devBid) {
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 【气象站】比较该设备上报的要素和配置是否达到预警条件
|
|
|
- *
|
|
|
- * @param devId 设备id
|
|
|
- * @param config 对应的配置
|
|
|
- * @param jsonObject 上报的数据
|
|
|
- */
|
|
|
- WarnResult comparableQxzReportData(IotDevice iotDevice, List<WarnConfigInfo> configList, JSONObject jsonObject) {
|
|
|
+ private Map<String, String> getQxzCurrentValueMap(IotDevice iotDevice, JSONArray jsonArray) {
|
|
|
String devCode = iotDevice.getDevCode();
|
|
|
- String devBid = iotDevice.getDevBid();
|
|
|
- WarnConfigInfo configInfo = configList.get(0);
|
|
|
- String wcCondition = configInfo.getWcCondition();
|
|
|
- Map<String, String> elementMap = new HashMap<>();
|
|
|
- JSONArray jsonArray = JSONArray.from(jsonObject);
|
|
|
BigDecimal errorValue = new BigDecimal("-99");
|
|
|
+ Map<String, String> currentValueMap = new HashMap<>();
|
|
|
for(Object obj : jsonArray) {
|
|
|
- JSONObject ob = (JSONObject) obj;
|
|
|
+ JSONObject ob;
|
|
|
+ try {
|
|
|
+ ob = JSONObject.from(obj);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[设备告警] 数据类型转换异常,devCode:{}, obj:{}", devCode, obj);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
String eNum = ob.getString("eNum");
|
|
|
String eName = ob.getString("eName");
|
|
|
String eKey = ob.getString("eKey");
|
|
|
String address = ElementFormatUtil.getAddress(eName, eKey);
|
|
|
String key = address + eNum;
|
|
|
+
|
|
|
BigDecimal eValue = null;
|
|
|
- try{
|
|
|
+ try {
|
|
|
eValue = ob.getBigDecimal("eValue");
|
|
|
- if(eValue.compareTo(errorValue) <= 0){
|
|
|
+ if (eValue.compareTo(errorValue) <= 0) {
|
|
|
throw new Exception();
|
|
|
}
|
|
|
- }catch (Exception e){
|
|
|
- log.error("[设备告警] 数据类型转换异常,devId:{}, eNum:{}, eName:{}, eKey:{}", devCode, eNum, eName, eKey);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[设备告警] 数据类型转换异常,devCode:{}, eNum:{}, eName:{}, eKey:{}", devCode, eNum, eName, eKey);
|
|
|
continue;
|
|
|
}
|
|
|
- elementMap.put(key, String.valueOf(eValue));
|
|
|
+ currentValueMap.put(key, String.valueOf(eValue));
|
|
|
}
|
|
|
+ return currentValueMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【气象站】检测该设备上报的要素和配置是否达到预警条件
|
|
|
+ * @param config
|
|
|
+ * @param factorMap
|
|
|
+ * @param currentValueMap
|
|
|
+ * @param devCode
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<String, Object> getQxzCondition(WarnConfigInfo config, Map<String, IotDevicefactor> factorMap,
|
|
|
+ Map<String, String> currentValueMap, String devCode) {
|
|
|
+ String targetValue = config.getWiValue();
|
|
|
+ String wiAddress = config.getWiAddress();
|
|
|
+ String wiCode = config.getWiCode();
|
|
|
+ String key = wiAddress + wiCode;
|
|
|
+
|
|
|
+ if (!currentValueMap.containsKey(key)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String wiName = config.getWiName();
|
|
|
+ if(factorMap.containsKey(key)){
|
|
|
+ IotDevicefactor iotDevicefactor = factorMap.get(key);
|
|
|
+ String displayname = iotDevicefactor.getDfDisplayname();
|
|
|
+ if(StringUtils.isNotEmpty(displayname)){
|
|
|
+ wiName = displayname;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String wiUnit = config.getWiUnit();
|
|
|
+
|
|
|
+ String currentValue = currentValueMap.get(key);
|
|
|
+ String expression = config.getWiExpression();
|
|
|
+ EnumWarnRuleOp warnRuleOp = EnumWarnRuleOp.findEnumByCode(expression);
|
|
|
+ String ruleName = warnRuleOp.getName();
|
|
|
+
|
|
|
+ boolean tempSuccess = CompareUtil.comp(currentValue, expression, targetValue);
|
|
|
+ String warnMessage = WarnMessageBuilderUtil.buildQxzWarningMessage(
|
|
|
+ "气象站设备", devCode, wiName, currentValue, wiUnit, ruleName, targetValue);
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("warnMessage", warnMessage);
|
|
|
+ map.put("tempSuccess", tempSuccess);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【气象站】比较该设备上报的要素和配置是否达到预警条件
|
|
|
+ *
|
|
|
+ * @param devBid 设备id
|
|
|
+ * @param config 对应的配置
|
|
|
+ * @param jsonObject 上报的数据
|
|
|
+ */
|
|
|
+ private WarnResult comparableQxzReportData(IotDevice iotDevice, List<WarnConfigInfo> configList, JSONObject jsonObject) {
|
|
|
+ String devCode = iotDevice.getDevCode();
|
|
|
+ String devBid = iotDevice.getDevBid();
|
|
|
+ WarnConfigInfo configInfo = configList.get(0);
|
|
|
+ String wcCondition = configInfo.getWcCondition();
|
|
|
+ JSONArray jsonArray = jsonObject.getJSONArray("data");
|
|
|
+
|
|
|
+ Map<String, String> currentValueMap = getQxzCurrentValueMap(iotDevice, jsonArray);
|
|
|
+
|
|
|
+ IotWarnconfig iotWarnconfig = new IotWarnconfig();
|
|
|
+ BeanUtils.copyProperties(configInfo, iotWarnconfig);
|
|
|
+
|
|
|
+ WarnResult warnResult = new WarnResult();
|
|
|
+ warnResult.setMessageId(warnResult.getUUId());
|
|
|
+ warnResult.setDevId(devBid);
|
|
|
+ warnResult.setTid(iotDevice.getTid());
|
|
|
+ warnResult.setConfigId(configInfo.getWcBid());
|
|
|
+ warnResult.setReportData(jsonObject.toJSONString());
|
|
|
+ warnResult.setTargetReCount(configInfo.getWcRepeatnum());
|
|
|
+ warnResult.setDevtypeBid(iotDevice.getDevtypeBid());
|
|
|
+ warnResult.setConfig(iotWarnconfig);
|
|
|
+ warnResult.setTriggered(false);
|
|
|
+
|
|
|
Map<String, IotDevicefactor> factorMap = getDevicefactorMap(devBid);
|
|
|
String message = null;
|
|
|
StringBuilder messageBuilder = new StringBuilder();
|
|
|
for (WarnConfigInfo config : configList) {
|
|
|
- String currentValue = config.getWiValue();
|
|
|
- String wiAddress = config.getWiAddress();
|
|
|
- String wiCode = config.getWiCode();
|
|
|
- String key = wiAddress + wiCode;
|
|
|
- String wiName = config.getWiName();
|
|
|
- if(factorMap.containsKey(key)){
|
|
|
- IotDevicefactor iotDevicefactor = factorMap.get(key);
|
|
|
- String displayname = iotDevicefactor.getDfDisplayname();
|
|
|
- if(StringUtils.isNotEmpty(displayname)){
|
|
|
- wiName = displayname;
|
|
|
- }
|
|
|
- }
|
|
|
- String wiUnit = config.getWiUnit();
|
|
|
- if (!elementMap.containsKey(key)) {
|
|
|
+ Map<String, Object> conditionResult = getQxzCondition(config, factorMap, currentValueMap, devCode);
|
|
|
+ if (conditionResult == null) {
|
|
|
continue;
|
|
|
}
|
|
|
- String targetValue = elementMap.get(key);
|
|
|
- String expression = config.getWiExpression();
|
|
|
- EnumWarnRuleOp warnRuleOp = EnumWarnRuleOp.findEnumByCode(expression);
|
|
|
- String ruleName = warnRuleOp.getName();
|
|
|
- String warnMessage = WarnMessageBuilderUtil.buildQxzWarningMessage(
|
|
|
- "气象站设备", devCode, wiName, currentValue, wiUnit, ruleName, targetValue);
|
|
|
-
|
|
|
- boolean tempSuccess = CompareUtil.comp(currentValue, expression, targetValue);
|
|
|
+ String warnMessage = (String) conditionResult.get("warnMessage");
|
|
|
+ boolean tempSuccess = (boolean) conditionResult.get("tempSuccess");
|
|
|
+
|
|
|
if ("0".equals(wcCondition)) {
|
|
|
if (tempSuccess) {
|
|
|
message = warnMessage;
|
|
|
- return new WarnResult(true, message);
|
|
|
+ warnResult.setMessage(message);
|
|
|
+ warnResult.setTriggered(true);
|
|
|
+ return warnResult;
|
|
|
}
|
|
|
} else {
|
|
|
if (!tempSuccess) {
|
|
|
- return new WarnResult(false, null);
|
|
|
+ return warnResult;
|
|
|
}
|
|
|
messageBuilder.append(warnMessage);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(messageBuilder.length() > 0){
|
|
|
- return new WarnResult(true, messageBuilder.toString());
|
|
|
+ warnResult.setMessage(messageBuilder.toString());
|
|
|
+ warnResult.setTriggered(true);
|
|
|
+ return warnResult;
|
|
|
}
|
|
|
- return new WarnResult(false, null);
|
|
|
+ return warnResult;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 处理单一指标是否达到预警条件。
|
|
|
- *
|
|
|
- * @param devId 设备ID
|
|
|
- * @param config 配置对象
|
|
|
- * @param jsonObject 上报的数据
|
|
|
- * @return WarnResult 包含是否触发告警的信息
|
|
|
- */
|
|
|
-// public WarnResult comparableQxzSingleIndicator(String devId, Object config, JSONObject jsonObject) {
|
|
|
-// //从config取出需要匹配的要素列表 todo
|
|
|
-// List<Object> items = new ArrayList<>();
|
|
|
-// //循环要素,一个个对比
|
|
|
-// for (Object item : items) {
|
|
|
-// // 取出来需要比较的要素 todo
|
|
|
-// String comparableItem = "item.getField";
|
|
|
-// //要对比的目标值 todo
|
|
|
-// String targetValue = "item.targetValue";
|
|
|
-// //取出来当前的最新值 todo
|
|
|
-// String currentValue = "jsonObject.get[item.getField]";
|
|
|
-// // 字段表达式比较
|
|
|
-// boolean result = CompareUtil.comp(currentValue, EnumWarnRuleOp.EQUAL.getCode(), targetValue);
|
|
|
-// if (result) {
|
|
|
-// String message = WarnMessageBuilderUtil.buildQxzWarningMessage("气象站设备", devId, comparableItem, Double.parseDouble(currentValue), "℃", "超过", targetValue);
|
|
|
-// return new WarnResult(true, message);
|
|
|
-// }
|
|
|
-// }
|
|
|
-// return new WarnResult(false, null);
|
|
|
-// }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理多个指标是否同时满足预警条件。
|
|
|
- *
|
|
|
- * @param devId 设备ID
|
|
|
- * @param config 告警规则
|
|
|
- * @param jsonObject 上报的数据
|
|
|
- * @return WarnResult 包含是否触发告警的信息
|
|
|
- */
|
|
|
-// public WarnResult comparableQxzMultipleIndicators(String devId, Object config, JSONObject jsonObject) {
|
|
|
-// //从config取出需要匹配的要素列表 todo
|
|
|
-// List<Object> items = new ArrayList<>();
|
|
|
-// int successCount = 0;
|
|
|
-// StringBuilder messages = new StringBuilder();
|
|
|
-// //循环要素,一个个对比
|
|
|
-// for (Object item : items) {
|
|
|
-// // 取出来需要比较的要素 todo
|
|
|
-// String comparableItem = "item.getField";
|
|
|
-// //要对比的目标值 todo
|
|
|
-// String targetValue = "item.targetValue";
|
|
|
-// //取出来当前的最新值 todo
|
|
|
-// String currentValue = "jsonObject.get[item.getField]";
|
|
|
-// // 字段表达式比较
|
|
|
-// boolean result = CompareUtil.comp(currentValue, EnumWarnRuleOp.EQUAL.getCode(), targetValue);
|
|
|
-// if (result) {
|
|
|
-// messages.append(WarnMessageBuilderUtil.buildQxzWarningMessage("气象站设备", devId, comparableItem, Double.parseDouble(currentValue), "℃", "超过", targetValue));
|
|
|
-// successCount += 1;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// if (successCount == items.size()) {
|
|
|
-// return new WarnResult(true, messages.toString());
|
|
|
-// } else {
|
|
|
-// return new WarnResult(false, null);
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
* 【墒情站】比较该设备上报的要素和配置是否达到预警条件,暂不实现,预留
|