Browse Source

功能调整

hxd 3 months ago
parent
commit
7beb527f0e

+ 4 - 3
portal-service-application/src/main/java/com/hfln/portal/application/controller/wap/DeviceController.java

@@ -6,6 +6,7 @@ import com.hfln.portal.common.dto.data.device.DeviceDTO;
 import com.hfln.portal.common.request.device.DeviceBandingParams;
 import com.hfln.portal.common.request.device.DeviceListParams;
 import com.hfln.portal.common.request.device.DeviceLocationParams;
+import com.hfln.portal.common.request.device.DeviceTransferParams;
 import com.hfln.portal.common.response.device.WcTimesQueryRes;
 import com.hfln.portal.domain.gateway.DeviceGateway;
 import io.swagger.v3.oas.annotations.Operation;
@@ -79,7 +80,7 @@ public class DeviceController {
 
     @GetMapping("/checkDevByUserId")
     @Operation(summary = "检测用户是否拥有设备")
-    public ApiResult<Boolean> checkDevByUserId(@RequestParam("userId") Long userId, @RequestParam("devId") Long devId) {
+    public ApiResult<Boolean> checkDevByUserId(@RequestParam("user_id") Long userId, @RequestParam("dev_id") Long devId) {
         return ApiResult.success(deviceGateway.checkDevByUserId(userId, devId));
     }
 
@@ -91,8 +92,8 @@ public class DeviceController {
 
     @PostMapping("/transfer")
     @Operation(summary = "设备转移")
-    public ApiResult<Boolean> deviceTransfer(@RequestParam("phone") String phone, @RequestParam("devId") String devId) {
-        return ApiResult.success(deviceGateway.deviceTransfer(phone, devId));
+    public ApiResult<Boolean> deviceTransfer(@RequestBody @Valid DeviceTransferParams params) {
+        return ApiResult.success(deviceGateway.deviceTransfer(params));
     }
 
     @PostMapping("/statusLight")

+ 3 - 3
portal-service-common/src/main/java/com/hfln/portal/common/constant/mqtt/topic/TopicConstants.java

@@ -18,10 +18,10 @@ public interface TopicConstants {
     String TOPIC_DEV_GET_PARAM = "/mps/get_device_param";
 
 
-    String TOPIC_DEV_UPDATE = "/dev/%s/set_device_param";
+    String TOPIC_SET_DEVICE_PARAM = "/dev/%s/set_device_param";
 
     //向雷达发送设备信息
-    String TOPIC_SET_DEVICE_PARAM = "/mps/set_device_param";
+    String TOPIC_MPS_SET_DEVICE_PARAM = "/mps/set_device_param";
 
 
     // 订阅主题
@@ -33,7 +33,7 @@ public interface TopicConstants {
     // 信息更新
     String TOPIC_DAS_DEV_STATUS = "/das/dev_status";
     // 存在事件
-    String TOPIC_DAS_ESIST = "/das/exist";
+    String TOPIC_DAS_EXIST = "/das/exist";
 
 
     /**

+ 27 - 0
portal-service-common/src/main/java/com/hfln/portal/common/request/device/DeviceTransferParams.java

@@ -0,0 +1,27 @@
+package com.hfln.portal.common.request.device;
+
+
+import com.hfln.portal.common.vo.BaseVO;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+
+@EqualsAndHashCode(callSuper = true)
+@Data
+public class DeviceTransferParams extends BaseVO {
+
+    @Schema(description = "用户id")
+    @NotNull
+    private Long userId;
+
+    @Schema(description = "设备主键")
+    @NotNull
+    private Long devId;
+
+    @Schema(description = "转移对象手机号")
+    @NotBlank
+    private String phone;
+}

+ 4 - 2
portal-service-domain/src/main/java/com/hfln/portal/domain/exception/ErrorEnum.java

@@ -16,7 +16,8 @@ public enum ErrorEnum implements ErrorEnumInterface{
     USER_NOT_FOUND( "10001", "用户不存在"),
     MOBILE_EXISTS("10002", "手机号已存在"),
     MOBILE_NOT_SIGN("10003", "手机号未注册"),
-    DATA_NOT_EXISTS("10004", "数据不存在!"),
+    DATA_NOT_EXISTS("10004", "数据不存在"),
+    MQTT_SEND_ERROR("10005", "MQTT发送失败"),
 
 
     /**
@@ -55,7 +56,8 @@ public enum ErrorEnum implements ErrorEnumInterface{
     CLIENT_ID_ID_NULL("50003", "设备ID不能为空!"),
     DEVICE_IS_EXIST("50004", "设备已存在!"),
     DEVICE_PARAM_ERROR("50005", "设备参数范围错误!"),
-    STATUS_LIGHT_IS_NOT_EXIST("50006","指示灯状态不合法"),
+    STATUS_LIGHT_IS_NOT_EXIST("50006","指示灯状态不合法!"),
+    USER_NO_PERMISSION("50007", "当前用户没有权限!"),
 
     /**
      * 设备房间相关

+ 4 - 1
portal-service-domain/src/main/java/com/hfln/portal/domain/gateway/DeviceGateway.java

@@ -8,6 +8,7 @@ import com.hfln.portal.common.dto.data.share.ShareDto;
 import com.hfln.portal.common.request.device.DeviceBandingParams;
 import com.hfln.portal.common.request.device.DeviceListParams;
 import com.hfln.portal.common.request.device.DeviceLocationParams;
+import com.hfln.portal.common.request.device.DeviceTransferParams;
 import com.hfln.portal.common.request.event.EventListParams;
 import com.hfln.portal.common.request.share.ShareConfirmParam;
 import com.hfln.portal.common.request.share.ShareParam;
@@ -46,6 +47,8 @@ public interface DeviceGateway {
 
     Boolean statusLight(Long devId, Integer statusLight);
 
+    Boolean deviceTransfer(DeviceTransferParams params);
+
 
     /**
      * 房间布局
@@ -96,7 +99,7 @@ public interface DeviceGateway {
 
     WcTimesQueryRes getWcTimes(Long devId, String time);
 
-    Boolean deviceTransfer(String phone, String devId);
+
 
     //String checkDevByOpenId(String openId, String clientId);
 }

+ 1 - 1
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/config/MqttConfig.java

@@ -115,7 +115,7 @@ public class MqttConfig {
                 TopicConstants.TOPIC_DAS_EVENT,
                 TopicConstants.TOPIC_DAS_ALARM_EVENT,
                 TopicConstants.TOPIC_DAS_DEV_STATUS,
-                TopicConstants.TOPIC_DAS_ESIST,
+                TopicConstants.TOPIC_DAS_EXIST,
                 TopicConstants.TOPIC_DAS_REALTIME_POS
         };
         

+ 70 - 44
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/DeviceGatewayImpl.java

@@ -19,6 +19,7 @@ import com.hfln.portal.common.dto.data.share.ShareDto;
 import com.hfln.portal.common.request.device.DeviceBandingParams;
 import com.hfln.portal.common.request.device.DeviceListParams;
 import com.hfln.portal.common.request.device.DeviceLocationParams;
+import com.hfln.portal.common.request.device.DeviceTransferParams;
 import com.hfln.portal.common.request.event.EventListParams;
 import com.hfln.portal.common.request.share.ShareConfirmParam;
 import com.hfln.portal.common.request.share.ShareParam;
@@ -203,20 +204,22 @@ public class DeviceGatewayImpl implements DeviceGateway {
 
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Boolean deviceBind(DeviceBandingParams request) {
         // 1.参数范围大小校验
-        if (request.getXxEnd().compareTo(request.getXxStart()) <= 0) {
-            throw new BizException(ErrorEnum.DEVICE_PARAM_ERROR.getErrorCode(), ErrorEnum.DEVICE_PARAM_ERROR.getErrorMessage());
-        }
-        if (request.getYyEnd().compareTo(request.getYyStart()) <= 0) {
+        if (request.getXxEnd().compareTo(request.getXxStart()) <= 0 || request.getYyEnd().compareTo(request.getYyStart()) <= 0) {
             throw new BizException(ErrorEnum.DEVICE_PARAM_ERROR.getErrorCode(), ErrorEnum.DEVICE_PARAM_ERROR.getErrorMessage());
         }
-        if (!Arrays.asList(
+
+        List<BigDecimal> validAngles = Arrays.asList(
                 BigDecimal.ZERO,
-                new BigDecimal("90"),
-                new BigDecimal("180"),
-                new BigDecimal("270")
-        ).contains(request.getNorthAngle())) {
+                BigDecimal.valueOf(90),
+                BigDecimal.valueOf(180),
+                BigDecimal.valueOf(270)
+        );
+        boolean valid = validAngles.stream()
+                .anyMatch(angle -> angle.compareTo(request.getNorthAngle()) == 0);
+        if (!valid) {
             throw new BizException(ErrorEnum.DEVICE_PARAM_ERROR.getErrorCode(), ErrorEnum.DEVICE_PARAM_ERROR.getErrorMessage());
         }
 
@@ -234,26 +237,45 @@ public class DeviceGatewayImpl implements DeviceGateway {
 
         // 3. 更新设备信息
         boolean updateResult = devInfoService.updateDevice(devInfo.getClientId(), request);
+        if (!updateResult){
+            log.warn("更新设备信息失败,clientId:{}", devInfo.getClientId());
+        }
 
         //4.通过MQTT把信息参数发送到设备
-        String topic = TopicConstants.TOPIC_SET_DEVICE_PARAM;
-        JSONObject msg = new JSONObject();
-        msg.put("dev_id", devInfo.getDevId());
-        msg.put("mounting_plain", request.getMountPlain());
-        msg.put("height", request.getHeight());
-
-        JSONObject area = new JSONObject();
-        area.put("start_x", request.getXxStart());
-        area.put("stop_x", request.getXxEnd());
-        area.put("start_y", request.getYyStart());
-        area.put("stop_y", request.getYyEnd());
-        area.put("start_z", request.getZzStart());
-        area.put("stop_z", request.getZzEnd());
-        msg.put("area", area);
+        String topic = String.format(TopicConstants.TOPIC_SET_DEVICE_PARAM, devInfo.getClientId());
+
+        //构建检测区域
+        Map<String, BigDecimal> base = new HashMap<>();
+        base.put("x_cm_start", request.getXxStart());
+        base.put("x_cm_stop", request.getXxEnd());
+        base.put("y_cm_start", request.getYyStart());
+        base.put("y_cm_stop", request.getYyEnd());
+        base.put("z_cm_start", request.getZzStart());
+        base.put("z_cm_stop", request.getZzEnd());
+        Map<String, Map<String, BigDecimal>> baseParent = new HashMap<>();
+        baseParent.put("base", base);
+
+        //构建安装高度
+        Map<String, BigDecimal> z_cm = new HashMap<>();
+        z_cm.put("z_cm", request.getHeight());
+
+        //构建呼吸灯
+        int indicatorLed = 0;
+        if (request.getStatusLight() == 0){
+            indicatorLed = 1;
+        }
+
+        //发送最终消息
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("ext_region", baseParent);
+        jsonObject.put("sensor_location", z_cm);
+        jsonObject.put("indicator_led", indicatorLed);
+
         try {
-            mqttClient.sendMessage(topic, msg.toJSONString());
+            mqttClient.sendMessage(topic, jsonObject.toJSONString());
         } catch (Exception e) {
             log.error("发送设备参数到MQTT失败", e);
+            throw new BizException(ErrorEnum.MQTT_SEND_ERROR.getErrorCode(), ErrorEnum.MQTT_SEND_ERROR.getErrorMessage());
         }
 
         return updateResult;
@@ -416,6 +438,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
     }
 
     @Override
+    @Transactional
     public Boolean statusLight(Long devId, Integer statusLight) {
         //1.校验设备是否存在
         DevInfo devInfo = devInfoService.getById(devId);
@@ -436,10 +459,14 @@ public class DeviceGatewayImpl implements DeviceGateway {
         );
 
         //4.通过MQTT把信息参数发送到设备
-        String topic = TopicConstants.TOPIC_SET_DEVICE_PARAM;
+        String topic = String.format(TopicConstants.TOPIC_SET_DEVICE_PARAM, devInfo.getClientId());
         JSONObject msg = new JSONObject();
-        msg.put("dev_id", devId);
-        msg.put("status_light", statusLight);
+        int indicatorLed = 0;
+
+        if (statusLight == 0){
+            indicatorLed = 1;
+        }
+        msg.put("indicator_led", indicatorLed);
         try {
             mqttClient.sendMessage(topic, msg.toJSONString());
         } catch (Exception e) {
@@ -809,33 +836,32 @@ public class DeviceGatewayImpl implements DeviceGateway {
     }
 
     @Override
-    public Boolean deviceTransfer(String phone, String devId){
-        //1.检查手机号是非为空
-        if (StringUtils.isEmpty(phone)){
-            throw new BizException(ErrorEnum.PHONE_IS_NULL.getErrorCode(),ErrorEnum.PHONE_IS_NULL.getErrorMessage());
-        }
+    public Boolean deviceTransfer(DeviceTransferParams param){
 
-        //2.根据手机号查询user_info看是否注册
-        UserInfo userInfo = userService.queryByPhone(phone);
-        if (userInfo == null){
-            throw new BizException(ErrorEnum.USER_IS_NOT_EXIST.getErrorCode(), ErrorEnum.USER_IS_NOT_EXIST.getErrorMessage());
-        }
-
-        //3.检查设备是否存在
+        //1. 查询设备信息并校验是否存在
         DevInfo devInfo = devInfoService.getOne(
                 Wrappers.<DevInfo>lambdaQuery()
-                        .eq(DevInfo::getDevId, devId)
+                        .eq(DevInfo::getDevId, param.getDevId())
                         .eq(DevInfo::getIsDeleted,  BasePO.DeleteFlag.NOT_DELETED)
         );
         if (devInfo == null) {
             throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
         }
 
-        //4.更新设备拥有者
-        devInfo.setUserId(userInfo.getUserId());
-        boolean updateResult = devInfoService.updateById(devInfo);
+        //2.校验用户权限
+        if(!Objects.equals(param.getUserId(), devInfo.getUserId())){
+            throw new BizException(ErrorEnum.USER_NO_PERMISSION.getErrorCode(), ErrorEnum.USER_NO_PERMISSION.getErrorMessage());
+        }
 
-        return updateResult;
+        //3.根据手机号查询转移对象是否注册
+        UserInfo userInfo = userService.queryByPhone(param.getPhone());
+        if (userInfo == null){
+            throw new BizException(ErrorEnum.USER_IS_NOT_EXIST.getErrorCode(), ErrorEnum.USER_IS_NOT_EXIST.getErrorMessage());
+        }
+
+        //4.转移设备归属
+        devInfo.setUserId(userInfo.getUserId());
+        return devInfoService.updateById(devInfo);
     }
 
 

+ 1 - 0
portal-service-server/src/main/resources/bootstrap-dev.yml

@@ -96,6 +96,7 @@ sa-token:
   admin:
     timeout: 36000
     active-timeout: 1800
+  dynamic-active-timeout: true
 
 # mqtt相关
 mqtt: