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
|
* 5 0/1 * * * ?
|
*/
|
@Component("modelShallowTask")
|
public class ModelShallowTask implements ITask {
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
private final String MODEL_CODE_307 = "shallow_groove_separation_control307";
|
|
private final String MODEL_CODE_308 = "shallow_groove_separation_control308";
|
|
private final String mParamCode = "isDev";
|
|
private final List<String> MODEL_CODE_LIST = new ArrayList<>();
|
|
// 浅槽分选故障需切换手动
|
private final String ALM_POINT = WatchConstant.CONTROLLER_ALM_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;
|
|
ModelShallowTask() {
|
MODEL_CODE_LIST.add(MODEL_CODE_307);
|
MODEL_CODE_LIST.add(MODEL_CODE_308);
|
}
|
|
@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("ModelShallowTask定时任务正在执行,参数为:{}", 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_1)) {
|
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("ModelShallowTask运行异常");
|
ex.printStackTrace();
|
}
|
logger.info("ModelShallowTask运行完成");
|
}
|
|
/**
|
* 数据下发
|
*
|
* @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_1);
|
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);
|
});
|
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);
|
}
|
});
|
}
|
}
|