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