|
|
@ -2,12 +2,99 @@ package com.xydl.cac.task;
|
|
|
|
|
|
|
|
|
|
|
|
import com.xydl.cac.comparator.FloatCompare;
|
|
|
|
import com.xydl.cac.comparator.FloatCompare;
|
|
|
|
import com.xydl.cac.comparator.IntCompare;
|
|
|
|
import com.xydl.cac.comparator.IntCompare;
|
|
|
|
|
|
|
|
import com.xydl.cac.entity.ModevTypePoint;
|
|
|
|
|
|
|
|
import com.xydl.cac.entity.NSensor;
|
|
|
|
|
|
|
|
import com.xydl.cac.entity.WarnRule;
|
|
|
|
|
|
|
|
import com.xydl.cac.entity.Warning;
|
|
|
|
|
|
|
|
import com.xydl.cac.entity.constants.Constants;
|
|
|
|
|
|
|
|
import com.xydl.cac.repository.WarnRuleRepository;
|
|
|
|
|
|
|
|
import com.xydl.cac.repository.WarningRepository;
|
|
|
|
|
|
|
|
import com.xydl.cac.service.DataService;
|
|
|
|
|
|
|
|
import com.xydl.cac.service.WarnRuleService;
|
|
|
|
|
|
|
|
import com.xydl.cac.util.DateUtil;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
@Service
|
|
|
|
@Slf4j
|
|
|
|
@Slf4j
|
|
|
|
public class RuleCheckTask {
|
|
|
|
public class RuleCheckTask {
|
|
|
|
public static FloatCompare floatCompare = new FloatCompare();
|
|
|
|
@Resource
|
|
|
|
public static IntCompare intCompare = new IntCompare();
|
|
|
|
WarnRuleService ruleService;
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
|
|
|
WarnRuleRepository ruleRepository;
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
|
|
|
DataService dataService;
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
|
|
|
WarningRepository warningRepository;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Scheduled(initialDelay = 60000, fixedDelay = 60000)
|
|
|
|
|
|
|
|
private void checkAll() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
List<WarnRule> ruleList = ruleService.listAll();
|
|
|
|
|
|
|
|
for (WarnRule rule : ruleList) {
|
|
|
|
|
|
|
|
this.ruleCheck(rule);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
log.error("RuleCheckTask.checkAll error.", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ruleCheck(WarnRule rule) {
|
|
|
|
|
|
|
|
if (Constants.FloatCompare.equalsIgnoreCase(rule.getComparator())) {
|
|
|
|
|
|
|
|
FloatCompare comparat = new FloatCompare();
|
|
|
|
|
|
|
|
comparat.setOperator(rule.getOperator());
|
|
|
|
|
|
|
|
rule.setComparat(comparat);
|
|
|
|
|
|
|
|
} else if (Constants.IntCompare.equalsIgnoreCase(rule.getComparator())) {
|
|
|
|
|
|
|
|
IntCompare comparat = new IntCompare();
|
|
|
|
|
|
|
|
comparat.setOperator(rule.getOperator());
|
|
|
|
|
|
|
|
rule.setComparat(comparat);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.error("该规则(" + rule.getId() + ")缺少比较器");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
NSensor sensor = rule.getNSensor();
|
|
|
|
|
|
|
|
ModevTypePoint typePoint = rule.getTypePoint();
|
|
|
|
|
|
|
|
List<Map<String, Object>> list = dataService.getLatestData(sensor, typePoint, rule.getLastDTime());
|
|
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(list)) {
|
|
|
|
|
|
|
|
Date date = null;
|
|
|
|
|
|
|
|
for (Map<String, Object> map : list) {
|
|
|
|
|
|
|
|
String str = (String) map.get("acquisitionTime");
|
|
|
|
|
|
|
|
date = DateUtil.parse(str);
|
|
|
|
|
|
|
|
Object value = map.get(typePoint.getField());
|
|
|
|
|
|
|
|
boolean r = rule.getComparat().compare(value, rule.getThreshold());
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
|
|
|
|
this.sendWarning(rule, date, value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.setLastDTime(date);
|
|
|
|
|
|
|
|
ruleRepository.save(rule);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
log.error("RuleCheckTask.ruleCheck error.", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void sendWarning(WarnRule rule, Date date, Object value) throws Exception {
|
|
|
|
|
|
|
|
NSensor sensor = rule.getNSensor();
|
|
|
|
|
|
|
|
ModevTypePoint typePoint = rule.getTypePoint();
|
|
|
|
|
|
|
|
Warning warning = Warning.builder()
|
|
|
|
|
|
|
|
.eqmid(sensor.getDevId())
|
|
|
|
|
|
|
|
.threadval(rule.getThreshold())
|
|
|
|
|
|
|
|
.warningValue((String) value)
|
|
|
|
|
|
|
|
.warnLevel(rule.getLevel())
|
|
|
|
|
|
|
|
.warnDesc(sensor.getName() + " " + typePoint.getFieldDesc() + " 告警")
|
|
|
|
|
|
|
|
.processTime(date)
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
warningRepository.save(warning);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|