dongyukun
6 天以前 95066d1bdaf67f414f01d1686e7ea9c0ebc07e26
提交 | 用户 | 时间
7fd198 1 package com.iailab.module.model.api;
2
3 import com.alibaba.fastjson.JSON;
95066d 4 import com.iailab.module.data.api.point.DataPointApi;
D 5 import com.iailab.module.data.api.point.dto.ApiPointValueWriteDTO;
6 import com.iailab.module.model.api.mcs.dto.StScheduleModelOutDTO;
7fd198 7 import com.iailab.module.model.api.mdk.MdkApi;
8 import com.iailab.module.model.api.mdk.dto.*;
9 import com.iailab.module.model.mcs.pre.entity.DmModuleEntity;
10 import com.iailab.module.model.mcs.pre.service.DmModuleService;
11 import com.iailab.module.model.mcs.pre.service.MmPredictItemService;
95066d 12 import com.iailab.module.model.mcs.sche.service.StScheduleModelOutService;
ac52ae 13 import com.iailab.module.model.mcs.sche.service.StScheduleRecordService;
14 import com.iailab.module.model.mcs.sche.service.StScheduleSchemeService;
7fd198 15 import com.iailab.module.model.mdk.predict.PredictModuleHandler;
9162d9 16 import com.iailab.module.model.mdk.predict.PredictResultHandler;
054fb9 17 import com.iailab.module.model.mdk.schedule.ScheduleModelHandler;
9162d9 18 import com.iailab.module.model.mdk.vo.DataValueVO;
7fd198 19 import com.iailab.module.model.mdk.vo.ItemVO;
20 import com.iailab.module.model.mdk.vo.PredictResultVO;
054fb9 21 import com.iailab.module.model.mdk.vo.ScheduleResultVO;
7fd198 22 import lombok.extern.slf4j.Slf4j;
23 import org.springframework.beans.factory.annotation.Autowired;
9162d9 24 import org.springframework.util.CollectionUtils;
7fd198 25 import org.springframework.validation.annotation.Validated;
26 import org.springframework.web.bind.annotation.RestController;
27
b2aca2 28 import java.util.*;
7fd198 29 import java.util.stream.Collectors;
30
31 /**
32  * @author PanZhibao
33  * @Description
34  * @createTime 2024年08月26日
35  */
36 @Slf4j
37 @RestController
38 @Validated
39 public class MdkApiImpl implements MdkApi {
40
41     @Autowired
42     private DmModuleService dmModuleService;
43
44     @Autowired
45     private MmPredictItemService mmPredictItemService;
46
47     @Autowired
48     private PredictModuleHandler predictModuleHandler;
9162d9 49
50     @Autowired
51     private PredictResultHandler predictResultHandler;
054fb9 52
53     @Autowired
54     private ScheduleModelHandler scheduleModelHandler;
ac52ae 55
56     @Autowired
57     private StScheduleRecordService stScheduleRecordService;
58
59     @Autowired
60     private StScheduleSchemeService stScheduleSchemeService;
95066d 61
D 62     @Autowired
63     private StScheduleModelOutService stScheduleModelOutService;
64
65     @Autowired
66     private DataPointApi dataPointApi;
7fd198 67
68     /**
69      * 按模块预测
70      *
71      * @param reqDTO
72      * @return
73      */
74     @Override
148842 75     public MdkPredictModuleRespDTO predictModule(MdkPredictReqDTO reqDTO) {
7fd198 76         MdkPredictModuleRespDTO resp = new MdkPredictModuleRespDTO();
69bd5e 77         resp.setPredictTime(reqDTO.getPredictTime());
D 78         resp.setModuleType(reqDTO.getModuleType());
79
7fd198 80         Map<String, MdkPredictItemRespDTO> predictItemRespMap = new HashMap<>();
81         try {
82             if (reqDTO.getPredictTime() == null) {
83                 throw new Exception("PredictTime不能为空");
84             }
85             if (reqDTO.getModuleType() == null) {
86                 throw new Exception("ModuleType不能为空");
87             }
b2aca2 88             Calendar calendar = Calendar.getInstance();
D 89             calendar.setTime(reqDTO.getPredictTime());
90             calendar.set(Calendar.MILLISECOND, 0);
91             calendar.set(Calendar.SECOND, 0);
92             reqDTO.setPredictTime(calendar.getTime());
7fd198 93             log.info("预测参数:" + JSON.toJSONString(reqDTO));
94             MdkPredictModuleRespDTO result = new MdkPredictModuleRespDTO();
95             result.setPredictTime(reqDTO.getPredictTime());
96             result.setModuleType(reqDTO.getModuleType());
97             List<DmModuleEntity> moduleList = dmModuleService.getModuleByModuleType(reqDTO.getModuleType());
98             log.info("预测计算开始: " + System.currentTimeMillis());
99             for (DmModuleEntity module : moduleList) {
100                 int intervalTime = 0;
101                 if (module.getPredicttime() != null) {
102                     intervalTime = (int) (reqDTO.getPredictTime().getTime() - module.getPredicttime().getTime()) / (1000 * 60);
103                 }
104                 List<ItemVO> predictItemList = mmPredictItemService.getByModuleId(module.getId());
07890e 105                 Map<String, PredictResultVO> predictResultMap = new HashMap<>(predictItemList.size());
D 106                 // 分组,先运行normal预测项,再将结果传递给merge预测项
107                 List<ItemVO> normalItems = predictItemList.stream().filter(e -> e.getItemType().equals("NormalItem")).collect(Collectors.toList());
108                 if (!CollectionUtils.isEmpty(normalItems)) {
ac52ae 109                     predictModuleHandler.predict(normalItems, reqDTO.getPredictTime(), intervalTime, predictResultMap);
fde993 110                     List<ItemVO> mergeItems = predictItemList.stream().filter(e -> e.getItemType().equals("MergeItem")).collect(Collectors.toList());
D 111                     if (!CollectionUtils.isEmpty(mergeItems)) {
ac52ae 112                         predictModuleHandler.predict(mergeItems, reqDTO.getPredictTime(), intervalTime, predictResultMap);
07890e 113                     }
D 114                 }
4f1717 115                 // 更新Module时间
116                 dmModuleService.updatePredictTime(module.getId(), reqDTO.getPredictTime());
117                 if (reqDTO.getIsResult() == null || !reqDTO.getIsResult()) {
118                     return resp;
119                 }
120                 for (Map.Entry<String, PredictResultVO> entry : predictResultMap.entrySet()) {
121                     MdkPredictItemRespDTO itemResp = new MdkPredictItemRespDTO();
122                     itemResp.setItemId(entry.getKey());
123                     itemResp.setPredictTime(reqDTO.getPredictTime());
124                     Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>();
125
126                     Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(entry.getValue());
127                     for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) {
128                         List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> {
129                             MdkPredictDataDTO dto1 = new MdkPredictDataDTO();
130                             dto1.setDataTime(t.getDataTime());
131                             dto1.setDataValue(t.getDataValue());
132                             return dto1;
133                         }).collect(Collectors.toList());
134                         itemPredictData.put(dataListEntry.getKey(), predictData);
135                     }
136                     itemResp.setPredictData(itemPredictData);
137                     predictItemRespMap.put(entry.getKey(), itemResp);
138                 }
7fd198 139             }
140             log.info("预测计算结束: " + System.currentTimeMillis());
141         } catch (Exception ex) {
148842 142             ex.printStackTrace();
143             return resp;
7fd198 144         }
145         resp.setPredictItemRespMap(predictItemRespMap);
148842 146         return resp;
7fd198 147     }
148
149     /**
150      * 单个预测
151      *
152      * @param reqDTO
153      * @return
154      */
155     @Override
148842 156     public MdkPredictItemRespDTO predictItem(MdkPredictReqDTO reqDTO) {
7fd198 157         MdkPredictItemRespDTO resp = new MdkPredictItemRespDTO();
9162d9 158         try {
1178da 159
D 160             ItemVO itemByItemNo = mmPredictItemService.getItemByItemNo(reqDTO.getItemNo());
161             List<ItemVO> predictItemList = new ArrayList<>();
162             predictItemList.add(itemByItemNo);
163             Map<String, PredictResultVO> predictResultMap = new HashMap<>(predictItemList.size());
ac52ae 164             predictModuleHandler.predict(predictItemList, reqDTO.getPredictTime(), 0, predictResultMap);
1178da 165
D 166             Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>();
167
168             Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(predictResultMap.get(reqDTO.getItemNo()));
169             for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) {
170                 List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> {
171                     MdkPredictDataDTO dto1 = new MdkPredictDataDTO();
172                     dto1.setDataTime(t.getDataTime());
173                     dto1.setDataValue(t.getDataValue());
174                     return dto1;
175                 }).collect(Collectors.toList());
176                 itemPredictData.put(dataListEntry.getKey(), predictData);
9162d9 177             }
1178da 178             resp.setItemId(reqDTO.getItemNo());
9162d9 179             resp.setPredictTime(reqDTO.getPredictTime());
1178da 180             resp.setPredictData(itemPredictData);
D 181         } catch (Exception e) {
182             throw new RuntimeException(e);
9162d9 183         }
184
148842 185         return resp;
7fd198 186     }
187
188     /**
189      * 预测调整
190      *
191      * @param reqDTO
192      * @return
193      */
194     @Override
148842 195     public Boolean predictAutoAdjust(MdkPredictReqDTO reqDTO) {
7fd198 196
197
148842 198         return true;
7fd198 199     }
200
201     /**
202      * 执行调度模型
203      *
204      * @param reqDTO
205      * @return
206      */
207     @Override
148842 208     public MdkScheduleRespDTO doSchedule(MdkScheduleReqDTO reqDTO) {
7fd198 209         MdkScheduleRespDTO resp = new MdkScheduleRespDTO();
054fb9 210         resp.setScheduleCode(reqDTO.getScheduleCode());
211         resp.setScheduleTime(reqDTO.getScheduleTime());
212         try {
213             log.info("调度计算开始: " + System.currentTimeMillis());
51c1c2 214             ScheduleResultVO scheduleResult = scheduleModelHandler.doSchedule(reqDTO.getScheduleCode(), reqDTO.getScheduleTime());
054fb9 215             resp.setResult(scheduleResult.getResult());
ac52ae 216             stScheduleRecordService.create(scheduleResult);
217             stScheduleSchemeService.updateTime(scheduleResult.getSchemeId(), scheduleResult.getScheduleTime());
054fb9 218             log.info("预测计算结束: " + System.currentTimeMillis());
219         } catch (Exception ex) {
220             log.info("调度计算异常: " + System.currentTimeMillis());
ac52ae 221             ex.printStackTrace();
148842 222             return resp;
054fb9 223         }
148842 224         return resp;
7fd198 225     }
95066d 226
D 227     @Override
228     public Boolean scheduleModelOut(MdkScheduleRespDTO dto) {
229         String modelId = stScheduleSchemeService.getByCode(dto.getScheduleCode()).getModelId();
230         Map<String,Object> result =  dto.getResult();
231         List<StScheduleModelOutDTO> list =  stScheduleModelOutService.list(modelId);
232         try{
233             list.forEach(
234                     item -> {
235                         double value = 0;
236                         //判断点位是否下发以及返回结果是否存在
237                         if(item.getIsWrite()==1){
238                             if(result.get(item.getResultKey())==null){
239                                 log.error(result.get(item.getResultKey()) + "resultKey匹配失败");
240                             }
241                             Object resultValue = result.get(item.getResultKey());
242                             //判断解析方式
243                             if(item.getResultType().equals("double")){
244                                 if(resultValue instanceof Double) {
245                                     value = (Double) resultValue;
246                                 }
247                             }else if(item.getResultType().equals("double[]")){
248                                 ArrayList<Double> doubleList = (ArrayList<Double>) resultValue;
249                                 double[] array = new double[doubleList.size()];
250                                 for (int i = 0; i < doubleList.size(); i++) {
251                                     array[i] = doubleList.get(i);
252                                 }
253                                 if(array!= null && item.getResultPort() < array.length){
254                                     value = array[item.getResultPort()];
255                                 }else{
256                                     log.error(result.get(item.getResultKey()) + "下角标超限");
257                                 }
258
259                             }else if(item.getResultType().equals("double[][]")){
260                                 if (item.getResultType().equals("double[][]")) {
261                                     if (resultValue instanceof ArrayList) {
262                                         ArrayList<ArrayList<Double>> doubleListList = (ArrayList<ArrayList<Double>>) resultValue;
263                                         double[][] array = new double[doubleListList.size()][];
264                                         for (int i = 0; i < doubleListList.size(); i++) {
265                                             ArrayList<Double> doubleList = doubleListList.get(i);
266                                             array[i] = new double[doubleList.size()];
267                                             for (int j = 0; j < doubleList.size(); j++) {
268                                                 array[i][j] = doubleList.get(j);
269                                             }
270                                         }
271                                         if (array != null && item.getResultPort() < array.length && item.getResultIndex() < array[item.getResultPort()].length) {
272                                             value = array[item.getResultPort()][item.getResultIndex()];
273                                         } else {
274                                             log.error(result.get(item.getResultKey()) + "下标超限");
275                                         }
276                                     }
277                                 }
278                             }
279                             //下发到point点位
280                             ApiPointValueWriteDTO ApiPointValueWriteDTO = new ApiPointValueWriteDTO();
281                             ApiPointValueWriteDTO.setPointNo(item.getPointNo());
282                             ApiPointValueWriteDTO.setValue(value);
283                             if (!dataPointApi.writePointRealValue(ApiPointValueWriteDTO)) {
284                                 log.error(result.get(item.getResultKey()) + "下发数据异常");
285                             }
286
287                         }
288                     }
289             );
290         }catch (Exception ex){
291             log.error("下发数据异常");
292             ex.printStackTrace();
293         }
294         return true;
295     }
7fd198 296 }