package com.iailab.module.model.api; import com.alibaba.fastjson.JSON; import com.iailab.module.data.api.point.DataPointApi; import com.iailab.module.data.api.point.dto.ApiPointValueWriteDTO; import com.iailab.module.model.api.mcs.dto.StScheduleModelOutDTO; import com.iailab.module.model.api.mdk.MdkApi; import com.iailab.module.model.api.mdk.dto.*; import com.iailab.module.model.common.enums.IsWriteEnum; import com.iailab.module.model.common.enums.ModelOutResultType; import com.iailab.module.model.common.enums.OutResultType; import com.iailab.module.model.mcs.pre.entity.DmModuleEntity; import com.iailab.module.model.mcs.pre.service.DmModuleService; import com.iailab.module.model.mcs.pre.service.MmPredictItemService; import com.iailab.module.model.mcs.sche.service.StScheduleModelOutService; import com.iailab.module.model.mcs.sche.service.StScheduleRecordService; import com.iailab.module.model.mcs.sche.service.StScheduleSchemeService; import com.iailab.module.model.mdk.predict.PredictModuleHandler; import com.iailab.module.model.mdk.predict.PredictResultHandler; import com.iailab.module.model.mdk.schedule.ScheduleModelHandler; import com.iailab.module.model.mdk.vo.DataValueVO; import com.iailab.module.model.mdk.vo.ItemVO; import com.iailab.module.model.mdk.vo.PredictResultVO; import com.iailab.module.model.mdk.vo.ScheduleResultVO; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RestController; import java.util.*; import java.util.stream.Collectors; import static com.iailab.module.model.common.enums.ModelOutResultType.D; /** * @author PanZhibao * @Description * @createTime 2024年08月26日 */ @Slf4j @RestController @Validated public class MdkApiImpl implements MdkApi { @Autowired private DmModuleService dmModuleService; @Autowired private MmPredictItemService mmPredictItemService; @Autowired private PredictModuleHandler predictModuleHandler; @Autowired private PredictResultHandler predictResultHandler; @Autowired private ScheduleModelHandler scheduleModelHandler; @Autowired private StScheduleRecordService stScheduleRecordService; @Autowired private StScheduleSchemeService stScheduleSchemeService; @Autowired private StScheduleModelOutService stScheduleModelOutService; @Autowired private DataPointApi dataPointApi; /** * 按模块预测 * * @param reqDTO * @return */ @Override public MdkPredictModuleRespDTO predictModule(MdkPredictReqDTO reqDTO) { MdkPredictModuleRespDTO resp = new MdkPredictModuleRespDTO(); resp.setPredictTime(reqDTO.getPredictTime()); resp.setModuleType(reqDTO.getModuleType()); Map predictItemRespMap = new HashMap<>(); try { if (reqDTO.getPredictTime() == null) { throw new Exception("PredictTime不能为空"); } if (reqDTO.getModuleType() == null) { throw new Exception("ModuleType不能为空"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(reqDTO.getPredictTime()); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); reqDTO.setPredictTime(calendar.getTime()); log.info("预测参数:" + JSON.toJSONString(reqDTO)); MdkPredictModuleRespDTO result = new MdkPredictModuleRespDTO(); result.setPredictTime(reqDTO.getPredictTime()); result.setModuleType(reqDTO.getModuleType()); List moduleList = dmModuleService.getModuleByModuleType(reqDTO.getModuleType()); log.info("预测计算开始: " + System.currentTimeMillis()); for (DmModuleEntity module : moduleList) { int intervalTime = 0; if (module.getPredicttime() != null) { intervalTime = (int) (reqDTO.getPredictTime().getTime() - module.getPredicttime().getTime()) / (1000 * 60); } List predictItemList = mmPredictItemService.getByModuleId(module.getId()); Map predictResultMap = new HashMap<>(predictItemList.size()); // 分组,先运行normal预测项,再将结果传递给merge预测项 List normalItems = predictItemList.stream().filter(e -> e.getItemType().equals("NormalItem")).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(normalItems)) { predictModuleHandler.predict(normalItems, reqDTO.getPredictTime(), intervalTime, predictResultMap); List mergeItems = predictItemList.stream().filter(e -> e.getItemType().equals("MergeItem")).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(mergeItems)) { predictModuleHandler.predict(mergeItems, reqDTO.getPredictTime(), intervalTime, predictResultMap); } } // 更新Module时间 dmModuleService.updatePredictTime(module.getId(), reqDTO.getPredictTime()); if (reqDTO.getIsResult() == null || !reqDTO.getIsResult()) { return resp; } for (Map.Entry entry : predictResultMap.entrySet()) { MdkPredictItemRespDTO itemResp = new MdkPredictItemRespDTO(); itemResp.setItemId(entry.getKey()); itemResp.setPredictTime(reqDTO.getPredictTime()); Map> itemPredictData = new HashMap<>(); Map> predictLists = predictResultHandler.convertToPredictData2(entry.getValue()); for (Map.Entry> dataListEntry : predictLists.entrySet()) { List predictData = dataListEntry.getValue().stream().map(t -> { MdkPredictDataDTO dto1 = new MdkPredictDataDTO(); dto1.setDataTime(t.getDataTime()); dto1.setDataValue(t.getDataValue()); return dto1; }).collect(Collectors.toList()); itemPredictData.put(dataListEntry.getKey(), predictData); } itemResp.setPredictData(itemPredictData); predictItemRespMap.put(entry.getKey(), itemResp); } } log.info("预测计算结束: " + System.currentTimeMillis()); } catch (Exception ex) { ex.printStackTrace(); return resp; } resp.setPredictItemRespMap(predictItemRespMap); return resp; } /** * 单个预测 * * @param reqDTO * @return */ @Override public MdkPredictItemRespDTO predictItem(MdkPredictReqDTO reqDTO) { MdkPredictItemRespDTO resp = new MdkPredictItemRespDTO(); try { ItemVO itemByItemNo = mmPredictItemService.getItemByItemNo(reqDTO.getItemNo()); List predictItemList = new ArrayList<>(); predictItemList.add(itemByItemNo); Map predictResultMap = new HashMap<>(predictItemList.size()); predictModuleHandler.predict(predictItemList, reqDTO.getPredictTime(), 0, predictResultMap); Map> itemPredictData = new HashMap<>(); Map> predictLists = predictResultHandler.convertToPredictData2(predictResultMap.get(reqDTO.getItemNo())); for (Map.Entry> dataListEntry : predictLists.entrySet()) { List predictData = dataListEntry.getValue().stream().map(t -> { MdkPredictDataDTO dto1 = new MdkPredictDataDTO(); dto1.setDataTime(t.getDataTime()); dto1.setDataValue(t.getDataValue()); return dto1; }).collect(Collectors.toList()); itemPredictData.put(dataListEntry.getKey(), predictData); } resp.setItemId(reqDTO.getItemNo()); resp.setPredictTime(reqDTO.getPredictTime()); resp.setPredictData(itemPredictData); } catch (Exception e) { throw new RuntimeException(e); } return resp; } /** * 预测调整 * * @param reqDTO * @return */ @Override public Boolean predictAutoAdjust(MdkPredictReqDTO reqDTO) { return true; } /** * 执行调度模型 * * @param reqDTO * @return */ @Override public MdkScheduleRespDTO doSchedule(MdkScheduleReqDTO reqDTO) { MdkScheduleRespDTO resp = new MdkScheduleRespDTO(); resp.setScheduleCode(reqDTO.getScheduleCode()); resp.setScheduleTime(reqDTO.getScheduleTime()); try { log.info("调度计算开始: " + System.currentTimeMillis()); ScheduleResultVO scheduleResult = scheduleModelHandler.doSchedule(reqDTO.getScheduleCode(), reqDTO.getScheduleTime()); resp.setStatusCode(scheduleResult.getResultCode()); resp.setResult(scheduleResult.getResult()); stScheduleRecordService.create(scheduleResult); stScheduleSchemeService.updateTime(scheduleResult.getSchemeId(), scheduleResult.getScheduleTime(), scheduleResult.getResultCode()); log.info("预测计算结束: " + System.currentTimeMillis()); } catch (Exception ex) { log.info("调度计算异常: " + System.currentTimeMillis()); ex.printStackTrace(); return resp; } return resp; } @Override public Boolean scheduleModelOut(MdkScheduleRespDTO dto) { String modelId = stScheduleSchemeService.getByCode(dto.getScheduleCode()).getModelId(); Map result = dto.getResult(); List list = stScheduleModelOutService.list(modelId); try { for (StScheduleModelOutDTO stScheduleModelOutDTO : list) { double value = 0; //判断点位是否下发 if (stScheduleModelOutDTO.getIsWrite().equals(IsWriteEnum.NOTWRITE.value())) { continue; } //返回结果是否存在 if (result.get(stScheduleModelOutDTO.getResultKey()) == null) { log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "resultKey匹配失败"); continue; } Object resultValue = result.get(stScheduleModelOutDTO.getResultKey()); //判断解析方式 ModelOutResultType modelOutResultType = ModelOutResultType.getEumByCode(stScheduleModelOutDTO.getResultType()); switch (modelOutResultType) { case D: value = (Double) resultValue; break; case D1: ArrayList doubleList = (ArrayList) resultValue; double[] array1 = new double[doubleList.size()]; for (int i = 0; i < doubleList.size(); i++) { array1[i] = doubleList.get(i); } if (stScheduleModelOutDTO.getResultPort() < array1.length) { value = array1[stScheduleModelOutDTO.getResultPort()]; } else { log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "下角标超限"); } break; case D2: ArrayList> doubleListList = (ArrayList>) resultValue; double[][] array2 = new double[doubleListList.size()][]; for (int i = 0; i < doubleListList.size(); i++) { ArrayList doubleList2 = doubleListList.get(i); array2[i] = new double[doubleList2.size()]; for (int j = 0; j < doubleList2.size(); j++) { array2[i][j] = doubleList2.get(j); } } if (stScheduleModelOutDTO.getResultPort() < array2.length && stScheduleModelOutDTO.getResultIndex() < array2[stScheduleModelOutDTO.getResultPort()].length) { value = array2[stScheduleModelOutDTO.getResultPort()][stScheduleModelOutDTO.getResultIndex()]; } else { log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "下标超限"); } break; } //下发到point点位 ApiPointValueWriteDTO ApiPointValueWriteDTO = new ApiPointValueWriteDTO(); ApiPointValueWriteDTO.setPointNo(stScheduleModelOutDTO.getPointNo()); ApiPointValueWriteDTO.setValue(value); if (!dataPointApi.writePointRealValue(ApiPointValueWriteDTO)) { log.error(result.get(stScheduleModelOutDTO.getResultKey()) + "下发数据异常"); } } } catch (Exception ex) { log.error("下发数据异常"); ex.printStackTrace(); } return true; } }