zhang
2 天以前 3cb33dcc9cdb55006a6332dd7d5222b05b6ab12b
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
package com.zy.acs.manager.core.scheduler;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.common.constant.RedisConstant;
import com.zy.acs.common.utils.RedisSupport;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.SnowflakeIdWorker;
import com.zy.acs.manager.common.domain.TaskDto;
import com.zy.acs.manager.core.constant.AgvGroupConstant;
import com.zy.acs.manager.core.constant.LocGroupConstant;
import com.zy.acs.manager.core.constant.StaGroupConstant;
import com.zy.acs.manager.core.service.MainService;
import com.zy.acs.manager.manager.controller.param.OpenBusSubmitParam;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.AgvModelType;
import com.zy.acs.manager.manager.enums.LocStsType;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.manager.enums.TaskStsType;
import com.zy.acs.manager.manager.service.*;
import com.zy.acs.manager.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.*;
 
@Slf4j
@Component
public class EfficiencyTestScheduler {
 
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
 
    @Autowired
    private AgvService agvService;
    @Autowired
    private MainService mainService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private LocService locService;
    @Autowired
    private StaService staService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private AgvModelService agvModelService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
 
 
    @Scheduled(fixedDelay = 1000) // 固定频率执行,同步
    private void execute() {
        String efficiency = configService.getVal("Efficiency", String.class);
        // 1/0,23,1/2,1-5,1   1,2,2,6,24,10che
        // 是否开启,1开启,0关闭 哪些区,1入库还是2出库,一次生成任务数量,最大任务数
        if (!Cools.isEmpty(efficiency)) {
            String[] split = efficiency.split(",");
            if (split.length >= 6 && split[0].equals("1")) {
                for (char c : split[1].toCharArray()) {
                    switch (c) {
                        case '1':
                            this.autoRun(LocGroupConstant.FAR_RIGHT_LOC_ROW_LIST, StaGroupConstant.FAR_RIGHT_STA_ROW_LIST, AgvGroupConstant.FIFTH_AGV_GROUP, Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]),split[5]);
                            break;
                        case '2':
                            this.autoRun(LocGroupConstant.RIGHT_LOC_ROW_LIST, StaGroupConstant.RIGHT_STA_ROW_LIST, AgvGroupConstant.FOURTH_AGV_GROUP, Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]),split[5]);
                            break;
                        case '3':
                            this.autoRun(LocGroupConstant.MIDDLE_LOC_ROW_LIST, StaGroupConstant.MIDDLE_STA_ROW_LIST, AgvGroupConstant.THIRD_AGV_GROUP, Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]),split[5]);
                            break;
                        case '4':
                            this.autoRun(LocGroupConstant.LEFT_LOC_ROW_LIST, StaGroupConstant.LEFT_STA_ROW_LIST, AgvGroupConstant.SECOND_AGV_GROUP, Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]),split[5]);
                            break;
                        case '5':
                            this.autoRun(LocGroupConstant.FAR_LEFT_LOC_ROW_LIST, StaGroupConstant.FAR_LEFT_STA_ROW_LIST, AgvGroupConstant.FIRST_AGV_GROUP, Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]),split[5]);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
 
    private void autoRun(List<Integer> locGroupList, List<String> staGroupList, List<String> agvGroupList, Integer mode, Integer taskNum, Integer taskMax,String memo) {
//        int availableAgvCount = this.getAvailableAgvCount(agvGroupList);
//        if (0 == availableAgvCount) {
//            return;
//        }
 
        List<String> staPreNos = getStaPrefixes(staGroupList);
        String staTaskMemo = "DEMO_STA_" + String.join("-", staPreNos)+"_"+memo;
 
        // 最多 ? 个任务运行
        if (taskMax <= taskService.count(new LambdaQueryWrapper<Task>().in(Task::getTaskSts, TaskStsType.INIT.val(), TaskStsType.WAITING.val(), TaskStsType.ASSIGN.val(), TaskStsType.PROGRESS.val()).in(Task::getMemo, staTaskMemo))) {
            return;
        }
 
        if (mode != null && mode == 1) {
            // 入库
            this.runStaToLoc(locGroupList, staGroupList, staTaskMemo, taskNum,"3-1");
            this.runStaToLoc(locGroupList, staGroupList, staTaskMemo, taskNum,"4-1");
        } else if (mode == 2) {
            // 出库
            this.runLocToSta(locGroupList, staGroupList, staTaskMemo, taskNum,"3-1");
            this.runLocToSta(locGroupList, staGroupList, staTaskMemo, taskNum,"4-1");
        }
 
    }
 
 
    // 入库
    private void runStaToLoc(List<Integer> locGroupList, List<String> staGroupList, String memo, Integer taskNum,String sta) {
        Integer startRow = Collections.min(locGroupList);
        Integer endRow = Collections.max(locGroupList);
 
 
        AgvModel agvModel = agvModelService.getOne(new LambdaQueryWrapper<AgvModel>().eq(AgvModel::getType, AgvModelType.CTU_BOX_TRANSPORT_AGV.toString()));
        if (null == agvModel) {
            return;
        }
 
        // STOCK
        LambdaQueryWrapper<Sta> stockWrapper = new LambdaQueryWrapper<Sta>().eq(Sta::getStatus, StatusType.ENABLE.val).eq(Sta::getInEnable, "Y").in(Sta::getStaNo, staGroupList);
        List<Sta> stockList = staService.list(stockWrapper);
        if (Cools.isEmpty(stockList)) {
            return;
        }
        Collections.shuffle(stockList);
 
        // IDLE
        LambdaQueryWrapper<Loc> idleWrapper = new LambdaQueryWrapper<Loc>().eq(Loc::getLocSts, LocStsType.IDLE.val());
        if (null != startRow) {
            idleWrapper.ge(Loc::getRow, startRow);
        }
        if (null != endRow) {
            idleWrapper.le(Loc::getRow, endRow);
        }
        List<Loc> idleList = locService.list(idleWrapper);
        if (Cools.isEmpty(idleList)) {
            return;
        }
        Collections.shuffle(idleList);
 
        OpenBusSubmitParam param = new OpenBusSubmitParam();
        param.setBatch(String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19));
        for (int i = 0; i <taskNum; i++) {
            Loc idleLoc = idleList.get(i);
            TaskDto taskDto = new TaskDto();
            taskDto.setOriSta(sta);
            taskDto.setDestLoc(idleLoc.getLocNo());
            taskDto.setPriority(100);
            taskDto.setSeqNum(String.valueOf(snowflakeIdWorker.nextId()).substring(11, 19));
            taskDto.setMemo(memo);
            param.getTaskList().add(taskDto);
        }
 
        mainService.generateBusAndTask(param, memo);
    }
 
 
    // 出库
    private void runLocToSta(List<Integer> locGroupList, List<String> staGroupList, String memo, Integer taskNum,String sta) {
        Integer startRow = Collections.min(locGroupList);
        Integer endRow = Collections.max(locGroupList);
 
        AgvModel agvModel = agvModelService.getOne(new LambdaQueryWrapper<AgvModel>().eq(AgvModel::getType, AgvModelType.CTU_BOX_TRANSPORT_AGV.toString()));
        if (null == agvModel) {
            return;
        }
 
        // STA IDLE
        LambdaQueryWrapper<Sta> idleWrapper = new LambdaQueryWrapper<Sta>().eq(Sta::getStatus, StatusType.ENABLE.val).eq(Sta::getOutEnable, "Y").in(Sta::getStaNo, staGroupList);
        List<Sta> idleList = staService.list(idleWrapper);
        if (Cools.isEmpty(idleList)) {
            return;
        }
        Collections.shuffle(idleList);
 
        // LOC STOCK
        LambdaQueryWrapper<Loc> stockWrapper = new LambdaQueryWrapper<Loc>().eq(Loc::getLocSts, LocStsType.STOCK.val());
        if (null != startRow) {
            stockWrapper.ge(Loc::getRow, startRow);
        }
        if (null != endRow) {
            stockWrapper.le(Loc::getRow, endRow);
        }
        List<Loc> stockList = locService.list(stockWrapper);
        if (Cools.isEmpty(stockList)) {
            return;
        }
        Collections.shuffle(stockList);
 
        OpenBusSubmitParam param = new OpenBusSubmitParam();
        param.setBatch(String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19));
        for (int i = 0; i < taskNum; i++) {
            Loc stockLoc = stockList.get(i);
 
            TaskDto taskDto = new TaskDto();
            taskDto.setOriLoc(stockLoc.getLocNo());
            taskDto.setDestSta(sta);
            taskDto.setPriority(100);
            taskDto.setSeqNum(String.valueOf(snowflakeIdWorker.nextId()).substring(11, 19));
            taskDto.setMemo(memo);
            param.getTaskList().add(taskDto);
        }
 
        mainService.generateBusAndTask(param, memo);
    }
 
 
    public static List<String> getStaPrefixes(List<String> staGroupList) {
        Set<String> rowSet = new HashSet<>();
        for (String s : staGroupList) {
            rowSet.add(s.split("-")[0]);
        }
        List<String> result = new ArrayList<>(rowSet);
        result.sort(Comparator.comparingInt(Integer::parseInt));
        return result;
    }
 
    private int getAvailableAgvCount(List<String> agvNos) {
        int res = 0;
        if (Cools.isEmpty(agvNos)) {
            return res;
        }
        for (String agvNo : agvNos) {
            if (null == redis.getObject(RedisConstant.AGV_ONLINE_FLAG, agvNo)) {
                continue;
            }
            Agv agv = agvService.selectByUuid(agvNo);
            if (!agv.getStatusBool()) {
                continue;
            }
            res++;
        }
 
        return res;
    }
 
}