From 2234842c283fca11307ce90712687fa02f6c5b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?LAPTOP-S9HJSOEB=5C=E6=98=8A=E5=A4=A9?= Date: Mon, 6 May 2024 15:29:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=A4=E4=BA=95=E4=BD=BF=E7=94=A8=E8=87=AA?= =?UTF-8?q?=E8=BA=ABtcp=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/config/ConfigProperties.java | 8 +- .../com/zhehekeji/web/service/PlcService.java | 17 ++- .../zhehekeji/web/service/StockService.java | 14 +- .../web/service/algorithm/AlgorithmClint.java | 125 ++++++++++++++++++ .../service/algorithm/AlgorithmService.java | 23 +++- web/src/main/resources/application-prod.yml | 3 +- 6 files changed, 172 insertions(+), 18 deletions(-) create mode 100644 web/src/main/java/com/zhehekeji/web/service/algorithm/AlgorithmClint.java diff --git a/web/src/main/java/com/zhehekeji/web/config/ConfigProperties.java b/web/src/main/java/com/zhehekeji/web/config/ConfigProperties.java index bc75730..e3326a1 100644 --- a/web/src/main/java/com/zhehekeji/web/config/ConfigProperties.java +++ b/web/src/main/java/com/zhehekeji/web/config/ConfigProperties.java @@ -1,6 +1,5 @@ package com.zhehekeji.web.config; -import io.lettuce.core.dynamic.annotation.Value; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @@ -46,12 +45,13 @@ public class ConfigProperties { private VisualSense visualSense; private Integer orderTest = 0; private String productDoc; - private List smokeBoxTypeList; + private List smokeBoxType; @Data public static class SmokeBoxType{ - private Integer type= 1; - private String name= "古20"; + private String type= ""; + private String name= ""; + private String code= ""; private Integer numOfLayers= 1; private Integer numLayers= 6; } 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 41d1326..27bacd8 100644 --- a/web/src/main/java/com/zhehekeji/web/service/PlcService.java +++ b/web/src/main/java/com/zhehekeji/web/service/PlcService.java @@ -233,8 +233,17 @@ public class PlcService { Street street = streetMapper.selectById(order.getStreetId()); //关闭光源 - streetController.closeStreetLightSource(street.getId()); + Thread thread = new Thread(()->{ + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + streetController.closeStreetLightSource(street.getId()); + }); + thread.start(); LocalDateTime endDownLoadTime = endTime.plusSeconds(5); Duration duration = Duration.between(order.getStartTime(), endDownLoadTime); @@ -254,6 +263,8 @@ public class PlcService { } + + public void orderStopByWarn(String orderNum) { Order order = orderMapper.getOneByOrderNum(orderNum); if (order == null) { @@ -716,9 +727,7 @@ public class PlcService { //核对异常 Integer status = 1; //托盘和货物都正确 - if (algorithmPojo != null && algorithmPojo.getResult() != null && algorithmPojo.getResult() == 1 - && algorithmPojo.getGoodsNumber() != null && algorithmPojo.getGoodsNumber().equals(algorithmPojo.getGoodsNumberResult()) - && algorithmPojo.getGoodsType() != null && algorithmPojo.getGoodsType().equals(algorithmPojo.getGoodsTypeResult())) { + if (algorithmPojo != null && algorithmPojo.getResult() != null && algorithmPojo.getResult() == 1) { status = 2; } if (stock == null) { diff --git a/web/src/main/java/com/zhehekeji/web/service/StockService.java b/web/src/main/java/com/zhehekeji/web/service/StockService.java index c20bf3a..ff241e0 100644 --- a/web/src/main/java/com/zhehekeji/web/service/StockService.java +++ b/web/src/main/java/com/zhehekeji/web/service/StockService.java @@ -373,7 +373,8 @@ public class StockService { Street street = streetService.getStreetByPlcId(stockCheckSearch.getSRMNumber()); Stock stock = stockMapper.getByStreetAndDirectionAndSideAndRowColumn(street.getId(),stockCheckSearch.getDirection(),stockCheckSearch.getSeparation(),stockCheckSearch.getRow(),stockCheckSearch.getColumn()); if(stock != null){ - String ip ="http://"+ street.getPlcIp()+":9009/pic/"+stock.getCode()+"/"+stock.getLotnum()+"/"+stock.getLotnum()+"/"; + + String ip ="http://"+ street.getPlcIp()+":9009/pic/"+stock.getCheckNum(); stock.setSidePic1(String.format("%s_1.JPEG",ip)); stock.setSidePic2(String.format("%s_5.JPEG",ip)); stock.setSidePic3(String.format("%s_6.JPEG",ip)); @@ -394,7 +395,7 @@ public class StockService { Stock stock = stockInfo(stockCheck); Assert.isTrue(stock != null && stock.getId() != null, "该货位暂时没有记录"); Integer oldStatus = stock.getStatus(); - Assert.isTrue( StockStatus.PENDING.getStatus().equals(oldStatus) || StockStatus.ERROR.getStatus().equals(oldStatus), "无需核对"); + //Assert.isTrue( StockStatus.PENDING.getStatus().equals(oldStatus) || StockStatus.ERROR.getStatus().equals(oldStatus), "无需核对"); log.info("check stock correct, street_id:{},direction:{},side:{},row:{},column:{}", stockCheck.getStreetId(),stockCheck.getDirection(),stockCheck.getSide(), stockCheck.getRow(), stockCheck.getColumn()); stock.setStatus(StockStatus.MANUAL.getStatus()); @@ -473,6 +474,15 @@ public class StockService { } } + Street street = streetMapper.selectById(stock.getStreetId()); + String ip ="http://"+ street.getPlcIp()+":9009/pic/"+stock.getCheckNum(); + stock.setSidePic1(String.format("%s_1.JPEG",ip)); + stock.setSidePic2(String.format("%s_5.JPEG",ip)); + stock.setSidePic3(String.format("%s_6.JPEG",ip)); + stock.setSidePic4(String.format("%s_4.JPEG",ip)); + stock.setTopPic1(String.format("%s_2.JPEG",ip)); + stock.setTopPic2(String.format("%s_3.JPEG",ip)); + return stock; } diff --git a/web/src/main/java/com/zhehekeji/web/service/algorithm/AlgorithmClint.java b/web/src/main/java/com/zhehekeji/web/service/algorithm/AlgorithmClint.java new file mode 100644 index 0000000..1af5169 --- /dev/null +++ b/web/src/main/java/com/zhehekeji/web/service/algorithm/AlgorithmClint.java @@ -0,0 +1,125 @@ +package com.zhehekeji.web.service.algorithm; + +import com.zhehekeji.web.config.ConfigProperties; +import com.zhehekeji.web.entity.AlgorithmPojo; +import lombok.extern.slf4j.Slf4j; + +import java.io.*; +import java.net.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Slf4j +public class AlgorithmClint { + AlgorithmPojo clint(String ip, Integer port, AlgorithmPojo algorithmPojo, List list) { + try { + Socket socket = new Socket(); + Map map = list.stream().collect(Collectors.toMap(ConfigProperties.SmokeBoxType::getType, k->k)); + + socket.connect(new InetSocketAddress(ip, port), 5000); + System.out.println("Connected to server"); + socket.setSoTimeout(10000); + + BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); + //修改全局变量 + updateTaskId(reader,writer,algorithmPojo); + Thread.sleep(600); + + //获取结果并拍照 + String message = "trigger:1"; + + writer.write(message); + + writer.flush(); + System.out.println("Sent message to server: " + message); + + StringBuilder response =new StringBuilder(); + // 接收服务器的响应 + while (true){ + char[] lengthBytes = new char[1]; + reader.read(lengthBytes); + if (lengthBytes[0] == ';') { + break; + } + response.append(lengthBytes[0]); + + } + if (response != null) { + System.out.println("Server response: " + response); + log.info("respon:{}",response.toString()); + } else { + System.out.println("No response from server. Closing connection."); + } + String respon = response.toString(); + respon = respon.replace(";",""); + String[] strings = respon.toString().split( ","); + + Map charCountMap = new HashMap<>(); + + // 统计每个字符的数量 + for (String c : strings) { + charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1); + } + + // 找出数量最多的字符及其次数 + String mostFrequentChar = null; + int len = 0; + for (Map.Entry entry : charCountMap.entrySet()) { + if (entry.getValue() > len) { + mostFrequentChar = entry.getKey(); + len = entry.getValue(); + } + } + + len = len-1; + ConfigProperties.SmokeBoxType smokeBoxType = map.get(mostFrequentChar); + //计算个数 + if (smokeBoxType != null && smokeBoxType.getNumOfLayers() != 0 && smokeBoxType.getNumLayers() != 0 ){ + algorithmPojo.setGoodsTypeResult(smokeBoxType.getCode()); + algorithmPojo.setGoodsNumberResult( len / smokeBoxType.getNumOfLayers() * smokeBoxType.getNumLayers()); + } + if(algorithmPojo.getGoodsType().equals(algorithmPojo.getGoodsTypeResult())){ + algorithmPojo.setResult(1); + }else { + algorithmPojo.setResult(0); + } + + + // 关闭连接 + socket.close(); + + System.out.println("Connection closed"); + } catch (IOException e) { + algorithmPojo.setResult(0); + log.error("连接失败:"+ip,e); + } catch (InterruptedException e) { + algorithmPojo.setResult(0); + }catch (Exception e){ + algorithmPojo.setResult(0); + log.error("连接失败:"+ip,e); + } + return algorithmPojo; + } + + public static void main(String[] args) { + System.out.println(4/1*13); + } + + void updateTaskId(BufferedReader reader ,BufferedWriter writer ,AlgorithmPojo algorithmPojo){ + try { + String message = "taskId:"+algorithmPojo.getTaskId(); + + writer.write(message); + writer.flush(); + System.out.println("Sent message to server: " + message); + + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/web/src/main/java/com/zhehekeji/web/service/algorithm/AlgorithmService.java b/web/src/main/java/com/zhehekeji/web/service/algorithm/AlgorithmService.java index b20bfd7..5ced20a 100644 --- a/web/src/main/java/com/zhehekeji/web/service/algorithm/AlgorithmService.java +++ b/web/src/main/java/com/zhehekeji/web/service/algorithm/AlgorithmService.java @@ -32,11 +32,15 @@ public class AlgorithmService { public static void main(String[] args) { AlgorithmPojo algorithmPojo= new AlgorithmPojo(); algorithmPojo.setGoodsTypeResult("qqq"); - algorithmPojo.setIp("12"); - algorithmPojo.setPort(8083); + algorithmPojo.setIp("127.0.0.1"); + algorithmPojo.setPort(8001); + algorithmPojo.setTaskId("8001"); AlgorithmService algorithmService = new AlgorithmService(); - algorithmService.getGoodResult(algorithmPojo); + //algorithmService.getGoodResultTcp(algorithmPojo); + + AlgorithmClint clint = new AlgorithmClint(); + clint.clint(algorithmPojo.getIp(),8001,algorithmPojo,new ArrayList<>()); System.out.println(algorithmPojo); @@ -45,6 +49,12 @@ public class AlgorithmService { } + public AlgorithmPojo getGoodResultTcp(AlgorithmPojo algorithmPojo) { + AlgorithmClint clint = new AlgorithmClint(); + clint.clint(algorithmPojo.getIp(),9040,algorithmPojo,configProperties.getSmokeBoxType()); + return algorithmPojo; + } + public AlgorithmPojo getGoodResult(AlgorithmPojo algorithmPojo) { String url = "http://"+algorithmPojo.getIp()+":"+algorithmPojo.getPort()+configProperties.getVisualSense().getUrl(); @@ -63,9 +73,7 @@ public class AlgorithmService { body = restTemplate.postForEntity(url, algorithmPojo, String.class); algorithmPojo = JSONObject.parseObject(body.getBody(), AlgorithmPojo.class); - if( algorithmPojo.getGoodsTypeResult().equals(algorithmPojo.getGoodsType())){ - algorithmPojo.setResult(1); - } + System.out.println(body); }catch (RestClientException e){ log.error("未连接上堆垛机"+e); @@ -76,10 +84,11 @@ public class AlgorithmService { } return algorithmPojo; } + public AlgorithmPojo getGoodResult(Street street, KsecDataInfo dataInfo ) { AlgorithmPojo algorithmPojo = AlgorithmPojo.buildAlgorithmPojo(street, dataInfo); if(configProperties.getVisualSense().getAble()) { - algorithmPojo = getGoodResult(algorithmPojo); + algorithmPojo = getGoodResultTcp(algorithmPojo); }else { algorithmPojo.setResult(1); algorithmPojo.setGoodsNumberResult(algorithmPojo.getGoodsNumber()); diff --git a/web/src/main/resources/application-prod.yml b/web/src/main/resources/application-prod.yml index 95f5a19..c1fa4d4 100644 --- a/web/src/main/resources/application-prod.yml +++ b/web/src/main/resources/application-prod.yml @@ -110,11 +110,12 @@ asyncExecutorThread: #订单测试 orderTest: 1 #品规文档 -productDoc: "C:/Users/昊天/Desktop/新建 文本文档 (3).txt" +productDoc: "C:/Users/昊天/Desktop/新建文本文档 (3).txt" smokeBoxType: - type: 1 name: 古20 + code: "125425" #用于视觉的一层个数 numOfLayers: 1 #用于计算总数的个数