| | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.ard.common.exception.NoPermissionException; |
| | | import com.ard.common.utils.SecurityUtils; |
| | | import com.ard.tianyu.websocket.domain.ControlMessage; |
| | | import com.ard.tianyu.websocket.domain.WsMessage; |
| | | import com.ard.tianyu.websocket.domain.ControlAuthorityDTO; |
| | | import com.ard.tianyu.websocket.domain.BaseMessage; |
| | | import com.ard.tianyu.websocket.utils.WebSocketUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | |
| | | /** |
| | | * 无人机控制管理器 |
| | | * |
| | | * @author liusuyi |
| | | * @date 2024年02月21日8:37 |
| | | **/ |
| | | @Service |
| | | @Slf4j |
| | | public class UavControlManager { |
| | | |
| | | // 存储每个无人机的当前控制者 |
| | | public static final ConcurrentHashMap<String, Long> droneControlMap = new ConcurrentHashMap<>(); |
| | | private static final String CONTROL_REQUEST_ACTION = "controlRequest"; |
| | | private static final String REQUEST_ACTION = "request"; |
| | | private static final String APPROVE_ACTION = "approve"; |
| | | private static final String REJECT_ACTION = "reject"; |
| | | |
| | | // 存储每个无人机的当前控制者 |
| | | public static final ConcurrentHashMap<String, Long> uavControlMap = new ConcurrentHashMap<>(); |
| | | // 存储无人机的控制请求(用于判断哪个控制者正在等待) |
| | | public static final ConcurrentHashMap<String, Long> controlRequestMap = new ConcurrentHashMap<>(); |
| | | |
| | | // 发送通知(通过 WebSocket推送) |
| | | public void notifyMessage(String boxSn, String action, Long source, Long target) { |
| | | ControlMessage controlMessage = ControlMessage.builder() |
| | | .boxSn(boxSn) |
| | | .userId(source) |
| | | .action(action) |
| | | .build(); |
| | | WsMessage<ControlMessage> wsMessage = new WsMessage<>("controlRequest", controlMessage); |
| | | WebSocketUtil.sendMessage(target.toString(), JSON.toJSONString(wsMessage)); |
| | | } |
| | | |
| | | // 申请控制 |
| | | /** |
| | | * 申请控制 |
| | | * |
| | | * @param boxSn 盒子编号 |
| | | */ |
| | | public boolean requestControl(String boxSn) { |
| | | Long requester = SecurityUtils.getUserId(); |
| | | Long controller = uavControlMap.get(boxSn); |
| | | // 如果无人机没有控制者,直接控制 |
| | | if (droneControlMap.get(boxSn) == null) { |
| | | droneControlMap.put(boxSn, requester); |
| | | if (controller == null) { |
| | | uavControlMap.put(boxSn, requester); |
| | | log.info("控制权已授予给用户: {}", requester); |
| | | return true; // 控制成功 |
| | | } |
| | | // 如果无人机已经有控制者,则发送申请给当前控制者 |
| | | Long controller = droneControlMap.get(boxSn); |
| | | if (!controller.equals(requester)) { |
| | | // 则将控制请求加入请求队列 |
| | | controlRequestMap.put(boxSn, requester); |
| | | // 通知当前控制者,有新的控制请求 |
| | | notifyMessage(boxSn, "request", requester, controller); |
| | | notifyMessage(boxSn, REQUEST_ACTION, requester, controller); |
| | | log.info("控制请求已发送给当前控制者: {}", controller); |
| | | } else { |
| | | throw new NoPermissionException("当前已经获取控制权,无需重复申请"); |
| | | } |
| | | return false; // 等待当前控制者的同意 |
| | | } |
| | | |
| | | // 处理控制请求的同意或拒绝 |
| | | /** |
| | | * 处理控制请求 |
| | | * |
| | | * @param boxSn 盒子编号 |
| | | * @param approve 是否同意 |
| | | */ |
| | | public boolean handleControlResponse(String boxSn, boolean approve) { |
| | | Long controller = SecurityUtils.getUserId(); |
| | | Long requester = controlRequestMap.get(boxSn); |
| | | // 确保请求者是当前的控制者 |
| | | if (requester != null && !requester.equals(controller)) { |
| | | if (approve) { |
| | | // 同意控制,将控制权赋予请求者 |
| | | droneControlMap.put(boxSn, requester); |
| | | notifyMessage(boxSn, "approve", controller, requester); |
| | | } else { |
| | | notifyMessage(boxSn, "reject", controller, requester); |
| | | } |
| | | // 无论同意还是拒绝,都移除控制请求 |
| | | controlRequestMap.remove(boxSn); |
| | | return true; |
| | | } |
| | | else { |
| | | |
| | | if (requester == null || requester.equals(controller)) { |
| | | throw new NoPermissionException("无申请不需要处理控制权"); |
| | | } |
| | | |
| | | if (approve) { |
| | | uavControlMap.put(boxSn, requester); |
| | | notifyMessage(boxSn, APPROVE_ACTION, controller, requester); |
| | | log.info("控制权已转移给用户: {}", requester); |
| | | } else { |
| | | notifyMessage(boxSn, REJECT_ACTION, controller, requester); |
| | | log.info("控制请求被拒绝,请求者: {}", requester); |
| | | } |
| | | |
| | | controlRequestMap.remove(boxSn); |
| | | return true; |
| | | } |
| | | |
| | | // 释放控制 |
| | | /** |
| | | * 释放控制 |
| | | * |
| | | * @param boxSn 盒子编号 |
| | | */ |
| | | public boolean releaseControl(String boxSn) { |
| | | Long controller = droneControlMap.get(boxSn); |
| | | if (controller != null) { |
| | | if (controller.equals(SecurityUtils.getUserId())) { |
| | | droneControlMap.remove(boxSn); |
| | | return true; |
| | | } else { |
| | | // 当前控制者不匹配,可以记录日志或者抛出异常 |
| | | log.warn("无权限释放控制, 实际控制者是 {}", controller); |
| | | throw new NoPermissionException("无权限释放控制, 实际控制者是 " + controller); |
| | | } |
| | | } else { |
| | | // 说明无人机没有控制者 |
| | | Long controller = uavControlMap.get(boxSn); |
| | | |
| | | if (controller == null) { |
| | | log.warn("无人机当前 {} 没有控制者,无需释放", boxSn); |
| | | throw new NoPermissionException("无人机" + boxSn + "当前没有控制者,无需释放"); |
| | | } |
| | | |
| | | if (!controller.equals(SecurityUtils.getUserId())) { |
| | | log.warn("无权限释放控制, 实际控制者是 {}", controller); |
| | | throw new NoPermissionException("无权限释放控制, 实际控制者是 " + controller); |
| | | } |
| | | |
| | | uavControlMap.remove(boxSn); |
| | | log.info("控制权已释放,无人机: {}", boxSn); |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 释放指定用户控制的无人机 |
| | | * |
| | | * @param userId 用户id |
| | | */ |
| | | public void releaseAllControlByUser(String userId) { |
| | | String boxSn = uavControlMap.entrySet().stream() |
| | | .filter(entry -> entry.getValue().toString().equals(userId)) |
| | | .map(ConcurrentHashMap.Entry::getKey) |
| | | .findFirst() |
| | | .orElse(null); |
| | | if (boxSn != null) { |
| | | // 用户控制了某个无人机,移除缓存 |
| | | uavControlMap.remove(boxSn); |
| | | log.info("移除{}控制的无人机: {}", userId, boxSn); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送消息给指定用户 |
| | | * |
| | | * @param boxSn 盒子编号 |
| | | * @param action 指令() |
| | | * @param source 发送方用户ID |
| | | * @param target 接收方用户ID |
| | | */ |
| | | private void notifyMessage(String boxSn, String action, Long source, Long target) { |
| | | ControlAuthorityDTO controlAuthorityDTO = buildControlAuthorityDTO(boxSn, action, source); |
| | | BaseMessage<ControlAuthorityDTO> baseMessage = buildBaseMessage(CONTROL_REQUEST_ACTION, controlAuthorityDTO); |
| | | WebSocketUtil.sendMessage(target.toString(), JSON.toJSONString(baseMessage)); |
| | | } |
| | | |
| | | private ControlAuthorityDTO buildControlAuthorityDTO(String boxSn, String action, Long userId) { |
| | | return ControlAuthorityDTO.builder() |
| | | .boxSn(boxSn) |
| | | .userId(userId) |
| | | .action(action) |
| | | .build(); |
| | | } |
| | | |
| | | private <T> BaseMessage<T> buildBaseMessage(String type, T data) { |
| | | return new BaseMessage<>(type, data); |
| | | } |
| | | } |