From c247d56deb4965fd1923a463b0822b54a1b79355 Mon Sep 17 00:00:00 2001 From: yinq Date: Wed, 10 Jul 2024 16:11:10 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20add=E9=87=87=E9=9B=86=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=91=8A=E8=AD=A6=E3=80=81=E8=B6=85=E8=BF=87=E7=94=B5?= =?UTF-8?q?=E9=98=88=E5=80=BC=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EmsBaseMonitorThresholdServiceImpl.java | 4 +- .../service/IEmsRecordAlarmDataService.java | 11 + .../impl/EmsRecordAlarmDataServiceImpl.java | 216 ++++++++++++++++-- os-quartz/pom.xml | 12 +- .../main/java/com/os/quartz/task/EMSTask.java | 39 ++++ 5 files changed, 255 insertions(+), 27 deletions(-) create mode 100644 os-quartz/src/main/java/com/os/quartz/task/EMSTask.java diff --git a/os-ems/src/main/java/com/os/ems/base/service/impl/EmsBaseMonitorThresholdServiceImpl.java b/os-ems/src/main/java/com/os/ems/base/service/impl/EmsBaseMonitorThresholdServiceImpl.java index f19bd2f..3c4ed3a 100644 --- a/os-ems/src/main/java/com/os/ems/base/service/impl/EmsBaseMonitorThresholdServiceImpl.java +++ b/os-ems/src/main/java/com/os/ems/base/service/impl/EmsBaseMonitorThresholdServiceImpl.java @@ -48,11 +48,11 @@ public class EmsBaseMonitorThresholdServiceImpl implements IEmsBaseMonitorThresh public List selectEmsBaseMonitorThresholdList(EmsBaseMonitorThreshold emsBaseMonitorThreshold) { List monitorThresholds = emsBaseMonitorThresholdMapper.selectEmsBaseMonitorThresholdList(emsBaseMonitorThreshold); - if (monitorThresholds.size() == 0 && StringUtils.isNotEmpty(emsBaseMonitorThreshold.getMonitorCode())){ + if (monitorThresholds.size() == 0 && StringUtils.isNotEmpty(emsBaseMonitorThreshold.getMonitorCode())) { EmsBaseMonitorInfo monitorInfo = new EmsBaseMonitorInfo(); monitorInfo.setMonitorCode(emsBaseMonitorThreshold.getMonitorCode()); List monitorInfoList = emsBaseMonitorInfoMapper.selectEmsBaseMonitorInfoList(monitorInfo); - if (monitorInfoList.size() > 0) { + if (monitorInfoList.size() > 0 && monitorInfoList.get(0).getIsAmmeter().equals("1")) { EmsBaseMonitorInfo emsBaseMonitorInfo = monitorInfoList.get(0); emsBaseMonitorThreshold.setCreateBy(SecurityUtils.getUsername()); emsBaseMonitorThreshold.setMonitorType(emsBaseMonitorInfo.getMonitorType()); diff --git a/os-ems/src/main/java/com/os/ems/record/service/IEmsRecordAlarmDataService.java b/os-ems/src/main/java/com/os/ems/record/service/IEmsRecordAlarmDataService.java index c0ee589..b37bba4 100644 --- a/os-ems/src/main/java/com/os/ems/record/service/IEmsRecordAlarmDataService.java +++ b/os-ems/src/main/java/com/os/ems/record/service/IEmsRecordAlarmDataService.java @@ -65,4 +65,15 @@ public interface IEmsRecordAlarmDataService * @return */ public int handleExceptionsAlarmData(Long[] objIds); + + /** + * 采集设备告警定时任务 + */ + public void collectDeviceAlarmsTask(); + + /** + * 超过电阈值定时任务 + */ + public void exceedDnbThresholdAlarmsTask(); + } diff --git a/os-ems/src/main/java/com/os/ems/record/service/impl/EmsRecordAlarmDataServiceImpl.java b/os-ems/src/main/java/com/os/ems/record/service/impl/EmsRecordAlarmDataServiceImpl.java index 3b200fd..2739e88 100644 --- a/os-ems/src/main/java/com/os/ems/record/service/impl/EmsRecordAlarmDataServiceImpl.java +++ b/os-ems/src/main/java/com/os/ems/record/service/impl/EmsRecordAlarmDataServiceImpl.java @@ -1,7 +1,22 @@ package com.os.ems.record.service.impl; +import java.math.BigDecimal; +import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + import com.os.common.utils.DateUtils; +import com.os.common.utils.StringUtils; +import com.os.ems.base.domain.EmsBaseCollectDeviceInfo; +import com.os.ems.base.domain.EmsBaseMonitorThreshold; +import com.os.ems.base.mapper.EmsBaseCollectDeviceInfoMapper; +import com.os.ems.base.mapper.EmsBaseMonitorThresholdMapper; +import com.os.ems.record.domain.EmsRecordAlarmRule; +import com.os.ems.record.domain.EmsRecordDnbInstant; +import com.os.ems.record.mapper.EmsRecordAlarmRuleMapper; +import com.os.ems.record.mapper.EmsRecordDnbInstantMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.os.ems.record.mapper.EmsRecordAlarmDataMapper; @@ -12,92 +27,98 @@ import static com.os.common.utils.SecurityUtils.getUsername; /** * 异常数据记录Service业务层处理 - * + * * @author Yinq * @date 2024-05-15 */ -@Service -public class EmsRecordAlarmDataServiceImpl implements IEmsRecordAlarmDataService -{ +@Service("AlarmDataService") +public class EmsRecordAlarmDataServiceImpl implements IEmsRecordAlarmDataService { @Autowired private EmsRecordAlarmDataMapper emsRecordAlarmDataMapper; + @Autowired + private EmsRecordAlarmRuleMapper emsRecordAlarmRuleMapper; + + @Autowired + private EmsBaseCollectDeviceInfoMapper emsBaseCollectDeviceInfoMapper; + + @Autowired + private EmsRecordDnbInstantMapper emsRecordDnbInstantMapper; + + @Autowired + private EmsBaseMonitorThresholdMapper emsBaseMonitorThresholdMapper; + /** * 查询异常数据记录 - * + * * @param objId 异常数据记录主键 * @return 异常数据记录 */ @Override - public EmsRecordAlarmData selectEmsRecordAlarmDataByObjId(Long objId) - { + public EmsRecordAlarmData selectEmsRecordAlarmDataByObjId(Long objId) { return emsRecordAlarmDataMapper.selectEmsRecordAlarmDataByObjId(objId); } /** * 查询异常数据记录列表 - * + * * @param emsRecordAlarmData 异常数据记录 * @return 异常数据记录 */ @Override - public List selectEmsRecordAlarmDataList(EmsRecordAlarmData emsRecordAlarmData) - { + public List selectEmsRecordAlarmDataList(EmsRecordAlarmData emsRecordAlarmData) { return emsRecordAlarmDataMapper.selectEmsRecordAlarmDataList(emsRecordAlarmData); } /** * 新增异常数据记录 - * + * * @param emsRecordAlarmData 异常数据记录 * @return 结果 */ @Override - public int insertEmsRecordAlarmData(EmsRecordAlarmData emsRecordAlarmData) - { + public int insertEmsRecordAlarmData(EmsRecordAlarmData emsRecordAlarmData) { emsRecordAlarmData.setCreateTime(DateUtils.getNowDate()); return emsRecordAlarmDataMapper.insertEmsRecordAlarmData(emsRecordAlarmData); } /** * 修改异常数据记录 - * + * * @param emsRecordAlarmData 异常数据记录 * @return 结果 */ @Override - public int updateEmsRecordAlarmData(EmsRecordAlarmData emsRecordAlarmData) - { + public int updateEmsRecordAlarmData(EmsRecordAlarmData emsRecordAlarmData) { emsRecordAlarmData.setUpdateTime(DateUtils.getNowDate()); return emsRecordAlarmDataMapper.updateEmsRecordAlarmData(emsRecordAlarmData); } /** * 批量删除异常数据记录 - * + * * @param objIds 需要删除的异常数据记录主键 * @return 结果 */ @Override - public int deleteEmsRecordAlarmDataByObjIds(Long[] objIds) - { + public int deleteEmsRecordAlarmDataByObjIds(Long[] objIds) { return emsRecordAlarmDataMapper.deleteEmsRecordAlarmDataByObjIds(objIds); } /** * 删除异常数据记录信息 - * + * * @param objId 异常数据记录主键 * @return 结果 */ @Override - public int deleteEmsRecordAlarmDataByObjId(Long objId) - { + public int deleteEmsRecordAlarmDataByObjId(Long objId) { return emsRecordAlarmDataMapper.deleteEmsRecordAlarmDataByObjId(objId); } /** * 处理异常数据 + * * @param objIds * @return */ @@ -113,4 +134,155 @@ public class EmsRecordAlarmDataServiceImpl implements IEmsRecordAlarmDataService } return 1; } + + /** + * 采集设备告警定时任务 + */ + public void collectDeviceAlarmsTask() { + long minuteValue = 1000 * 60; + Date date = new Date(); + EmsRecordAlarmRule alarmRule = new EmsRecordAlarmRule(); + alarmRule.setTriggerRule(1L); + List alarmRules = emsRecordAlarmRuleMapper.selectEmsRecordAlarmRuleList(alarmRule); + if (alarmRules.size() > 0) { + minuteValue = alarmRules.get(0).getDeviceOfflineTime() * minuteValue; + } else { + return; + } + + EmsBaseCollectDeviceInfo collectDeviceInfo = new EmsBaseCollectDeviceInfo(); + collectDeviceInfo.setIsFlag("1"); + List deviceInfoList = emsBaseCollectDeviceInfoMapper.selectEmsBaseCollectDeviceInfoList(collectDeviceInfo); + + if (deviceInfoList.size() > 0) { + EmsRecordAlarmData recordAlarmData = new EmsRecordAlarmData(); + recordAlarmData.setAlarmType(1L); + recordAlarmData.setAlarmStatus(1L); + List alarmDataList = emsRecordAlarmDataMapper.selectEmsRecordAlarmDataList(recordAlarmData); + List collectDeviceIdList = alarmDataList.stream().map(EmsRecordAlarmData::getCollectDeviceId).collect(Collectors.toList()); + for (EmsBaseCollectDeviceInfo deviceInfo : deviceInfoList) { + if (collectDeviceIdList.contains(deviceInfo.getCollectDeviceId())) { + continue; + } + if ((deviceInfo.getUpdateTime().getTime() + minuteValue) < date.getTime()) { + EmsRecordAlarmData alarmData = new EmsRecordAlarmData(); + alarmData.setCollectDeviceId(deviceInfo.getCollectDeviceId()); + alarmData.setCollectTime(new Date()); + alarmData.setAlarmType(1L); + alarmData.setAlarmStatus(1L); + alarmData.setAlarmData("设备离线已超过" + alarmRules.get(0).getDeviceOfflineTime() + "分钟"); + this.insertEmsRecordAlarmData(alarmData); + } + } + } + } + + /** + * 超过电阈值定时任务 + */ + public void exceedDnbThresholdAlarmsTask() { + EmsBaseMonitorThreshold monitorThreshold = new EmsBaseMonitorThreshold(); + monitorThreshold.setMonitorType(2L); + List thresholdList = emsBaseMonitorThresholdMapper.selectEmsBaseMonitorThresholdList(monitorThreshold); + + EmsRecordDnbInstant dnbInstant = new EmsRecordDnbInstant(); + List dnbInstantList = emsRecordDnbInstantMapper.selectEmsRecordDnbInstantList(dnbInstant); + Map thresholdMap = compareThresholdAndRecord(thresholdList, dnbInstantList); + //防止多次存入异常数据 + EmsRecordAlarmData recordAlarmData = new EmsRecordAlarmData(); + recordAlarmData.setAlarmType(0L); + recordAlarmData.setAlarmStatus(1L); + List alarmDataList = emsRecordAlarmDataMapper.selectEmsRecordAlarmDataList(recordAlarmData); + List monitorIdList = alarmDataList.stream().map(EmsRecordAlarmData::getMonitorId).collect(Collectors.toList()); + for (String monitorId : thresholdMap.keySet()) { + if (monitorIdList.contains(monitorId)) { + continue; + } + EmsRecordAlarmData alarmData = new EmsRecordAlarmData(); + alarmData.setMonitorId(monitorId); + alarmData.setCollectTime(new Date()); + alarmData.setAlarmType(0L); + alarmData.setAlarmStatus(1L); + alarmData.setAlarmData(thresholdMap.get(monitorId)); + this.insertEmsRecordAlarmData(alarmData); + } + + } + + /** + * 超过电阈值 对比记录 + * + * @param baseDnbThresholds + * @param recordDnbInstants + * @return + */ + public Map compareThresholdAndRecord(List baseDnbThresholds, List recordDnbInstants) { + Map resultMap = new HashMap<>(); + for (EmsRecordDnbInstant recordDnbInstant : recordDnbInstants) { + String monitorCode = recordDnbInstant.getMonitorCode(); + EmsBaseMonitorThreshold baseDnbThreshold = null; + for (EmsBaseMonitorThreshold threshold : baseDnbThresholds) { + if (threshold.getMonitorCode().equals(monitorCode)) { + baseDnbThreshold = threshold; + break; + } + } + if (baseDnbThreshold != null) { + String reason = compare(recordDnbInstant, baseDnbThreshold); + if (reason != null) { + resultMap.put(monitorCode, reason); + } + } + } + return resultMap; + } + + private String compare(EmsRecordDnbInstant recordDnbInstant, EmsBaseMonitorThreshold baseDnbThreshold) { + if (StringUtils.isNull(recordDnbInstant.getIA())) { + return null; + } + BigDecimal zero = new BigDecimal("0.00"); + if (recordDnbInstant.getVA().compareTo(zero) == 0 || recordDnbInstant.getVB().compareTo(zero) == 0 + || recordDnbInstant.getVC().compareTo(zero) == 0) { + return "缺项报警"; + } + if (recordDnbInstant.getIA().compareTo(baseDnbThreshold.getiAMax()) > 0) { + return "超过A相电流最大值"; + } + if (recordDnbInstant.getIB().compareTo(baseDnbThreshold.getiBMax()) > 0) { + return "超过B相电流最大值"; + } + if (recordDnbInstant.getIC().compareTo(baseDnbThreshold.getiCMax()) > 0) { + return "超过C相电流最大值"; + } + if (recordDnbInstant.getVA().compareTo(baseDnbThreshold.getvAMax()) > 0) { + return "超过A相电压最大值"; + } + if (recordDnbInstant.getVB().compareTo(baseDnbThreshold.getvBMax()) > 0) { + return "超过B相电压最大值"; + } + if (recordDnbInstant.getVC().compareTo(baseDnbThreshold.getvCMax()) > 0) { + return "超过C相电压最大值"; + } + if (recordDnbInstant.getIA().compareTo(baseDnbThreshold.getiAMin()) < 0) { + return "小于A相电流最小值"; + } + if (recordDnbInstant.getIB().compareTo(baseDnbThreshold.getiBMin()) < 0) { + return "小于B相电流最小值"; + } + if (recordDnbInstant.getIC().compareTo(baseDnbThreshold.getiCMin()) < 0) { + return "小于C相电流最小值"; + } + if (recordDnbInstant.getVA().compareTo(baseDnbThreshold.getvAMin()) < 0) { + return "小于A相电压最小值"; + } + if (recordDnbInstant.getVB().compareTo(baseDnbThreshold.getvBMin()) < 0) { + return "小于B相电压最小值"; + } + if (recordDnbInstant.getVC().compareTo(baseDnbThreshold.getvCMin()) < 0) { + return "小于C相电压最小值"; + } + return null; + } + } diff --git a/os-quartz/pom.xml b/os-quartz/pom.xml index 0633a31..c58750a 100644 --- a/os-quartz/pom.xml +++ b/os-quartz/pom.xml @@ -30,15 +30,21 @@ + + + + + + com.haiwei - haiwei-common + haiwei-mes - + com.haiwei - haiwei-mes + haiwei-ems diff --git a/os-quartz/src/main/java/com/os/quartz/task/EMSTask.java b/os-quartz/src/main/java/com/os/quartz/task/EMSTask.java new file mode 100644 index 0000000..d021cf6 --- /dev/null +++ b/os-quartz/src/main/java/com/os/quartz/task/EMSTask.java @@ -0,0 +1,39 @@ +package com.os.quartz.task; + + +import com.os.ems.record.service.IEmsRecordAlarmDataService; +import com.os.mes.api.domain.ERPParamDto; +import com.os.mes.api.service.IERPPortService; +import com.os.mes.api.utils.ERPConstants; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + + +/** + * @Author YinQ + * @create 2023-10-08 9:11 + */ +@Component("EMSTask") +public class EMSTask { + + @Autowired + private IEmsRecordAlarmDataService emsRecordAlarmDataService; + + /** + * 采集设备告警定时任务 + */ + public void collectDeviceAlarmsTask() { + emsRecordAlarmDataService.collectDeviceAlarmsTask(); + } + + /** + * 超过电阈值定时任务 + */ + public void exceedDnbThresholdAlarmsTask() { + emsRecordAlarmDataService.exceedDnbThresholdAlarmsTask(); + } + +}