package com.iailab.module.job.task; import com.alibaba.fastjson.JSONObject; import com.iailab.common.constant.BusinessConstant; import com.iailab.module.data.dto.FeignWritePointValueDTO; import com.iailab.module.data.api.IFeignDataApi; 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.util.*; /** * 智能配仓 * ZX1 * warehouse_allocation * 5 0/1 * * * ? * */ @Component("modelWarehouseTask") public class ModelWarehouseTask implements ITask { private Logger logger = LoggerFactory.getLogger(getClass()); private final String MODEL_CODE = "warehouse_allocation"; private final String mParamCode = "isDev"; private final List MODEL_CODE_LIST = new ArrayList<>(); private final String ALM_POINT = WatchConstant.CONTROLLER_ALM_7; @Resource private StModelService stModelService; @Resource private ModelHandler modelHandler; @Resource private IFeignDataApi feignDataApi; @Resource private ConfigApi configApi; @Resource private StModelOutService stModelOutService; @Resource private WatchDog watchDog; ModelWarehouseTask() { 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("ModelWarehouseTask定时任务正在执行,参数为:{}", 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; } // 2、判断控制器状态 if (!watchDog.isUse(WatchConstant.CONTROLLER_ONSTS_7)) { 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("ModelWarehouseTask运行异常"); ex.printStackTrace(); } logger.info("ModelWarehouseTask运行完成"); } /** * 数据下发 * * @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_7); 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(); writePointValue.setPointCode(pointItem.getPointNo()); writePointValue.setPointValue(data.get(pointItem.getResultKey())); feignDataApi.setPointValue(writePointValue); }); return true; } /** * 无扰模式 * 反馈写入下发点位 */ private void writeBySetValue(String modelCode) { logger.info("=====writeBySetValue Start======="); 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())); feignDataApi.setPointValue(writePointValue); } }); } }