#
Junjie
2025-11-13 1b4fbdb92537036aed4d648967ef7e7ab8842aec
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
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.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(Math.max(2, Runtime.getRuntime().availableProcessors()));
 
    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) {
        NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class);
        if (navigateUtils == null) {
            return;
        }
 
        Integer taskNo = command.getTaskNo();
        Integer stationId = command.getStationId();
        Integer targetStationId = command.getTargetStaNo();
 
        List<NavigateNode> navigateNodes = null;
 
        try {
            navigateNodes = navigateUtils.calcByStationId(stationId, targetStationId);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        if (navigateNodes == null) {
            return;
        }
 
        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) {
                        break;
                    }
 
                    sleep(100);
                }
            } catch (Exception e) {
                continue;
            }
 
            synchronized (status) {
                status.setTaskNo(taskNo);
                status.setTargetStaNo(targetStationId);
                status.setLoading(true);
            }
 
            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);
                    }
                }
            }
            lastStationId = currentStationId;
            sleep(1000);
        }
 
        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();
        }
    }
}