#
Junjie
3 天以前 6d99081f3f6e0363b0bd28053e545a8e4589b979
src/main/java/com/zy/core/thread/impl/ZyStationThread.java
@@ -20,6 +20,7 @@
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.StationCommandType;
import com.zy.core.model.CommandResponse;
import com.zy.core.model.Task;
import com.zy.core.model.command.StationCommand;
@@ -45,9 +46,6 @@
    private ZyStationConnectDriver zyStationConnectDriver;
    private int deviceLogCollectTime = 200;
    private long deviceDataLogTime = System.currentTimeMillis();
    private volatile boolean closed = false;
    private Thread mainThread;
    private Thread readThread;
    public ZyStationThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
        this.deviceConfig = deviceConfig;
@@ -59,16 +57,13 @@
    public void run() {
        this.connect();
        deviceLogCollectTime = Utils.getDeviceLogCollectTime();
        mainThread = Thread.currentThread();
        readThread = new Thread(() -> {
            while (!closed && !Thread.currentThread().isInterrupted()) {
        Thread readThread = new Thread(() -> {
            while (true) {
                try {
                    deviceLogCollectTime = Utils.getDeviceLogCollectTime();
                    readStatus();
                    Thread.sleep(200);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    break;
                    Thread.sleep(100);
                } catch (Exception e) {
                    log.error("StationThread Fail", e);
                }
@@ -76,28 +71,25 @@
        });
        readThread.start();
        while (!closed && !Thread.currentThread().isInterrupted()) {
            try {
                int step = 1;
                Task task = MessageQueue.poll(SlaveType.Devp, deviceConfig.getDeviceNo());
                if (task != null) {
                    step = task.getStep();
                }
                switch (step) {
                    case 2:
        Thread processThread = new Thread(() -> {
            while (true) {
                try {
                    int step = 1;
                    Task task = MessageQueue.poll(SlaveType.Devp, deviceConfig.getDeviceNo());
                    if (task != null) {
                        step = task.getStep();
                    }
                    if (step == 2) {
                        sendCommand((StationCommand) task.getData());
                        break;
                    default:
                        break;
                    }
                    Thread.sleep(100);
                } catch (Exception e) {
                    log.error("StationProcess Fail", e);
                }
                Thread.sleep(200);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        });
        processThread.start();
    }
    private void readStatus() {
@@ -144,7 +136,11 @@
                    stationProtocol.setFullPlt(statusEntity.isFullPlt());
                    stationProtocol.setPalletHeight(statusEntity.getPalletHeight());
                    stationProtocol.setError(statusEntity.getError());
                    stationProtocol.setErrorMsg(statusEntity.getErrorMsg());
                    stationProtocol.setBarcode(statusEntity.getBarcode());
                    stationProtocol.setRunBlock(statusEntity.isRunBlock());
                    stationProtocol.setEnableIn(statusEntity.isEnableIn());
                    stationProtocol.setWeight(statusEntity.getWeight());
                }
            }
        }
@@ -168,7 +164,7 @@
    @Override
    public boolean connect() {
        zyStationConnectDriver = new ZyStationConnectDriver(deviceConfig);
        zyStationConnectDriver = new ZyStationConnectDriver(deviceConfig, redisUtil);
        zyStationConnectDriver.start();
        DeviceConnectPool.put(SlaveType.Devp, deviceConfig.getDeviceNo(), zyStationConnectDriver);
        return true;
@@ -176,15 +172,6 @@
    @Override
    public void close() {
        closed = true;
        Thread t = mainThread;
        if (t != null) {
            try { t.interrupt(); } catch (Exception ignore) {}
        }
        Thread rt = readThread;
        if (rt != null) {
            try { rt.interrupt(); } catch (Exception ignore) {}
        }
        if (zyStationConnectDriver != null) {
            zyStationConnectDriver.close();
        }
@@ -196,12 +183,13 @@
    }
    @Override
    public StationCommand getMoveCommand(Integer taskNo, Integer stationId, Integer targetStationId, Integer palletSize) {
    public StationCommand getCommand(StationCommandType commandType, Integer taskNo, Integer stationId, Integer targetStationId, Integer palletSize) {
        StationCommand stationCommand = new StationCommand();
        stationCommand.setTaskNo(taskNo);
        stationCommand.setStationId(stationId);
        stationCommand.setTargetStaNo(targetStationId);
        stationCommand.setPalletSize(palletSize);
        stationCommand.setCommandType(commandType);
        return stationCommand;
    }
@@ -252,4 +240,14 @@
        }
        return map;
    }
    @Override
    public CommandResponse sendOriginCommand(String address, short[] data) {
        return zyStationConnectDriver.sendOriginCommand(address, data);
    }
    @Override
    public byte[] readOriginCommand(String address, int length) {
        return zyStationConnectDriver.readOriginCommand(address, length);
    }
}