1
zhang
3 天以前 c42a7c76e24940db9e81307dc67104d9068c3119
zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/FuncStaServiceImpl.java
@@ -2,22 +2,25 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
import com.zy.acs.common.enums.AgvStatusType;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.common.utils.CommonUtil;
import com.zy.acs.manager.core.service.AgvAreaDispatcher;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.FuncStaType;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.manager.enums.TaskStsType;
import com.zy.acs.manager.manager.enums.TaskTypeType;
import com.zy.acs.manager.manager.enums.*;
import com.zy.acs.manager.manager.mapper.FuncStaMapper;
import com.zy.acs.manager.manager.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@Slf4j
@Service("funcStaService")
public class FuncStaServiceImpl extends ServiceImpl<FuncStaMapper, FuncSta> implements FuncStaService {
@@ -31,6 +34,11 @@
    private AgvModelService agvModelService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private AgvAreaDispatcher agvAreaDispatcher;
    @Override
    public FuncSta getByCodeAndType(Long codeId, String type) {
@@ -53,14 +61,31 @@
    @Override
    public List<FuncSta> findInIdleStatus(FuncStaType type, Long agvId) {
        LambdaQueryWrapper<FuncSta> wrapper = new LambdaQueryWrapper<FuncSta>()
                .eq(FuncSta::getType, type).eq(FuncSta::getStatus, StatusType.ENABLE.val);
                .eq(FuncSta::getType, type).eq(FuncSta::getStatus, StatusType.ENABLE.val).eq(FuncSta::getState, FuncStaStateType.IDLE.toString());
        List<FuncSta> funcStaList = this.list(wrapper);
        if (Cools.isEmpty(funcStaList)) {
            return new ArrayList<>();
        }
        Collections.shuffle(funcStaList);
        // area limit
        if (agvAreaDispatcher.isAgvExistsInAnyArea(agvId)) {
            List<String> areaCodeList = agvAreaDispatcher.getCodesByAgvId(agvId);
            if (Cools.isEmpty(areaCodeList)) {
                funcStaList.clear();
            } else {
                funcStaList.removeIf(funcSta -> {
                    Code code = codeService.getCacheById(funcSta.getCode());
                    if (Cools.isEmpty(code, code.getData())) { return true; }
                    return !areaCodeList.contains(code.getData());
                });
            }
        }
        if (!Cools.isEmpty(funcStaList)) {
            Collections.shuffle(funcStaList);
        }
        // filter idle
        funcStaList = funcStaList.stream().filter(funcSta -> {
@@ -77,7 +102,7 @@
                    AgvModel agvModel = agvModelService.getByAgvId(agv.getId());
                    AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
                    if (agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
                        if (agvDetail.getVol() < agvModel.getQuaBattery()) {
                        if (agvDetail.getSoc() < agvModel.getQuaBattery()) {
                            return false;
                        }
                    } else {
@@ -171,6 +196,37 @@
    }
    @Override
    public FuncSta checkoutFurthestFunSta(Long codeId, List<FuncSta> funcStaList) {
        if (Cools.isEmpty(funcStaList)) {
            return null;
        }
        if (null != codeId) {
            Code currCode = codeService.getCacheById(codeId);
            Double[] startPos = new Double[]{currCode.getX(), currCode.getY()};
            // checkout one funSta which is the closest
            // compare => compare返回负数,则排在集合前面 (asc)
            funcStaList.sort(new Comparator<FuncSta>() {
                @Override
                public int compare(FuncSta o1, FuncSta o2) {
                    Code o1Code = codeService.getCacheById(o1.getCode());
                    int o1Distance = CommonUtil.calcDistance(startPos, new Double[]{o1Code.getX(), o1Code.getY()});
                    Code o2Code = codeService.getCacheById(o2.getCode());
                    int o2Distance = CommonUtil.calcDistance(startPos, new Double[]{o2Code.getX(), o2Code.getY()});
                    return o2Distance - o1Distance;
                }
            });
        }
        return funcStaList.stream().findFirst().orElse(null);
    }
    @Override
    public Boolean isCanBeIdle(FuncSta funcSta) {
        Agv agv;
        switch (Objects.requireNonNull(FuncStaType.query(funcSta.getType()))) {