#
Junjie
1 天以前 6c8ee211ae63de5f4ce0bd13b9c34f0524cd656f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.zy.core.network.fake;
 
import com.zy.core.News;
import com.zy.core.network.entity.ZyStationStatusEntity;
 
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 * 仿真站点移动引擎
 * 负责站点间的移动执行:初始化、移动到下一站、清除站点
 * 使用 ReentrantLock 做站点级并发控制
 */
public class FakeStationMoveEngine {
 
    private final Map<Integer, ReentrantLock> stationLocks = new ConcurrentHashMap<>();
    private final FakeStationStateManager stateManager;
 
    public FakeStationMoveEngine(FakeStationStateManager stateManager) {
        this.stateManager = stateManager;
    }
 
    public boolean initStationMove(Integer lockTaskNo, Integer currentStationId,
            Integer currentStationDeviceNo, Integer taskNo, Integer targetStationId,
            Boolean isLoading, String barcode) {
        lockStations(currentStationId);
        try {
            ZyStationStatusEntity currentStatus = stateManager.findStationStatus(currentStationDeviceNo, currentStationId);
            if (currentStatus == null) { return false; }
            if (currentStatus.getTaskNo() != null && currentStatus.getTaskNo() > 0
                    && !currentStatus.getTaskNo().equals(taskNo) && currentStatus.isLoading()) {
                return false;
            }
            boolean updated = stateManager.updateStationDataWithHeldLock(currentStationId, currentStationDeviceNo,
                    taskNo, targetStationId, isLoading, barcode, false);
            if (updated) {
                stateManager.publishTaskLocation(taskNo, currentStationDeviceNo, currentStationId,
                        Boolean.TRUE.equals(isLoading), false);
            }
            return updated;
        } finally {
            unlockStations(currentStationId);
        }
    }
 
    public boolean stationMoveToNext(Integer lockTaskNo, Integer currentStationId,
            Integer currentStationDeviceNo, Integer nextStationId, Integer nextStationDeviceNo,
            Integer taskNo, Integer targetStaNo) {
        News.info("[WCS Debug] fake stationMoveToNext开始,taskNo={},currentStationId={},currentDeviceNo={},nextStationId={},nextDeviceNo={},targetStaNo={}",
                taskNo, currentStationId, currentStationDeviceNo, nextStationId, nextStationDeviceNo, targetStaNo);
        lockStations(currentStationId, nextStationId);
        try {
            ZyStationStatusEntity currentStatus = stateManager.findStationStatus(currentStationDeviceNo, currentStationId);
            ZyStationStatusEntity nextStatus = stateManager.findStationStatus(nextStationDeviceNo, nextStationId);
            if (currentStatus == null || nextStatus == null) {
                News.info("仿真站点移动失败,原因=station_status_missing,taskNo={},currentStationId={},currentDeviceNo={},nextStationId={},nextDeviceNo={},targetStaNo={},currentStatusNull={},nextStatusNull={}",
                        taskNo, currentStationId, currentStationDeviceNo, nextStationId, nextStationDeviceNo, targetStaNo,
                        currentStatus == null, nextStatus == null);
                return false;
            }
            if ((nextStatus.getTaskNo() != null && nextStatus.getTaskNo() > 0) || nextStatus.isLoading()) {
                News.info("仿真站点移动失败,原因=next_station_busy,taskNo={},currentStationId={},nextStationId={},targetStaNo={},currentTaskNo={},currentLoading={},currentRunBlock={},nextTaskNo={},nextLoading={},nextRunBlock={},nextTargetStaNo={}",
                        taskNo, currentStationId, nextStationId, targetStaNo,
                        currentStatus.getTaskNo(), currentStatus.isLoading(), currentStatus.isRunBlock(),
                        nextStatus.getTaskNo(), nextStatus.isLoading(), nextStatus.isRunBlock(), nextStatus.getTargetStaNo());
                return false;
            }
            if (currentStatus.getTaskNo() == null || !taskNo.equals(currentStatus.getTaskNo()) || !currentStatus.isLoading()) {
                News.info("仿真站点移动失败,原因=current_station_task_mismatch,taskNo={},currentStationId={},nextStationId={},currentTaskNo={},currentLoading={},currentTargetStaNo={}",
                        taskNo, currentStationId, nextStationId,
                        currentStatus.getTaskNo(), currentStatus.isLoading(), currentStatus.getTargetStaNo());
                return false;
            }
 
            String barcode = currentStatus.getBarcode();
            News.info("[WCS Debug] fake handoff准备提交,taskNo={},sourceStationId={},sourceTaskNo={},sourceLoading={},sourceTargetStaNo={},sourceBarcode={},sourceRunBlock={},targetStationId={},targetTaskNo={},targetLoading={},targetTargetStaNo={},targetBarcode={},targetRunBlock={}",
                    taskNo,
                    currentStationId, currentStatus.getTaskNo(), currentStatus.isLoading(), currentStatus.getTargetStaNo(), currentStatus.getBarcode(), currentStatus.isRunBlock(),
                    nextStationId, nextStatus.getTaskNo(), nextStatus.isLoading(), nextStatus.getTargetStaNo(), nextStatus.getBarcode(), nextStatus.isRunBlock());
 
            boolean prepared = stateManager.updateStationDataWithHeldLock(nextStationId, nextStationDeviceNo,
                    taskNo, targetStaNo, false, barcode, false);
            if (!prepared) {
                News.info("仿真站点移动失败,原因=next_station_prepare_failed,taskNo={},currentStationId={},nextStationId={},targetStaNo={}",
                        taskNo, currentStationId, nextStationId, targetStaNo);
                return false;
            }
            boolean loaded = stateManager.updateStationDataWithHeldLock(nextStationId, nextStationDeviceNo,
                    null, null, true, null, false);
            if (!loaded) {
                News.info("仿真站点移动失败,原因=next_station_loading_commit_failed,taskNo={},currentStationId={},nextStationId={},targetStaNo={}",
                        taskNo, currentStationId, nextStationId, targetStaNo);
                stateManager.updateStationDataWithHeldLock(nextStationId, nextStationDeviceNo, 0, 0, false, "", false);
                return false;
            }
            boolean cleared = stateManager.updateStationDataWithHeldLock(currentStationId, currentStationDeviceNo,
                    0, 0, false, "", false);
            if (!cleared) {
                News.info("仿真站点移动失败,原因=current_station_clear_failed,taskNo={},currentStationId={},nextStationId={},targetStaNo={},currentTaskNo={},currentLoading={},currentRunBlock={},nextTaskNo={},nextLoading={},nextRunBlock={}",
                        taskNo, currentStationId, nextStationId, targetStaNo,
                        currentStatus.getTaskNo(), currentStatus.isLoading(), currentStatus.isRunBlock(),
                        taskNo, true, false);
                stateManager.updateStationDataWithHeldLock(nextStationId, nextStationDeviceNo, 0, 0, false, "", false);
                return false;
            }
            News.info("[WCS Debug] fake handoff提交成功,taskNo={},sourceStationId={},targetStationId={},targetStaNo={},sourceCleared=true,targetLoading=true",
                    taskNo, currentStationId, nextStationId, targetStaNo);
 
            stateManager.clearTaskLocationIfMatches(taskNo, currentStationDeviceNo, currentStationId);
            stateManager.publishTaskLocation(taskNo, nextStationDeviceNo, nextStationId, true, false);
            return true;
        } finally {
            unlockStations(currentStationId, nextStationId);
        }
    }
 
    public boolean clearStation(Integer deviceNo, Integer lockTaskNo, Integer currentStationId) {
        lockStations(currentStationId);
        try {
            ZyStationStatusEntity currentStatus = stateManager.findStationStatus(deviceNo, currentStationId);
            if (currentStatus == null) { return false; }
            Integer taskNo = currentStatus.getTaskNo();
            boolean cleared = stateManager.updateStationDataWithHeldLock(currentStationId, deviceNo, 0, 0, false, "", false);
            if (cleared) {
                stateManager.clearTaskLocationIfMatches(taskNo, deviceNo, currentStationId);
            }
            return cleared;
        } finally {
            unlockStations(currentStationId);
        }
    }
 
    private ReentrantLock getStationLock(Integer stationId) {
        return stationLocks.computeIfAbsent(stationId, key -> new ReentrantLock());
    }
 
    public void lockStations(Integer... stationIds) {
        Integer[] sorted = Arrays.copyOf(stationIds, stationIds.length);
        Arrays.sort(sorted);
        for (Integer stationId : sorted) {
            getStationLock(stationId).lock();
        }
    }
 
    public void unlockStations(Integer... stationIds) {
        Integer[] sorted = Arrays.copyOf(stationIds, stationIds.length);
        Arrays.sort(sorted);
        for (int i = sorted.length - 1; i >= 0; i--) {
            getStationLock(sorted[i]).unlock();
        }
    }
}