zhaowenyu
2026-02-11 a12665923c5ec933e2c92ea74135680cc8b3af4a
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package com.ard.web.controller.sample;
 
import com.ard.common.core.controller.BaseController;
import com.ard.common.core.page.TableDataInfo;
import com.ard.device.camera.domain.ArdCamera;
import com.ard.sample.common.error.CommonErrorEnum;
import com.ard.sample.common.model.PaginationData;
import com.ard.sample.common.model.ResponseResult;
import com.ard.sample.component.mqtt.model.ChannelName;
import com.ard.sample.component.mqtt.model.CommonTopicReceiver;
import com.ard.sample.component.mqtt.model.CommonTopicResponse;
import com.ard.sample.manage.model.dto.DeviceDTO;
import com.ard.sample.manage.model.dto.DeviceFirmwareUpgradeDTO;
import com.ard.sample.manage.model.entity.DeviceEntity;
import com.ard.sample.manage.model.enums.DeviceFirmwareStatusEnum;
import com.ard.sample.manage.model.enums.DeviceSetPropertyEnum;
import com.ard.sample.manage.model.receiver.StatusGatewayReceiver;
import com.ard.sample.manage.service.IDeviceService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import java.time.ZoneId;
import java.util.List;
import java.util.Optional;
 
import static com.ard.common.utils.PageUtils.startPage;
 
/**
 * @author sean.zhou
 * @version 0.1
 * @date 2021/11/15
 */
@Api(tags = "设备管理")
@RestController
@Slf4j
@RequestMapping("/dock/device")
public class DeviceController extends BaseController {
 
    @Autowired
    private IDeviceService deviceService;
 
    /**
     * 处理上线的消息
     *
     * @param receiver The drone information is not empty.
     */
    @ServiceActivator(inputChannel = ChannelName.INBOUND_STATUS_ONLINE, outputChannel = ChannelName.OUTBOUND)
    public void deviceOnline(CommonTopicReceiver<StatusGatewayReceiver> receiver) {
        boolean online = deviceService.deviceOnline(receiver.getData());
        if (online) {
            // Notify pilot that the drone is online successfully.
            deviceService.publishStatusReply(receiver.getData().getSn(),
                    CommonTopicResponse.builder()
                            .tid(receiver.getTid())
                            .bid(receiver.getBid())
                            .timestamp(System.currentTimeMillis())
                            .method(receiver.getMethod())
                            .build());
        }
    }
 
    /**
     * 处理离线的消息
     *
     * @param receiver The drone information is empty.
     */
    @ServiceActivator(inputChannel = ChannelName.INBOUND_STATUS_OFFLINE, outputChannel = ChannelName.OUTBOUND)
    public void deviceOffline(CommonTopicReceiver<StatusGatewayReceiver> receiver) {
 
        boolean offline = deviceService.deviceOffline(receiver.getData());
        if (offline) {
            // Notify pilot that the device is offline successfully.
            deviceService.publishStatusReply(receiver.getData().getSn(),
                    CommonTopicResponse.builder()
                            .tid(receiver.getTid())
                            .bid(receiver.getBid())
                            .timestamp(System.currentTimeMillis())
                            .method(receiver.getMethod())
                            .build());
 
        }
    }
 
    /**
     * 获取所有在线设备列表
     *
     * @return
     */
    @ApiOperation("设备列表")
    @PreAuthorize("@ss.hasPermi('device:dock:list')")
    @GetMapping("/list")
    public ResponseResult<List<DeviceDTO>> getDevices(@RequestParam(required = false) String nickname) {
//        List<DeviceDTO> devicesList = deviceService.getDevicesTopoForWeb();
        List<DeviceDTO> devicesList = deviceService.getDockList(nickname);
        return ResponseResult.success(devicesList);
    }
 
//    /**
//     * 获取按nickname筛选在线设备列表
//     *
//     * @return
//     */
//    @ApiOperation("设备列表-按nickname筛选")
//    @GetMapping("/cameralist")
//    public ResponseResult<List<DeviceDTO>> getCameraDevices(@RequestParam(required = false) String nickname) {
//        List<DeviceDTO> devicesList = deviceService.getDockCameraList(nickname);
//        return ResponseResult.success(devicesList);
//    }
 
    /**
     * 绑定设备
     *
     * @param device
     * @param deviceSn
     * @return
     */
    @ApiOperation("设备绑定")
    @PostMapping("/{device_sn}/binding")
    public ResponseResult bindDevice(@RequestBody DeviceDTO device, @PathVariable("device_sn") String deviceSn) {
        device.setDeviceSn(deviceSn);
        boolean isUpd = deviceService.bindDevice(device);
        return isUpd ? ResponseResult.success() : ResponseResult.error();
    }
 
    /**
     * Obtain device information according to device sn.
     *
     * @param deviceSn
     * @return
     */
    @ApiOperation("设备信息")
    @GetMapping("/{device_sn}")
    public ResponseResult getDevice(@PathVariable("device_sn") String deviceSn) {
        Optional<DeviceDTO> deviceOpt = deviceService.getDeviceBySn(deviceSn);
        return deviceOpt.isEmpty() ? ResponseResult.error("device not found.") :
                ResponseResult.success(deviceOpt.get());
    }
 
    /**
     * Get the binding devices list
     * 获取绑定设备列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("绑定设备列表")
    @GetMapping("/list/bound")
    @PreAuthorize("@ss.hasPermi('device:dock:list')")
    public ResponseResult<PaginationData<DeviceDTO>> getBoundDevicesWithDomain(Integer domain,
                                                                               @RequestParam(defaultValue = "1") Long page,
                                                                               @RequestParam(value = "page_size",
                                                                                       defaultValue = "50") Long pageSize,
                                                                               @RequestParam(required = false) Long deptId) {
        PaginationData<DeviceDTO> devices = deviceService.getBoundDevicesWithDomain(page, pageSize, domain, deptId);
 
        return ResponseResult.success(devices);
    }
 
    /**
     * Removing the binding state of the device.
     *
     * @param deviceSn
     * @return
     */
    @ApiOperation("设备解绑")
    @DeleteMapping("/{device_sn}/unbinding")
    public ResponseResult unbindingDevice(@PathVariable("device_sn") String deviceSn) {
        deviceService.unbindDevice(deviceSn);
        return ResponseResult.success();
    }
 
    /**
     * Update device information.
     *
     * @param device
     * @param deviceSn
     * @return
     */
    @ApiOperation("设备更新")
    @PutMapping("/{device_sn}")
    public ResponseResult updateDevice(@RequestBody DeviceDTO device,
                                       @PathVariable("device_sn") String deviceSn) {
        device.setDeviceSn(deviceSn);
        boolean isUpd = deviceService.updateDevice(device);
        return isUpd ? ResponseResult.success() : ResponseResult.error();
    }
 
    /**
     * Delivers offline firmware upgrade tasks.
     *
     * @param upgradeDTOS
     * @return
     */
    @ApiOperation("固件升级")
    @PostMapping("/ota")
    public ResponseResult createOtaJob(@RequestBody List<DeviceFirmwareUpgradeDTO> upgradeDTOS) {
        return deviceService.createDeviceOtaJob(upgradeDTOS);
    }
 
    /**
     * Set the property parameters of the drone.
     *
     * @param dockSn
     * @param param
     * @return
     */
    @ApiOperation("属性设置")
    @PutMapping("{device_sn}/property")
    public ResponseResult devicePropertySet(@PathVariable("device_sn") String dockSn,
                                            @RequestBody JsonNode param) {
        if (param.size() != 1) {
            //return ResponseResult.error(CommonErrorEnum.ILLEGAL_ARGUMENT);
            return ResponseResult.error(999,"参数不合法");
        }
        String property = param.fieldNames().next();
        Optional<DeviceSetPropertyEnum> propertyEnumOpt = DeviceSetPropertyEnum.find(property);
        if (propertyEnumOpt.isEmpty()) {
            //return ResponseResult.error(CommonErrorEnum.ILLEGAL_ARGUMENT);
            return ResponseResult.error(999,"参数无效");
        }
        return deviceService.devicePropertySet(dockSn, propertyEnumOpt.get(), param);
//        return ResponseResult.success();
    }
 
    //.get(property)
 
//    /**
//     * 查询机场设备列表
//     */
//    @PreAuthorize("@ss.hasPermi('device:camera:list')")
//    @GetMapping("/test/list")
//    @ApiOperation("查询机场设备列表")
//    public TableDataInfo list(DeviceDTO device) {
//        startPage();
//        DeviceEntity deviceEntity = deviceDTO2Entity(device);
//        QueryWrapper<DeviceEntity> queryWrapper=new QueryWrapper<>(deviceEntity);
//        List<DeviceEntity> list = deviceService.selectList(queryWrapper);
//        return getDataTable(list);
//    }
 
    private DeviceEntity deviceDTO2Entity(DeviceDTO dto) {
        DeviceEntity.DeviceEntityBuilder builder = DeviceEntity.builder();
        if (dto == null) {
            return builder.build();
        }
 
        return builder.deviceSn(dto.getDeviceSn())
                .userId(dto.getUserId())
                .nickname(dto.getNickname())
                .boundStatus(dto.getBoundStatus())
                .loginTime(dto.getLoginTime() != null ?
                        dto.getLoginTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() : null)
                .boundTime(dto.getBoundTime() != null ?
                        dto.getBoundTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() : null)
                .childSn(dto.getChildDeviceSn())
                .domain(dto.getDomain())
                .firmwareVersion(dto.getFirmwareVersion())
                .workType(dto.getWorkType())
                .webrtcUrl(dto.getWebrtcUrl())
                .cameraWebrtcUrl(dto.getCameraWebrtcUrl())
                .cameraState(dto.getCameraState())
                .compatibleStatus(dto.getFirmwareStatus() == null ? null :
                        DeviceFirmwareStatusEnum.CONSISTENT_UPGRADE != DeviceFirmwareStatusEnum.find(dto.getFirmwareStatus()))
                .build();
    }
}