|
|
@@ -0,0 +1,204 @@
|
|
|
+package com.yunfeiyun.agmp.iots.warn.service;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.yunfeiyun.agmp.iot.common.enums.EnumWarnRuleOp;
|
|
|
+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 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 预警核心服务类
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class WarnService {
|
|
|
+
|
|
|
+ @Resource(name = "threadPoolTaskExecutor")
|
|
|
+ private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统一处理上报数据
|
|
|
+ *
|
|
|
+ * @param devId 设备id
|
|
|
+ * @param data 上报的数据对象
|
|
|
+ */
|
|
|
+ public void processWarningReportData(String devId, JSONObject data) {
|
|
|
+ //转异步处理
|
|
|
+ processWarningReportDataSyn(devId, data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统一处理上报数据:异步处理
|
|
|
+ *
|
|
|
+ * @param devId 设备id
|
|
|
+ * @param data 上报的数据对象
|
|
|
+ */
|
|
|
+ private void processWarningReportDataSyn(String devId, JSONObject data) {
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ String devClass = "设备大类"; // todo
|
|
|
+ // 获取该设备有哪些告警配置
|
|
|
+ List<Object> objects = getConfigByDevId(devId);
|
|
|
+ //配置一个个检查
|
|
|
+ for (Object config : objects) {
|
|
|
+ WarnResult warnResult = null;
|
|
|
+ switch (devClass) {
|
|
|
+ case "气象站": {
|
|
|
+ warnResult = comparableQxzReportData(devId, config, data);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case "墒情站": {
|
|
|
+ warnResult = comparableSqzReportData(devId, config, data);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case "病虫害": {
|
|
|
+ warnResult = comparableBchReportData(devId, config, data);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (warnResult != null) {
|
|
|
+ handleWarnRecord(warnResult);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, threadPoolTaskExecutor);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 基于预警结果统一处理入库操作
|
|
|
+ *
|
|
|
+ * @param warnResult
|
|
|
+ */
|
|
|
+ void handleWarnRecord(WarnResult warnResult) {
|
|
|
+ if (warnResult.isTriggered()) {
|
|
|
+ //进行预警重复次数处理机制
|
|
|
+ //入库 todo
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据设备id获取策略,可能有多个
|
|
|
+ *
|
|
|
+ * @param devId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ List<Object> getConfigByDevId(String devId) {
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【气象站】比较该设备上报的要素和配置是否达到预警条件
|
|
|
+ *
|
|
|
+ * @param devId 设备id
|
|
|
+ * @param config 对应的配置
|
|
|
+ * @param jsonObject 上报的数据
|
|
|
+ */
|
|
|
+ WarnResult comparableQxzReportData(String devId, Object config, JSONObject jsonObject) {
|
|
|
+ WarnResult warnResult = null;
|
|
|
+ if ("指标类型" == "多指标满足") {
|
|
|
+ warnResult = comparableQxzMultipleIndicators(devId, config, jsonObject);
|
|
|
+ } else {
|
|
|
+ warnResult = comparableQxzSingleIndicator(devId, config, jsonObject);
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【墒情站】比较该设备上报的要素和配置是否达到预警条件,暂不实现,预留
|
|
|
+ *
|
|
|
+ * @param devId
|
|
|
+ * @param config
|
|
|
+ * @param jsonObject
|
|
|
+ */
|
|
|
+ WarnResult comparableSqzReportData(String devId, Object config, JSONObject jsonObject) {
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 【病虫害】比较该设备上报的要素和配置是否达到预警条件,暂不实现,预留
|
|
|
+ *
|
|
|
+ * @param devId
|
|
|
+ * @param config
|
|
|
+ * @param jsonObject
|
|
|
+ */
|
|
|
+ WarnResult comparableBchReportData(String devId, Object config, JSONObject jsonObject) {
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|