liusuyi
2025-10-13 b9461fd1ec44382c27ea35c66970d5ba14b64056
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
package com.ard.web.controller.sample;
 
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.enums.DeviceSetPropertyEnum;
import com.ard.sample.manage.model.receiver.StatusGatewayReceiver;
import com.ard.sample.manage.service.IDeviceService;
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.web.bind.annotation.*;
 
import java.util.List;
import java.util.Optional;
 
/**
 * @author sean.zhou
 * @version 0.1
 * @date 2021/11/15
 */
@Api(tags = "设备管理")
@RestController
@Slf4j
@RequestMapping("/dock/device")
public class DeviceController {
 
    @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("设备列表")
    @GetMapping("/list")
    public ResponseResult<List<DeviceDTO>> getDevices() {
        List<DeviceDTO> devicesList = deviceService.getDevicesTopoForWeb();
        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")
    public ResponseResult<PaginationData<DeviceDTO>> getBoundDevicesWithDomain(Integer domain,
                                                                               @RequestParam(defaultValue = "1") Long page,
                                                                               @RequestParam(value = "page_size",
                                                                                       defaultValue = "50") Long pageSize) {
        PaginationData<DeviceDTO> devices = deviceService.getBoundDevicesWithDomain(page, pageSize, domain);
 
        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);
        }
        String property = param.fieldNames().next();
        Optional<DeviceSetPropertyEnum> propertyEnumOpt = DeviceSetPropertyEnum.find(property);
        if (propertyEnumOpt.isEmpty()) {
            return ResponseResult.error(CommonErrorEnum.ILLEGAL_ARGUMENT);
        }
        deviceService.devicePropertySet(dockSn, propertyEnumOpt.get(), param.get(property));
        return ResponseResult.success();
    }
}