From 89fd947f31f5b98953796d790d627bc4fc63ea0e Mon Sep 17 00:00:00 2001 From: htjcAdmin Date: Mon, 21 Apr 2025 10:59:25 +0800 Subject: [PATCH] =?UTF-8?q?/*=E7=BA=A2=E5=A4=96=E7=AE=97=E6=B3=95=E5=85=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=B9=E6=88=90=E8=A7=84=E8=8C=83=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=9A=84picAnalyse*/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/InfraredController.java | 8 +- .../simulator/service/HikVisionService.java | 3 +- .../service/impl/HikVisionServiceImpl.java | 162 ++++++++---- .../simulator/utils/HttpClientUtils.java | 236 ++++++++++++++++++ 4 files changed, 354 insertions(+), 55 deletions(-) create mode 100644 src/main/java/com/inspect/simulator/utils/HttpClientUtils.java diff --git a/src/main/java/com/inspect/simulator/controller/InfraredController.java b/src/main/java/com/inspect/simulator/controller/InfraredController.java index 86d4195..fc172fa 100644 --- a/src/main/java/com/inspect/simulator/controller/InfraredController.java +++ b/src/main/java/com/inspect/simulator/controller/InfraredController.java @@ -4,6 +4,7 @@ import com.inspect.simulator.domain.Infrared.*; import com.inspect.simulator.service.HikVisionService; import com.inspect.simulator.hikVision.utils.AjaxResult; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -51,10 +52,9 @@ public class InfraredController { } //图谱解析 - @PostMapping("/ir/calculatePicture") + @PostMapping("/ir/picAnalyse") @ResponseBody - public AjaxResult calculatePicture(@RequestBody InfraPictureInfo infraPictureInfo) { - - return hikVisionService.calculatePicture(infraPictureInfo); + public ResponseEntity irPicAnalyse(@RequestBody final String analyseRequest) { + return hikVisionService.irPicAnalyse(analyseRequest); } } diff --git a/src/main/java/com/inspect/simulator/service/HikVisionService.java b/src/main/java/com/inspect/simulator/service/HikVisionService.java index fced16d..0fbd661 100644 --- a/src/main/java/com/inspect/simulator/service/HikVisionService.java +++ b/src/main/java/com/inspect/simulator/service/HikVisionService.java @@ -2,6 +2,7 @@ package com.inspect.simulator.service; import com.inspect.simulator.domain.Infrared.*; import com.inspect.simulator.hikVision.utils.AjaxResult; +import org.springframework.http.ResponseEntity; public interface HikVisionService { @@ -18,5 +19,5 @@ public interface HikVisionService { public float[][] UploadFtpImage( String url, String type, String image); //图谱解析 - AjaxResult calculatePicture(InfraPictureInfo infraPictureInfo); + ResponseEntity irPicAnalyse(String analyseRequest); } diff --git a/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java b/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java index 4b4c25e..dcf1dd1 100644 --- a/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java +++ b/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java @@ -9,7 +9,13 @@ import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; import com.inspect.simulator.domain.Infrared.*; +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.domain.basedata.BasedataEqpBookChannel; import com.inspect.simulator.hikVision.utils.AjaxResult; import com.inspect.simulator.hikVision.utils.StringUtils; @@ -17,6 +23,7 @@ import com.inspect.simulator.hikVision.utils.jna.HikVisionUtils; import com.inspect.simulator.hikVision.utils.jna.HCNetSDK; import com.inspect.simulator.mapper.BasedataEqpBookChannelMapper; import com.inspect.simulator.service.HikVisionService; +import com.inspect.simulator.utils.HttpClientUtils; import com.inspect.simulator.utils.sftp.SftpClient; import com.sun.jna.ptr.IntByReference; @@ -31,6 +38,7 @@ 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.http.ResponseEntity; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -313,13 +321,13 @@ public class HikVisionServiceImpl implements HikVisionService { int secondY = 640; int imgWidth = 640; int imgHeight = 512; - if(infraPictureInfo.getCoordinates() != null && !infraPictureInfo.getCoordinates().isEmpty()) { + if (infraPictureInfo.getCoordinates() != null && !infraPictureInfo.getCoordinates().isEmpty()) { firstX = infraPictureInfo.getCoordinates().get(0).getFirstX(); firstY = infraPictureInfo.getCoordinates().get(0).getFirstY(); secondX = infraPictureInfo.getCoordinates().get(0).getSecondX(); secondY = infraPictureInfo.getCoordinates().get(0).getSecondY(); imgWidth = infraPictureInfo.getImgHeight(); - imgHeight = infraPictureInfo.getImgWidth(); + imgHeight = infraPictureInfo.getImgWidth(); } String channelContent = firstX + "," + firstY + "," + secondX + "," + secondY + "," + imgWidth + "," + imgHeight; @@ -1174,58 +1182,112 @@ public class HikVisionServiceImpl implements HikVisionService { @Override - public AjaxResult calculatePicture(InfraPictureInfo infraPictureInfo) { + public ResponseEntity irPicAnalyse(final String analyseRequestJson) { + log.info("[INFRARED] irPicAnalyse: analyseRequestJson={}", analyseRequestJson); + AnalyseRequest analyseRequest; + try { + analyseRequest = new Gson().fromJson(analyseRequestJson, AnalyseRequest.class); + } catch (Exception e) { + log.error("[INFRARED] irPicAnalyse: analyseRequestJson parse exception: {}", e.getMessage()); + return ResponseEntity.ok().body("{\"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 ResponseEntity.ok().body("{\"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); - InfraredInfo infraredInfo = new InfraredInfo(); try { - String imagePath = infraPictureInfo.getFilePath(); -// String imagePath = "/images/ir_test.jpg"; - InputStream inputStream = downloadFtp(imagePath); - String protocol = hrFtpUrl.substring(0, 6); // "ftp://" - String withoutProtocol = hrFtpUrl.substring(6); // "zthr02:zthr02@123.184.14.138:50021/" - String[] userAndHost = withoutProtocol.split("@"); - if (userAndHost.length != 2) { - throw new IllegalArgumentException("URL 格式错误,缺少 @ 分隔符"); - } - String[] userPass = userAndHost[0].split(":"); - String username = userPass[0]; - String password = userPass[1]; - String hostPort = userAndHost[1].replace("/", ""); // 移除末尾的 "/" - String[] hostAndPort = hostPort.split(":"); - String host = hostAndPort[0]; - int port = Integer.parseInt(hostAndPort[1]); - picFtp("/", inputStream, host, port, username, password); - - String imageName = Paths.get(imagePath).getFileName().toString(); - String ftpUrlName = "[\"" + hrFtpUrl + imageName + "\"]"; - - - if (infraPictureInfo.getImgType() == 1) { - byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath)); - infraredInfo = readDataHw(imageBytes, infraPictureInfo.getCoordinates().get(0)); - } else if (infraPictureInfo.getImgType() == 2) { - -// String s = ImageOverlaysIvs(infraPictureInfo.getCoordinates().get(0), infraredInfo); -// infraredInfo.setOutPath(s); - } else if (infraPictureInfo.getImgType() == 3) { - //sdk版本调用x86_32 - //float[][] imageTem = djService.getImageTem(coordinate.getFilePath()); - - //调用华软接口 - float[][] imageTem = UploadFtpImage(hrUavUrl, null, ftpUrlName); - if (imageTem != null && imageTem.length > 0 && imageTem[0].length > 0) { - infraredInfo.setTemperatureMatrix(imageTem); - String s = ImageOverlaysUav(infraPictureInfo, infraredInfo); - infraredInfo.setOutPath(s); - } else { - return AjaxResult.error("温度解析错误!"); - } - } + 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) { - e.printStackTrace(); - return AjaxResult.error(); + log.info("[INFRARED] irPicAnalyse: feedbackUrl={}, EXCEPTION: {}", feedbackUrl, e.getMessage()); } - return AjaxResult.success(infraredInfo); + + return ResponseEntity.ok().body("{\"code\":\"200\"}"); +// InfraredInfo infraredInfo = new InfraredInfo(); +// try { +// String imagePath = infraPictureInfo.getFilePath(); +//// String imagePath = "/images/ir_test.jpg"; +// InputStream inputStream = downloadFtp(imagePath); +// String protocol = hrFtpUrl.substring(0, 6); // "ftp://" +// String withoutProtocol = hrFtpUrl.substring(6); // "zthr02:zthr02@123.184.14.138:50021/" +// String[] userAndHost = withoutProtocol.split("@"); +// if (userAndHost.length != 2) { +// throw new IllegalArgumentException("URL 格式错误,缺少 @ 分隔符"); +// } +// String[] userPass = userAndHost[0].split(":"); +// String username = userPass[0]; +// String password = userPass[1]; +// String hostPort = userAndHost[1].replace("/", ""); // 移除末尾的 "/" +// String[] hostAndPort = hostPort.split(":"); +// String host = hostAndPort[0]; +// int port = Integer.parseInt(hostAndPort[1]); +// picFtp("/", inputStream, host, port, username, password); +// +// String imageName = Paths.get(imagePath).getFileName().toString(); +// String ftpUrlName = "[\"" + hrFtpUrl + imageName + "\"]"; +// +// +// if (infraPictureInfo.getImgType() == 1) { +// byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath)); +// infraredInfo = readDataHw(imageBytes, infraPictureInfo.getCoordinates().get(0)); +// } else if (infraPictureInfo.getImgType() == 2) { +// +//// String s = ImageOverlaysIvs(infraPictureInfo.getCoordinates().get(0), infraredInfo); +//// infraredInfo.setOutPath(s); +// } else if (infraPictureInfo.getImgType() == 3) { +// //sdk版本调用x86_32 +// //float[][] imageTem = djService.getImageTem(coordinate.getFilePath()); +// +// //调用华软接口 +// float[][] imageTem = UploadFtpImage(hrUavUrl, null, ftpUrlName); +// if (imageTem != null && imageTem.length > 0 && imageTem[0].length > 0) { +// infraredInfo.setTemperatureMatrix(imageTem); +// String s = ImageOverlaysUav(infraPictureInfo, infraredInfo); +// infraredInfo.setOutPath(s); +// } else { +// return AjaxResult.error("温度解析错误!"); +// } +// } +// } catch (Exception e) { +// e.printStackTrace(); +// return AjaxResult.error(); +// } +// return AjaxResult.success(infraredInfo); } diff --git a/src/main/java/com/inspect/simulator/utils/HttpClientUtils.java b/src/main/java/com/inspect/simulator/utils/HttpClientUtils.java new file mode 100644 index 0000000..943a5a6 --- /dev/null +++ b/src/main/java/com/inspect/simulator/utils/HttpClientUtils.java @@ -0,0 +1,236 @@ +package com.inspect.simulator.utils; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; + +public class HttpClientUtils { + public static String post(String requestUrl) { + return post(requestUrl, "", ""); + } + + public static String post(String requestUrl, String requestPram) { + return post(requestUrl, requestPram, ""); + } + + public static String post(String requestUrl, String requestPram, String accessToken) { + OutputStreamWriter outputStreamWriter = null; + BufferedReader bufferedReader = null; + StringBuffer responseResult = new StringBuffer(); + HttpURLConnection httpURLConnection = null; + + try { + URL realUrl = new URL(requestUrl); + httpURLConnection = (HttpURLConnection) realUrl.openConnection(); + httpURLConnection.setRequestProperty("accept", "*/*"); + httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + httpURLConnection.setRequestProperty("connection", "Keep-Alive"); + if (!accessToken.equals("") || accessToken != null) { + httpURLConnection.setRequestProperty("access-token", accessToken); + } + + httpURLConnection.setRequestProperty("Content-Length", String.valueOf(requestPram.length())); + httpURLConnection.setDoOutput(true); + httpURLConnection.setDoInput(true); + outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8"); + outputStreamWriter.write(requestPram); + outputStreamWriter.flush(); + bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8")); + + String line; + while ((line = bufferedReader.readLine()) != null) { + responseResult.append(line); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + httpURLConnection.disconnect(); + + try { + if (outputStreamWriter != null) { + outputStreamWriter.close(); + } + + if (bufferedReader != null) { + bufferedReader.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + return responseResult.toString(); + } + + public static InputStream getStream(String url, String param) throws Exception { + param = param.replace("#", "%23"); + URLConnection connection = getConnection(url, param); + return connection.getInputStream(); + } + + public static URLConnection getConnection(String url, String param) throws Exception { + String urlNameString = url + "?" + param; + URL realUrl = new URL(urlNameString); + URLConnection connection = realUrl.openConnection(); + connection.setRequestProperty("accept", "*/*"); + connection.setRequestProperty("connection", "Keep-Alive"); + connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + connection.connect(); + return connection; + } + + public static HttpURLConnection getConnection2(String url, String param) throws Exception { + String urlNameString = url + "?" + param; + URL realUrl = new URL(urlNameString); + HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); + connection.setRequestProperty("accept", "*/*"); + connection.setRequestProperty("connection", "Keep-Alive"); + connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + connection.connect(); + return connection; + } + + public static String get(String url, String param) throws Exception { + StringBuilder result = new StringBuilder(); + BufferedReader in = null; + try { + param = param.replace(" ", "%20"); + param = param.replace("#", "%23"); + URL realUrl = new URL(url + "?" + param); + URLConnection connection = realUrl.openConnection(); + connection.setRequestProperty("accept", "*/*"); + connection.setRequestProperty("connection", "Keep-Alive"); + connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + connection.connect(); + in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + + String line; + while ((line = in.readLine()) != null) { + result.append(line); + } + } catch (Exception e) { + throw e; + } finally { + if (null != in) { + in.close(); + } + } + + return result.toString(); + } + + public static String postJson(String requestUrl, String requestPram, String tokenKey, String accessToken) { + OutputStreamWriter outputStreamWriter = null; + BufferedReader bufferedReader = null; + StringBuffer responseResult = new StringBuffer(); + HttpURLConnection httpURLConnection = null; + + try { + URL realUrl = new URL(requestUrl); + httpURLConnection = (HttpURLConnection) realUrl.openConnection(); + httpURLConnection.setRequestProperty("accept", "*/*"); + httpURLConnection.setRequestProperty("Content-Type", "application/json"); + httpURLConnection.setRequestProperty("connection", "Keep-Alive"); + if (StringUtils.isNotBlank(accessToken)) { + if (StringUtils.isNotBlank(tokenKey)) { + httpURLConnection.setRequestProperty(tokenKey, accessToken); + } else { + httpURLConnection.setRequestProperty("access-token", accessToken); + } + } + + httpURLConnection.setRequestProperty("Content-Length", String.valueOf(requestPram.length())); + httpURLConnection.setDoOutput(true); + httpURLConnection.setDoInput(true); + outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8"); + outputStreamWriter.write(requestPram); + outputStreamWriter.flush(); + bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8")); + + String line; + while ((line = bufferedReader.readLine()) != null) { + responseResult.append(line); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + httpURLConnection.disconnect(); + + try { + if (outputStreamWriter != null) { + outputStreamWriter.close(); + } + + if (bufferedReader != null) { + bufferedReader.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + return responseResult.toString(); + } + + public static String postJson(String requestUrl, String requestPram, String accessToken) { + return postJson(requestUrl, requestPram, "", accessToken); + } + + public static String sendPostAgain(String url, String param) { + PrintWriter out = null; + BufferedReader in = null; + StringBuilder result = new StringBuilder(); + URLConnection conn = null; + + try { + URL realUrl = new URL(url); + conn = realUrl.openConnection(); + conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); + conn.setConnectTimeout(30000); + conn.setReadTimeout(120000); + conn.setDoOutput(true); + conn.setDoInput(true); + out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8")); + out.print(param); + out.flush(); + InputStream stream = conn.getInputStream(); + in = new BufferedReader(new InputStreamReader(stream, "UTF-8")); + + String line; + while ((line = in.readLine()) != null) { + result.append(line); + } + } catch (Exception e) { + e.printStackTrace(); + + try { + in = new BufferedReader(new InputStreamReader(((HttpURLConnection) conn).getErrorStream(), "UTF-8")); + + String line; + while ((line = in.readLine()) != null) { + result.append(line); + } + } catch (IOException e2) { + e2.printStackTrace(); + } + } finally { + try { + if (out != null) { + out.close(); + } + + if (in != null) { + in.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + return result.toString(); + } +}