diff --git a/src/main/java/com/inspect/simulator/controller/AlgorithmController.java b/src/main/java/com/inspect/simulator/controller/AlgorithmController.java index b91c869..3bc2744 100644 --- a/src/main/java/com/inspect/simulator/controller/AlgorithmController.java +++ b/src/main/java/com/inspect/simulator/controller/AlgorithmController.java @@ -5,10 +5,13 @@ import com.inspect.simulator.domain.algorithm.in.AnalyseRequest; import com.inspect.simulator.domain.algorithm.out.AnalyseResItem; import com.inspect.simulator.domain.algorithm.out.AnalyseResPoint; import com.inspect.simulator.domain.algorithm.out.AnalyseResult; +import com.inspect.simulator.service.AlgorithmService; import com.inspect.simulator.service.remote.AnalysisRemoteService; +import com.inspect.simulator.utils.HttpClientUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -25,78 +28,100 @@ public class AlgorithmController { private final Logger log = LoggerFactory.getLogger(this.getClass()); @Resource - private AnalysisRemoteService analysisRemoteService; + private AlgorithmService algorithmService; @PostMapping("/simu/filter/picAnalyse") public ResponseEntity filterPicAnalyse(@RequestBody final String analyseRequestStr) { - log.info("[FILTER] filterPicAnalyse: analyseRequestStr={}", analyseRequestStr); - AnalyseRequest analyseRequest = new Gson().fromJson(analyseRequestStr, AnalyseRequest.class); - log.info("[FILTER] filterPicAnalyse: analyseRequest={}", new Gson().toJson(analyseRequest)); - - AnalyseResult analyseResult = new AnalyseResult(); - analyseResult.setRequestId(analyseRequest.getRequestId()); - AnalyseResPoint analyseResPoint = new AnalyseResPoint(); - analyseResPoint.setValue("0"); - analyseResPoint.setConf("0.85"); - analyseResPoint.setCode("2000"); - analyseResPoint.setResImageUrl(analyseRequest.getObjectList().get(0).getImageUrlList()[0]); - List analyseResPoints = new ArrayList<>(); - analyseResPoints.add(analyseResPoint); - AnalyseResItem analyseResItem = new AnalyseResItem(); - analyseResItem.setObjectId(analyseRequest.getObjectList().get(0).getObjectId()); - analyseResItem.setAlgFactory("yd"); - analyseResItem.setResults(analyseResPoints); - List analyseResItems = new ArrayList<>(); - analyseResItems.add(analyseResItem); - analyseResult.setResultList(analyseResItems); - - log.info("[FILTER] filterPicAnalyse: analyseResult={}", new Gson().toJson(analyseResult)); - try { - Call call = analysisRemoteService.picAnalyseRetNotify(analyseResult); - Response response = call.execute(); - Object result = response.body(); - System.out.println("result: " + result); - } catch (Exception e) { - e.printStackTrace(); - } - - return ResponseEntity.ok().body("{\"code\":\"200\"}"); +// log.info("[FILTER] filterPicAnalyse: analyseRequestStr={}", analyseRequestStr); +// AnalyseRequest analyseRequest = new Gson().fromJson(analyseRequestStr, AnalyseRequest.class); +// log.info("[FILTER] filterPicAnalyse: analyseRequest={}", new Gson().toJson(analyseRequest)); +// +// AnalyseResult analyseResult = new AnalyseResult(); +// analyseResult.setRequestId(analyseRequest.getRequestId()); +// AnalyseResPoint analyseResPoint = new AnalyseResPoint(); +// analyseResPoint.setValue("0"); +// analyseResPoint.setConf("0.85"); +// analyseResPoint.setCode("2000"); +// analyseResPoint.setResImageUrl(analyseRequest.getObjectList().get(0).getImageUrlList()[0]); +// List analyseResPoints = new ArrayList<>(); +// analyseResPoints.add(analyseResPoint); +// AnalyseResItem analyseResItem = new AnalyseResItem(); +// analyseResItem.setObjectId(analyseRequest.getObjectList().get(0).getObjectId()); +// analyseResItem.setAlgFactory("yd"); +// analyseResItem.setResults(analyseResPoints); +// List analyseResItems = new ArrayList<>(); +// analyseResItems.add(analyseResItem); +// analyseResult.setResultList(analyseResItems); +// +// log.info("[FILTER] filterPicAnalyse: analyseResult={}", new Gson().toJson(analyseResult)); +// try { +// Call call = analysisRemoteService.picAnalyseRetNotify(analyseResult); +// Response response = call.execute(); +// Object result = response.body(); +// System.out.println("result: " + result); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// +// return ResponseEntity.ok().body("{\"code\":\"200\"}"); + return ResponseEntity.ok().body(algorithmService.filterPicAnalyse(analyseRequestStr)); } @PostMapping("/simu/bigModel/picAnalyse") public ResponseEntity bigModelPicAnalyse(@RequestBody final String analyseRequestStr) { - log.info("[FILTER] bigModelPicAnalyse: analyseRequestStr={}", analyseRequestStr); - AnalyseRequest analyseRequest = new Gson().fromJson(analyseRequestStr, AnalyseRequest.class); - log.info("[FILTER] bigModelPicAnalyse: analyseRequest={}", new Gson().toJson(analyseRequest)); - - AnalyseResult analyseResult = new AnalyseResult(); - analyseResult.setRequestId(analyseRequest.getRequestId()); - AnalyseResPoint analyseResPoint = new AnalyseResPoint(); - analyseResPoint.setValue("1"); - analyseResPoint.setConf("0.95"); - analyseResPoint.setCode("2000"); - analyseResPoint.setResImageUrl(analyseRequest.getObjectList().get(0).getImageUrlList()[0]); - List analyseResPoints = new ArrayList<>(); - analyseResPoints.add(analyseResPoint); - AnalyseResItem analyseResItem = new AnalyseResItem(); - analyseResItem.setObjectId(analyseRequest.getObjectList().get(0).getObjectId()); - analyseResItem.setAlgFactory("yd"); - analyseResItem.setResults(analyseResPoints); - List analyseResItems = new ArrayList<>(); - analyseResItems.add(analyseResItem); - analyseResult.setResultList(analyseResItems); +// log.info("[FILTER] bigModelPicAnalyse: analyseRequestStr={}", analyseRequestStr); +// AnalyseRequest analyseRequest = new Gson().fromJson(analyseRequestStr, AnalyseRequest.class); +// log.info("[FILTER] bigModelPicAnalyse: analyseRequest={}", new Gson().toJson(analyseRequest)); +// +// AnalyseResult analyseResult = new AnalyseResult(); +// analyseResult.setRequestId(analyseRequest.getRequestId()); +// AnalyseResPoint analyseResPoint = new AnalyseResPoint(); +// analyseResPoint.setValue("1"); +// analyseResPoint.setConf("0.95"); +// analyseResPoint.setCode("2000"); +// analyseResPoint.setResImageUrl(analyseRequest.getObjectList().get(0).getImageUrlList()[0]); +// List analyseResPoints = new ArrayList<>(); +// analyseResPoints.add(analyseResPoint); +// AnalyseResItem analyseResItem = new AnalyseResItem(); +// analyseResItem.setObjectId(analyseRequest.getObjectList().get(0).getObjectId()); +// analyseResItem.setAlgFactory("yd"); +// analyseResItem.setResults(analyseResPoints); +// List analyseResItems = new ArrayList<>(); +// analyseResItems.add(analyseResItem); +// analyseResult.setResultList(analyseResItems); +// +// +// if(infraredPath.equals("null")) { +// // 如果nacos中没有配置红外路径,则将结果返回调用方 +// try { +// log.info("[BIG MODEL] filterPicAnalyse: nacos file.infraredPath not config, feedback to caller, analyseResult={}", new Gson().toJson(analyseResult)); +// Call call = analysisRemoteService.picAnalyseRetNotify(analyseResult); +// Response response = call.execute(); +// Object result = response.body(); +// System.out.println("result: " + result); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } else { +// // 如果nacos中配置了红外路径,则继续调用红外模块 +// try { +// log.info("[BIG MODEL] filterPicAnalyse: continue to call infrared, infraredPath: {}, analyseRequestStr={}", infraredPath, analyseRequestStr); +// String result = HttpClientUtils.sendPostAgain(infraredPath, analyseRequestStr); +// log.info("[BIG MODEL] continue to call infrared RESULT: {}", result); +// } catch (Exception e) { +// log.info("[BIG MODEL] continue to call infrared EXCEPTION: {}", e.getMessage()); +// } +// } +// return ResponseEntity +// .ok() +// .body("{\"code\":\"200\"}"); + return ResponseEntity.ok().body(algorithmService.bigModelPicAnalyse(analyseRequestStr)); + } - log.info("[FILTER] bigModelPicAnalyse: analyseResult={}", new Gson().toJson(analyseResult)); - try { - Call call = analysisRemoteService.picAnalyseRetNotify(analyseResult); - Response response = call.execute(); - Object result = response.body(); - System.out.println("result: " + result); - } catch (Exception e) { - e.printStackTrace(); - } + @PostMapping("/simu/infrared/picAnalyse") + public ResponseEntity infraredPicAnalyse(@RequestBody final String analyseRequestStr) { return ResponseEntity .ok() - .body("{\"code\":\"200\"}"); + .body(algorithmService.infraredPicAnalyse(analyseRequestStr)); } } diff --git a/src/main/java/com/inspect/simulator/domain/algorithm/in/AnalyseRequest.java b/src/main/java/com/inspect/simulator/domain/algorithm/in/AnalyseRequest.java index 8bbf436..3b3bd17 100644 --- a/src/main/java/com/inspect/simulator/domain/algorithm/in/AnalyseRequest.java +++ b/src/main/java/com/inspect/simulator/domain/algorithm/in/AnalyseRequest.java @@ -21,6 +21,10 @@ public class AnalyseRequest implements Serializable { private String requestId; private String algorithmPath; private String taskPatrolId; + private String sftpHostIp; + private String sftpHostPort; + private String sftpUsername; + private String sftpPassword; public AnalyseRequest clone() { return JSONObject.parseObject(JSONObject.toJSONString(this), AnalyseRequest.class); diff --git a/src/main/java/com/inspect/simulator/service/AlgorithmService.java b/src/main/java/com/inspect/simulator/service/AlgorithmService.java new file mode 100644 index 0000000..4f4a4c1 --- /dev/null +++ b/src/main/java/com/inspect/simulator/service/AlgorithmService.java @@ -0,0 +1,13 @@ +package com.inspect.simulator.service; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestBody; + +public interface AlgorithmService { + + String filterPicAnalyse(final String analyseRequestStr); + + String bigModelPicAnalyse(final String analyseRequestStr); + + String infraredPicAnalyse(final String analyseRequestStr); +} diff --git a/src/main/java/com/inspect/simulator/service/impl/AlgorithmServiceImpl.java b/src/main/java/com/inspect/simulator/service/impl/AlgorithmServiceImpl.java new file mode 100644 index 0000000..9f31b19 --- /dev/null +++ b/src/main/java/com/inspect/simulator/service/impl/AlgorithmServiceImpl.java @@ -0,0 +1,175 @@ +package com.inspect.simulator.service.impl; + +import com.alibaba.fastjson.JSONObject; +import com.google.gson.Gson; +import com.inspect.simulator.domain.algorithm.in.AnalyseReqItem; +import com.inspect.simulator.domain.algorithm.in.AnalyseRequest; +import com.inspect.simulator.domain.algorithm.out.AnalyseResItem; +import com.inspect.simulator.domain.algorithm.out.AnalyseResPoint; +import com.inspect.simulator.domain.algorithm.out.AnalyseResult; +import com.inspect.simulator.service.AlgorithmService; +import com.inspect.simulator.service.remote.AnalysisRemoteService; +import com.inspect.simulator.utils.HttpClientUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import retrofit2.Call; +import retrofit2.Response; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; + +@Service +public class AlgorithmServiceImpl implements AlgorithmService { + private final Logger log = LoggerFactory.getLogger(this.getClass()); + + @Value("${file.infraredPath:null}") + private String infraredPath; + + @Resource + private AnalysisRemoteService analysisRemoteService; + + @Override + public String filterPicAnalyse(String analyseRequestStr) { + log.info("[FILTER] filterPicAnalyse: analyseRequestStr={}", analyseRequestStr); + AnalyseRequest analyseRequest = new Gson().fromJson(analyseRequestStr, AnalyseRequest.class); + log.info("[FILTER] filterPicAnalyse: analyseRequest={}", new Gson().toJson(analyseRequest)); + + AnalyseResult analyseResult = new AnalyseResult(); + analyseResult.setRequestId(analyseRequest.getRequestId()); + AnalyseResPoint analyseResPoint = new AnalyseResPoint(); + analyseResPoint.setValue("0"); + analyseResPoint.setConf("0.85"); + analyseResPoint.setCode("2000"); + analyseResPoint.setResImageUrl(analyseRequest.getObjectList().get(0).getImageUrlList()[0]); + List analyseResPoints = new ArrayList<>(); + analyseResPoints.add(analyseResPoint); + AnalyseResItem analyseResItem = new AnalyseResItem(); + analyseResItem.setObjectId(analyseRequest.getObjectList().get(0).getObjectId()); + analyseResItem.setAlgFactory("yd"); + analyseResItem.setResults(analyseResPoints); + List analyseResItems = new ArrayList<>(); + analyseResItems.add(analyseResItem); + analyseResult.setResultList(analyseResItems); + + log.info("[FILTER] filterPicAnalyse: analyseResult={}", new Gson().toJson(analyseResult)); + try { + Call call = analysisRemoteService.picAnalyseRetNotify(analyseResult); + Response response = call.execute(); + Object result = response.body(); + System.out.println("result: " + result); + } catch (Exception e) { + e.printStackTrace(); + } + return "{\"code\":\"200\"}"; + } + + @Override + public String bigModelPicAnalyse(String analyseRequestStr) { + log.info("[FILTER] bigModelPicAnalyse: analyseRequestStr={}", analyseRequestStr); + AnalyseRequest analyseRequest = new Gson().fromJson(analyseRequestStr, AnalyseRequest.class); + log.info("[FILTER] bigModelPicAnalyse: analyseRequest={}", new Gson().toJson(analyseRequest)); + + AnalyseResult analyseResult = new AnalyseResult(); + analyseResult.setRequestId(analyseRequest.getRequestId()); + AnalyseResPoint analyseResPoint = new AnalyseResPoint(); + analyseResPoint.setValue("1"); + analyseResPoint.setConf("0.95"); + analyseResPoint.setCode("2000"); + analyseResPoint.setResImageUrl(analyseRequest.getObjectList().get(0).getImageUrlList()[0]); + List analyseResPoints = new ArrayList<>(); + analyseResPoints.add(analyseResPoint); + AnalyseResItem analyseResItem = new AnalyseResItem(); + analyseResItem.setObjectId(analyseRequest.getObjectList().get(0).getObjectId()); + analyseResItem.setAlgFactory("yd"); + analyseResItem.setResults(analyseResPoints); + List analyseResItems = new ArrayList<>(); + analyseResItems.add(analyseResItem); + analyseResult.setResultList(analyseResItems); + + if (infraredPath.equals("null")) { + // 如果nacos中没有配置红外路径,则将结果返回调用方 + try { + log.info("[BIG MODEL] filterPicAnalyse: nacos file.infraredPath not config, feedback to caller, analyseResult={}", new Gson().toJson(analyseResult)); + Call call = analysisRemoteService.picAnalyseRetNotify(analyseResult); + Response response = call.execute(); + Object result = response.body(); + System.out.println("result: " + result); + } catch (Exception e) { + e.printStackTrace(); + } + } else { + // 如果nacos中配置了红外路径,则继续调用红外模块 + try { + log.info("[BIG MODEL] filterPicAnalyse: continue to call infrared, infraredPath: {}, analyseRequestStr={}", infraredPath, analyseRequestStr); + String result = HttpClientUtils.sendPostAgain(infraredPath, analyseRequestStr); + log.info("[BIG MODEL] continue to call infrared RESULT: {}", result); + } catch (Exception e) { + log.info("[BIG MODEL] continue to call infrared EXCEPTION: {}", e.getMessage()); + } + } + return "{\"code\":\"200\"}"; + } + + @Override + public String infraredPicAnalyse(String analyseRequestStr) { + log.info("[INFRARED] irPicAnalyse: analyseRequestStr={}", analyseRequestStr); + AnalyseRequest analyseRequest; + try { + analyseRequest = new Gson().fromJson(analyseRequestStr, AnalyseRequest.class); + } catch (Exception e) { + log.error("[INFRARED] irPicAnalyse: analyseRequestJson parse exception: {}", e.getMessage()); + return "{\"code\":\"201\"}"; + } + log.info("[INFRARED] irPicAnalyse: analyseRequest={}", new Gson().toJson(analyseRequest)); + + + List analyseReqItemList = analyseRequest.getObjectList(); + if(analyseReqItemList == null || analyseReqItemList.isEmpty()) { + log.error("[INFRARED] irPicAnalyse: analyseReqItemList empty!"); + return "{\"code\":\"202\"}"; + } + + final AnalyseReqItem analyseReqItem = analyseReqItemList.get(0); + final String[] typeList = analyseReqItem.getTypeList(); + String algType = "unKnownType"; + if(typeList != null && typeList.length != 0) { + algType = typeList[0]; + } + final String feedBackHostIp = analyseRequest.getRequestHostIp(); + final String feedBackPort = analyseRequest.getRequestHostPort(); + final String feedbackUrl = feedBackHostIp + feedBackPort + "/picAnalyseRetNotify"; + log.info("[INFRARED] irPicAnalyse: feedbackUrl={}", feedbackUrl); + + AnalyseResult analyseResult = new AnalyseResult(); + analyseResult.setRequestId(analyseRequest.getRequestId()); + AnalyseResPoint analyseResPoint = new AnalyseResPoint(); + analyseResPoint.setValue("0"); + analyseResPoint.setConf("0.85"); + analyseResPoint.setCode("2000"); + analyseResPoint.setResImageUrl(analyseRequest.getObjectList().get(0).getImageUrlList()[0]); + List analyseResPoints = new ArrayList<>(); + analyseResPoints.add(analyseResPoint); + AnalyseResItem analyseResItem = new AnalyseResItem(); + analyseResItem.setObjectId(analyseRequest.getObjectList().get(0).getObjectId()); + analyseResItem.setAlgFactory(algType); + analyseResItem.setResults(analyseResPoints); + List analyseResItems = new ArrayList<>(); + analyseResItems.add(analyseResItem); + analyseResult.setResultList(analyseResItems); + + try { + String analyseResultOutJson = JSONObject.toJSONString(analyseResult); + log.info("[INFRARED] irPicAnalyse: feedbackUrl={}, analyseResultOutJson={}", feedbackUrl, analyseResultOutJson); + String result = HttpClientUtils.sendPostAgain(feedbackUrl, analyseResultOutJson); + log.info("[INFRARED] irPicAnalyse: feedbackUrl={}, result: {}", feedbackUrl, result); + } catch (Exception e) { + log.info("[INFRARED] irPicAnalyse: feedbackUrl={}, EXCEPTION: {}", feedbackUrl, e.getMessage()); + } + + return "{\"code\":\"200\"}"; + } +}