huang
5 天以前 9571229a2013472dc701ecf5767f2873b36d8f90
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.mes.interaction.vehicle.coordination;
 
import com.mes.interaction.vehicle.model.VehicleState;
import com.mes.interaction.vehicle.model.VehicleStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
 
/**
 * 车辆状态管理器
 * 管理所有大车设备实例的运行时状态
 * 
 * @author huang
 * @since 2025-11-21
 */
@Slf4j
@Service
public class VehicleStatusManager {
    
    /**
     * 存储所有车辆实例的状态:deviceId -> VehicleStatus
     */
    private final Map<String, VehicleStatus> vehicleStatusMap = new ConcurrentHashMap<>();
    
    /**
     * 获取车辆状态
     * 如果不存在则创建并返回空闲状态
     * 
     * @param deviceId 设备ID
     * @return 车辆状态
     */
    public VehicleStatus getVehicleStatus(String deviceId) {
        if (deviceId == null || deviceId.isEmpty()) {
            return null;
        }
        return vehicleStatusMap.get(deviceId);
    }
    
    /**
     * 获取或创建车辆状态
     * 
     * @param deviceId 设备ID
     * @param deviceName 设备名称
     * @return 车辆状态
     */
    public VehicleStatus getOrCreateVehicleStatus(String deviceId, String deviceName) {
        return vehicleStatusMap.computeIfAbsent(deviceId, 
            k -> new VehicleStatus(deviceId, deviceName));
    }
    
    /**
     * 更新车辆状态
     * 
     * @param deviceId 设备ID
     * @param state 新状态
     */
    public void updateVehicleStatus(String deviceId, VehicleState state) {
        if (deviceId == null || deviceId.isEmpty()) {
            log.warn("设备ID为空,无法更新状态");
            return;
        }
        
        VehicleStatus status = vehicleStatusMap.computeIfAbsent(
            deviceId, 
            k -> new VehicleStatus(deviceId)
        );
        status.setState(state);
        
        log.debug("更新车辆状态: deviceId={}, state={}", deviceId, state);
    }
    
    /**
     * 更新车辆状态(带设备名称)
     * 
     * @param deviceId 设备ID
     * @param deviceName 设备名称
     * @param state 新状态
     */
    public void updateVehicleStatus(String deviceId, String deviceName, VehicleState state) {
        VehicleStatus status = getOrCreateVehicleStatus(deviceId, deviceName);
        status.setState(state);
        log.debug("更新车辆状态: deviceId={}, deviceName={}, state={}", deviceId, deviceName, state);
    }
    
    /**
     * 获取所有空闲的车辆
     * 
     * @return 空闲车辆状态列表
     */
    public List<VehicleStatus> getIdleVehicles() {
        return vehicleStatusMap.values().stream()
            .filter(v -> v.getState() == VehicleState.IDLE)
            .collect(Collectors.toList());
    }
    
    /**
     * 获取所有执行中的车辆
     * 
     * @return 执行中车辆状态列表
     */
    public List<VehicleStatus> getExecutingVehicles() {
        return vehicleStatusMap.values().stream()
            .filter(v -> v.getState() == VehicleState.EXECUTING)
            .collect(Collectors.toList());
    }
    
    /**
     * 获取所有等待中的车辆
     * 
     * @return 等待中车辆状态列表
     */
    public List<VehicleStatus> getWaitingVehicles() {
        return vehicleStatusMap.values().stream()
            .filter(v -> v.getState() == VehicleState.WAITING)
            .collect(Collectors.toList());
    }
    
    /**
     * 检查车辆是否可用
     * 
     * @param deviceId 设备ID
     * @return true表示可用,false表示不可用
     */
    public boolean isVehicleAvailable(String deviceId) {
        if (deviceId == null || deviceId.isEmpty()) {
            return false;
        }
        
        VehicleStatus status = vehicleStatusMap.get(deviceId);
        return status == null || status.isAvailable();
    }
    
    /**
     * 设置车辆任务
     * 
     * @param deviceId 设备ID
     * @param task 任务
     */
    public void setVehicleTask(String deviceId, com.mes.interaction.vehicle.model.VehicleTask task) {
        VehicleStatus status = vehicleStatusMap.get(deviceId);
        if (status != null) {
            status.setCurrentTask(task);
            if (task != null && task.getPlannedPath() != null) {
                status.setTargetPosition(task.getPlannedPath().getEndPosition());
            }
        }
    }
    
    /**
     * 清除车辆任务
     * 
     * @param deviceId 设备ID
     */
    public void clearVehicleTask(String deviceId) {
        VehicleStatus status = vehicleStatusMap.get(deviceId);
        if (status != null) {
            status.setCurrentTask(null);
            status.setTargetPosition(null);
        }
    }
    
    /**
     * 移除车辆状态(当设备被删除时)
     * 
     * @param deviceId 设备ID
     */
    public void removeVehicleStatus(String deviceId) {
        vehicleStatusMap.remove(deviceId);
        log.info("移除车辆状态: deviceId={}", deviceId);
    }
    
    /**
     * 获取所有车辆状态
     * 
     * @return 所有车辆状态列表
     */
    public List<VehicleStatus> getAllVehicleStatuses() {
        return vehicleStatusMap.values().stream()
            .collect(Collectors.toList());
    }
    
    /**
     * 获取指定设备组中的车辆状态
     * 
     * @param deviceIds 设备ID列表
     * @return 车辆状态列表
     */
    public List<VehicleStatus> getVehicleStatuses(List<String> deviceIds) {
        return deviceIds.stream()
            .map(this::getVehicleStatus)
            .filter(status -> status != null)
            .collect(Collectors.toList());
    }
}