#
vincentlu
昨天 6bde5b6f7f4ad8c1bac3732e46cf8d966deab845
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
package com.zy.acs.manager.core.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.common.enums.AgvStatusType;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.*;
import com.zy.acs.manager.manager.service.AgvDetailService;
import com.zy.acs.manager.manager.service.AgvService;
import com.zy.acs.manager.manager.service.SegmentService;
import com.zy.acs.manager.manager.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
 
@Slf4j
@Service
public class GuaranteeRuntimeService {
 
    @Autowired
    private SegmentService segmentService;
 
    private final AgvService agvService;
    private final AgvDetailService agvDetailService;
    private final TaskService taskService;
    private final MainLockWrapService mainLockWrapService;
 
    public GuaranteeRuntimeService(AgvService agvService,
                                   AgvDetailService agvDetailService,
                                   TaskService taskService,
                                   MainLockWrapService mainLockWrapService) {
        this.agvService = agvService;
        this.agvDetailService = agvDetailService;
        this.taskService = taskService;
        this.mainLockWrapService = mainLockWrapService;
    }
 
    public void prepare(Guarantee plan, LocalDateTime targetTime) {
        int requiredCount = plan.getRequiredCount() == null ? 0 : plan.getRequiredCount();
        if (requiredCount <= 0) {
            log.warn("Guarantee[{}] requiredCount is not configured, skip", plan.getName());
            return;
        }
        int minSoc = plan.getMinSoc();
        List<Agv> scopedAgvList = findScopedAgvList(plan);
        int available = 0;
        List<ChargeCandidate> candidates = new ArrayList<>();
        for (Agv agv : scopedAgvList) {
            AgvDetail detail = agvDetailService.selectMajorByAgvId(agv.getId());
            if (null == detail || null == detail.getSoc() || null == detail.getAgvStatus()) {
                continue;
            }
            if (detail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
                continue;
            }
            if (!isIdle(agv, detail)) {
                continue;
            }
            int soc = detail.getSoc();
            if (soc >= minSoc) {
                available++;
            } else {
                candidates.add(new ChargeCandidate(agv, soc));
            }
        }
        if (available >= requiredCount) {
//            log.debug("Guarantee[{}] already has {} vehicles >= {}% SOC for {}", plan.getName(), available, minSoc, targetTime);
            return;
        }
        int shortage = requiredCount - available;
        candidates.sort(Comparator.comparingInt(ChargeCandidate::getSoc));
        int scheduled = 0;
        for (ChargeCandidate candidate : candidates) {
            if (scheduled >= shortage) {
                break;
            }
            log.info("Guarantee[{}] schedule AGV {} charging (soc={}%) for target {}",
                    plan.getName(), candidate.getAgv().getName(), candidate.getSoc(), targetTime);
            mainLockWrapService.buildMinorTask(candidate.getAgv().getId(), TaskTypeType.TO_CHARGE, null, null);
            scheduled++;
        }
        if (scheduled < shortage) {
            log.warn("Guarantee[{}] still short of {} vehicles for {} (only {} idle low-soc AGVs)",
                    plan.getName(), shortage - scheduled, targetTime, candidates.size());
        }
    }
 
    private boolean isIdle(Agv agv, AgvDetail detail) {
        if (!Objects.equals(agv.getStatus(), StatusType.ENABLE.val)) {
            return false;
        }
        if (detail.getAgvStatus() != null && detail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
            return false;
        }
 
        if (0 < taskService.count(new LambdaQueryWrapper<Task>()
                .eq(Task::getAgvId, agv.getId())
//                .and(i -> {
//                    i.eq(Task::getTaskSts, TaskStsType.WAITING.val())
//                            .or().eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
//                            .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
//                })
                .in(Task::getTaskSts,
                        TaskStsType.WAITING.val(),
                        TaskStsType.ASSIGN.val(),
                        TaskStsType.PROGRESS.val())
        )) {
            return false;
        }
        if (0 < segmentService.count(new LambdaQueryWrapper<Segment>()
                        .eq(Segment::getAgvId, agv.getId())
                        .and( i -> {
//                                i.eq(Segment::getState, SegmentStateType.WAITING.toString()).or()
                            i.eq(Segment::getState, SegmentStateType.RUNNING.toString());
                        })
        )) {
            return false;
        }
        return true;
    }
 
    private List<Agv> findScopedAgvList(Guarantee plan) {
        LambdaQueryWrapper<Agv> wrapper = new LambdaQueryWrapper<Agv>()
                .eq(Agv::getStatus, StatusType.ENABLE.val);
        if (GuaranteeScopeType.MODEL.toString().equalsIgnoreCase(plan.getScopeType()) && plan.getScopeValue() != null) {
            try {
                wrapper.eq(Agv::getAgvModel, Long.valueOf(plan.getScopeValue()));
            } catch (NumberFormatException ignore) {
                log.warn("Guarantee[{}] invalid scopeValue {}", plan.getName(), plan.getScopeValue());
            }
        }
        return agvService.list(wrapper);
    }
 
    private static class ChargeCandidate {
        private final Agv agv;
        private final int soc;
 
        ChargeCandidate(Agv agv, int soc) {
            this.agv = agv;
            this.soc = soc;
        }
 
        public Agv getAgv() {
            return agv;
        }
 
        public int getSoc() {
            return soc;
        }
    }
}