zhang
2 天以前 98670de02fde18c0aedaf349c0e165c59f57685b
zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/MaintainScheduler.java
@@ -1,18 +1,12 @@
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.*;
@@ -23,12 +17,9 @@
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
@@ -60,15 +51,6 @@
    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(){
@@ -87,19 +69,19 @@
                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());
                            i.eq(Task::getTaskSts, TaskStsType.WAITING.val())
                                    .or().eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
                                    .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
                        })
                )) {
                    continue;
                }
                if (0 < segmentService.count(new LambdaQueryWrapper<Segment>()
                        .eq(Segment::getAgvId, agv.getId())
                        .and( i -> {
                                .eq(Segment::getAgvId, agv.getId())
                                .and( i -> {
//                                i.eq(Segment::getState, SegmentStateType.WAITING.toString()).or()
                                i.eq(Segment::getState, SegmentStateType.RUNNING.toString());
                        })
                                    i.eq(Segment::getState, SegmentStateType.RUNNING.toString());
                                })
                )) {
                    continue;
                }
@@ -127,10 +109,6 @@
//            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()) {
@@ -148,28 +126,28 @@
            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());
                        i.eq(Task::getTaskSts, TaskStsType.WAITING.val())
                                .or().eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
                                .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
                    })
            )) {
                continue;
            }
            // the time between the latest task and now that be must more that @{param} seconds
//            if (!Optional.ofNullable((Boolean) redis.getObject(RedisConstant.AGV_TO_STANDBY_FLAG, agv.getUuid())).orElse(false)) {
                Integer intervalOfAutoStandby = configService.getVal("intervalOfAutoStandby", Integer.class);
                if (null != intervalOfAutoStandby && intervalOfAutoStandby > 0) {
                    Task latestTask = taskService.findLatestTask(agv.getId(), null);
                    if (null != latestTask) {
                        long seconds = DateUtils.diffToSeconds(
                                Optional.ofNullable(latestTask.getEndTime()).orElse(latestTask.getUpdateTime())
                                , new Date()
                        );
                        if (seconds < intervalOfAutoStandby) { continue; }
                    }
                } else {
                    continue;
            Integer intervalOfAutoStandby = configService.getVal("intervalOfAutoStandby", Integer.class);
            if (null != intervalOfAutoStandby && intervalOfAutoStandby > 0) {
                Task latestTask = taskService.findLatestTask(agv.getId(), null);
                if (null != latestTask) {
                    long seconds = DateUtils.diffToSeconds(
                            Optional.ofNullable(latestTask.getEndTime()).orElse(latestTask.getUpdateTime())
                            , new Date()
                    );
                    if (seconds < intervalOfAutoStandby) { continue; }
                }
            } else {
                continue;
            }
//            }
            mainLockWrapService.buildMinorTask(agv.getId(), TaskTypeType.TO_STANDBY, null, null);
@@ -212,67 +190,5 @@
            }
        }
    }
    @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");
                }
            }
        }
    }
}