diff --git a/web/src/main/java/com/zhehekeji/web/config/FusionDesignApplication.java b/web/src/main/java/com/zhehekeji/web/config/FusionDesignApplication.java index 68b486f..f0e2945 100644 --- a/web/src/main/java/com/zhehekeji/web/config/FusionDesignApplication.java +++ b/web/src/main/java/com/zhehekeji/web/config/FusionDesignApplication.java @@ -20,8 +20,8 @@ public class FusionDesignApplication { // 使用RestTemplateBuilder来实例化RestTemplate对象,spring默认已经注入了RestTemplateBuilder实例 @Bean public RestTemplate restTemplate() { - return builder.setConnectTimeout(Duration.ofSeconds(20)) // 设置连接超时时间,单位毫秒 - .setReadTimeout(Duration.ofSeconds(20)) // 设置读取超时时间,单位毫秒 + return builder.setConnectTimeout(Duration.ofMinutes(5)) // 设置连接超时时间,单位毫秒 + .setReadTimeout(Duration.ofMinutes(20)) // 设置读取超时时间,单位秒 .build(); } } diff --git a/web/src/main/java/com/zhehekeji/web/controller/IndustrialCameraController.java b/web/src/main/java/com/zhehekeji/web/controller/IndustrialCameraController.java index 77679c7..7776ebb 100644 --- a/web/src/main/java/com/zhehekeji/web/controller/IndustrialCameraController.java +++ b/web/src/main/java/com/zhehekeji/web/controller/IndustrialCameraController.java @@ -35,6 +35,7 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import static com.zhehekeji.web.service.algorithm.FeatureMatchingExample.base642Mat; @@ -70,12 +71,26 @@ public class IndustrialCameraController { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); if(configProperties.getCameraConfig().getIndustrialCamera()!=null) { - for (String camera:configProperties.getCameraConfig().getIndustrialCamera()) { + List> futures = new ArrayList<>(); - String path = "industrialCamera/" +currentDate.format(formatter)+"/"+camera+ UUID.randomUUID() + ".jpeg"; - hikSaveImage.saveImage(camera, configProperties.getSavePath().getMediaPath() + path, "sn"); - list.add(path); + for (String camera : configProperties.getCameraConfig().getIndustrialCamera()) { + String path = "industrialCamera/" + currentDate.format(formatter) + "/" + camera + UUID.randomUUID() + ".jpeg"; + String fullPath = configProperties.getSavePath().getMediaPath() + path; + + // 提交异步任务 + CompletableFuture future = CompletableFuture.runAsync(() -> { + hikSaveImage.saveImage(camera, fullPath, "sn"); + synchronized (list) { + list.add(path); + } + }); + + futures.add(future); } + + // 等待所有任务完成 + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); + return new Result<>(list); }else { return new Result<>(list); @@ -186,7 +201,7 @@ public class IndustrialCameraController { kuKou.setWmsCode(industrialCameraVo.getCode()); kuKou.setPath(configProperties.getSavePath().getNetPicPath()+pa+".jpg"); kuKouService.save(kuKou); - serialPortExample.openLight(0); +// serialPortExample.openLight(0); return new Result<>(kuKou); } diff --git a/web/src/main/java/com/zhehekeji/web/service/IndustrialCamera/HikSaveImage.java b/web/src/main/java/com/zhehekeji/web/service/IndustrialCamera/HikSaveImage.java index 0715c6d..d537c25 100644 --- a/web/src/main/java/com/zhehekeji/web/service/IndustrialCamera/HikSaveImage.java +++ b/web/src/main/java/com/zhehekeji/web/service/IndustrialCamera/HikSaveImage.java @@ -167,33 +167,23 @@ public class HikSaveImage implements CameraSaveUtil{ @Override public boolean saveImage(String sn, String path,String type) { int nRet = MV_OK; - ArrayList stDeviceList; do { System.out.println("SDK Version " + MvCameraControl.MV_CC_GetSDKVersion()); // Enuerate GigE and USB devices - try - { - stDeviceList = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE); - if (0 >= stDeviceList.size()) - { - System.out.println("No devices found!"); - break; - } - } - catch (CameraControlException e) - { - System.err.println("Enumrate devices failed!" + e.toString()); - e.printStackTrace(); - break; - } // choose camera - Handle hCamera = init(sn,stDeviceList,type); + Handle hCamera = null; + if (handleMap.containsKey(sn) && handleMap.get(sn) != null) { + //已经获得句柄 + hCamera = handleMap.get(sn); + }else { + System.out.println("未找到句柄"); + } // Create handle diff --git a/web/src/main/java/com/zhehekeji/web/service/InitService.java b/web/src/main/java/com/zhehekeji/web/service/InitService.java index c294b66..3222e1c 100644 --- a/web/src/main/java/com/zhehekeji/web/service/InitService.java +++ b/web/src/main/java/com/zhehekeji/web/service/InitService.java @@ -89,15 +89,19 @@ public class InitService implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { + if (configProperties.getComConfig()!=null&& !configProperties.getComConfig().equals("")) SerialPortManager.connectSerialPort(configProperties.getComConfig()); ConfigProperties.KSEC ksec = configProperties.getKsec(); inventoryService.init(); - hikSaveImage.init(configProperties.getCameraConfig().getIndustrialCamera(),"sn"); + if (configProperties.getCameraConfig()!=null + && configProperties.getCameraConfig().getIndustrialCamera()!=null + && !configProperties.getCameraConfig().getIndustrialCamera().equals("")) + hikSaveImage.init(configProperties.getCameraConfig().getIndustrialCamera(),"sn"); if(ksec != null&& ksec.getIp()!=null&& !ksec.getIp().equals("")){ TaskDelayExecutor.runMp4DownloadExecutor(); - nettyServer.CreateNettyServer(configProperties.getServerPort()); +// nettyServer.CreateNettyServer(configProperties.getServerPort()); //球机登录 List cameras = cameraMapper.selectByMap(new HashMap<>(0)); cameras.forEach(camera -> { diff --git a/web/src/main/java/com/zhehekeji/web/service/PlcService.java b/web/src/main/java/com/zhehekeji/web/service/PlcService.java index 0e09ecb..ab8cc95 100644 --- a/web/src/main/java/com/zhehekeji/web/service/PlcService.java +++ b/web/src/main/java/com/zhehekeji/web/service/PlcService.java @@ -522,20 +522,20 @@ public class PlcService { } public boolean checkVision(KsecDataInfo dataInfo,Boolean flag){ + IndustrialCameraVO scTransmission = new IndustrialCameraVO(); + scTransmission.setTypeMacth(dataInfo.getTypeNum()); + scTransmission.setCount(dataInfo.getQuantity()); + + scTransmission.setCode(dataInfo.getTrayCode()); try { HttpHeaders headers = new HttpHeaders(); headers.set("Content-Type", "application/json"); headers.set("User-Agent", "Mozilla/5.0"); - IndustrialCameraVO scTransmission = new IndustrialCameraVO(); - scTransmission.setTypeMacth(dataInfo.getTypeNum()); - scTransmission.setCount(dataInfo.getQuantity()); - - scTransmission.setCode(dataInfo.getTrayCode()); // 创建 HttpEntity 对象 HttpEntity entity = new HttpEntity<>(scTransmission, headers); - Map map = configProperties.getTemplate().stream().collect(Collectors.toMap(k->k.getCode(), v->v)); +// Map map = configProperties.getTemplate().stream().collect(Collectors.toMap(k->k.getCode(), v->v)); // 发送 POST 请求 ResponseEntity> response = restTemplate.exchange( @@ -567,6 +567,12 @@ public class PlcService { } } catch (Exception e) { log.error("3D get pcd error", e); + + scTransmission.setWmsTrayCode(dataInfo.getTrayCode()); + + scTransmission.setTrayCode(""); + scTransmission.setCount(0); + kuKouService.setHttp(scTransmission,flag); return false; } } diff --git a/web/src/main/java/com/zhehekeji/web/service/algorithm/InventoryService.java b/web/src/main/java/com/zhehekeji/web/service/algorithm/InventoryService.java index 1b47e52..b1fcef6 100644 --- a/web/src/main/java/com/zhehekeji/web/service/algorithm/InventoryService.java +++ b/web/src/main/java/com/zhehekeji/web/service/algorithm/InventoryService.java @@ -66,7 +66,9 @@ public class InventoryService { } public void init(){ - PointerByReference handleRef = LxPointCloudSaveImage.init(configProperties.getCameraConfig().getCamera3D()); + if (configProperties.getCameraConfig()!=null && configProperties.getCameraConfig().getCamera3D()!=null && !configProperties.getCameraConfig().getCamera3D().equals("")) { + PointerByReference handleRef = LxPointCloudSaveImage.init(configProperties.getCameraConfig().getCamera3D()); + } } public int match3D(String category,String path){ //拍照 diff --git a/web/src/main/java/com/zhehekeji/web/service/algorithm/PointCloudProcessor.java b/web/src/main/java/com/zhehekeji/web/service/algorithm/PointCloudProcessor.java index 66cc2b0..aa53844 100644 --- a/web/src/main/java/com/zhehekeji/web/service/algorithm/PointCloudProcessor.java +++ b/web/src/main/java/com/zhehekeji/web/service/algorithm/PointCloudProcessor.java @@ -671,10 +671,12 @@ public class PointCloudProcessor { } } for (int h =5; h>0; h--){ - if (map.containsKey(h) && map.get(h).size()>500){ + if (map.containsKey(h) && map.get(h).size()>1000){ double area =IntervalPolygonArea.calculateArea(map.get(h),pojo.minBounds[0],10); if (area>0){ - int i =(layersCount*(h-1))+(int) Math.round(area/(double) (l*w)); + System.out.println("面积:"+area); + System.out.println("个数:"+area/(double) (l*w)); + int i =(layersCount*(h-1))+(int) Math.min(Math.round(area/(double) (l*w)),layersCount); return i; } break; @@ -684,9 +686,9 @@ public class PointCloudProcessor { } public static void main(String[] args) { - String path ="E:\\工作\\泸州测试\\2\\62de6477-2ae4-47b8-91d8-f0ca54ea4a05--192.168.40.11.pcd"; - String configPath = "E:\\工作\\泸州测试\\2\\27.json"; - String typeConfPath = "E:\\工作\\泸州测试\\2\\40014845.json"; + String path ="E:\\泸州\\pcd\\实际53识别出54\\58ed9ebf-6956-427d-b248-50f254d652ef--192.168.32.11.pcd"; + String configPath = "E:\\工作\\泸州测试\\24\\27.json"; + String typeConfPath = "E:\\工作\\泸州测试\\24\\40016741.json"; List points = readPCD(path); PcdPojo pojo = new PcdPojo(); PcdPojo pcdPojo = new PcdPojo(); diff --git a/web/src/main/java/com/zhehekeji/web/service/com/SerialPortExample.java b/web/src/main/java/com/zhehekeji/web/service/com/SerialPortExample.java index 438233a..17310d1 100644 --- a/web/src/main/java/com/zhehekeji/web/service/com/SerialPortExample.java +++ b/web/src/main/java/com/zhehekeji/web/service/com/SerialPortExample.java @@ -13,6 +13,9 @@ import org.springframework.stereotype.Service; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -38,11 +41,31 @@ public class SerialPortExample { public static volatile SerialPort SERIAL_PORT_OBJECT = null; + private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + private Future lightFuture = null; public void openLight(int i){ - if(i==1) { + + if (i == 1) { + // 如果已经有任务在运行,先取消它 + if (lightFuture != null && !lightFuture.isDone()) { + lightFuture.cancel(false); + } + + // 开灯 SerialPortManager.sendSerialPortData(open); - }else { + + // 安排30分钟后执行关灯任务 + lightFuture = scheduler.schedule(() -> { + synchronized (this) { + SerialPortManager.sendSerialPortData(close); + } + }, 30, TimeUnit.MINUTES); + } else { + // 如果外部直接调用关灯,也取消定时任务 + if (lightFuture != null && !lightFuture.isDone()) { + lightFuture.cancel(false); + } SerialPortManager.sendSerialPortData(close); } }