沙钢智慧能源系统后端代码
潘志宝
2025-03-19 9028d0694549df46adf4cca71de823b5b35fc4c8
shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java
@@ -1,5 +1,6 @@
package com.iailab.module.shasteel.mq.consumer;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.module.model.api.mcs.McsApi;
@@ -45,13 +46,16 @@
    // 最早调度时间
    private static Date earliestScheduleTime = null;
    private static Date lastScheduleTime = null;
    /**
     * 监听报警信息,执行调度
     * 监听报警信息,执行煤气调度
     *
     * @param message
     */
    @RabbitListener(queues = QueueModelAlarmConfig.QUEUE_NAME)
    public void listen(Message message) {
        log.info("监听报警信息,执行煤气调度");
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        log.info("routingKey:" + routingKey);
        String messageBody = new String(message.getBody());
@@ -65,29 +69,41 @@
            log.info("过滤旧消息");
            return;
        }
        // 判断predictTime 是否大于outTime,否则不执行调度
        if (earliestScheduleTime != null && predictTime.compareTo(earliestScheduleTime) < 0) {
        if (!ModelPredictFinishConsumer.finishModuleMap.containsKey(predictTime.getTime())) {
            log.info("finishModuleMap not containsKey:" + predictTime.getTime());
            return;
        }
        List<String> finishModuleList = ModelPredictFinishConsumer.finishModuleMap.get(predictTime.getTime());
        if (CollectionUtils.isEmpty(finishModuleList)) {
            log.info("finishModuleList is empty");
            return;
        }
        if (!finishModuleList.contains(CommonConstant.NET_BFG) || !finishModuleList.contains(CommonConstant.NET_COG) ||
                !finishModuleList.contains(CommonConstant.NET_LDG)) {
            log.info("finishModuleList:" + JSONArray.toJSONString(finishModuleList));
            log.info("等待全部预测完成");
            return;
        }
        log.info("已全部预测完成,清空finishModuleMap");
        ModelPredictFinishConsumer.finishModuleMap.clear();
        List<AlarmMessageRespDTO> alarmList = JSONObject.parseArray(messageJson.get("alarmList").toString(),AlarmMessageRespDTO.class);
        // 取出最小outTime
        Date minOutTime = alarmList.stream().filter(e -> e.getOutTime() != null).map(AlarmMessageRespDTO::getOutTime).min(Date::compareTo).get();
        earliestScheduleTime = minOutTime;
        // 查找需要执行的调度方案
        log.info("查找需要执行的调度方案");
        List<StScheduleSchemeDTO> scheduleSchemeList = mcsApi.listScheduleScheme(ScheduleTriggerMethodEnum.EVENT.getCode(), routingKey);
        if (CollectionUtils.isEmpty(scheduleSchemeList)) {
            log.info("routingKey:" + routingKey + ",调度方案为空。");
            return;
        }
        for (StScheduleSchemeDTO stScheduleSchemeDTO : scheduleSchemeList) {
            String runKey = "GasSchedule_" +  stScheduleSchemeDTO.getCode() + "_" + predictTime.getTime();
            if (lastScheduleTime != null && stScheduleSchemeDTO.getAdjustRate() != null && stScheduleSchemeDTO.getAdjustRate() > 0 &&
                    (predictTime.getTime() - lastScheduleTime.getTime() > 1000 * 60 * stScheduleSchemeDTO.getAdjustRate().longValue()) ) {
                log.info("调整速率有效期内,跳过执行");
                continue;
            }
            String runKey = "GasSchedule_" + stScheduleSchemeDTO.getCode() + "_" + predictTime.getTime();
            log.info("runKey:" + runKey);
            log.info("lastRunGasSchedule:" + lastRunGasSchedule);
            if (lastRunGasSchedule.equals(runKey)) {
                log.info("lastRunGasSchedule:" + lastRunGasSchedule + "已执行完成");
                continue;
            }
            lastRunGasSchedule = runKey;
@@ -128,7 +144,7 @@
                    Boolean simAdjustFlag = mdkApi.predictSimAdjust(simAdjustReqDTO);
                    if (simAdjustFlag) {
                        log.info("模拟调整成功,ScheduleCode:" + CommonConstant.GAS_MODEL_CODE);
                    }else {
                    } else {
                        log.error("模拟调整失败,simAdjustReqDTO:" + simAdjustReqDTO);
                    }
@@ -218,6 +234,8 @@
                    break;
            }
        }
        lastScheduleTime = predictTime;
    }
    private void saveScheduleSuggest(String title, Object content, String scheduleObj, Date scheduleTime) {