Преглед изворни кода

feat(海康消费机对接): 消费机上报数据解析

luo.yibo@datuai.com пре 1 година
родитељ
комит
af37474acb

+ 10 - 0
ruoyi-server/ruoyi-server-hik/pom.xml

@@ -143,6 +143,16 @@
             <artifactId>hutool-all</artifactId>
             <version>5.8.27</version>
         </dependency>
+        <dependency>
+            <groupId>commons-fileupload</groupId>
+            <artifactId>commons-fileupload</artifactId>
+            <version>1.4</version>
+        </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.6</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 68 - 0
ruoyi-server/ruoyi-server-hik/src/main/java/org/dromara/server/hik/controller/TestController.java

@@ -1,6 +1,10 @@
 package org.dromara.server.hik.controller;
 
 import cn.dev33.satoken.annotation.SaIgnore;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
 import lombok.RequiredArgsConstructor;
 import org.dromara.common.core.domain.R;
 import org.dromara.server.hik.domain.dto.DeviceDto;
@@ -9,6 +13,11 @@ import org.dromara.server.hik.domain.dto.UploadEmpDto;
 import org.dromara.server.hik.service.ISendDeviceService;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.nio.file.Path;
 
 /**
  * 海康消费机
@@ -213,4 +222,63 @@ public class TestController {
         return sendDeviceService.deleteAllCardByUserNo(device,userNo);
     }
 
+    @PostMapping(("/info"))
+    public void receiveDevicePushData(HttpServletRequest request, HttpServletResponse response) throws IOException {
+        try {
+            // 判断请求类型
+            if (isMultipartRequest(request)) {
+                // 转换为 Multipart 请求对象
+                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
+
+                multipartRequest.getParameterMap().forEach((fieldName, values) -> {
+                    System.out.println("字段 [" + fieldName + "] 值: " + String.join(", ", values));
+                });
+
+                multipartRequest.getFileMap().forEach((fieldName, file) -> {
+                    if (!file.isEmpty()) {
+                        String fileName = file.getOriginalFilename();
+                        try {
+                            file.transferTo(Path.of("f:/uploads", fileName));
+                        } catch (IOException e) {
+                            throw new RuntimeException("文件保存失败: " + fileName, e);
+                        }
+                    }
+                });
+            } else {
+                // 处理 JSON 请求
+                handleJsonRequest(request);
+            }
+
+            response.getWriter().write("success");
+        } catch (Exception e) {
+            e.printStackTrace();
+            response.getWriter().write("false");
+        }
+    }
+
+    // 判断是否是 multipart 请求
+    private boolean isMultipartRequest(HttpServletRequest request) {
+        return request.getContentType() != null
+                   && request.getContentType().startsWith("multipart/form-data");
+    }
+
+      // 处理 JSON 请求
+    private void handleJsonRequest(HttpServletRequest request) throws IOException {
+        if (request.getContentType() != null
+                && request.getContentType().contains("application/json")) {
+
+            try (BufferedReader reader = request.getReader()) {
+                StringBuilder requestData = new StringBuilder();
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    requestData.append(line);
+                }
+
+                // 使用任意 JSON 库解析
+                JSONObject jsonObject = JSONUtil.parseObj(requestData.toString());
+                System.out.println("收到 JSON 数据:");
+                System.out.println(jsonObject.toString());
+            }
+        }
+    }
 }

+ 5 - 0
ruoyi-server/ruoyi-server-hik/src/main/java/org/dromara/server/hik/domain/dto/DeviceDto.java

@@ -61,4 +61,9 @@ public class DeviceDto implements Serializable {
      * 服务监听端口
      */
     private Integer serverPort;
+
+    /**
+     * 服务监听地址
+     */
+    private String serverUrl;
 }

+ 2 - 2
ruoyi-server/ruoyi-server-hik/src/main/java/org/dromara/server/hik/service/impl/SendDeviceServiceImpl.java

@@ -172,7 +172,7 @@ public class SendDeviceServiceImpl implements ISendDeviceService {
                                  "<HttpHostNotification version=\"2.0\"\n" +
                                  "    xmlns=\"http://www.isapi.org/ver20/XMLSchema\">\n" +
                                  "    <id>1</id>\n" +
-                                 "    <url>/hik/test</url>\n" +
+                                 "    <url>{}</url>\n" +
                                  "    <protocolType>HTTP</protocolType>\n" +
                                  "    <parameterFormatType>XML</parameterFormatType>\n" +
                                  "    <addressingFormatType>ipaddress</addressingFormatType>\n" +
@@ -190,7 +190,7 @@ public class SendDeviceServiceImpl implements ISendDeviceService {
                                  "    </SubscribeEvent>\n" +
                                  "</HttpHostNotification>\n";
 
-        return StringUtils.format(xmlTemplate, dto.getServerIp(), dto.getServerPort());
+        return StringUtils.format(xmlTemplate,dto.getServerUrl(), dto.getServerIp(), dto.getServerPort());
     }
 
     /**