From 866a3e6cbd2df9841dfbbd733e1128938cef3e00 Mon Sep 17 00:00:00 2001
From: zouyu <2723363702@qq.com>
Date: 星期二, 09 十二月 2025 17:45:16 +0800
Subject: [PATCH] 采集器调整

---
 src/main/java/com/chinaztt/mes/docx/service/impl/DocxServiceImpl.java |  113 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 103 insertions(+), 10 deletions(-)

diff --git a/src/main/java/com/chinaztt/mes/docx/service/impl/DocxServiceImpl.java b/src/main/java/com/chinaztt/mes/docx/service/impl/DocxServiceImpl.java
index f3335d8..a10f958 100644
--- a/src/main/java/com/chinaztt/mes/docx/service/impl/DocxServiceImpl.java
+++ b/src/main/java/com/chinaztt/mes/docx/service/impl/DocxServiceImpl.java
@@ -3,16 +3,20 @@
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.chinaztt.mes.docx.dto.GetFileDto;
-import com.chinaztt.mes.docx.dto.ThicknessData;
 import com.chinaztt.mes.docx.service.DocxService;
 import com.chinaztt.mes.docx.util.R;
 import com.chinaztt.mes.docx.util.TakeWords;
+import com.fazecast.jSerialComm.SerialPort;
+import com.fazecast.jSerialComm.SerialPortDataListener;
+import com.fazecast.jSerialComm.SerialPortEvent;
 import net.sourceforge.tess4j.TesseractException;
-import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
 
-import java.io.*;
-import java.sql.*;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
@@ -22,20 +26,24 @@
 
     @Override
     public R<?> getFile(GetFileDto getFileDto) throws IOException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, TesseractException {
-        File file = getFileStart(getFileDto.getFilePath(), getFileDto.getFileExtension());
+        String fileExtension = getFileDto.getFileExtension();
+        if(StringUtils.equals(".pngInExcel",getFileDto.getFileExtension())){
+            fileExtension = ".xls";
+        }
+        File file = getFileStart(getFileDto.getFilePath(), fileExtension);
         if (file != null && !file.exists()) {
             return R.failed("鏈煡璇㈠埌璇ヨ矾寰勶細" + getFileDto.getFilePath() + "涓嬪瓨鍦細" + getFileDto.getFileExtension() + "缁撳熬鐨勬枃浠讹紒");
         }
         switch (getFileDto.getFileExtension()) {
             case ".docx":
                 return R.ok(TakeWords.readWordFile(file));
-            //鍚庣紑涓�.xls鐨勬枃浠�
+            case ".pngInExcel":
+                return R.ok(TakeWords.readPngContextInExcel(file));
             case ".xls":
-                return R.ok(TakeWords.readExcelxlsFile(file));
             case ".xlsx":
                 try {
                     return R.ok(TakeWords.readExcelFile(file));
-                } catch (FileNotFoundException e) {
+                } catch (Exception e) {
                     return R.failed("鍙︿竴涓▼搴忔鍦ㄤ娇鐢ㄦ鏂囦欢锛屾棤娉曡繘琛屾暟鎹噰闆嗐��");
                 }
             case ".txt":
@@ -59,6 +67,8 @@
                 return R.ok(TakeWords.getMysqlFile(getFileDto));
             case ".png":
                 return R.ok(TakeWords.readPngFile(file));
+            case ".mqtt":
+                return R.ok("mqtt---");
             default:
                 return R.failed("鍚庣紑鍚嶉厤缃敊璇紒");
         }
@@ -112,10 +122,11 @@
             boolean b;
             switch (fileExtension) {
                 case ".docx":
-                    b = i.getName().endsWith(".docx") || i.getName().endsWith(".doc");
+                    b = i.getName().endsWith(".docx") || i.getName().endsWith(".doc") || i.getName().endsWith(".DOCX") || i.getName().endsWith(".DOC");
                     break;
                 case ".xlsx":
-                    b = i.getName().endsWith(".xlsx") || i.getName().endsWith(".xls");
+
+                    b = i.getName().endsWith(".xlsx") || i.getName().endsWith(".xls") || i.getName().endsWith(".XLSX") || i.getName().endsWith(".XLS");
                     break;
                 default:
                     b = i.getName().endsWith(fileExtension);
@@ -127,4 +138,86 @@
         });
         return getLatestFile(list);
     }
+
+    @Override
+    public void test(){
+        // 1. 鑾峰彇鎵�鏈夊彲鐢ㄧ殑涓插彛
+        SerialPort[] ports = SerialPort.getCommPorts();
+        if (ports.length == 0) {
+            System.err.println("鏈壘鍒板彲鐢ㄧ殑涓插彛锛�");
+            return;
+        }
+
+        // 鎵撳嵃鎵�鏈夊彲鐢ㄤ覆鍙o紙渚涚敤鎴烽�夋嫨锛�
+        System.out.println("鍙敤涓插彛鍒楄〃锛�");
+        SerialPort targetPort = null;
+        for (int i = 0; i < ports.length; i++) {
+            if(ports[i].getSystemPortName().equals("COM7")){
+                targetPort = ports[i];
+            }
+            System.out.printf("%d: %s (%s)\n", i + 1, ports[i].getSystemPortName(), ports[i].getPortDescription());
+        }
+
+        // 2. 閫夋嫨鐩爣涓插彛锛堢ず渚嬶細閫夋嫨绗竴涓覆鍙o級
+
+        System.out.println("\n閫夋嫨鐨勪覆鍙o細" + targetPort.getSystemPortName());
+
+        // 3. 閰嶇疆涓插彛鍙傛暟锛堝繀椤讳笌璁惧涓�鑷达紝鍚﹀垯閫氫俊澶辫触锛�
+        targetPort.setComPortParameters(
+                9600,                // 娉㈢壒鐜囷紙甯歌鍊硷細9600銆�19200銆�38400銆�115200锛�
+                8,                   // 鏁版嵁浣嶏紙閫氬父涓� 8锛�
+                SerialPort.ONE_STOP_BIT,  // 鍋滄浣嶏紙1 浣嶏級
+                SerialPort.NO_PARITY     // 鏍¢獙浣嶏紙鏃犳牎楠岋級
+        );
+        targetPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 0); // 璇诲彇瓒呮椂璁剧疆
+
+        // 4. 鎵撳紑涓插彛
+        if (!targetPort.openPort()) {
+            System.err.println("涓插彛鎵撳紑澶辫触锛佽妫�鏌ョ鍙f槸鍚﹁鍗犵敤鎴栨潈闄愭槸鍚﹁冻澶熴��");
+            return;
+        }
+        System.out.println("涓插彛鎵撳紑鎴愬姛锛�");
+
+        // 5. 娉ㄥ唽鏁版嵁鐩戝惉锛堝紓姝ヨ鍙栨暟鎹級
+        SerialPort finalTargetPort = targetPort;
+        targetPort.addDataListener(new SerialPortDataListener() {
+            @Override
+            public int getListeningEvents() {
+                return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; // 鐩戝惉鏁版嵁鍙敤浜嬩欢
+            }
+
+            @Override
+            public void serialEvent(SerialPortEvent event) {
+                System.out.println("asasa"+event.toString());
+                if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
+                    return;
+                }
+
+                // 璇诲彇鍙敤鏁版嵁
+                byte[] readBuffer = new byte[finalTargetPort.bytesAvailable()];
+                int bytesRead = finalTargetPort.readBytes(readBuffer, readBuffer.length);
+
+                if (bytesRead > 0) {
+                    // 杞崲涓哄瓧绗︿覆锛堟牴鎹澶囩紪鐮佽皟鏁达紝甯歌锛歎TF-8銆丟BK锛�
+                    String data = new String(readBuffer, 0, bytesRead, StandardCharsets.UTF_8);
+                    System.out.printf("鏀跺埌鏁版嵁锛�%d 瀛楄妭锛夛細%s", bytesRead, data);
+                }
+            }
+        });
+
+        // 6. 淇濇寔绋嬪簭杩愯锛堥伩鍏嶄富绾跨▼閫�鍑猴級
+        try {
+            while (true) {
+                Thread.sleep(1000); // 涓荤嚎绋嬩紤鐪狅紝涓嶅奖鍝嶇洃鍚嚎绋�
+            }
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            System.out.println("绋嬪簭琚腑鏂��");
+        } finally {
+            // 7. 鍏抽棴涓插彛锛堢▼搴忛��鍑烘椂閲婃斁璧勬簮锛�
+            targetPort.closePort();
+            System.out.println("涓插彛宸插叧闂��");
+        }
+    }
+
 }

--
Gitblit v1.9.3