From 9e47d1426f494d68726de1d5cf7e704221a3fe3a Mon Sep 17 00:00:00 2001
From: zhangyong <517047165@qq.com>
Date: 星期五, 06 九月 2024 10:55:45 +0800
Subject: [PATCH] 读扫码枪socket通讯部分增加测试代码
---
hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/BarcodeReadThread.java | 82 +++++++++++++++++++++++++++++++++++++++++
hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java | 31 +++++++++++++++
2 files changed, 113 insertions(+), 0 deletions(-)
diff --git a/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/BarcodeReadThread.java b/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/BarcodeReadThread.java
new file mode 100644
index 0000000..cc6dabc
--- /dev/null
+++ b/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/BarcodeReadThread.java
@@ -0,0 +1,82 @@
+package com.mes.job;
+
+
+import com.mes.glassinfo.service.GlassInfoService;
+import lombok.SneakyThrows;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+
+public class BarcodeReadThread extends Thread{
+
+ public BarcodeReadThread(String ip,Integer port,Downpush downpush)
+ {
+ this.ip=ip;
+ this.port=port;
+ this.downpush=downpush;
+ }
+ private Socket socket;
+ private String ip;
+ private Integer port;
+ private Downpush downpush;
+ private BufferedReader in;
+
+ @SneakyThrows
+ @Override
+ public void run()
+ {
+ connectCheckin();//鍒濇 寰幆灏濊瘯杩炴帴锛岀洿鍒拌繛鎺ユ垚鍔�
+ while (this!=null)
+ {
+ try
+ {
+ in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ String response = in.readLine();
+ if(response!=null)
+ {
+ downpush.barcodeCheckin(response);
+ }
+ }
+ catch (Exception exception)
+ {
+ //寮傚父锛岄噸杩�
+ connectCheckin();
+ }
+ finally {
+ if(in!=null)
+ {
+ in.close();
+ }
+ }
+
+ Thread.sleep(100);
+ }
+ }
+
+ @SneakyThrows
+ private void connectCheckin()
+ {
+ while (this!=null)
+ {
+ try
+ {
+ socket=new Socket();
+ socket.setSoTimeout(10000);
+ socket.connect(new InetSocketAddress(ip,port));
+ if(socket.isConnected())
+ {
+ return;
+ }
+ }
+ catch (Exception exception)
+ {
+
+ }
+
+ Thread.sleep(1000);
+ }
+ }
+}
diff --git a/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java b/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java
index 6a17f8a..7cfb89e 100644
--- a/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java
+++ b/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java
@@ -27,6 +27,7 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
+import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
@@ -233,4 +234,34 @@
}).start();
}
}
+
+ @Scheduled(fixedDelay = Long.MAX_VALUE)
+ public void scanCodeTaskCopy()
+ {
+ log.info("鎵弿浠诲姟宸插惎鍔�");
+ try {
+ BarcodeReadThread brt=new BarcodeReadThread(scanIp,scanPort,this);
+ brt.start();
+
+ }catch (Exception exception) {
+ log.info("鎵弿浠诲姟鍚姩寮傚父锛屽師鍥犱负{}", exception.getMessage());
+ }
+
+ }
+ //socket璇诲彇鍒版秷鎭悗浼氳皟鐢ㄨ繖涓嚱鏁�
+ public void barcodeCheckin(String response)
+ {
+ log.info("鎵弿鍒扮殑鐜荤拑id锛歿}", response);
+ List<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("scanCode");
+ if (CollectionUtils.isNotEmpty(sendwServer)) {
+ //鎸夌収鐜荤拑id鑾峰彇鐜荤拑淇℃伅杩斿洖缁欏墠绔晫闈紝鍏蜂綋闇�瑕佸摢浜涙暟鎹緟纭
+ GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>()
+ .eq(GlassInfo::getGlassId, response).last("limit 1"));
+ if (null == glassInfo) {
+ log.info("鎸夌収鐜荤拑id锛歿}锛屾棤娉曟壘鍒扮幓鐠冧俊鎭�", response);
+ } else {
+ sendwServer.get(0).sendMessage(glassInfo.toString());
+ }
+ }
+ }
}
--
Gitblit v1.8.0