Junjie
13 小时以前 03c3ae747f82ad22c761c79e7b1c0e0031c57d41
src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java
@@ -30,6 +30,9 @@
import com.zy.core.network.DeviceConnectPool;
import com.zy.core.network.ZyStationConnectDriver;
import com.zy.core.network.entity.ZyStationStatusEntity;
import com.zy.core.thread.impl.v5.StationMoveSegmentExecutor;
import com.zy.core.thread.support.RecentStationArrivalTracker;
import com.zy.core.thread.support.StationErrLogSupport;
import com.zy.core.utils.DeviceLogRedisKeyBuilder;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
@@ -48,17 +51,22 @@
    private static final double DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = 0.3d;
    private List<StationProtocol> statusList = new ArrayList<>();
    private volatile List<Integer> taskNoList = new ArrayList<>();
    private DeviceConfig deviceConfig;
    private RedisUtil redisUtil;
    private ZyStationConnectDriver zyStationConnectDriver;
    private StationMoveSegmentExecutor segmentExecutor;
    private int deviceLogCollectTime = 200;
    private boolean initStatus = false;
    private long deviceDataLogTime = System.currentTimeMillis();
    private ExecutorService executor = Executors.newFixedThreadPool(9999);
    private final RecentStationArrivalTracker recentArrivalTracker;
    public ZyStationV4Thread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
        this.deviceConfig = deviceConfig;
        this.redisUtil = redisUtil;
        this.recentArrivalTracker = new RecentStationArrivalTracker(redisUtil);
        this.segmentExecutor = new StationMoveSegmentExecutor(deviceConfig, redisUtil, this::sendCommand);
    }
    @Override
@@ -92,7 +100,7 @@
                    }
                    if (step == 2) {
                        StationCommand cmd = (StationCommand) task.getData();
                        executor.submit(() -> executeMoveWithSeg(cmd));
                        executor.submit(() -> segmentExecutor.execute(cmd));
                    }
                    Thread.sleep(100);
                } catch (Exception e) {
@@ -134,6 +142,7 @@
        }
        List<ZyStationStatusEntity> zyStationStatusEntities = zyStationConnectDriver.getStatus();
        LinkedHashSet<Integer> taskNoSet = new LinkedHashSet<>();
        for (ZyStationStatusEntity statusEntity : zyStationStatusEntities) {
            for (StationProtocol stationProtocol : statusList) {
                if (stationProtocol.getStationId().equals(statusEntity.getStationId())) {
@@ -153,6 +162,19 @@
                    stationProtocol.setEnableIn(statusEntity.isEnableIn());
                    stationProtocol.setWeight(statusEntity.getWeight());
                    stationProtocol.setTaskWriteIdx(statusEntity.getTaskWriteIdx());
                    stationProtocol.setTaskBufferItems(statusEntity.getTaskBufferItems());
                    if (statusEntity.getTaskNo() != null && statusEntity.getTaskNo() > 0) {
                        taskNoSet.add(statusEntity.getTaskNo());
                    }
                    if (statusEntity.getTaskBufferItems() != null) {
                        statusEntity.getTaskBufferItems().forEach(item -> {
                            Integer bufferTaskNo = item == null ? null : item.getTaskNo();
                            if (bufferTaskNo != null && bufferTaskNo > 0) {
                                taskNoSet.add(bufferTaskNo);
                            }
                        });
                    }
                    recentArrivalTracker.observe(statusEntity.getStationId(), statusEntity.getTaskNo(), statusEntity.isLoading());
                }
                if (!Cools.isEmpty(stationProtocol.getSystemWarning())) {
@@ -164,8 +186,10 @@
                }
            }
        }
        taskNoList = new ArrayList<>(taskNoSet);
        OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), deviceConfig.getDeviceNo()));
        StationErrLogSupport.sync(deviceConfig, redisUtil, statusList);
        if (System.currentTimeMillis() - deviceDataLogTime > deviceLogCollectTime) {
            DeviceDataLog deviceDataLog = new DeviceDataLog();
@@ -213,7 +237,27 @@
    }
    @Override
    public List<Integer> getAllTaskNoList() {
        return taskNoList;
    }
    @Override
    public boolean hasRecentArrival(Integer stationId, Integer taskNo) {
        return recentArrivalTracker.hasRecentArrival(stationId, taskNo);
    }
    @Override
    public StationCommand getCommand(StationCommandType commandType, Integer taskNo, Integer stationId, Integer targetStationId, Integer palletSize) {
        return getCommand(commandType, taskNo, stationId, targetStationId, palletSize, null);
    }
    @Override
    public StationCommand getCommand(StationCommandType commandType,
                                     Integer taskNo,
                                     Integer stationId,
                                     Integer targetStationId,
                                     Integer palletSize,
                                     Double pathLenFactor) {
        StationCommand stationCommand = new StationCommand();
        stationCommand.setTaskNo(taskNo);
        stationCommand.setStationId(stationId);
@@ -223,7 +267,7 @@
        if (commandType == StationCommandType.MOVE) {
            if (!stationId.equals(targetStationId)) {
                List<NavigateNode> nodes = calcPathNavigateNodes(stationId, targetStationId);
                List<NavigateNode> nodes = calcPathNavigateNodes(taskNo, stationId, targetStationId, pathLenFactor);
                List<Integer> path = new ArrayList<>();
                List<Integer> liftTransferPath = new ArrayList<>();
                for (NavigateNode n : nodes) {
@@ -261,6 +305,9 @@
            e.printStackTrace();
        } finally {
            BasStationOptService optService = SpringUtils.getBean(BasStationOptService.class);
            if (optService == null) {
                return commandResponse;
            }
            List<ZyStationStatusEntity> statusListEntity = zyStationConnectDriver.getStatus();
            ZyStationStatusEntity matched = null;
            if (statusListEntity != null) {
@@ -283,12 +330,10 @@
                    null,
                    JSON.toJSONString(command),
                    JSON.toJSONString(matched),
                    1,
                    commandResponse != null && Boolean.TRUE.equals(commandResponse.getResult()) ? 1 : 0,
                    JSON.toJSONString(commandResponse)
            );
            if (optService != null) {
                optService.save(basStationOpt);
            }
            optService.save(basStationOpt);
        }
        return commandResponse;
    }
@@ -303,12 +348,15 @@
        return zyStationConnectDriver.readOriginCommand(address, length);
    }
    private List<NavigateNode> calcPathNavigateNodes(Integer startStationId, Integer targetStationId) {
    private List<NavigateNode> calcPathNavigateNodes(Integer taskNo,
                                                     Integer startStationId,
                                                     Integer targetStationId,
                                                     Double pathLenFactor) {
        NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class);
        if (navigateUtils == null) {
            return new ArrayList<>();
        }
        return navigateUtils.calcByStationId(startStationId, targetStationId);
        return navigateUtils.calcOptimalPathByStationId(startStationId, targetStationId, taskNo, pathLenFactor);
    }
    private void executeMoveWithSeg(StationCommand original) {
@@ -545,4 +593,9 @@
        } catch (Exception ignore) {
        }
    }
    @Override
    public boolean clearPath(Integer taskNo) {
        return false;
    }
}