liusuyi
2025-12-29 e7af0cdd75430765fcb880fa32391847eb6e615d
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.ard.work.service.control;
 
import com.ard.common.exception.ControlException;
import com.ard.common.utils.SecurityUtils;
import com.ard.work.domain.message.UavControlType;
import com.ard.work.component.websocket.handler.UavControlManager;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * UAV控制服务类,负责管理对飞行和载荷的控制请求、释放和权限管理。
 */
@Service
public class UavControlService {
 
    @Resource
    private UavControlManager uavControlManager;
 
    /**
     * 记录当前获得飞行控制权限的用户
     */
    private final ConcurrentHashMap<String, Long> uavFlyControlMap = new ConcurrentHashMap<>();
    /**
     * 记录当前获得载荷控制权限的用户
     */
    private final ConcurrentHashMap<String, Long> uavPayloadControlMap = new ConcurrentHashMap<>();
 
    /**
     * 记录飞行控制请求
     */
    private final ConcurrentHashMap<String, Long> flyControlRequests = new ConcurrentHashMap<>();
    /**
     * 记录载荷控制请求
     */
    private final ConcurrentHashMap<String, Long> payloadControlRequests = new ConcurrentHashMap<>();
 
    /**
     * 请求获取 UAV 控制权限
     *
     * @param boxSn 设备序列号
     * @param type  控制类型("fly"、"payload")
     * @return 是否立即获得控制权限
     */
    public boolean requestControl(String boxSn, String type) {
        UavControlType controlType = UavControlType.fromString(type);
        Long requester = SecurityUtils.getUserId();
        return requestSingleControl(boxSn, controlType, requester);
    }
 
    /**
     * 处理单个控制类型的请求
     */
    private boolean requestSingleControl(String boxSn, UavControlType controlType, Long requester) {
 
        ConcurrentHashMap<String, Long> controlMap = getControlMap(controlType);
        ConcurrentHashMap<String, Long> requestMap = getRequestMap(controlType);
 
        Long controller = controlMap.get(boxSn);
        if (controller == null) {
            controlMap.put(boxSn, requester);
            return true;
        }
        if (!controller.equals(requester)) {
            requestMap.put(boxSn, requester);
            uavControlManager.sendControlRequest(controller, boxSn, controlType.getType(), requester);
        } else {
            throw new ControlException("当前已拥有控制权");
        }
        return false;
    }
 
    /**
     * 处理 UAV 控制请求的响应(同意或拒绝)
     *
     * @param boxSn   设备序列号
     * @param approve 是否同意
     * @param type    控制类型
     * @return 是否成功处理
     */
    public boolean handleControlResponse(String boxSn, boolean approve, String type) {
        UavControlType controlType = UavControlType.fromString(type);
        Long controller = SecurityUtils.getUserId();
        return handleSingleControlResponse(boxSn, approve, controlType, controller);
    }
 
    /**
     * 处理单个控制类型的请求响应
     */
    private boolean handleSingleControlResponse(String boxSn, boolean approve, UavControlType controlType,
                                                Long controller) {
        ConcurrentHashMap<String, Long> requestMap = getRequestMap(controlType);
        Long requester = requestMap.get(boxSn);
 
        if (requester == null || requester.equals(controller)) {
            throw new ControlException("无控制请求");
        }
 
        if (approve) {
            getControlMap(controlType).put(boxSn, requester);
            uavControlManager.sendControlApproval(requester, boxSn, controlType.getType());
        } else {
            uavControlManager.sendControlRejection(requester, boxSn, controlType.getType());
        }
 
        requestMap.remove(boxSn);
        return true;
    }
 
    /**
     * 释放 UAV 控制权限
     *
     * @param boxSn 设备序列号
     * @param type  控制类型
     * @return 是否成功释放控制
     */
    public boolean releaseControl(String boxSn, String type) {
        UavControlType controlType = UavControlType.fromString(type);
        Long controller = SecurityUtils.getUserId();
        return releaseSingleControl(boxSn, controlType, controller);
    }
 
    /**
     * 释放单个控制类型的权限
     */
    private boolean releaseSingleControl(String boxSn, UavControlType controlType, Long controller) {
        ConcurrentHashMap<String, Long> controlMap = getControlMap(controlType);
 
        if (!controller.equals(controlMap.get(boxSn))) {
            throw new ControlException("没有该设备的" + controlType.getType().toUpperCase() + "控制权");
        }
 
        controlMap.remove(boxSn);
        uavControlManager.sendControlRelease(boxSn, controlType.getType());
        return true;
    }
 
    /**
     * 释放用户所有 UAV 控制权
     */
    public void releaseAllControls(Long userId) {
        // 释放飞行控制
        uavFlyControlMap.entrySet().removeIf(entry -> entry.getValue().equals(userId));
 
        // 释放载荷控制
        uavPayloadControlMap.entrySet().removeIf(entry -> entry.getValue().equals(userId));
    }
 
    /**
     * 获取当前某设备的控制用户
     *
     * @param boxSn 设备序列号
     * @param type  控制类型
     * @return 当前控制该设备的用户 ID
     */
    public Long getCurrentController(String boxSn, String type) {
        UavControlType controlType = UavControlType.fromString(type);
        return getControlMap(controlType).get(boxSn);
    }
    public Map<String,Long> getCurrentController(String boxSn) {
        Long flyController = uavFlyControlMap.get(boxSn);
        Map<String,Long> map=new HashMap<>();
        map.put("flyController",flyController);
        Long payloadController = uavPayloadControlMap.get(boxSn);
        map.put("payloadController",payloadController);
        return map;
    }
    /**
     * 根据控制类型获取对应的控制 Map
     */
    private ConcurrentHashMap<String, Long> getControlMap(UavControlType controlType) {
        switch (controlType) {
            case FLY:
                return uavFlyControlMap;
            case PAYLOAD:
                return uavPayloadControlMap;
            default:
                throw new IllegalArgumentException("不支持的控制类型");
        }
    }
 
    /**
     * 根据控制类型获取对应的请求 Map
     */
    private ConcurrentHashMap<String, Long> getRequestMap(UavControlType controlType) {
        switch (controlType) {
            case FLY:
                return flyControlRequests;
            case PAYLOAD:
                return payloadControlRequests;
            default:
                throw new IllegalArgumentException("不支持的控制类型");
        }
    }
}