python调用
This commit is contained in:
parent
b86d8abd07
commit
8dce0a5a7f
@ -5,6 +5,7 @@ import java.util.Map;
|
||||
|
||||
public class InferRequest {
|
||||
private String modelDir;
|
||||
private String deviceType;
|
||||
private List<Map<String, Object>> batch;
|
||||
private Map<String, Object> features;
|
||||
private Map<String, Object> meta;
|
||||
@ -14,10 +15,23 @@ public class InferRequest {
|
||||
// Getter和Setter方法
|
||||
public String getModelDir() { return modelDir; }
|
||||
public void setModelDir(String modelDir) { this.modelDir = modelDir; }
|
||||
public String getDeviceType() { return deviceType; }
|
||||
public void setDeviceType(String deviceType) { this.deviceType = deviceType; }
|
||||
public List<Map<String, Object>> getBatch() { return batch; }
|
||||
public void setBatch(List<Map<String, Object>> batch) { this.batch = batch; }
|
||||
public Map<String, Object> getFeatures() { return features; }
|
||||
public void setFeatures(Map<String, Object> features) { this.features = features; }
|
||||
public Map<String, Object> getMeta() { return meta; }
|
||||
public void setMeta(Map<String, Object> meta) { this.meta = meta; }
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "InferRequest{" +
|
||||
"modelDir='" + modelDir + '\'' +
|
||||
", deviceType='" + deviceType + '\'' +
|
||||
", batch=" + batch +
|
||||
", features=" + features +
|
||||
", meta=" + meta +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package com.yfd.business.css.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class InferResponse {
|
||||
private int code;
|
||||
@ -12,7 +13,7 @@ public class InferResponse {
|
||||
private List<InferItem> items;
|
||||
private Map<String, Object> meta;
|
||||
private Map<String, Object> features;
|
||||
private Double keff;
|
||||
private BigDecimal keff;
|
||||
|
||||
// Getter和Setter方法
|
||||
public List<InferItem> getItems() { return items; }
|
||||
@ -21,22 +22,41 @@ public class InferResponse {
|
||||
public void setMeta(Map<String, Object> meta) { this.meta = meta; }
|
||||
public Map<String, Object> getFeatures() { return features; }
|
||||
public void setFeatures(Map<String, Object> features) { this.features = features; }
|
||||
public Double getKeff() { return keff; }
|
||||
public void setKeff(Double keff) { this.keff = keff; }
|
||||
public BigDecimal getKeff() { return keff; }
|
||||
public void setKeff(BigDecimal keff) { this.keff = keff; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "InferData{" +
|
||||
"items=" + items +
|
||||
", meta=" + meta +
|
||||
", features=" + features +
|
||||
", keff=" + keff +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
public static class InferItem {
|
||||
private Map<String, Object> meta;
|
||||
private Map<String, Object> features;
|
||||
private Double keff;
|
||||
private BigDecimal keff;
|
||||
|
||||
// Getter和Setter方法
|
||||
public Map<String, Object> getMeta() { return meta; }
|
||||
public void setMeta(Map<String, Object> meta) { this.meta = meta; }
|
||||
public Map<String, Object> getFeatures() { return features; }
|
||||
public void setFeatures(Map<String, Object> features) { this.features = features; }
|
||||
public Double getKeff() { return keff; }
|
||||
public void setKeff(Double keff) { this.keff = keff; }
|
||||
public BigDecimal getKeff() { return keff; }
|
||||
public void setKeff(BigDecimal keff) { this.keff = keff; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "InferItem{" +
|
||||
"meta=" + meta +
|
||||
", features=" + features +
|
||||
", keff=" + keff +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
// Getter和Setter方法
|
||||
@ -46,4 +66,13 @@ public class InferResponse {
|
||||
public void setMsg(String msg) { this.msg = msg; }
|
||||
public InferData getData() { return data; }
|
||||
public void setData(InferData data) { this.data = data; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "InferResponse{" +
|
||||
"code=" + code +
|
||||
", msg='" + msg + '\'' +
|
||||
", data=" + data +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.yfd.business.css.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.yfd.business.css.domain.ScenarioResult;
|
||||
import com.yfd.business.css.model.DeviceStepInfo;
|
||||
import com.yfd.business.css.model.InferRequest;
|
||||
@ -7,6 +8,7 @@ import com.yfd.business.css.model.InferResponse;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@ -28,13 +30,16 @@ public class DeviceInferService {
|
||||
@Resource
|
||||
private ScenarioResultService scenarioResultService;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
public void processDeviceInference(String projectId, String scenarioId,
|
||||
Map<String, List<DeviceStepInfo>> groupedDevices) {
|
||||
// 遍历每个设备类型,调用对应的模型进行推理
|
||||
for (Map.Entry<String, List<DeviceStepInfo>> entry : groupedDevices.entrySet()) {
|
||||
String deviceType = entry.getKey();
|
||||
// 假设 scenarioService 已通过依赖注入等方式注入,若未注入请添加对应字段及注入方式
|
||||
String algorithmType = "GPR";
|
||||
// algorithmType通过scenarioId获取
|
||||
String algorithmType = scenarioService.getAlgorithmType(scenarioId);
|
||||
if (algorithmType == null) {
|
||||
throw new IllegalArgumentException("场景 " + scenarioId + " 未配置算法类型");
|
||||
}
|
||||
@ -53,23 +58,48 @@ public class DeviceInferService {
|
||||
}
|
||||
|
||||
// 封装推理请求
|
||||
InferRequest request = buildInferenceRequest(devices,modelPath);
|
||||
InferRequest request = buildInferenceRequest(deviceType,devices,modelPath);
|
||||
System.out.println("request="+request);
|
||||
|
||||
// 调用Python推理服务
|
||||
InferResponse response = infer(deviceType, request);
|
||||
InferResponse response = infer(request);
|
||||
System.out.println("推理服务返回结果: " + response);
|
||||
|
||||
// 处理推理结果
|
||||
if (response != null && response.getCode() == 0) {
|
||||
processInferenceResults(projectId, scenarioId, deviceType, devices, response);
|
||||
// 重新构建InferResponse对象示例
|
||||
// 1. 从response获取数据
|
||||
int code = response.getCode();
|
||||
String msg = response.getMsg();
|
||||
InferResponse.InferData originalData = response.getData();
|
||||
|
||||
// 2. 重新构建InferData
|
||||
InferResponse.InferData newData = new InferResponse.InferData();
|
||||
newData.setItems(originalData.getItems());
|
||||
newData.setMeta(originalData.getMeta());
|
||||
newData.setFeatures(originalData.getFeatures());
|
||||
newData.setKeff(originalData.getKeff());
|
||||
|
||||
// 3. 构建新的InferResponse
|
||||
InferResponse reconstructedResponse = new InferResponse();
|
||||
reconstructedResponse.setCode(code);
|
||||
reconstructedResponse.setMsg(msg);
|
||||
reconstructedResponse.setData(newData);
|
||||
|
||||
System.out.println("重新构建的response: " + reconstructedResponse);
|
||||
|
||||
// 使用重新构建的response处理结果
|
||||
processInferenceResults(projectId, scenarioId, deviceType, devices, reconstructedResponse);
|
||||
} else {
|
||||
throw new RuntimeException("推理服务调用失败: " + (response != null ? response.getMsg() : "未知错误"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private InferRequest buildInferenceRequest(List<DeviceStepInfo> devices,String modelPath) {
|
||||
private InferRequest buildInferenceRequest(String deviceType,List<DeviceStepInfo> devices,String modelPath) {
|
||||
InferRequest request = new InferRequest();
|
||||
request.setModelDir(modelPath); // 设置模型路径
|
||||
request.setDeviceType(deviceType);
|
||||
|
||||
// 构建批量推理数据
|
||||
List<Map<String, Object>> batchData = new ArrayList<>();
|
||||
@ -110,9 +140,15 @@ public class DeviceInferService {
|
||||
result.setScenarioId(scenarioId);
|
||||
//result.put("deviceType", deviceType);
|
||||
result.setDeviceId((String) item.getMeta().get("deviceId"));
|
||||
result.setKeffValue(null);
|
||||
result.setKeffValue(item.getKeff());
|
||||
// 将 Map<String,Object> 转为 JSON 字符串后再设置
|
||||
result.setAttrState("");
|
||||
try {
|
||||
Map<String, Object> features = item.getFeatures();
|
||||
String featuresJson = objectMapper.writeValueAsString(features);
|
||||
result.setAttrState(featuresJson);
|
||||
} catch (Exception e) {
|
||||
result.setAttrState("{}");
|
||||
}
|
||||
//result.put("meta", item.getMeta());
|
||||
result.setStep((Integer) item.getMeta().get("time"));
|
||||
|
||||
@ -125,8 +161,8 @@ public class DeviceInferService {
|
||||
}
|
||||
|
||||
// 合并的Python推理调用方法
|
||||
public InferResponse infer(String deviceType, InferRequest request) {
|
||||
String url = pythonInferUrl + "/v1/infer/" + deviceType;
|
||||
public InferResponse infer(InferRequest request) {
|
||||
String url = pythonInferUrl + "/v1/infer" ;
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
@ -279,3 +279,57 @@ Action:
|
||||
|
||||
Consider defining a bean of type 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor' in your configuration.
|
||||
|
||||
2026-01-05T17:30:52.702+08:00 INFO 27432 --- [business-css] [main] c.y.b.css.CriticalScenarioApplication : Starting CriticalScenarioApplication using Java 17.0.17 with PID 27432 (E:\projectJava\JavaProjectRepo\business-css\target\classes started by Admin in E:\projectJava\JavaProjectRepo)
|
||||
2026-01-05T17:30:52.705+08:00 INFO 27432 --- [business-css] [main] c.y.b.css.CriticalScenarioApplication : The following 2 profiles are active: "framework", "business"
|
||||
2026-01-05T17:30:54.802+08:00 INFO 27432 --- [business-css] [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8090 (http)
|
||||
2026-01-05T17:30:54.815+08:00 INFO 27432 --- [business-css] [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
|
||||
2026-01-05T17:30:54.816+08:00 INFO 27432 --- [business-css] [main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.24]
|
||||
2026-01-05T17:30:54.951+08:00 INFO 27432 --- [business-css] [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
|
||||
2026-01-05T17:30:54.952+08:00 INFO 27432 --- [business-css] [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2206 ms
|
||||
2026-01-05T17:30:55.307+08:00 WARN 27432 --- [business-css] [main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mybatisConfig': Injection of resource dependencies failed
|
||||
2026-01-05T17:30:55.310+08:00 INFO 27432 --- [business-css] [main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
|
||||
2026-01-05T17:30:55.353+08:00 INFO 27432 --- [business-css] [main] .s.b.a.l.ConditionEvaluationReportLogger :
|
||||
|
||||
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
|
||||
2026-01-05T17:30:55.377+08:00 ERROR 27432 --- [business-css] [main] o.s.b.d.LoggingFailureAnalysisReporter :
|
||||
|
||||
***************************
|
||||
APPLICATION FAILED TO START
|
||||
***************************
|
||||
|
||||
Description:
|
||||
|
||||
A component required a bean of type 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor' that could not be found.
|
||||
|
||||
|
||||
Action:
|
||||
|
||||
Consider defining a bean of type 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor' in your configuration.
|
||||
|
||||
2026-01-05T17:31:24.083+08:00 INFO 24748 --- [business-css] [main] c.y.b.css.CriticalScenarioApplication : Starting CriticalScenarioApplication using Java 17.0.17 with PID 24748 (E:\projectJava\JavaProjectRepo\business-css\target\classes started by Admin in E:\projectJava\JavaProjectRepo)
|
||||
2026-01-05T17:31:24.086+08:00 INFO 24748 --- [business-css] [main] c.y.b.css.CriticalScenarioApplication : The following 2 profiles are active: "framework", "business"
|
||||
2026-01-05T17:31:25.999+08:00 INFO 24748 --- [business-css] [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8090 (http)
|
||||
2026-01-05T17:31:26.013+08:00 INFO 24748 --- [business-css] [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
|
||||
2026-01-05T17:31:26.013+08:00 INFO 24748 --- [business-css] [main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.24]
|
||||
2026-01-05T17:31:26.151+08:00 INFO 24748 --- [business-css] [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
|
||||
2026-01-05T17:31:26.152+08:00 INFO 24748 --- [business-css] [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2022 ms
|
||||
2026-01-05T17:31:26.407+08:00 WARN 24748 --- [business-css] [main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mybatisConfig': Injection of resource dependencies failed
|
||||
2026-01-05T17:31:26.410+08:00 INFO 24748 --- [business-css] [main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
|
||||
2026-01-05T17:31:26.452+08:00 INFO 24748 --- [business-css] [main] .s.b.a.l.ConditionEvaluationReportLogger :
|
||||
|
||||
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
|
||||
2026-01-05T17:31:26.480+08:00 ERROR 24748 --- [business-css] [main] o.s.b.d.LoggingFailureAnalysisReporter :
|
||||
|
||||
***************************
|
||||
APPLICATION FAILED TO START
|
||||
***************************
|
||||
|
||||
Description:
|
||||
|
||||
A component required a bean of type 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor' that could not be found.
|
||||
|
||||
|
||||
Action:
|
||||
|
||||
Consider defining a bean of type 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor' in your configuration.
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user