Junjie
昨天 41aeff86351d1dd94fe2408175f96475f227c1b9
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
package com.zy.core.utils.station;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.asrs.domain.enums.NotifyMsgType;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.BasStation;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.BasDevpService;
import com.zy.asrs.service.BasStationService;
import com.zy.asrs.service.WrkAnalysisService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.utils.NotifyUtils;
import com.zy.common.entity.FindCrnNoResult;
import com.zy.common.service.CommonService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.dispatch.StationCommandDispatchResult;
import com.zy.core.dispatch.StationCommandDispatcher;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.StationCommandType;
import com.zy.core.enums.WrkStsType;
import com.zy.core.model.StationObjModel;
import com.zy.core.model.command.StationCommand;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.model.protocol.StationTaskBufferItem;
import com.zy.core.move.StationMoveCoordinator;
import com.zy.core.thread.StationThread;
import com.zy.core.utils.station.model.DispatchLimitConfig;
import com.zy.core.utils.station.model.LoadGuardState;
import com.zy.core.utils.station.model.LoopHitResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
@Component
public class StationRegularDispatchProcessor {
 
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private WrkAnalysisService wrkAnalysisService;
    @Autowired
    private BasStationService basStationService;
    @Autowired
    private NotifyUtils notifyUtils;
    @Autowired
    private StationMoveCoordinator stationMoveCoordinator;
    @Autowired(required = false)
    private StationCommandDispatcher stationCommandDispatcher;
    @Autowired
    private StationDispatchLoadSupport stationDispatchLoadSupport;
 
    public void stationInExecute() {
        try {
            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
            for (BasDevp basDevp : basDevps) {
                List<StationObjModel> stationList = basDevp.getBarcodeStationList$();
                for (StationObjModel entity : stationList) {
                    stationInExecute(basDevp, entity);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void stationOutExecuteFinish(StationObjModel stationObjModel) {
        try {
            if (stationObjModel == null) {
                return;
            }
 
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
            if (stationThread == null) {
                return;
            }
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationObjModel.getStationId());
            if (stationProtocol == null) {
                return;
            }
            if (stationProtocol.getTaskNo() <= 0) {
                return;
            }
            WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
            if (wrkMast == null) {
                return;
            }
 
            if (!wrkMast.getWrkSts().equals(WrkStsType.STATION_RUN.sts)) {
                return;
            }
            Integer wrkNo = wrkMast.getWrkNo();
 
            if (stationMoveCoordinator != null) {
                stationMoveCoordinator.finishSession(wrkMast.getWrkNo());
            }
            Date now = new Date();
            wrkMast.setWrkSts(WrkStsType.STATION_RUN_COMPLETE.sts);
            wrkMast.setIoTime(now);
            wrkMast.setModiTime(now);
            wrkMastService.updateById(wrkMast);
            wrkAnalysisService.markOutboundStationComplete(wrkMast, now);
            notifyUtils.notify(String.valueOf(SlaveType.Devp), stationObjModel.getDeviceNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.STATION_OUT_TASK_RUN_COMPLETE, null);
            redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkMast.getWrkNo(), "lock", 60);
            attemptClearTaskPath(stationThread, wrkNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void checkTaskToComplete() {
        try {
            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts));
            for (WrkMast wrkMast : wrkMasts) {
                checkTaskToComplete(wrkMast);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void checkTaskToComplete(WrkMast wrkMast) {
        try {
            if (wrkMast == null || wrkMast.getWrkNo() == null || wrkMast.getStaNo() == null) {
                return;
            }
            Integer wrkNo = wrkMast.getWrkNo();
            Integer targetStaNo = wrkMast.getStaNo();
 
            Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkNo);
            if (lock != null) {
                return;
            }
 
            BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
            if (basStation == null) {
                return;
            }
 
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
            if (stationThread == null) {
                return;
            }
 
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(basStation.getStationId());
            if (stationProtocol == null) {
                return;
            }
 
            if (!Objects.equals(stationProtocol.getTaskNo(), wrkNo)) {
                if (stationMoveCoordinator != null) {
                    stationMoveCoordinator.finishSession(wrkNo);
                }
                wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
                wrkMast.setIoTime(new Date());
                wrkMastService.updateById(wrkMast);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void attemptClearTaskPath(StationThread stationThread, Integer taskNo) {
        if (stationThread == null || taskNo == null || taskNo <= 0) {
            return;
        }
        try {
            boolean cleared = stationThread.clearPath(taskNo);
            if (cleared) {
                News.info("输送站点任务运行完成后清理残留路径,工作号={}", taskNo);
            }
        } catch (Exception e) {
            News.error("输送站点任务运行完成后清理残留路径异常,工作号={}", taskNo, e);
        }
    }
 
    private boolean offerDevpCommandWithDedup(Integer deviceNo, StationCommand command, String scene) {
        StationCommandDispatchResult dispatchResult = stationCommandDispatcher.dispatch(deviceNo, command, "station-operate-process", scene);
        return dispatchResult.isAccepted();
    }
 
    public void stationInExecute(BasDevp basDevp, StationObjModel entity) {
        if (basDevp == null || basDevp.getDevpNo() == null || entity == null || entity.getStationId() == null) {
            return;
        }
 
        Integer stationId = entity.getStationId();
        StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
        if (stationThread == null) {
            return;
        }
 
        Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
        if (stationMap == null || !stationMap.containsKey(stationId)) {
            return;
        }
 
        StationProtocol stationProtocol = stationMap.get(stationId);
        if (stationProtocol == null) {
            return;
        }
 
        Object lock = redisUtil.get(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId);
        if (lock != null) {
            return;
        }
 
        if (!stationProtocol.isAutoing()
                || !stationProtocol.isLoading()
                || stationProtocol.getTaskNo() <= 0) {
            return;
        }
 
        WrkMast wrkMast = wrkMastService.getOne(new QueryWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode()));
        if (wrkMast == null || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_INBOUND.sts)) {
            return;
        }
 
        String locNo = wrkMast.getLocNo();
        FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
        if (findCrnNoResult == null) {
            News.taskInfo(wrkMast.getWrkNo(), "{}工作,未匹配到堆垛机", wrkMast.getWrkNo());
            return;
        }
 
        Integer targetStationId = commonService.findInStationId(findCrnNoResult, stationId);
        if (targetStationId == null) {
            News.taskInfo(wrkMast.getWrkNo(), "{}站点,搜索入库站点失败", stationId);
            return;
        }
 
        DispatchLimitConfig limitConfig = stationDispatchLoadSupport.getDispatchLimitConfig(stationProtocol.getStationId(), targetStationId);
        int currentStationTaskCount = stationDispatchLoadSupport.countCurrentStationTask();
        LoadGuardState loadGuardState = stationDispatchLoadSupport.buildLoadGuardState(limitConfig);
        LoopHitResult loopHitResult = stationDispatchLoadSupport.findPathLoopHit(
                limitConfig,
                stationProtocol.getStationId(),
                targetStationId,
                loadGuardState
        );
        if (stationDispatchLoadSupport.isDispatchBlocked(limitConfig, currentStationTaskCount, loadGuardState, loopHitResult.isThroughLoop())) {
            return;
        }
 
        StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationId, targetStationId, 0);
        if (command == null) {
            News.taskInfo(wrkMast.getWrkNo(), "{}工作,获取输送线命令失败", wrkMast.getWrkNo());
            return;
        }
 
        Date now = new Date();
        wrkMast.setWrkSts(WrkStsType.INBOUND_STATION_RUN.sts);
        wrkMast.setSourceStaNo(stationProtocol.getStationId());
        wrkMast.setStaNo(targetStationId);
        wrkMast.setSystemMsg("");
        wrkMast.setIoTime(now);
        wrkMast.setModiTime(now);
        if (wrkMastService.updateById(wrkMast)) {
            wrkAnalysisService.markInboundStationStart(wrkMast, now);
            boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "stationInExecute");
            if (offered && stationMoveCoordinator != null) {
                stationMoveCoordinator.recordDispatch(
                        wrkMast.getWrkNo(),
                        stationProtocol.getStationId(),
                        "stationInExecute",
                        command,
                        false
                );
            }
            News.info("输送站点入库命令下发成功,站点号={},工作号={},命令数据={}", stationId, wrkMast.getWrkNo(), JSON.toJSONString(command));
            redisUtil.set(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId, "lock", 5);
            loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
            stationDispatchLoadSupport.saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
        }
    }
}