ZengTao
2024-09-06 bda4a97ee41aaac639220ebc030335f9d3342fdb
hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java
@@ -22,6 +22,8 @@
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -31,6 +33,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.stream.Collectors;
@Slf4j
@@ -47,6 +50,13 @@
    private DownGlassTaskService downGlassTaskService;
    @Autowired
    private GlassInfoService glassInfoService;
    private final ExecutorService scanCodeThreadPool = new ThreadPoolExecutor(
            10, // core pool size
            20, // maximum pool size
            60L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>()
    );
    private final ExecutorService isRunThreadPool = Executors.newFixedThreadPool(5);
    @Value("${mes.scan.ip}")
    private String scanIp;
@@ -185,6 +195,7 @@
    @Scheduled(fixedDelay = 1000)
    public void isRun() {
        isRunThreadPool.execute(() -> {
        JSONObject jsonObject = new JSONObject();
        //正在进行的任务
        List<DownWorkstation> downWorkstation = downWorkstationService.getoneDownWorkstations(1, 6);
@@ -201,36 +212,38 @@
                }
            }
        }
        });
    }
    @Async
    @Scheduled(fixedDelay = Long.MAX_VALUE)
    public void scanCodeTask() {
        JSONObject jsonObject = new JSONObject();
        log.info("扫描任务已启动");
        while (true) {
            new Thread(() -> {
                try (Socket socket = new Socket(scanIp, scanPort);
                     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                    // 接收服务器响应
                    String response;
//                socket.setSoTimeout(1000);
                    log.info("等待扫码中......");
                    while ((response = in.readLine()) != null) {
                        log.info("扫描到的玻璃id:{}", response);
                        List<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("scanCode");
                    String glassId = in.readLine();
                    log.info("扫描到的玻璃id:{}", glassId);
                    List<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("unloadglass");
                        if (CollectionUtils.isNotEmpty(sendwServer)) {
                            //按照玻璃id获取玻璃信息返回给前端界面,具体需要哪些数据待确认
                            GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>()
                                    .eq(GlassInfo::getGlassId, response).last("limit 1"));
                                .eq(GlassInfo::getGlassId, glassId).last("limit 1"));
                            if (null == glassInfo) {
                                log.info("按照玻璃id:{},无法找到玻璃信息", response);
                            log.info("按照玻璃id:{},无法找到玻璃信息", glassId);
                            } else {
                                sendwServer.get(0).sendMessage(glassInfo.toString());
                            }
                            jsonObject.append("scanGlass",glassInfo);
                            sendwServer.get(0).sendMessage(jsonObject.toString());
                        }
                    }
                } catch (Exception exception) {
                    log.info("读取异常,原因为{}", exception.getMessage());
                }
            }).start();
        }
    }
}