From a21bc9a519d548e476778dc22cee9d283577dcd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?LAPTOP-S9HJSOEB=5C=E6=98=8A=E5=A4=A9?= Date: Tue, 21 May 2024 18:11:01 +0800 Subject: [PATCH] =?UTF-8?q?ftp=E5=86=99=E5=87=BA=E6=9D=A5=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/pom.xml | 6 +- .../client/image/ImageQueueReplacer.java | 121 +++++++++++------- .../web/service/client/image/ImageSaver.java | 76 +++++++++-- 3 files changed, 144 insertions(+), 59 deletions(-) diff --git a/web/pom.xml b/web/pom.xml index f6e84a7..b956420 100644 --- a/web/pom.xml +++ b/web/pom.xml @@ -18,7 +18,11 @@ - + + commons-net + commons-net + 3.6 + com.zhehekeji base-assembly diff --git a/web/src/main/java/com/zhehekeji/web/service/client/image/ImageQueueReplacer.java b/web/src/main/java/com/zhehekeji/web/service/client/image/ImageQueueReplacer.java index a86be05..5073126 100644 --- a/web/src/main/java/com/zhehekeji/web/service/client/image/ImageQueueReplacer.java +++ b/web/src/main/java/com/zhehekeji/web/service/client/image/ImageQueueReplacer.java @@ -1,74 +1,99 @@ package com.zhehekeji.web.service.client.image; +import com.zhehekeji.common.util.SpringContextUtil; +import com.zhehekeji.web.lib.CameraControlModule; +import com.zhehekeji.web.lib.CameraDelayTask; +import com.zhehekeji.web.lib.TaskDelayExecutor; +import com.zhehekeji.web.service.PlcService; +import com.zhehekeji.web.service.client.TransmissionPojo; +import com.zhehekeji.web.service.ksec.KsecInfo; +import com.zhehekeji.web.service.ksec.KsecNettyClient; + import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; -import java.util.concurrent.LinkedBlockingQueue; +import java.time.LocalDateTime; +import java.util.concurrent.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.Semaphore; -import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; public class ImageQueueReplacer { - private final String targetDirectory; - private final LinkedBlockingQueue imageQueue; - private final Semaphore semaphore; - private volatile boolean running = true; + // 延时队列,存放CameraDelayTask任务 + public static DelayQueue cameraDelayTasks = new DelayQueue<>(); + // 线程池,用于执行延时任务 + private static ExecutorService exec = Executors.newFixedThreadPool(1); - public ImageQueueReplacer(String targetDirectory) { - this.targetDirectory = targetDirectory; - this.imageQueue = new LinkedBlockingQueue<>(); - this.semaphore = new Semaphore(0); // 初始信号量为0 - } + /** + * 向延时队列中添加一个任务。 + * + * @param cameraPlcId 相机的PLC编号 + * @param getPhotoCommand 获取照片的命令 + * @param time 任务延时时间 + */ + public static void addCameraDelayTask(String cameraPlcId, String getPhotoCommand, long time) { - public void queueImage(BufferedImage image) { - imageQueue.add(image); - semaphore.release(); // 图片入队时释放信号量,告知有图片可处理 } - public void startAutoReplace(String imageName) { - Thread autoReplaceThread = new Thread(() -> { - while (running) { - try { - semaphore.acquire(); // 等待信号量,表明有图片或开始指令 - BufferedImage imageToReplace; - while ((imageToReplace = imageQueue.poll()) != null) { - replaceImage(imageToReplace, imageName); - } - // 队列为空时,仅在第一次启动时执行覆盖(根据需求调整) - if (semaphore.availablePermits() == 0 && imageQueue.isEmpty()) { - replaceImage(null, imageName); // 使用null表示无图片可覆盖 - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } + /** + * 根据命令和相机PLC编号从延时队列中移除任务。 + * + * @param getPhotoCommand 获取巷道编号 + * @param cameraPlcId 命令行 + */ + public static void remove(String getPhotoCommand, String cameraPlcId) { + cameraPlcId = cameraPlcId.intern(); + synchronized (cameraPlcId) { + // 将延时队列中的任务转换为数组,便于遍历和移除 + Object[] cameraDelayTask = cameraDelayTasks.toArray(); + for (Object cameraDelayTask1 : cameraDelayTask) { + CameraDelayTask cameraDelayTask2 = (CameraDelayTask) cameraDelayTask1; + } - }); - autoReplaceThread.setDaemon(true); - autoReplaceThread.start(); + } } - private void replaceImage(BufferedImage image, String imageName) { - if (image != null) { - File outputFile = new File(targetDirectory, imageName); - try { - ImageIO.write(image, "jpg", outputFile); - System.out.println("Image saved: " + outputFile.getAbsolutePath()); - } catch (IOException e) { - System.err.println("Error saving image: " + e.getMessage()); - } - } else { - System.out.println("No image available to replace."); + /** + * 获取延时队列中最早的任务,并将其从队列中移除。 + * + * @param cameraPlcId 相机的PLC编号,该参数未被使用 + * @return 返回最早的任务,如果队列为空则返回null + */ + public static CameraDelayTask getNext(String cameraPlcId) { + Object[] cameraDelayTask = cameraDelayTasks.toArray(); + if (cameraDelayTask.length > 0) { + CameraDelayTask cameraDelayTask2 = (CameraDelayTask) cameraDelayTask[0]; + return cameraDelayTask2; } + return null; + } - public void stop() { - running = false; + /** + * 启动延时任务的执行器。 + */ + public static void runExecutor() { + exec.execute(new ImageQueueReplacer.Consumer()); } + + /** + * Consumer内部类,实现Runnable接口,用于消费延时队列中的任务。 + */ + private static class Consumer implements Runnable { + /** + * 无限循环,不断从延时队列中取出任务并执行。 + */ + @Override + public void run() { + while (true) { + + } + } + } + + } diff --git a/web/src/main/java/com/zhehekeji/web/service/client/image/ImageSaver.java b/web/src/main/java/com/zhehekeji/web/service/client/image/ImageSaver.java index 44c4b20..48d4450 100644 --- a/web/src/main/java/com/zhehekeji/web/service/client/image/ImageSaver.java +++ b/web/src/main/java/com/zhehekeji/web/service/client/image/ImageSaver.java @@ -1,28 +1,84 @@ package com.zhehekeji.web.service.client.image; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPClient; +import org.springframework.stereotype.Service; import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import javax.imageio.ImageIO; @Slf4j +@Service public class ImageSaver { - private final String targetDirectory; - public ImageSaver(String targetDirectory) { - this.targetDirectory = targetDirectory; + + private static final int BUFFER_SIZE = 4096; + public void copyFileToFolder(String sourceFilePath, String destinationFolderPath, String fileName) { + Path source = Paths.get(sourceFilePath); + Path destination = Paths.get(destinationFolderPath, fileName); + + try { + Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); + System.out.println("文件复制成功:" + source + " -> " + destination); + } catch (IOException e) { + System.err.println("文件复制过程中发生错误:" + e.getMessage()); + e.printStackTrace(); + } } - public void saveImage(BufferedImage image, String imageName) { - File outputFile = new File(targetDirectory, imageName); + public void uploadFileToFtp(String ip,String localFilePath) + { try { - ImageIO.write(image, "jpg", outputFile); - System.out.println("Image saved: " + outputFile.getAbsolutePath()); + uploadFileToFtp(ip,"admin","",localFilePath,"/compute.jpg"); } catch (IOException e) { - System.err.println("Error saving image: " + e.getMessage()); + throw new RuntimeException(e); + } + } + public void uploadFileToFtp(String ip,String username, String password,String localFilePath, String remoteFilePath ) throws IOException { + FTPClient ftpClient = new FTPClient(); + try { + // 连接到FTP服务器 + ftpClient.connect(ip); + ftpClient.login(username, password); + ftpClient.enterLocalPassiveMode(); // 设置被动模式,适应大多数网络环境 + ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 设置文件传输模式为二进制 + + // 读取本地文件 + File localFile = new File(localFilePath); + InputStream inputStream = new FileInputStream(localFile); + System.out.println(ftpClient.isConnected()); + + // 上传文件 + boolean uploadSuccess = ftpClient.storeFile(remoteFilePath, inputStream); + if (uploadSuccess) { + System.out.println("文件上传成功:" + remoteFilePath); + } else { + System.out.println("文件上传失败:" + remoteFilePath); + } + + // 关闭输入流 + inputStream.close(); + } catch (IOException ex) { + System.err.println("文件上传过程中发生错误:" + ex.getMessage()); + throw ex; + } finally { + // 登出并断开连接 + if (ftpClient.isConnected()) { + try { + ftpClient.logout(); + ftpClient.disconnect(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } } } + }