From d12c81a48ef10930b288353f2d5bda7579c7c332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?LAPTOP-S9HJSOEB=5C=E6=98=8A=E5=A4=A9?= Date: Wed, 11 Jun 2025 15:07:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BB=B6=E6=97=B6=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/pojo/IndustrialCameraVO.java | 33 ++++++++++++++++++- .../web/service/ksec/CommandQueue.java | 7 ++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/web/src/main/java/com/zhehekeji/web/pojo/IndustrialCameraVO.java b/web/src/main/java/com/zhehekeji/web/pojo/IndustrialCameraVO.java index 840179c..aae53df 100644 --- a/web/src/main/java/com/zhehekeji/web/pojo/IndustrialCameraVO.java +++ b/web/src/main/java/com/zhehekeji/web/pojo/IndustrialCameraVO.java @@ -3,9 +3,11 @@ package com.zhehekeji.web.pojo; import lombok.Data; import java.util.List; +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; @Data -public class IndustrialCameraVO { +public class IndustrialCameraVO implements Delayed { private String name; private String picImg; private String typeMacth; @@ -20,4 +22,33 @@ public class IndustrialCameraVO { private int flag; private String streetId; private int sentCount; + + private long executeTime; + public IndustrialCameraVO(){ + this.executeTime = System.currentTimeMillis() + 2000; + } + /** + * 获取延迟时间 + * + * @param unit 时间单位,用于转换延迟时间 + * @return 延迟时间,以指定的时间单位表示 + */ + @Override + public long getDelay(TimeUnit unit) { + // 计算延迟时间,将时间差转换为指定的时间单位 + return unit.convert(this.executeTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS); + } + + /** + * 比较两个Delayed对象的延迟时间 + * + * @param o 要比较的Delayed对象 + * @return 如果延迟时间相等,则返回0;如果当前对象的延迟时间小于参数对象的延迟时间,则返回负数;反之返回正数 + */ + @Override + public int compareTo(Delayed o) { + // 比较两个Delayed对象的延迟时间,以毫秒为单位 + return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS)); + } + } diff --git a/web/src/main/java/com/zhehekeji/web/service/ksec/CommandQueue.java b/web/src/main/java/com/zhehekeji/web/service/ksec/CommandQueue.java index 2e27385..e6f97c2 100644 --- a/web/src/main/java/com/zhehekeji/web/service/ksec/CommandQueue.java +++ b/web/src/main/java/com/zhehekeji/web/service/ksec/CommandQueue.java @@ -26,11 +26,12 @@ import static com.zhehekeji.web.service.KuKouService.setTrayCode; public class CommandQueue { // 增加queue public static void addCommand(IndustrialCameraVO scTransmission) { - queue.add(scTransmission); + delayQueue.add(scTransmission); } private static ExecutorService exec = Executors.newFixedThreadPool(1); + private static DelayQueue delayQueue = new DelayQueue<>(); - private static LinkedBlockingQueue queue = new LinkedBlockingQueue<>(); +// private static LinkedBlockingQueue queue = new LinkedBlockingQueue<>(); public static void sentHttp(RestTemplate restTemplate, ConfigProperties configProperties){ exec.execute(new Consumer(restTemplate, configProperties)); @@ -50,7 +51,7 @@ public class CommandQueue { while (true) { try { // CameraControlModule cameraControlModule = SpringContextUtil.getBean(CameraControlModule.class); - IndustrialCameraVO scTransmission = queue.take(); + IndustrialCameraVO scTransmission = delayQueue.take(); if(scTransmission != null){ try {