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());
|
}
|
}
|