From a7aaeabc7873a0eafb4a7ecad7f65b018b7a9bc9 Mon Sep 17 00:00:00 2001
From: sean.zhou <sean.zhou@dji.com>
Date: Fri, 24 Feb 2023 19:31:23 +0800
Subject: [PATCH] What's new? 1. Add license for dock. 2. Modify the logic corresponding to the firmware file and device type. 3. Add multiple mqtt clients options. 4. Modify the structure of the interface for obtaining the device list. 5. Fixed some issues.

---
 src/main/java/com/dji/sample/manage/controller/DeviceController.java |  174 ++++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 114 insertions(+), 60 deletions(-)

diff --git a/src/main/java/com/dji/sample/manage/controller/DeviceController.java b/src/main/java/com/dji/sample/manage/controller/DeviceController.java
index 08a245d..9fcc72d 100644
--- a/src/main/java/com/dji/sample/manage/controller/DeviceController.java
+++ b/src/main/java/com/dji/sample/manage/controller/DeviceController.java
@@ -1,32 +1,24 @@
 package com.dji.sample.manage.controller;
 
-import com.dji.sample.common.model.CustomClaim;
+import com.dji.sample.common.error.CommonErrorEnum;
+import com.dji.sample.common.model.PaginationData;
 import com.dji.sample.common.model.ResponseResult;
-import com.dji.sample.component.AuthInterceptor;
 import com.dji.sample.component.mqtt.model.ChannelName;
 import com.dji.sample.component.mqtt.model.CommonTopicReceiver;
 import com.dji.sample.component.mqtt.model.CommonTopicResponse;
-import com.dji.sample.component.websocket.model.BizCodeEnum;
-import com.dji.sample.component.websocket.model.CustomWebSocketMessage;
-import com.dji.sample.component.websocket.model.WebSocketManager;
-import com.dji.sample.component.websocket.service.ISendMessageService;
 import com.dji.sample.manage.model.dto.DeviceDTO;
-import com.dji.sample.manage.model.dto.WorkspaceDTO;
-import com.dji.sample.manage.model.enums.UserTypeEnum;
-import com.dji.sample.manage.model.param.DeviceQueryParam;
+import com.dji.sample.manage.model.dto.DeviceFirmwareUpgradeDTO;
+import com.dji.sample.manage.model.enums.DeviceSetPropertyEnum;
 import com.dji.sample.manage.model.receiver.StatusGatewayReceiver;
 import com.dji.sample.manage.service.IDeviceService;
+import com.fasterxml.jackson.databind.JsonNode;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.integration.annotation.ServiceActivator;
-import org.springframework.integration.mqtt.support.MqttHeaders;
-import org.springframework.messaging.Message;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
-import javax.servlet.http.HttpServletRequest;
 import java.util.List;
+import java.util.Optional;
 
 /**
  * @author sean.zhou
@@ -41,9 +33,6 @@
     @Autowired
     private IDeviceService deviceService;
 
-    @Autowired
-    private ISendMessageService sendMessageService;
-
     /**
      * Handles the message that the drone goes online.
      * @param receiver  The drone information is not empty.
@@ -57,11 +46,9 @@
                     CommonTopicResponse.builder()
                             .tid(receiver.getTid())
                             .bid(receiver.getBid())
+                            .timestamp(System.currentTimeMillis())
+                            .method(receiver.getMethod())
                             .build());
-
-            // Publish the latest device topology information in the current workspace to the pilot.
-            deviceService.pushDeviceOnlineTopo(WorkspaceDTO.DEFAULT_WORKSPACE_ID,
-                    receiver.getData().getSn(), receiver.getData().getSubDevices().get(0).getSn());
         }
     }
 
@@ -72,67 +59,134 @@
     @ServiceActivator(inputChannel = ChannelName.INBOUND_STATUS_OFFLINE, outputChannel = ChannelName.OUTBOUND)
     public void deviceOffline(CommonTopicReceiver<StatusGatewayReceiver> receiver) {
 
-        boolean offline = deviceService.deviceOffline(receiver.getData().getSn());
+        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());
 
-            // Publish the latest device topology information in the current workspace to the pilot.
-            deviceService.pushDeviceOfflineTopo(WorkspaceDTO.DEFAULT_WORKSPACE_ID, receiver.getData().getSn());
         }
     }
 
     /**
-     * Get the topology list of all devices in the current user workspace.
-     * @param request
+     * Get the topology list of all online devices in one workspace.
+     * @param workspaceId
      * @return
      */
-    @GetMapping("/devices")
-    public ResponseResult<List<DeviceDTO>> getDevices(HttpServletRequest request) {
-        // Get information about the current user.
-        CustomClaim claim = (CustomClaim)request.getAttribute(AuthInterceptor.TOKEN_CLAIM);
-        String workspaceId = claim.getWorkspaceId();
-        // Get information about the devices in the current user's workspace.
+    @GetMapping("/{workspace_id}/devices")
+    public ResponseResult<List<DeviceDTO>> getDevices(@PathVariable("workspace_id") String workspaceId) {
         List<DeviceDTO> devicesList = deviceService.getDevicesTopoForWeb(workspaceId);
 
         return ResponseResult.success(devicesList);
     }
 
-    @ServiceActivator(inputChannel = ChannelName.INBOUND_OSD)
-    public void osdRealTime(Message<?> message) {
-        String topic = message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC).toString();
-        byte[] payload = (byte[])message.getPayload();
-        deviceService.handleOSD(topic, payload);
+    /**
+     * After binding the device to the workspace, the device data can only be seen on the web.
+     * @param device
+     * @param deviceSn
+     * @return
+     */
+    @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();
     }
 
     /**
-     * Handles the payloads data of the drone.
-     * @param deviceSn  drone's sn
+     * Obtain device information according to device sn.
+     * @param workspaceId
+     * @param deviceSn
+     * @return
      */
-    @ServiceActivator(inputChannel = ChannelName.INBOUND_STATE_PAYLOAD_UPDATE)
-    public void pushWebSocketDevices(String deviceSn) {
-        List<DeviceDTO> devicesList = deviceService.getDevicesByParams(
-                DeviceQueryParam.builder()
-                        .deviceSn(deviceSn)
-                        .build());
-        // Get drone information based on the sn of the drone. The sn of the drone is unique.
-        DeviceDTO device = devicesList.get(0);
-        // Set the remote controller and payloads information of the drone.
-        deviceService.spliceDeviceTopo(device);
+    @GetMapping("/{workspace_id}/devices/{device_sn}")
+    public ResponseResult getDevice(@PathVariable("workspace_id") String workspaceId,
+                                               @PathVariable("device_sn") String deviceSn) {
+        Optional<DeviceDTO> deviceOpt = deviceService.getDeviceBySn(deviceSn);
+        return deviceOpt.isEmpty() ? ResponseResult.error("device not found.") : ResponseResult.success(deviceOpt.get());
+    }
 
-        CustomWebSocketMessage wsMessage = CustomWebSocketMessage.builder()
-                .timestamp(System.currentTimeMillis())
-                .bizCode(BizCodeEnum.DEVICE_UPDATE_TOPO.getCode())
-                .data(device)
-                .build();
-        // Update the topology of the drone via WebSocket notifications to the web side.
-        sendMessageService.sendBatch(WebSocketManager
-                .getValueWithWorkspaceAndUserType(
-                        device.getWorkspaceId(), UserTypeEnum.WEB.getVal()),
-                wsMessage);
+    /**
+     * Get the binding devices list in one workspace.
+     * @param workspaceId
+     * @param page
+     * @param pageSize
+     * @return
+     */
+    @GetMapping("/{workspace_id}/devices/bound")
+    public ResponseResult<PaginationData<DeviceDTO>> getBoundDevicesWithDomain(
+            @PathVariable("workspace_id") String workspaceId, Integer domain,
+            @RequestParam(defaultValue = "1") Long page,
+            @RequestParam(value = "page_size", defaultValue = "50") Long pageSize) {
+        PaginationData<DeviceDTO> devices = deviceService.getBoundDevicesWithDomain(workspaceId, page, pageSize, domain);
+
+        return ResponseResult.success(devices);
+    }
+
+    /**
+     * Removing the binding state of the device.
+     * @param deviceSn
+     * @return
+     */
+    @DeleteMapping("/{device_sn}/unbinding")
+    public ResponseResult unbindingDevice(@PathVariable("device_sn") String deviceSn) {
+        deviceService.unbindDevice(deviceSn);
+        return ResponseResult.success();
+    }
+
+    /**
+     * Update device information.
+     * @param device
+     * @param workspaceId
+     * @param deviceSn
+     * @return
+     */
+    @PutMapping("/{workspace_id}/devices/{device_sn}")
+    public ResponseResult updateDevice(@RequestBody DeviceDTO device,
+                                       @PathVariable("workspace_id") String workspaceId,
+                                       @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 workspaceId
+     * @param upgradeDTOS
+     * @return
+     */
+    @PostMapping("/{workspace_id}/devices/ota")
+    public ResponseResult createOtaJob(@PathVariable("workspace_id") String workspaceId,
+                                       @RequestBody List<DeviceFirmwareUpgradeDTO> upgradeDTOS) {
+        return deviceService.createDeviceOtaJob(workspaceId, upgradeDTOS);
+    }
+
+    /**
+     * Set the property parameters of the drone.
+     * @param workspaceId
+     * @param dockSn
+     * @param param
+     * @return
+     */
+    @PutMapping("/{workspace_id}/devices/{device_sn}/property")
+    public ResponseResult devicePropertySet(@PathVariable("workspace_id") String workspaceId,
+                                            @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(workspaceId, dockSn, propertyEnumOpt.get(), param.get(property));
+        return ResponseResult.success();
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3