#
Junjie
2025-11-25 1ba343ab3a7cd5cbabfef6a27a5344051f526793
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package com.zy.core.network.fake;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.common.model.NavigateNode;
import com.zy.common.utils.NavigateUtils;
import com.zy.core.News;
import com.zy.core.model.CommandResponse;
import com.zy.core.model.command.StationCommand;
import com.zy.core.network.api.ZyStationConnectApi;
import com.zy.core.network.entity.ZyStationStatusEntity;
 
import java.util.ArrayList;
import java.util.List;
 
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
/**
 * 输送站假连接(模拟)
 */
public class ZyStationFakeConnect implements ZyStationConnectApi {
 
    private List<ZyStationStatusEntity> statusList = new ArrayList<>();
    private final DeviceConfig deviceConfig;
    // 允许并行执行多个命令任务(固定线程池)。如需更高并发可调整大小。
    private final ExecutorService executor = Executors
            .newFixedThreadPool(9999);
 
    public ZyStationFakeConnect(DeviceConfig deviceConfig) {
        this.deviceConfig = deviceConfig;
    }
 
    @Override
    public boolean connect() {
        return true;
    }
 
    @Override
    public boolean disconnect() {
        executor.shutdownNow();
        return true;
    }
 
    @Override
    public List<ZyStationStatusEntity> getStatus() {
        if (this.statusList.isEmpty()) {
            this.statusList = JSON.parseArray(deviceConfig.getFakeInitStatus(), ZyStationStatusEntity.class);
 
            for (ZyStationStatusEntity status : this.statusList) {
                status.setAutoing(true);// 模拟自动运行
                status.setLoading(false);// 模拟有物
                status.setInEnable(true);// 模拟可入
                status.setOutEnable(true);// 模拟可出
                status.setEmptyMk(false);// 模拟空板信号
                status.setFullPlt(false);// 模拟满托盘
                status.setPalletHeight(0);// 模拟托盘高度为0
                status.setError(0);// 模拟无报警
                status.setBarcode("");// 模拟无条码
            }
        }
 
        return this.statusList;
    }
 
    @Override
    public CommandResponse sendCommand(StationCommand command) {
        executor.submit(() -> {
            try {
                handleCommand(command);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return new CommandResponse(true, "命令已受理(异步执行)");
    }
 
    private void handleCommand(StationCommand command) {
        News.info("[WCS Debug] 站点仿真模拟已启动,命令数据={}", JSON.toJSONString(command));
        Integer taskNo = command.getTaskNo();
        Integer stationId = command.getStationId();
        Integer targetStationId = command.getTargetStaNo();
 
        if(taskNo == 0 && targetStationId == 0){
            //清空站点
            resetStation(stationId);
            return;
        }
 
        if (taskNo == 9999 && targetStationId == 0) {
            //生成仿真数据
            generateFakeData(stationId, taskNo);
            return;
        }
 
        if (taskNo == 9998 && targetStationId == 0) {
            //生成出库站点仿真数据
            generateFakeOutStationData(stationId);
            return;
        }
 
        String startLev = String.valueOf(stationId).substring(0, 1);
        String endLev = String.valueOf(targetStationId).substring(0, 1);
 
        if (startLev.equals(endLev)) {
            currentLevCommand(command);
        }else {
            diffLevCommand(command);
        }
    }
 
    private void generateFakeData(Integer stationId, Integer taskNo) {
        ZyStationStatusEntity status = statusList.stream()
                .filter(item -> item.getStationId().equals(stationId)).findFirst().orElse(null);
        if (status == null) {
            return;
        }
 
        status.setTaskNo(taskNo);
        status.setLoading(true);
        status.setBarcode(String.valueOf(System.currentTimeMillis()));
    }
 
    private void generateFakeOutStationData(Integer stationId) {
        ZyStationStatusEntity status = statusList.stream()
                .filter(item -> item.getStationId().equals(stationId)).findFirst().orElse(null);
        if (status == null) {
            return;
        }
 
        status.setLoading(true);
    }
 
    private void resetStation(Integer stationId) {
        ZyStationStatusEntity status = statusList.stream()
                .filter(item -> item.getStationId().equals(stationId)).findFirst().orElse(null);
        if (status == null) {
            return;
        }
 
        status.setTaskNo(0);
        status.setLoading(false);
        status.setBarcode("");
    }
 
    private void currentLevCommand(StationCommand command) {
        NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class);
        if (navigateUtils == null) {
            return;
        }
 
        Integer taskNo = command.getTaskNo();
        Integer stationId = command.getStationId();
        Integer targetStationId = command.getTargetStaNo();
 
        String startLev = String.valueOf(stationId).substring(0, 1);
 
        List<NavigateNode> navigateNodes = null;
 
        try {
            navigateNodes = navigateUtils.calcByStationId(Integer.parseInt(startLev), stationId, targetStationId);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        if (navigateNodes == null) {
            return;
        }
 
        stationMove(navigateNodes, taskNo, targetStationId, false);
    }
 
    private void diffLevCommand(StationCommand command) {
        NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class);
        if (navigateUtils == null) {
            return;
        }
 
        Integer taskNo = command.getTaskNo();
        Integer stationId = command.getStationId();
        Integer targetStationId = command.getTargetStaNo();
 
        String startLev = String.valueOf(stationId).substring(0, 1);
        String endLev = String.valueOf(targetStationId).substring(0, 1);
        
        List<NavigateNode> navigateNodes = null;
        List<NavigateNode> targetNavigateNodes = null;
 
        try {
            List<NavigateNode> liftStationList = navigateUtils.findLiftStationList(Integer.parseInt(startLev));
            if(liftStationList.isEmpty()){
                //未找到提升机节点
                return;
            }
 
            List<NavigateNode> targetLiftStationList = navigateUtils.findLiftStationList(Integer.parseInt(endLev));
            if(targetLiftStationList.isEmpty()){
                //未找到提升机节点
                return;
            }
            for (NavigateNode liftStation : liftStationList) {
                JSONObject valuObject = JSON.parseObject(liftStation.getNodeValue());
                if(valuObject == null){
                    continue;
                }
                Integer liftStationId = valuObject.getInteger("stationId");
                Integer liftNo = valuObject.getInteger("liftNo");
 
                Integer targetLiftStationId = null;
                for (NavigateNode targetLiftStation : targetLiftStationList) {
                    JSONObject targetValuObject = JSON.parseObject(targetLiftStation.getNodeValue());
                    if(targetValuObject == null){
                        continue;
                    }
                    Integer targetLiftNo = targetValuObject.getInteger("liftNo");
                    if(liftNo.equals(targetLiftNo)){
                        targetLiftStationId = targetValuObject.getInteger("stationId");
                        break;
                    }
                }
 
                if(targetLiftStationId == null){
                    continue;
                }
 
                navigateNodes = navigateUtils.calcByStationId(Integer.parseInt(startLev), stationId, liftStationId);
                if(navigateNodes == null){
                    continue;
                }
 
                //计算提升机到目标站的路径
                targetNavigateNodes = navigateUtils.calcByStationId(Integer.parseInt(endLev), targetLiftStationId, targetStationId);
                if(targetNavigateNodes == null) {
                    continue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        if (navigateNodes == null || targetNavigateNodes == null) {
            return;
        }
 
        stationMove(navigateNodes, taskNo, stationId, true);
        stationMove(targetNavigateNodes, taskNo, targetStationId, false);
    }
 
    private void stationMove(List<NavigateNode> navigateNodes, Integer taskNo, Integer targetStationId, boolean clearData) {
        Integer lastStationId = null;
        for (int i = 0; i < navigateNodes.size(); i++) {
            NavigateNode navigateNode = navigateNodes.get(i);
            JSONObject valueObject = JSON.parseObject(navigateNode.getNodeValue());
            Integer currentStationId = valueObject.getInteger("stationId");
            ZyStationStatusEntity status = statusList.stream()
                    .filter(item -> item.getStationId().equals(currentStationId)).findFirst().orElse(null);
            if (status == null) {
                continue;
            }
 
            try {
                while (true) {
                    ZyStationStatusEntity nextStatus = statusList.stream()
                            .filter(item -> item.getStationId().equals(currentStationId)).findFirst().orElse(null);
                    if (nextStatus == null) {
                        continue;
                    }
 
                    if (nextStatus.getTaskNo() == 0 || nextStatus.getTaskNo() == 9999) {
                        break;
                    }
 
                    sleep(100);
                }
            } catch (Exception e) {
                continue;
            }
 
            if (lastStationId != null) {
                Integer finalLastStationId = lastStationId;
                ZyStationStatusEntity lastStatus = statusList.stream()
                        .filter(item -> item.getStationId().equals(finalLastStationId)).findFirst().orElse(null);
                if (lastStatus != null) {
                    synchronized (lastStatus) {
                        lastStatus.setTaskNo(0);
                        lastStatus.setTargetStaNo(0);
                        lastStatus.setLoading(false);
                    }
                }
            }
 
            synchronized (status) {
                status.setTaskNo(taskNo);
                status.setTargetStaNo(targetStationId);
                status.setLoading(true);
            }
 
            lastStationId = currentStationId;
            sleep(1000);
        }
 
        if (clearData) {
            sleep(10000);
            if (lastStationId != null) {
                Integer finalLastStationId = lastStationId;
                ZyStationStatusEntity lastStatus = statusList.stream()
                        .filter(item -> item.getStationId().equals(finalLastStationId)).findFirst().orElse(null);
                if (lastStatus != null) {
                    synchronized (lastStatus) {
                        lastStatus.setTaskNo(0);
                        lastStatus.setTargetStaNo(0);
                        lastStatus.setLoading(false);
                    }
                }
            }
        }
    }
 
    private void sleep(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}