zhang
2025-07-23 d127e27168d07199b0d4d55c0a5d431761f490d1
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
package com.zy.acs.manager.core.scheduler;
 
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.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.TaskStsType;
import com.zy.acs.manager.manager.service.*;
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.Date;
import java.util.List;
 
@Slf4j
@Component
public class StatisticsScheduler {
 
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
 
    @Autowired
    private AgvService agvService;
 
    @Autowired
    private AgvDetailService agvDetailService;
 
    @Autowired
    private TaskService taskService;
 
    @Autowired
    private AgvDurationService agvDurationService;
 
    @Autowired
    private OfflineLogService offlineLogService;
 
    /**
     * 定时检查AGV是否掉线,并且保存记录
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void execute() {
        List<Agv> list = agvService.list();
        Object object = null;
        OfflineLog offlineLog;
        for (Agv agv : list) {
            if (agv.getStatus() == 1) {
                object = redis.getObject(RedisConstant.AGV_ONLINE_FLAG, agv.getUuid());
                if (object == null) {
                    offlineLog = offlineLogService.getTopOfflineLog(agv.getId());
                    if (offlineLog == null) {
                        offlineLog = new OfflineLog();
                        offlineLog.setAgvId(agv.getId());
                        offlineLog.setHappenTime(new Date());
                        offlineLogService.save(offlineLog);
                    }
                } else {
                    offlineLog = offlineLogService.getTopOfflineLog(agv.getId());
                    if (offlineLog != null) {
                        offlineLog.setResetTime(new Date());
                        offlineLog.setDuration(offlineLog.getResetTime().getTime() - offlineLog.getHappenTime().getTime());
                        offlineLogService.updateById(offlineLog);
                    }
                }
            }
        }
    }
 
    @Scheduled(cron = "0/5 * * * * ? ")
    public void execute2() {
        List<AgvDuration> agvDurations = agvDurationService.listUnFinish();
        for (AgvDuration agvDuration : agvDurations) {
            Task task = taskService.getById(agvDuration.getTaskNo());
            if (task.getTaskSts() == TaskStsType.COMPLETE.val()) {
                AgvDetail agvDetail = agvDetailService.selectByAgvId(agvDuration.getAgvId());
                if (agvDetail.getAgvStatus() != AgvStatusType.CHARGE) {
                    agvDuration.setResetTime(new Date());
                    agvDuration.setDuration(agvDuration.getResetTime().getTime() - agvDuration.getHappenTime().getTime());
                    agvDurationService.updateById(agvDuration);
                }
            } else if (task.getTaskSts() == TaskStsType.CANCEL.val()) {
                agvDurationService.removeById(agvDuration.getId());
            } else {
 
            }
        }
    }
}