Junjie
8 天以前 fc67e7de548d5fc4ba84a2c09d3e99aad2fc11d7
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
329
330
331
332
333
334
335
336
package com.zy.core.plugin.station;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.Cools;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.service.CommonService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.dispatch.StationCommandDispatcher;
import com.zy.core.enums.*;
import com.zy.core.model.StationObjModel;
import com.zy.core.model.command.StationCommand;
import com.zy.core.model.protocol.CrnProtocol;
import com.zy.core.model.protocol.DualCrnProtocol;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.thread.DualCrnThread;
import com.zy.core.task.MainProcessLane;
import com.zy.core.task.MainProcessTaskSubmitter;
import com.zy.core.thread.CrnThread;
import com.zy.core.thread.StationThread;
import com.zy.core.cache.SlaveConnection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
 
@Slf4j
@Component
public class FakeStationMonitor {
 
    private static final Map<Integer, Long> stationStayTimeMap = new ConcurrentHashMap<>();
 
    private volatile String enableFake = "N";
    private volatile String fakeGenerateInTask = "Y";
 
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private BasCrnpService basCrnpService;
    @Autowired
    private BasDualCrnpService basDualCrnpService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private StationCommandDispatcher stationCommandDispatcher;
    @Autowired
    private MainProcessTaskSubmitter mainProcessTaskSubmitter;
 
    public void setEnableFake(String enableFake) {
        this.enableFake = enableFake;
    }
 
    public void setFakeGenerateInTask(String fakeGenerateInTask) {
        this.fakeGenerateInTask = fakeGenerateInTask;
    }
 
    public void submitMonitorTasks(long minIntervalMs) {
        submitAsyncTask("checkInStationHasTask", minIntervalMs, this::checkInStationHasTask);
        submitAsyncTask("calcAllStationStayTime", minIntervalMs, this::calcAllStationStayTime);
        submitAsyncTask("checkOutStationStayTimeOut", minIntervalMs, this::checkOutStationStayTimeOut);
        submitAsyncTask("checkInStationCrnTake", minIntervalMs, this::checkInStationCrnTake);
    }
 
    private void submitAsyncTask(String taskName, long minIntervalMs, Runnable task) {
        mainProcessTaskSubmitter.submitKeyedSerialTask(
                MainProcessLane.FAKE_ASYNC,
                taskName,
                taskName,
                minIntervalMs,
                task
        );
    }
 
    // 计算所有站点停留时间
    public void calcAllStationStayTime() {
        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
        for (BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                continue;
            }
 
            List<StationProtocol> list = stationThread.getStatus();
            for (StationProtocol stationProtocol : list) {
                if (stationProtocol.getTaskNo() > 0
                        && !stationStayTimeMap.containsKey(stationProtocol.getStationId())) {
                    stationStayTimeMap.put(stationProtocol.getStationId(), System.currentTimeMillis());
                }
 
                if (stationProtocol.getTaskNo() <= 0
                        && stationStayTimeMap.containsKey(stationProtocol.getStationId())) {
                    stationStayTimeMap.remove(stationProtocol.getStationId());
                }
            }
        }
    }
 
    // 检测出库站点停留是否超时
    public void checkOutStationStayTimeOut() {
        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
        for (BasDevp basDevp : basDevps) {
            List<StationObjModel> outStationList = basDevp.getOutStationList$();
            if (outStationList.isEmpty()) {
                News.info("输送线:{} 出库站点未设置", basDevp.getDevpNo());
                continue;
            }
 
            for (StationObjModel stationObjModel : outStationList) {
                Object lock = redisUtil
                        .get(RedisKeyType.CHECK_OUT_STATION_STAY_TIME_OUT_LIMIT.key + stationObjModel.getStationId());
                if (lock != null) {
                    continue;
                }
 
                Long stayTime = stationStayTimeMap.get(stationObjModel.getStationId());
                if (stayTime == null) {
                    continue;
                }
 
                if (System.currentTimeMillis() - stayTime > 1000 * 60) {
                    StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp,
                            stationObjModel.getDeviceNo());
                    if (stationThread == null) {
                        continue;
                    }
 
                    StationCommand command = stationThread.getCommand(StationCommandType.RESET, 0,
                            stationObjModel.getStationId(), 0, 0);
                    if (command == null) {
                        continue;
                    }
 
                    stationCommandDispatcher.dispatch(stationObjModel.getDeviceNo(), command, "fake-process", "out-station-reset");
                    redisUtil.set(
                            RedisKeyType.CHECK_OUT_STATION_STAY_TIME_OUT_LIMIT.key + stationObjModel.getStationId(),
                            "lock", 10);
                    News.info("输送站点出库重置命令下发成功,站点号={},命令数据={}", stationObjModel.getStationId(),
                            JSON.toJSONString(command));
                }
            }
        }
    }
 
    // 检测入库站点堆垛机是否取走货物
    public void checkInStationCrnTake() {
        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
        for (BasCrnp basCrnp : basCrnps) {
            List<StationObjModel> inStationList = basCrnp.getInStationList$();
            if (inStationList.isEmpty()) {
                News.info("堆垛机:{} 入库站点未设置", basCrnp.getCrnNo());
                continue;
            }
            checkInStationListCrnTake(inStationList);
        }
 
        List<BasDualCrnp> basDualCrnps = basDualCrnpService.list(new QueryWrapper<>());
        for (BasDualCrnp basDualCrnp : basDualCrnps) {
            List<StationObjModel> inStationList = basDualCrnp.getInStationList$();
            if (inStationList.isEmpty()) {
                News.info("双工位堆垛机:{} 入库站点未设置", basDualCrnp.getCrnNo());
                continue;
            }
            checkInStationListCrnTake(inStationList);
        }
    }
 
    private void checkInStationListCrnTake(List<StationObjModel> inStationList) {
        for (StationObjModel stationObjModel : inStationList) {
            Object lock = redisUtil
                    .get(RedisKeyType.CHECK_IN_STATION_STAY_TIME_OUT_LIMIT.key + stationObjModel.getStationId());
            if (lock != null) {
                continue;
            }
 
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp,
                    stationObjModel.getDeviceNo());
            if (stationThread == null) {
                continue;
            }
 
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = statusMap.get(stationObjModel.getStationId());
            if (stationProtocol == null) {
                continue;
            }
 
            if (stationProtocol.getTaskNo() > 0) {
                StationCommand command = stationThread.getCommand(StationCommandType.RESET, 0,
                        stationObjModel.getStationId(), 0, 0);
                if (command == null) {
                    continue;
                }
 
                WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
                if (wrkMast == null) {
                    News.info("入库站点清理命中,站点号={},站点taskNo={},原因=wrk_missing,准备RESET",
                            stationObjModel.getStationId(), stationProtocol.getTaskNo());
                    stationCommandDispatcher.dispatch(stationObjModel.getDeviceNo(), command, "fake-process", "in-station-reset-task-over");
                    redisUtil.set(
                            RedisKeyType.CHECK_IN_STATION_STAY_TIME_OUT_LIMIT.key + stationObjModel.getStationId(),
                            "lock", 10);
                    News.info("输送站点重置命令下发成功(task_over),站点号={},命令数据={}", stationObjModel.getStationId(),
                            JSON.toJSONString(command));
                } else {
                    if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.INBOUND_STATION_RUN_COMPLETE.sts)
                            || Objects.equals(wrkMast.getWrkSts(), WrkStsType.INBOUND_RUN.sts)) {
                        Integer crnNo = wrkMast.getCrnNo();
                        if (crnNo != null) {
                            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crnNo);
                            if (crnThread == null) {
                                continue;
                            }
                            CrnProtocol crnProtocol = crnThread.getStatus();
                            boolean sameTaskRunning = stationProtocol.getTaskNo().equals(crnProtocol.getTaskNo())
                                    && Objects.equals(crnProtocol.getLoaded(), 1);
                            if (!sameTaskRunning) {
                                News.info("入库站点清理忽略,站点号={},站点taskNo={},工作状态={},堆垛机号={},堆垛机taskNo={},statusType={},loaded={},原因=crn_not_loaded_this_task",
                                        stationObjModel.getStationId(), stationProtocol.getTaskNo(), wrkMast.getWrkSts(), crnNo,
                                        crnProtocol.getTaskNo(), crnProtocol.getStatusType(), crnProtocol.getLoaded());
                                continue;
                            }
 
                            News.info("入库站点清理命中,站点号={},站点taskNo={},工作状态={},堆垛机号={},堆垛机taskNo={},statusType={},loaded={},原因=crn_loaded_running",
                                    stationObjModel.getStationId(), stationProtocol.getTaskNo(), wrkMast.getWrkSts(), crnNo,
                                    crnProtocol.getTaskNo(), crnProtocol.getStatusType(), crnProtocol.getLoaded());
                            stationCommandDispatcher.dispatch(stationObjModel.getDeviceNo(), command, "fake-process", "in-station-reset-crn-running");
                            redisUtil.set(RedisKeyType.CHECK_IN_STATION_STAY_TIME_OUT_LIMIT.key
                                    + stationObjModel.getStationId(), "lock", 10);
                            News.info("输送站点重置命令下发成功(crn_running),站点号={},命令数据={}", stationObjModel.getStationId(),
                                    JSON.toJSONString(command));
                        } else {
                            Integer dualCrnNo = wrkMast.getDualCrnNo();
                            DualCrnThread dualCrnThread = (DualCrnThread) SlaveConnection.get(SlaveType.DualCrn,
                                    dualCrnNo);
                            if (dualCrnThread == null) {
                                continue;
                            }
                            DualCrnProtocol dualCrnProtocol = dualCrnThread.getStatus();
 
                            Integer taskNo = stationProtocol.getTaskNo();
                            boolean sameTaskRunning = taskNo != null && (
                                    taskNo.equals(dualCrnProtocol.getTaskNo()) && dualCrnProtocol.getLoaded() == 1
                                            || taskNo.equals(dualCrnProtocol.getTaskNoTwo()) && dualCrnProtocol.getLoadedTwo() == 1
                            );
                            if (!sameTaskRunning) {
                                News.info("入库站点清理忽略,站点号={},站点taskNo={},工作状态={},双工位堆垛机号={},taskNo1={},loaded1={},taskNo2={},loaded2={},原因=dual_crn_not_running_this_task",
                                        stationObjModel.getStationId(), stationProtocol.getTaskNo(), wrkMast.getWrkSts(), dualCrnNo,
                                        dualCrnProtocol.getTaskNo(), dualCrnProtocol.getLoaded(),
                                        dualCrnProtocol.getTaskNoTwo(), dualCrnProtocol.getLoadedTwo());
                                continue;
                            }
 
                            News.info("入库站点清理命中,站点号={},站点taskNo={},工作状态={},双工位堆垛机号={},taskNo1={},loaded1={},taskNo2={},loaded2={},原因=dual_crn_running",
                                    stationObjModel.getStationId(), stationProtocol.getTaskNo(), wrkMast.getWrkSts(), dualCrnNo,
                                    dualCrnProtocol.getTaskNo(), dualCrnProtocol.getLoaded(),
                                    dualCrnProtocol.getTaskNoTwo(), dualCrnProtocol.getLoadedTwo());
                            stationCommandDispatcher.dispatch(stationObjModel.getDeviceNo(), command,
                                    "fake-process", "in-station-reset-dual-crn-running");
                            redisUtil.set(RedisKeyType.CHECK_IN_STATION_STAY_TIME_OUT_LIMIT.key
                                    + stationObjModel.getStationId(), "lock", 10);
                            News.info("输送站点重置命令下发成功(dual_crn_running),站点号={},命令数据={}",
                                    stationObjModel.getStationId(), JSON.toJSONString(command));
                        }
                    } else {
                        News.info("入库站点清理忽略,站点号={},站点taskNo={},工作状态={},原因=wrk_sts_not_3",
                                stationObjModel.getStationId(), stationProtocol.getTaskNo(), wrkMast.getWrkSts());
                    }
                }
            }
        }
    }
 
    // 检测入库站是否有任务生成,并仿真生成模拟入库站点数据
    private void checkInStationHasTask() {
        if (!enableFake.equals("Y")) {
            return;
        }
 
        if (!fakeGenerateInTask.equals("Y")) {
            return;
        }
 
        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
        for (BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                continue;
            }
 
            Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
 
            List<StationObjModel> list = basDevp.getInStationList$();
            for (StationObjModel entity : list) {
                Integer stationId = entity.getStationId();
                if (!stationMap.containsKey(stationId)) {
                    continue;
                }
 
                StationProtocol stationProtocol = stationMap.get(stationId);
                if (stationProtocol == null) {
                    continue;
                }
 
                Object lock = redisUtil.get(RedisKeyType.GENERATE_FAKE_IN_STATION_DATA_LIMIT.key + stationId);
                if (lock != null) {
                    continue;
                }
 
                // 满足自动、无物、工作号0,生成入库数据
                if (stationProtocol.isAutoing()
                        && !stationProtocol.isLoading()
                        && stationProtocol.getTaskNo() == 0) {
                    StationCommand command = stationThread.getCommand(StationCommandType.MOVE,
                            commonService.getWorkNo(WrkIoType.FAKE_TASK_NO.id), stationId,
                            entity.getBarcodeStation().getStationId(), 0);
                    stationCommandDispatcher.dispatch(basDevp.getDevpNo(), command, "fake-process", "fake-enable-in");
                    if (entity.getBarcodeStation() != null && entity.getBarcodeStation().getStationId() != null) {
                        Utils.precomputeInTaskEnableRow(entity.getBarcodeStation().getStationId());
                    }
                    redisUtil.set(RedisKeyType.GENERATE_FAKE_IN_STATION_DATA_LIMIT.key + stationId, "lock", 5);
                }
            }
        }
    }
}