#
Junjie
22 小时以前 d193355c76cfbec572408f9a256c970699c9a542
src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java
@@ -3,7 +3,7 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
@@ -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.utils.DeviceLogRedisKeyBuilder;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -41,12 +44,15 @@
@Data
@Slf4j
public class ZyStationV4Thread implements Runnable, com.zy.core.thread.StationThread {
    private static final String CFG_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = "stationCommandSegmentAdvanceRatio";
    private static final double DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = 0.3d;
    private List<StationProtocol> statusList = new ArrayList<>();
    private DeviceConfig deviceConfig;
    private RedisUtil redisUtil;
    private ZyStationConnectDriver zyStationConnectDriver;
    private int deviceLogCollectTime = 200;
    private boolean initStatus = false;
    private long deviceDataLogTime = System.currentTimeMillis();
    private ExecutorService executor = Executors.newFixedThreadPool(9999);
@@ -64,14 +70,16 @@
        Thread readThread = new Thread(() -> {
            while (true) {
                try {
                    deviceLogCollectTime = Utils.getDeviceLogCollectTime();
                    if (initStatus) {
                        deviceLogCollectTime = Utils.getDeviceLogCollectTime();
                    }
                    readStatus();
                    Thread.sleep(100);
                } catch (Exception e) {
                    log.error("StationV4Thread Fail", e);
                }
            }
        });
        }, "DevpRead-" + deviceConfig.getDeviceNo());
        readThread.start();
        Thread processThread = new Thread(() -> {
@@ -91,7 +99,7 @@
                    log.error("StationV4Process Fail", e);
                }
            }
        });
        }, "DevpProcess-" + deviceConfig.getDeviceNo());
        processThread.start();
    }
@@ -111,7 +119,7 @@
            }
            BasDevp basDevp = basDevpService
                    .selectOne(new EntityWrapper<BasDevp>().eq("devp_no", deviceConfig.getDeviceNo()));
                    .getOne(new QueryWrapper<BasDevp>().eq("devp_no", deviceConfig.getDeviceNo()));
            if (basDevp == null) {
                return;
            }
@@ -122,6 +130,7 @@
                stationProtocol.setStationId(entity.getStationId());
                statusList.add(stationProtocol);
            }
            initStatus = true;
        }
        List<ZyStationStatusEntity> zyStationStatusEntities = zyStationConnectDriver.getStatus();
@@ -143,6 +152,7 @@
                    stationProtocol.setRunBlock(statusEntity.isRunBlock());
                    stationProtocol.setEnableIn(statusEntity.isEnableIn());
                    stationProtocol.setWeight(statusEntity.getWeight());
                    stationProtocol.setTaskWriteIdx(statusEntity.getTaskWriteIdx());
                }
                if (!Cools.isEmpty(stationProtocol.getSystemWarning())) {
@@ -165,7 +175,7 @@
            deviceDataLog.setDeviceNo(deviceConfig.getDeviceNo());
            deviceDataLog.setCreateTime(new Date());
            redisUtil.set(RedisKeyType.DEVICE_LOG_KEY.key + System.currentTimeMillis(), deviceDataLog, 60 * 60 * 24);
            redisUtil.set(DeviceLogRedisKeyBuilder.build(deviceDataLog), deviceDataLog, 60 * 60 * 24);
            deviceDataLogTime = System.currentTimeMillis();
        }
    }
@@ -272,7 +282,7 @@
                    JSON.toJSONString(commandResponse)
            );
            if (optService != null) {
                optService.insert(basStationOpt);
                optService.save(basStationOpt);
            }
        }
        return commandResponse;
@@ -297,10 +307,23 @@
    }
    private void executeMoveWithSeg(StationCommand original) {
        double segmentAdvanceRatio = loadSegmentAdvanceRatio();
        if(original.getCommandType() == StationCommandType.MOVE){
            List<Integer> path = JSON.parseArray(JSON.toJSONString(original.getNavigatePath(), SerializerFeature.DisableCircularReferenceDetect), Integer.class);
            List<Integer> liftTransferPath = JSON.parseArray(JSON.toJSONString(original.getLiftTransferPath(), SerializerFeature.DisableCircularReferenceDetect), Integer.class);
            if (path == null || path.isEmpty()) {
                // 同站点任务不会生成路径,但仍需下发命令写入任务数据
                if (Objects.equals(original.getStationId(), original.getTargetStaNo())) {
                    while (true) {
                        CommandResponse commandResponse = sendCommand(original);
                        if (commandResponse != null && commandResponse.getResult()) {
                            break;
                        }
                        try {
                            Thread.sleep(200);
                        } catch (Exception ignore) {}
                    }
                }
                return;
            }
@@ -406,7 +429,7 @@
                    int currentSegStartIndex = segCursor == 0 ? 0 : segmentEndIndices.get(segCursor - 1);
                    int segLen = currentSegEndIndex - currentSegStartIndex + 1;
                    int remainingSegment = Math.max(0, currentSegEndIndex - currentIndex);
                    int thresholdSegment = (int) Math.ceil(segLen * 0.3);
                    int thresholdSegment = (int) Math.ceil(segLen * segmentAdvanceRatio);
                    if (remainingSegment <= thresholdSegment && segCursor < segmentCommands.size() - 1) {
                        segCursor++;
                        while (true) {
@@ -434,13 +457,58 @@
        }
    }
    private double loadSegmentAdvanceRatio() {
        try {
            ConfigService configService = SpringUtils.getBean(ConfigService.class);
            if (configService == null) {
                return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
            }
            Config config = configService.getOne(new QueryWrapper<Config>()
                    .eq("code", CFG_STATION_COMMAND_SEGMENT_ADVANCE_RATIO));
            if (config == null || Cools.isEmpty(config.getValue())) {
                return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
            }
            return normalizeSegmentAdvanceRatio(config.getValue());
        } catch (Exception ignore) {
            return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
        }
    }
    private double normalizeSegmentAdvanceRatio(String valueText) {
        if (valueText == null) {
            return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
        }
        String text = valueText.trim();
        if (text.isEmpty()) {
            return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
        }
        if (text.endsWith("%")) {
            text = text.substring(0, text.length() - 1).trim();
        }
        try {
            double ratio = Double.parseDouble(text);
            if (ratio > 1d && ratio <= 100d) {
                ratio = ratio / 100d;
            }
            if (ratio < 0d) {
                return 0d;
            }
            if (ratio > 1d) {
                return 1d;
            }
            return ratio;
        } catch (Exception ignore) {
            return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
        }
    }
    private StationProtocol findCurrentStationByTask(Integer taskNo) {
        try {
            com.zy.asrs.service.DeviceConfigService deviceConfigService = SpringUtils.getBean(com.zy.asrs.service.DeviceConfigService.class);
            if (deviceConfigService == null) {
                return null;
            }
            List<DeviceConfig> devpList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
            List<DeviceConfig> devpList = deviceConfigService.list(new QueryWrapper<DeviceConfig>()
                    .eq("device_type", String.valueOf(SlaveType.Devp)));
            for (DeviceConfig dc : devpList) {
                com.zy.core.thread.StationThread t = (com.zy.core.thread.StationThread) SlaveConnection.get(SlaveType.Devp, dc.getDeviceNo());