houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.iailab.module.model.handler;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iailab.common.utils.PythonUtil;
import com.iailab.module.mcs.dto.StModelDTO;
import com.iailab.module.mcs.dto.StModelRequestLogDTO;
import com.iailab.module.mcs.dto.StModelRunlogDTO;
import com.iailab.module.mcs.service.*;
import com.iailab.module.model.IAILModel;
import com.iailab.module.model.command.MLCommand;
import com.iailab.module.model.command.PyCommand;
import com.iailab.module.model.sample.constructor.SampleConstructor;
import com.iailab.module.model.sample.dto.SampleData;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.util.*;
 
/**
 * 浮选模型处理
 *
 * @author PanZhibao
 * @Description
 * @createTime 2023年05月16日 13:33:00
 */
@Slf4j
@Component
public class ModelHandler {
 
    @Resource
    private PyCommand pyCommand;
 
    @Resource
    private MLCommand mLCommand;
 
    @Resource
    private StModelService stModelService;
 
    @Resource
    private StModelSettingService stModelSettingService;
 
    @Resource
    private StModelRunlogService runlogService;
 
    @Resource
    private StModelResultService stModelResultService;
 
    @Resource
    private SampleConstructor sampleConstructor;
 
    @Resource
    private StModelRequestLogService stModelRequestLogService;
 
    /**
     * 运行模型
     *
     * @param modelCode
     * @param runTime
     * @return
     * @throws Exception
     */
    public Map<String, Object> run(String modelCode, Date runTime) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(runTime);
        calendar.set(Calendar.SECOND, 0);
        Date dataTime = calendar.getTime();
        try {
            // 1、查找模型
            StModelDTO stModel = stModelService.getByCode(modelCode);
            if (stModel == null) {
                throw new Exception("modelCode错误!");
            }
 
            // 2、构建输入参数
            List<SampleData> sampleDataList = sampleConstructor.constructSample(stModel.getId(), dataTime);
            List<Object> params = new ArrayList<>();
            if (!CollectionUtils.isEmpty(sampleDataList)) {
                sampleDataList.forEach(item -> {
                    params.add(item.getMatrix());
                });
            }
 
            // 3、构建配置参数
            Map<String, Object> settings = stModelSettingService.getByModelId(stModel.getId());
 
            // 4、执行算法
            log.info("################ modelCode=" + modelCode + " ###################");
            String[] command = pyCommand.getCommand(stModel, params, settings);
            log.info("command=" + JSONArray.toJSONString(command));
            result = PythonUtil.execPy(command);
            log.info("result=" + JSON.toJSONString(result));
            stModelResultService.addML(stModel.getId(), result, runTime);
            stModelService.updateTime(stModel.getId(), runTime);
 
            // 6、插入运行日志
            String jsonString = dealResult(result);
            this.addLog(stModel.getId(), modelCode, runTime, jsonString);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return result;
    }
 
    public String dealResult(Map<String, Object> result) {
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = "";
        try {
            jsonString = mapper.writeValueAsString(result);
            jsonString = jsonString.replace("mv_fl_kd_307", "307分流阀开度");
            jsonString = jsonString.replace("mv_hj_water_valve_main_307", "315合介桶加水阀开度");
            jsonString = jsonString.replace("mv_xj_water_valve_307", "333稀介桶加水阀开度");
            jsonString = jsonString.replace("mv_hj_water_valve_small_307", "315合介桶密度微调阀开度");
            jsonString = jsonString.replace("mv_mn_water_valve_307", "325煤泥桶加水阀开度");
 
            jsonString = jsonString.replace("mv_fl_kd_308", "308分流阀开度");
            jsonString = jsonString.replace("mv_hj_water_valve_main_308", "316合介桶加水阀开度");
            jsonString = jsonString.replace("mv_xj_water_valve_308", "332稀介桶加水阀开度");
            jsonString = jsonString.replace("mv_hj_water_valve_small_308", "316合介桶密度微调阀开度");
            jsonString = jsonString.replace("mv_mn_water_valve_308", "325煤泥桶加水阀开度");
 
            jsonString = jsonString.replace("mv_valve_fl_3318", "3318分流阀开度");
            jsonString = jsonString.replace("mv_pump_hl_3318", "3308混料桶排水泵频率");
            jsonString = jsonString.replace("mv_pump_hj_3318", "3344合介桶排水泵频率");
            jsonString = jsonString.replace("mv_valve_hj_water_3318", "3344合介桶加水阀开度");
            jsonString = jsonString.replace("mv_valve_jj_3318", "3344合介桶加介阀开度");
            jsonString = jsonString.replace("mv_valve_xj_water_3318", "3348稀介桶加水阀开度");
            jsonString = jsonString.replace("mv_pump_xj_3318", "3348稀介桶排水泵频率");
            jsonString = jsonString.replace("mv_pump_mn_3318", "4402煤泥桶排水泵频率");
            jsonString = jsonString.replace("mv_valve_mn_water_3318", "4402煤泥桶加水阀开度");
            jsonString = jsonString.replace("target_md_3318", "3318目标合介密度");
 
 
            jsonString = jsonString.replace("mv_valve_fl_3319", "3319分流阀开度");
            jsonString = jsonString.replace("mv_pump_hl_3319", "3317混料桶排水泵频率");
            jsonString = jsonString.replace("mv_pump_hj_3319", "3345合介桶排水泵频率");
            jsonString = jsonString.replace("mv_valve_hj_water_3319", "3345合介桶加水阀开度");
            jsonString = jsonString.replace("mv_valve_jj_3319", "3345合介桶加介阀开度");
            jsonString = jsonString.replace("mv_valve_xj_water_3319", "3349稀介桶加水阀开度");
            jsonString = jsonString.replace("mv_pump_xj_3319", "3349稀介桶排水泵频率");
            jsonString = jsonString.replace("mv_pump_mn_3319", "4403煤泥桶排水泵频率");
            jsonString = jsonString.replace("mv_valve_mn_water_3319", "4403煤泥桶加水阀开度");
            jsonString = jsonString.replace("target_md_3319", "3319目标合介密度");
 
            jsonString = jsonString.replace("status_first_start", "第一组风机启动信号");
            jsonString = jsonString.replace("status_first_stop", "第一组风机停止信号");
            jsonString = jsonString.replace("status_second_start", "第二组风机启动信号");
            jsonString = jsonString.replace("status_second_stop", "第二组风机停止信号");
            jsonString = jsonString.replace("mv_1_346", "346高压风机频率设定");
            jsonString = jsonString.replace("mv_1_347", "347高压风机频率设定");
            jsonString = jsonString.replace("mv_2_4454", "4454高压风机频率设定");
 
            jsonString = jsonString.replace("Thickener_OF_XL", "501 浓缩池循环水池浊度预测值");
            jsonString = jsonString.replace("Thickener_BF_XL", "501浓缩池出料浓度预测值");
            jsonString = jsonString.replace("Thickener_H_XL", "501浓缩池污泥厚度预测值");
            jsonString = jsonString.replace("Dos_yin_det1_XL", "507_1加药泵频率");
            jsonString = jsonString.replace("Dos_yang_det1_XL", "508_1加药泵频率");
            jsonString = jsonString.replace("Dos_yin_det2_XL", "507_2加药泵频率");
            jsonString = jsonString.replace("Dos_yang_det2_XL", "508_2加药泵频率");
 
            jsonString = jsonString.replace("Thickener_OF_QC", "浓缩池循环水池浊度预测值");
            jsonString = jsonString.replace("Thickener_BF_QC", "浓缩池出料浓度预测值");
            jsonString = jsonString.replace("Thickener_H_QC", "浓缩池污泥厚度预测值");
            jsonString = jsonString.replace("Dos_yin_det_QC", "350加药系统频率");
            jsonString = jsonString.replace("Dos_yang_det_QC", "351加药系统频率");
 
            jsonString = jsonString.replace("UF_pumb1", "503底流泵启停");
            jsonString = jsonString.replace("UF_pumb2", "502底流泵启停");
            jsonString = jsonString.replace("zt4418_res", "4418启停信号");
            jsonString = jsonString.replace("zt4419_res", "4419启停信号");
            jsonString = jsonString.replace("zt4420_res", "4420启停信号");
            jsonString = jsonString.replace("zt4421_res", "4421启停信号");
            jsonString = jsonString.replace("zt4422_res", "4422启停信号");
            jsonString = jsonString.replace("zt4428_res", "4428启停信号");
            jsonString = jsonString.replace("zt4429_res", "4429启停信号");
            jsonString = jsonString.replace("zt4430_res", "4430启停信号");
            jsonString = jsonString.replace("zt4431_res", "4431启停信号");
            jsonString = jsonString.replace("zt4432_res", "4432启停信号");
 
 
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return jsonString;
    }
 
    /**
     * 调用模型
     *
     * @param modelCode
     * @param runTime
     * @param sampleDataList
     * @param appKey
     * @return
     * @throws Exception
     */
    public Map<String, Object> run(String modelCode, Date runTime, List<double[][]> sampleDataList, String appKey) throws Exception {
        Map<String, Object> result = new HashMap<>();
        try {
            // 1、查找模型
            StModelDTO stModel = stModelService.getByCode(modelCode);
            if (stModel == null) {
                throw new Exception("modelCode错误!");
            }
 
            // 2、构建输入参数
            List<Object> params = new ArrayList<>();
            if (!CollectionUtils.isEmpty(sampleDataList)) {
                sampleDataList.forEach(item -> {
                    params.add(item);
                });
            }
 
            // 3、构建配置参数
            Map<String, Object> settings = stModelSettingService.getByModelId(stModel.getId());
 
            // 4、执行算法
            log.info("################ modelCode=" + modelCode + " ###################");
            String[] command = pyCommand.getCommand(stModel, params, settings);
            log.info("command=" + JSONArray.toJSONString(command));
            result = PythonUtil.execPy(command);
            log.info("result=" + JSON.toJSONString(result));
 
            // 5、插入调用日志
            StModelRequestLogDTO logDto = new StModelRequestLogDTO();
            logDto.setModelId(stModel.getId());
            logDto.setAppKey(appKey);
            logDto.setRequestTime(runTime);
            logDto.setRequestParams(JSONArray.toJSONString(sampleDataList));
            logDto.setResponseResult(JSONObject.toJSONString(result));
            stModelRequestLogService.save(logDto);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return result;
    }
 
    /**
     * 调用模型
     *
     * @param modelCode
     * @param sampleDataList
     * @return
     * @throws Exception
     */
    public Map<String, Object> run(String modelCode, List<double[][]> sampleDataList) throws Exception {
        Map<String, Object> result = new HashMap<>();
        try {
            // 1、查找模型
            StModelDTO stModel = stModelService.getByCode(modelCode);
            if (stModel == null) {
                throw new Exception("modelCode错误!");
            }
 
            // 2、构建输入参数
            List<Object> params = new ArrayList<>();
            if (!CollectionUtils.isEmpty(sampleDataList)) {
                sampleDataList.forEach(item -> {
                    params.add(item);
                });
            }
 
            // 3、构建配置参数
            Map<String, Object> settings = stModelSettingService.getByModelId(stModel.getId());
 
            // 4、执行算法
            log.info("################ modelCode=" + modelCode + " ###################");
            String[] command = pyCommand.getCommand(stModel, params, settings);
            log.info("command=" + JSONArray.toJSONString(command));
            result = PythonUtil.execPy(command);
            log.info("result=" + JSON.toJSONString(result));
 
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return result;
    }
 
    /**
     * 调用模型
     *
     * @param modelCode
     * @param sampleDataList
     * @param settings
     * @return
     * @throws Exception
     */
    public Map<String, Object> run(String modelCode, List<double[][]> sampleDataList, Map<String, Object> settings) throws Exception {
        Map<String, Object> result = new HashMap<>();
        try {
            // 1、查找模型
            StModelDTO stModel = stModelService.getByCode(modelCode);
            if (stModel == null) {
                throw new Exception("modelCode错误!");
            }
 
            // 2、构建输入参数
            List<Object> params = new ArrayList<>();
            if (!CollectionUtils.isEmpty(sampleDataList)) {
                sampleDataList.forEach(item -> {
                    params.add(item);
                });
            }
 
            // 4、执行算法
            log.info("################ modelCode=" + modelCode + " ###################");
            String[] command = pyCommand.getCommand(stModel, params, settings);
            log.info("command=" + JSONArray.toJSONString(command));
            result = PythonUtil.execPy(command);
            log.info("result=" + JSON.toJSONString(result));
 
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return result;
    }
 
    /**
     * 构造IAILMDK.run()方法的newModelBean参数
     *
     * @param stModel
     * @return
     */
    private IAILModel composeNewModelBean(StModelDTO stModel) {
        IAILModel newModelBean = new IAILModel();
        newModelBean.setClassName(stModel.getClassName().trim());
        newModelBean.setMethodName(stModel.getMethodName().trim());
        //构造参数类型
        String[] paArStr = stModel.getParamsStructure().trim().split(",");
        Class<?>[] paramsArray = new Class[paArStr.length];
        for (int i = 0; i < paArStr.length; i++) {
            if ("[[D".equals(paArStr[i])) {
                paramsArray[i] = double[][].class;
            } else if ("Map".equals(paArStr[i]) || "java.util.HashMap".equals(paArStr[i])) {
                paramsArray[i] = HashMap.class;
            }
        }
        newModelBean.setParamsArray(paramsArray);
        return newModelBean;
    }
 
    public void addLog(String modelId, String modelCode, Date runTime, String jsonString) {
        StModelRunlogDTO logDto = new StModelRunlogDTO();
        logDto.setModelid(modelId);
        logDto.setRunTime(runTime);
        logDto.setRunType(modelCode);
        logDto.setRunResult(jsonString);
        runlogService.add(logDto);
    }
}