Junjie
21 小时以前 1654b0a8c149f86d38f3202cb88c655ad0a25384
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
package com.zy.core.plugin;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.Cools;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.service.BasDevpService;
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.StationCommandDispatcher;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.StationCommandType;
import com.zy.core.enums.WrkIoType;
import com.zy.core.model.StationObjModel;
import com.zy.core.model.command.StationCommand;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.plugin.api.MainProcessPluginApi;
import com.zy.core.plugin.store.InTaskApplyRequest;
import com.zy.core.plugin.store.StoreInTaskContext;
import com.zy.core.plugin.store.StoreInTaskGenerationService;
import com.zy.core.plugin.store.StoreInTaskPolicy;
import com.zy.core.task.MainProcessAsyncTaskScheduler;
import com.zy.core.thread.StationThread;
import com.zy.core.utils.CrnOperateProcessUtils;
import com.zy.core.utils.StationOperateProcessUtils;
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;
 
@Slf4j
@Component
public class GslProcess implements MainProcessPluginApi, StoreInTaskPolicy {
    private static final String CRN_TASK_LANE = "crn";
    private static final String STATION_TASK_LANE = "station";
    private static final String GENERATE_STORE_TASK_LANE_PREFIX = "generate-store-";
    private static final long DISPATCH_INTERVAL_MS = 200L;
    private static final long MAINTENANCE_INTERVAL_MS = 500L;
    private static final long TASK_SLOW_LOG_THRESHOLD_MS = 1000L;
 
    @Autowired
    private CrnOperateProcessUtils crnOperateUtils;
    @Autowired
    private StationOperateProcessUtils stationOperateProcessUtils;
    @Autowired
    private CommonService commonService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private StoreInTaskGenerationService storeInTaskGenerationService;
    @Autowired
    private StationCommandDispatcher stationCommandDispatcher;
    @Autowired
    private MainProcessAsyncTaskScheduler mainProcessAsyncTaskScheduler;
 
    @Override
    public void run() {
        //检测入库站是否有任务生成,并启动入库
        checkInStationHasTask();
        //按站点拆分生成入库任务,避免单个站点阻塞整轮扫描
        submitGenerateStoreTasks();
 
        //堆垛机与输送站点都按单个任务提交到各自串行通道,逐个执行
        submitCrnTask("crnIoExecute", DISPATCH_INTERVAL_MS, crnOperateUtils::crnIoExecute);
        submitCrnTask("crnIoExecuteFinish", DISPATCH_INTERVAL_MS, crnOperateUtils::crnIoExecuteFinish);
        submitStationTask("stationInExecute", DISPATCH_INTERVAL_MS, stationOperateProcessUtils::stationInExecute);
        submitStationTask("crnStationOutExecute", DISPATCH_INTERVAL_MS, stationOperateProcessUtils::crnStationOutExecute);
        submitStationTask("checkStationOutOrder", MAINTENANCE_INTERVAL_MS, stationOperateProcessUtils::checkStationOutOrder);
        submitStationTask("watchCircleStation", MAINTENANCE_INTERVAL_MS, stationOperateProcessUtils::watchCircleStation);
        submitStationTask("checkStationRunBlock", MAINTENANCE_INTERVAL_MS, stationOperateProcessUtils::checkStationRunBlock);
        submitStationTask("checkStationIdleRecover", MAINTENANCE_INTERVAL_MS, stationOperateProcessUtils::checkStationIdleRecover);
    }
 
    @Override
    public boolean matchCandidate(StoreInTaskContext context) {
        StationProtocol stationProtocol = context.getStationProtocol();
        return stationProtocol.isAutoing()
                && stationProtocol.isLoading()
                && stationProtocol.isInEnable()
                && stationProtocol.getTaskNo() > 0
                && !Cools.isEmpty(stationProtocol.getBarcode());
    }
 
    @Override
    public boolean beforeApply(StoreInTaskContext context) {
        StationProtocol stationProtocol = context.getStationProtocol();
        if (stationProtocol.getError() <= 0) {
            return true;
        }
 
        Object lock = redisUtil.get(RedisKeyType.GENERATE_STATION_BACK_LIMIT.key + stationProtocol.getStationId());
        if (lock != null) {
            return false;
        }
 
        StationObjModel backStation = context.getStationObjModel().getBackStation();
        StationCommand command = context.getStationThread().getCommand(StationCommandType.MOVE,
                commonService.getWorkNo(WrkIoType.STATION_BACK.id),
                context.getStationObjModel().getStationId(),
                backStation.getStationId(), 0);
        if (command == null) {
            News.taskInfo(stationProtocol.getTaskNo(), "{}工作,获取输送线命令失败", stationProtocol.getTaskNo());
            return false;
        }
        stationCommandDispatcher.dispatch(context.getBasDevp().getDevpNo(), command, "gsl-process", "station-back");
        News.taskInfo(stationProtocol.getTaskNo(), "{}扫码异常,已退回至{}", backStation.getStationId());
        redisUtil.set(RedisKeyType.GENERATE_STATION_BACK_LIMIT.key + stationProtocol.getStationId(), "lock", 10);
        return true;
    }
 
    @Override
    public InTaskApplyRequest buildApplyRequest(StoreInTaskContext context) {
        InTaskApplyRequest request = StoreInTaskPolicy.super.buildApplyRequest(context);
        request.getExtraParams().put("weight", context.getStationProtocol().getWeight());
        return request;
    }
 
    //检测入库站是否有任务生成,并启动入库
    private synchronized void checkInStationHasTask() {
        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_ENABLE_IN_STATION_DATA_LIMIT.key + stationId);
                if(lock != null){
                    continue;
                }
 
                //满足自动、无物、工作号0,生成入库数据
                if (stationProtocol.isAutoing()
                        && stationProtocol.isLoading()
                        && stationProtocol.getTaskNo() == 0
                        && stationProtocol.isEnableIn()
                ) {
                    StationCommand command = stationThread.getCommand(StationCommandType.MOVE, commonService.getWorkNo(WrkIoType.ENABLE_IN.id), stationId, entity.getBarcodeStation().getStationId(), 0);
                    stationCommandDispatcher.dispatch(basDevp.getDevpNo(), command, "gsl-process", "enable-in");
                    redisUtil.set(RedisKeyType.GENERATE_ENABLE_IN_STATION_DATA_LIMIT.key + stationId, "lock", 15);
                    News.info("{}站点启动入库成功,数据包:{}", stationId, JSON.toJSONString(command));
                }
            }
        }
    }
 
    private void submitStationTask(String taskName, long minIntervalMs, Runnable task) {
        submitProcessTask(STATION_TASK_LANE, taskName, minIntervalMs, task);
    }
 
    private void submitGenerateStoreTasks() {
        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
        for (BasDevp basDevp : basDevps) {
            List<StationObjModel> barcodeStations = getBarcodeStations(basDevp);
            for (StationObjModel stationObjModel : barcodeStations) {
                Integer stationId = stationObjModel == null ? null : stationObjModel.getStationId();
                if (stationId == null) {
                    continue;
                }
                submitGenerateStoreTask(stationId, DISPATCH_INTERVAL_MS,
                        () -> storeInTaskGenerationService.generate(this, basDevp, stationObjModel));
            }
        }
    }
 
    private void submitGenerateStoreTask(Integer stationId, long minIntervalMs, Runnable task) {
        submitProcessTask(GENERATE_STORE_TASK_LANE_PREFIX + stationId, "generateStoreWrkFile", minIntervalMs, task);
    }
 
    private void submitCrnTask(String taskName, long minIntervalMs, Runnable task) {
        submitProcessTask(CRN_TASK_LANE, taskName, minIntervalMs, task);
    }
 
    private void submitProcessTask(String laneName, String taskName, long minIntervalMs, Runnable task) {
        mainProcessAsyncTaskScheduler.submit(
                laneName,
                taskName,
                minIntervalMs,
                TASK_SLOW_LOG_THRESHOLD_MS,
                task
        );
    }
 
}