package com.iailab.module.job.task; import com.alibaba.fastjson.JSONObject; import com.iailab.common.constant.BusinessConstant; import com.iailab.module.data.api.IFeignDataApi; import com.iailab.module.data.dto.FeignWritePointValueDTO; import com.iailab.module.infra.api.config.ConfigApi; import com.iailab.module.mcs.dto.StModelDTO; import com.iailab.module.mcs.dto.StModelOutDTO; import com.iailab.module.mcs.service.StModelOutService; import com.iailab.module.mcs.service.StModelService; import com.iailab.module.model.handler.ModelHandler; import com.iailab.module.watch.WatchConstant; import com.iailab.module.watch.WatchDog; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.util.*; /** * 智能压风 * ZX1,ZX2 * blower_control * 5 0/1 * * * ? */ @Component("modelBlowerTask") public class ModelBlowerTask implements ITask { private Logger logger = LoggerFactory.getLogger(getClass()); private final String MODEL_CODE = "blower_control"; private final String mParamCode = "isDev"; private final List MODEL_CODE_LIST = new ArrayList<>(); private final String ALM_POINT = WatchConstant.CONTROLLER_ALM_6; private final String unWrite = "-1"; @Resource private StModelService stModelService; @Resource private ModelHandler modelHandler; @Resource private IFeignDataApi feignDataApi; @Resource private ConfigApi configApi; @Resource private StModelOutService stModelOutService; @Resource private WatchDog watchDog; ModelBlowerTask() { MODEL_CODE_LIST.add(MODEL_CODE); } @Override public void run(String params) { String isDev = configApi.queryConfigByCode(mParamCode); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); logger.info("ModelBlowerTask定时任务正在执行,参数为:{}", params); if (BusinessConstant.DEV_Y.equals(isDev)) { logger.info("模型采用开发模型"); } else { logger.info("模型采用生产模型"); } try { if (!BusinessConstant.DEV_Y.equals(isDev)) { // 1、判断通讯是否正常 if (!watchDog.isConnect(WatchConstant.ZX1_STATUS)) { logger.info("watchDog.isConnect=false"); return; } if (!watchDog.isConnect(WatchConstant.ZX2_STATUS)) { logger.info("watchDog.isConnect=false"); return; } // 2、判断控制器状态 if (!watchDog.isUse(WatchConstant.CONTROLLER_ONSTS_6)) { logger.info("将反馈值写入下发值,无扰动切换"); MODEL_CODE_LIST.forEach(item -> { this.writeBySetValue(item); }); return; } logger.info("控制器启动"); } // 3、运行模型 Map> allCommonResult = new HashMap<>(); for (int i = 0; i < MODEL_CODE_LIST.size(); i++) { StModelDTO stModel = stModelService.getByCode(MODEL_CODE_LIST.get(i)); if (stModel == null) { logger.info("model为空!"); return; } logger.info("运行模型:" + stModel.getModelCode()); Map modelCommonResult = modelHandler.run(stModel.getModelCode(), calendar.getTime()); allCommonResult.put(MODEL_CODE_LIST.get(i), modelCommonResult); } // 4、下发数据 if (!BusinessConstant.DEV_Y.equals(isDev)) { for (int i = 0; i < MODEL_CODE_LIST.size(); i++) { if (!writeToDcs(allCommonResult.get(MODEL_CODE_LIST.get(i)), MODEL_CODE_LIST.get(i))) { throw new Exception(MODEL_CODE_LIST.get(i) + "下发数据异常"); } } } } catch (Exception ex) { logger.error("ModelBlowerTask运行异常"); ex.printStackTrace(); } logger.info("ModelBlowerTask运行完成"); } /** * 数据下发 * * @param data * @param modelCode */ private boolean writeToDcs(Map data, String modelCode) { String code = data.get(BusinessConstant.MODELE_RESULT_STATUS).toString(); if (BusinessConstant.MODEL_R_ERROR.equals(code.substring(0,1))) { logger.warn("code=" + code + ",结果异常,不下发数据!恢复手动"); watchDog.stopControllerSts(WatchConstant.CONTROLLER_ONSTS_6); return false; } else if (BusinessConstant.MODEL_R_WARN.equals(code.substring(0,1))) { logger.warn("code=" + code + ",结果警告"); FeignWritePointValueDTO writeAlm = new FeignWritePointValueDTO(); writeAlm.setPointCode(ALM_POINT); writeAlm.setPointValue(BusinessConstant.STATUS_FALSE); feignDataApi.setPointValue(writeAlm); } logger.info("下发的数据=" + JSONObject.toJSONString(data)); List outPoints = stModelOutService.getOutPoint(modelCode); if (CollectionUtils.isEmpty(outPoints)) { logger.info("outPoints 为空!"); return true; } outPoints.forEach(pointItem -> { FeignWritePointValueDTO writePointValue = new FeignWritePointValueDTO(); if (!unWrite.equals(data.get(pointItem.getResultKey()).toString())) { writePointValue.setPointCode(pointItem.getPointNo()); writePointValue.setPointValue(new BigDecimal(data.get(pointItem.getResultKey()).toString())); feignDataApi.setPointValue(writePointValue); } }); return true; } /** * 无扰模式 * 反馈写入下发点位 */ private void writeBySetValue(String modelCode) { logger.info("=====writeBySetValue Start======="); // 低压输出KEY要特殊处理 String dy_key = "_control"; List outPoints = stModelOutService.getOutPoint(modelCode); if (CollectionUtils.isEmpty(outPoints)) { return; } outPoints.forEach(item -> { if (StringUtils.isNotBlank(item.getPointNo()) && StringUtils.isNotBlank(item.getDisturbancePointNo())) { List points = new ArrayList<>(); points.add(item.getDisturbancePointNo()); Map value = feignDataApi.getCurrentValue(points); FeignWritePointValueDTO writePointValue = new FeignWritePointValueDTO(); writePointValue.setPointCode(item.getPointNo()); writePointValue.setPointValue(value.get(item.getDisturbancePointNo())); if(item.getResultKey().contains(dy_key)) { Object tempValue = value.get(item.getDisturbancePointNo()); // 7或8:启动;10:停止 if (tempValue.toString().equals("7") || tempValue.toString().equals("8")) { writePointValue.setPointValue(new Integer(1)); } else if (tempValue.toString().equals("10")) { writePointValue.setPointValue(new Integer(0)); } } feignDataApi.setPointValue(writePointValue); } }); logger.info("=====智能压风无扰完成======="); } }