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.*;
|
import java.util.concurrent.Executors;
|
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* 旋流器分选控制
|
* ZX2
|
* 5 0/1 * * * ?
|
*/
|
@Component("modelSeparationTask")
|
public class ModelSeparationTask implements ITask {
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
private final String MODEL_CODE_3318 = "separation_control3318";
|
|
private final String MODEL_CODE_3319 = "separation_control3319";
|
|
private final String mParamCode = "isDev";
|
|
private final List<String> MODEL_CODE_LIST = new ArrayList<>();
|
|
private final String ALM_POINT = WatchConstant.CONTROLLER_ALM_2;
|
|
@Resource
|
private StModelService stModelService;
|
|
@Resource
|
private ModelHandler modelHandler;
|
|
@Resource
|
private IFeignDataApi feignDataApi;
|
|
@Resource
|
private ConfigApi configApi;
|
|
@Resource
|
private StModelOutService stModelOutService;
|
|
@Resource
|
private WatchDog watchDog;
|
|
private final String MV_VALVE_JJ = "mv_valve_jj";
|
|
private int writeDelayTime = 20;
|
|
private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
|
ModelSeparationTask() {
|
MODEL_CODE_LIST.add(MODEL_CODE_3318);
|
MODEL_CODE_LIST.add(MODEL_CODE_3319);
|
}
|
|
@Override
|
public void run(String params) {
|
String isDev = configApi.queryConfigByCode(mParamCode);
|
Calendar calendar = Calendar.getInstance();
|
calendar.set(Calendar.MILLISECOND, 0);
|
calendar.add(Calendar.SECOND, -5); // 滞后5s
|
logger.info("ModelSeparationTask定时任务正在执行,参数为:{}", params);
|
if (BusinessConstant.DEV_Y.equals(isDev)) {
|
logger.info("模型采用开发模型");
|
} else {
|
logger.info("模型采用生产模型");
|
}
|
try {
|
if (!BusinessConstant.DEV_Y.equals(isDev)) {
|
// 1、判断通讯是否正常
|
if (!watchDog.isConnect(WatchConstant.ZX2_STATUS)) {
|
logger.info("watchDog.isConnect=false");
|
return;
|
}
|
|
// 2、判断控制器状态
|
if (!watchDog.isUse(WatchConstant.CONTROLLER_ONSTS_2)) {
|
logger.info("将反馈值写入下发值,无扰动切换");
|
MODEL_CODE_LIST.forEach(item -> {
|
this.writeBySetValue(item);
|
});
|
return;
|
}
|
logger.info("控制器启动");
|
}
|
|
// 3、运行模型
|
Map<String, Map<String, Object>> 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<String, Object> 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("ModelSeparationTask运行异常");
|
ex.printStackTrace();
|
}
|
logger.info("ModelSeparationTask运行完成");
|
}
|
|
/**
|
* 数据下发
|
*
|
* @param data
|
* @param modelCode
|
*/
|
private boolean writeToDcs(Map<String, Object> 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_2);
|
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<StModelOutDTO> 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(new BigDecimal(data.get(pointItem.getResultKey()).toString()));
|
feignDataApi.setPointValue(writePointValue);
|
|
if (pointItem.getResultKey().contains(MV_VALVE_JJ)) {
|
executor.schedule(() -> {
|
try {
|
logger.info("20秒后加介阀开度复位0!");
|
FeignWritePointValueDTO fw = new FeignWritePointValueDTO();
|
fw.setPointCode(pointItem.getPointNo());
|
fw.setPointValue(BigDecimal.ZERO);
|
feignDataApi.setPointValue(fw);
|
logger.info("复位完成!");
|
} catch (Exception ex) {
|
logger.info("复位失败!");
|
ex.printStackTrace();
|
}
|
}, writeDelayTime, TimeUnit.SECONDS);
|
}
|
});
|
return true;
|
}
|
|
/**
|
* 无扰模式
|
* 反馈写入下发点位
|
*/
|
private void writeBySetValue(String modelCode) {
|
logger.info("=====writeBySetValue Start=======");
|
List<StModelOutDTO> outPoints = stModelOutService.getOutPoint(modelCode);
|
if (CollectionUtils.isEmpty(outPoints)) {
|
return;
|
}
|
outPoints.forEach(item -> {
|
if (StringUtils.isNotBlank(item.getPointNo()) && StringUtils.isNotBlank(item.getDisturbancePointNo())) {
|
List<String> points = new ArrayList<>();
|
points.add(item.getDisturbancePointNo());
|
Map<String, Object> value = feignDataApi.getCurrentValue(points);
|
FeignWritePointValueDTO writePointValue = new FeignWritePointValueDTO();
|
writePointValue.setPointCode(item.getPointNo());
|
writePointValue.setPointValue(value.get(item.getDisturbancePointNo()));
|
feignDataApi.setPointValue(writePointValue);
|
}
|
});
|
}
|
}
|