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