From 577e866c218a797770b73081586b66fd6a8f4e41 Mon Sep 17 00:00:00 2001
From: xyc <jc_xiong@hotmail.com>
Date: 星期五, 26 四月 2024 11:02:11 +0800
Subject: [PATCH] 测试调用python算法库
---
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/async/PythonLib.java | 102 ++++++++++++++++++++++++++++++++++
starter/src/main/resources/application.yml | 4 -
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/PythonResult.java | 13 ++++
web/packages/views/pages/generator.vue | 32 ++++++++++
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/controller/ModelLineController.java | 11 +--
5 files changed, 151 insertions(+), 11 deletions(-)
diff --git a/modules/mainPart/src/main/java/com/zt/life/modules/mainPart/async/PythonLib.java b/modules/mainPart/src/main/java/com/zt/life/modules/mainPart/async/PythonLib.java
new file mode 100644
index 0000000..3da299e
--- /dev/null
+++ b/modules/mainPart/src/main/java/com/zt/life/modules/mainPart/async/PythonLib.java
@@ -0,0 +1,102 @@
+package com.zt.life.modules.mainPart.async;
+
+import com.alibaba.fastjson.JSONObject;
+import com.zt.common.servlet.Result;
+import com.zt.life.modules.mainPart.taskReliability.model.PythonResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Component;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+@Component
+public class PythonLib {
+ private static final Logger logger = LoggerFactory.getLogger(PythonLib.class);
+
+ @Value("${spring.redis.host}")
+ private String redisHost;
+ @Value("${spring.redis.port}")
+ private String redisPort;
+
+ private Long taskId = 123L;
+ private String taskFlag = "relia";
+
+ @Autowired
+ private RedisTemplate redisTemplate;
+
+ public Result callPython() {
+ Result result = null;
+ InputStream is = null;
+ BufferedReader br = null;
+ try {
+ Process process = null;
+ String command = "python D:\\work\\python\\relia_calc.py";
+ command += " " + redisHost + " " + redisPort;
+ command += " " + taskFlag + " " + taskId.toString();
+ logger.info("cmd鍛戒护涓猴細" + command);
+ if(System.getProperty("os.name").toLowerCase().indexOf("windows") > -1){
+ process = Runtime.getRuntime().exec(new String[]{"cmd", "/c", command});
+ }else if(System.getProperty("os.name").toLowerCase().indexOf("linux") > -1){
+ process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command});
+ }else{
+ throw new Exception("鏆備笉鏀寔璇ユ搷浣滅郴缁燂紝杩涜鍚姩python璁$畻锛�");
+ }
+ is = process.getInputStream();
+ // 浠ュ懡浠よ鏂瑰紡璋冪敤python鏃讹紝杩斿洖鐨勭粨鏋滄槸GBK缂栫爜鐨勶紝鑰岄潪utf-8
+ br = new BufferedReader(new InputStreamReader(is,"gb2312"));
+ String line = br.readLine();
+ logger.info("python杩斿洖缁撴灉锛�" + line);
+ int exitCode = process.waitFor();
+ if (exitCode == 0) {
+ logger.info("鍚姩python璁$畻鎴愬姛");
+ if (line != null) {
+ PythonResult rtn = JSONObject.parseObject(line, PythonResult.class);
+ if ("0".equals(rtn.getCode())) {
+ result = Result.ok();
+ } else {
+ String errorMsg = rtn.getErrorMsg();
+ throw new RuntimeException("鍚姩python璁$畻澶辫触: errorMsg=" + errorMsg);
+ }
+ }
+ } else {
+ logger.error("鍚姩python璁$畻澶辫触: exitCode=" + exitCode);
+ throw new RuntimeException("鍚姩python璁$畻澶辫触: exitCode=" + exitCode);
+ }
+ } catch (Exception e) {
+ logger.error("鍚姩python璁$畻鏃跺彂鐢烢xception锛�", e);
+ e.printStackTrace();
+ result = Result.error(e.getMessage());
+ } finally {
+ if (is == null) {
+ try {
+ is.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ if (br == null) {
+ try {
+ br.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ return result;
+ }
+
+ public String getPythonCalcResult() {
+ String key = taskFlag + taskId.toString();
+ logger.info("redis key锛�" + key);
+ String progress = (String)redisTemplate.opsForValue().get(key);
+ if (progress==null) progress = "0";
+ return progress;
+ }
+}
diff --git a/modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/controller/ModelLineController.java b/modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/controller/ModelLineController.java
index 77253f9..f305c0a 100644
--- a/modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/controller/ModelLineController.java
+++ b/modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/controller/ModelLineController.java
@@ -12,6 +12,7 @@
import com.zt.common.validator.group.AddGroup;
import com.zt.common.validator.group.DefaultGroup;
import com.zt.common.validator.group.UpdateGroup;
+import com.zt.life.modules.mainPart.async.PythonLib;
import com.zt.life.modules.mainPart.taskReliability.model.ModelLine;
import com.zt.life.modules.mainPart.taskReliability.model.ModelRbd;
import com.zt.life.modules.mainPart.taskReliability.service.ModelLineService;
@@ -42,16 +43,15 @@
@Autowired
private ModelLineService modelLineService;
-// @Autowired
-// private PythonLib pythonLib;
+ @Autowired
+ private PythonLib pythonLib;
-/*
@GetMapping("callPythonCalc")
@ApiOperation("淇℃伅")
public Result callPythonCalc() {
- pythonLib.callPython();
+ Result result = pythonLib.callPython();
- return Result.ok();
+ return result;
}
@GetMapping("getPythonCalcResult")
@@ -61,7 +61,6 @@
return Result.ok(result);
}
-*/
@GetMapping("page")
@ApiOperation("鍒嗛〉")
diff --git a/modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/PythonResult.java b/modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/PythonResult.java
new file mode 100644
index 0000000..1f7685f
--- /dev/null
+++ b/modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/PythonResult.java
@@ -0,0 +1,13 @@
+package com.zt.life.modules.mainPart.taskReliability.model;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+@Data
+public class PythonResult {
+ @ApiModelProperty(value = "code")
+ private String code;
+
+ @ApiModelProperty(value = "errorMsg")
+ private String errorMsg;
+}
diff --git a/starter/src/main/resources/application.yml b/starter/src/main/resources/application.yml
index 98d86e8..52ff66e 100644
--- a/starter/src/main/resources/application.yml
+++ b/starter/src/main/resources/application.yml
@@ -17,10 +17,8 @@
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
- #url: jdbc:mysql://127.0.0.1:3306/csiczb?allowMultiQueries=true&hive.exec.orc.split.strategy=ETL&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
- #url: jdbc:mysql://127.0.0.1:3306/test_project?allowMultiQueries=true&hive.exec.orc.split.strategy=ETL&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
url: jdbc:mysql://192.168.31.26:3366/reliability_simulat?allowMultiQueries=true&hive.exec.orc.split.strategy=ETL&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
- #url: jdbc:mysql://127.0.0.1:3306/zhpt_djxl?serverTimezone=GMT&allowMultiQueries=true&hive.exec.orc.split.strategy=ETL&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
+# url: jdbc:mysql://127.0.0.1:3305/reliability_simulat?allowMultiQueries=true&hive.exec.orc.split.strategy=ETL&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
username: root
password: root
#password: 123456
diff --git a/web/packages/views/pages/generator.vue b/web/packages/views/pages/generator.vue
index a04809b..58596ac 100644
--- a/web/packages/views/pages/generator.vue
+++ b/web/packages/views/pages/generator.vue
@@ -10,6 +10,11 @@
<el-button @click="getTableInfo()">鏌ヨ</el-button>
</el-form-item>
</el-form>
+ <el-form>
+ <el-button type="primary" @click="callPythonCalc()">鍚姩python璁$畻</el-button>
+<!-- <el-button type="primary" @click="getPythonCalcResult()">鑾峰彇python璁$畻杩涘害</el-button>-->
+ <el-tag>浠跨湡杩涘害锛歿{progress}}%</el-tag>
+ </el-form>
<el-table :data="dataForm.tableData" height="320" stripe style="width: 100%">
<el-table-column prop="columnName" label="CODE"></el-table-column>
<el-table-column prop="remarks" label="鍚嶇О">
@@ -114,8 +119,9 @@
isExport: false,
tableData: [],
tableList:[]
- }
-
+ },
+ progress: 0,
+ timer: null,
}
},
@@ -151,6 +157,28 @@
this.getInfo()
},
methods: {
+ async callPythonCalc() {
+ // 鍚姩python璁$畻
+ let res = await this.$http.get('/taskReliability/ModelLine/callPythonCalc')
+ if (res.success) {
+ this.$alert('鍚姩python璁$畻鎴愬姛锛�')
+ // 瀹氭椂鑾峰彇python璁$畻杩涘害
+ this.timer = setInterval(this.getPythonCalcResult, 5000)
+ }
+ },
+
+ async getPythonCalcResult() {
+ // setTimeout(() => {
+ let res = await this.$http.get('/taskReliability/ModelLine/getPythonCalcResult')
+ if (res.success) {
+ this.progress = res.data
+ if (this.progress==='100') {
+ clearInterval(this.timer)
+ }
+ }
+ // }, 5000)
+ },
+
// 鑾峰彇淇℃伅
async getInfo() {
let res = await this.$http.get(`/sys/generator/properties`)
--
Gitblit v1.9.1