zhang
6 天以前 39efc87b3794f957aad73acf3a430b84e3a19b27
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
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.service.AreaGovernService;
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.BusStsType;
import com.zy.acs.manager.manager.enums.LocStsType;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.manager.service.*;
import com.zy.acs.manager.manager.service.impl.CodeServiceImpl;
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 AutoTestDeviationScheduler {
 
    private static final AgvModelType DEFAULT_AGV_MODEL = AgvModelType.HEAVY_LOAD_STACKING_ROBOT;
 
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
 
    @Autowired
    private AgvService agvService;
    @Autowired
    private BusService busService;
    @Autowired
    private MainService mainService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private LocService locService;
    @Autowired
    private StaService staService;
    @Autowired
    private AgvModelService agvModelService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private AreaGovernService areaGovernService;
    @Autowired
    private CodeServiceImpl codeService;
 
    /**
     * 循环跑库,测试偏差
     */
    //    @Scheduled(fixedRate = 500) // 固定频率执行,不同步
    //@Scheduled(fixedDelay = 1000) // 固定频率执行,同步
//    @Scheduled(cron = "0/1 * * * * ? ")
    private void execute() {
        if (!configService.getVal("TestDeviationMode", Boolean.class)) { return; }
        AgvModel agvModel = agvModelService.getOne(new LambdaQueryWrapper<AgvModel>().eq(AgvModel::getType, DEFAULT_AGV_MODEL.toString()));
        if (null == agvModel) { return; }
 
        this.autoRun(agvModel);
    }
 
    private void autoRun(AgvModel agvModel) {
        int availableAgvCount = this.getAvailableAgvCount();
        if (0 == availableAgvCount) { return; }
 
//        List<String> staPreNos = getStaPrefixes(staGroupList);
        List<String> staPreNos = new ArrayList<>();
        String memo = "DEMO_STA_" + String.join("-", staPreNos);
 
        // 移库
        this.runLocToLoc(agvModel, memo);
    }
 
 
    // 移库
    private void runLocToLoc(AgvModel agvModel, String staTaskMemo) {
        String memo = "DEMO_LOC";
 
        int availableAgvCount = this.getAvailableAgvCount();
 
        // 最多 ? 组bus运行
        if (availableAgvCount <= busService.count(new LambdaQueryWrapper<Bus>()
                .in(Bus::getBusSts, BusStsType.RECEIVE.val(), BusStsType.PROGRESS.val())
                .in(Bus::getMemo, memo, staTaskMemo)
        )) { return; }
 
        int maxCapacity = agvModel.getBackpack();
 
        // STOCK
        List<Loc> stockLocList = locService.selectRandByLocSts(LocStsType.STOCK.val(), maxCapacity);
        if (Cools.isEmpty(stockLocList)) {
            return;
        }
        Collections.shuffle(stockLocList);
 
        // IDLE
        List<Loc> idleLocList = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getStatus,1).eq(Loc::getLocSts,LocStsType.IDLE.val()).ne(Loc::getMemo,"1").orderByAsc(Loc::getRow).orderByAsc(Loc::getBay).orderByAsc(Loc::getLev));
        if (Cools.isEmpty(idleLocList)) {
            return;
        }
 
        OpenBusSubmitParam param = new OpenBusSubmitParam();
        param.setBatch(String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19));
        for (int i = 0; i < Math.min(maxCapacity, Math.min(stockLocList.size(), idleLocList.size())); i++) {
            Loc stockLoc = stockLocList.get(i);
            Loc idleLoc = idleLocList.get(i);
            idleLoc.setMemo("1");
            locService.updateById(idleLoc);
            TaskDto taskDto = new TaskDto();
            taskDto.setOriLoc(stockLoc.getLocNo());
            taskDto.setDestLoc(idleLoc.getLocNo());
            taskDto.setSeqNum(String.valueOf(snowflakeIdWorker.nextId()).substring(15, 19));
 
            param.getTaskList().add(taskDto);
        }
        if (Cools.isEmpty(param.getTaskList())) { return; }
 
        mainService.generateBusAndTask(param, memo);
    }
 
 
    private int getAvailableAgvCount() {
        int res = 0;
        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
        if (Cools.isEmpty(agvList)) {
            return res;
        }
        for (Agv agv : agvList) {
            if (null == redis.getObject(RedisConstant.AGV_ONLINE_FLAG, agv.getUuid())) {
                continue;
            }
            if (!agv.getStatusBool()) {
                continue;
            }
            res++;
        }
 
        return res;
    }
 
}