1
zhang
3 天以前 c42a7c76e24940db9e81307dc67104d9068c3119
zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/MaintainScheduler.java
@@ -1,12 +1,18 @@
package com.zy.acs.manager.core.scheduler;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xingshuangs.iot.protocol.modbus.service.ModbusRtuOverTcp;
import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
import com.zy.acs.charge.ChargeCoreService;
import com.zy.acs.common.constant.RedisConstant;
import com.zy.acs.common.enums.AgvStatusType;
import com.zy.acs.common.utils.RedisSupport;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.DateUtils;
import com.zy.acs.manager.common.config.UplinkProperties;
import com.zy.acs.manager.core.integrate.wms.TaskReportService;
import com.zy.acs.manager.core.integrate.wms.FaultReportService;
import com.zy.acs.manager.core.service.ChargeService;
import com.zy.acs.manager.core.service.MainLockWrapService;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.*;
@@ -17,9 +23,12 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;
/**
 * judge whether agv go to funcSta which be charging or standby
@@ -51,9 +60,19 @@
    private SegmentService segmentService;
    @Autowired
    private TaskReportService taskReportService;
    @Autowired
    private VehFaultRecService vehFaultRecService;
    @Autowired
    private FaultReportService faultReportService;
    @Autowired
    private ChargeService chargeService;
    @Autowired
    private ChargeCoreService chargeCoreService;
    @Scheduled(cron = "0/5 * * * * ? ")
    private synchronized void autoCharge(){
        if (!configService.getVal("TaskAssignMode", Boolean.class)) { return; }
        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
        for (Agv agv : agvList) {
            AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
@@ -94,6 +113,7 @@
    @Scheduled(cron = "0/1 * * * * ? ")
//    @Scheduled(cron = "0 */2 * * * ? ")
    private synchronized void autoStandby(){
        if (!configService.getVal("TaskAssignMode", Boolean.class)) { return; }
        if (!configService.getVal("automaticStandbyPosition", Boolean.class)) { return; }
        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
@@ -107,6 +127,10 @@
//            if (agvDetailService.isPowerLoss(agv, agvDetail, agvModel)) {
//                continue;
//            }
            // 存在充电标记,跳过
            if (redis.getMap(RedisConstant.AGV_CHARGE_FLAG, agv.getUuid()) != null) {
                continue;
            }
            // is charging ?
            if (agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
                if (agvDetail.getSoc() < agvModel.getQuaBattery()) {
@@ -189,4 +213,66 @@
        }
    }
    @Scheduled(cron = "0/3 * * * * ? ")
    private void reportFault() {
        String reportFaultUrl = configService.getVal("reportFaultUrl", String.class);
        if (Cools.isEmpty(reportFaultUrl)) { return; }
        List<VehFaultRec> vehFaultRecList = vehFaultRecService.list((new LambdaQueryWrapper<VehFaultRec>().eq(VehFaultRec::getState, VehFaultRecStateType.PENDING).ge(VehFaultRec::getHappenTime, Instant.now().minusSeconds(3).atZone(ZoneId.systemDefault()).toLocalDateTime())));
        if (Cools.isEmpty(vehFaultRecList)) { return; }
        for (VehFaultRec vehFaultRec : vehFaultRecList) {
            boolean finished = faultReportService.reportFinished(vehFaultRec,reportFaultUrl);
            if (finished) {
                vehFaultRec.setState(VehFaultRecStateType.REPORT_SUCCESS.name());
            } else {
                log.error("failed to report vehFaultRec to uplink");
                vehFaultRec.setState(VehFaultRecStateType.REPORT_FAILED.name());
            }
            vehFaultRecService.updateById(vehFaultRec);
        }
    }
    /**
     * 调度对接充电桩
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    private synchronized void startCharge() {
        Set<String> mapKeys = redis.getMapKeys(RedisConstant.AGV_CHARGE_FLAG);
        for (String key : mapKeys) {
            Integer status = redis.getMap(RedisConstant.AGV_CHARGE_FLAG, key);
            AgvDetail agvDetail = agvDetailService.selectByAgvNo(key);
            FuncSta funcSta = funcStaService.getByCodeAndType(agvDetail.getCode(), FuncStaType.CHARGE.toString());
            ModbusRtuOverTcp modbusTcp = chargeService.get(funcSta.getUuid());
            if (status != null && status == 1) {
                // 后退信号消失,说明马达正在前进
                if (chargeCoreService.checkBackwardRelayOffline(modbusTcp)) {
                    chargeCoreService.startCharging(modbusTcp);
                }
                while (chargeCoreService.checkForwardRelayOnline(modbusTcp)) {
                    double current = chargeCoreService.getCurrent(modbusTcp);
                    double voltage = chargeCoreService.getVoltage(modbusTcp);
                    if (current > 0 && voltage > 0) {
                        redis.setMap(RedisConstant.AGV_CHARGE_FLAG, key, 2);
                        log.info("charge complete");
                    }else {
                        log.info("read charge current and voltage: {},{}", current, voltage);
                    }
                }
            } else if (status != null && status == 2) {
                // 后退信号消失,说明马达正在前进
                if (chargeCoreService.checkForwardRelayOnline(modbusTcp)) {
                    chargeCoreService.startCharging(modbusTcp);
                }
                while (chargeCoreService.checkBackwardRelayOffline(modbusTcp)) {
                    redis.deleteMap(RedisConstant.AGV_CHARGE_FLAG, key);
                    log.info("charge over");
                }
            }
        }
    }
}