Browse Source

feat(device):优化设备绑定与更新逻辑并调整MQTT消息质量等级

移除了 DeviceBandingParams 中冗余的字段校验注解,统一使用 javax.validation.constraints。
删除了 DeviceGatewayImpl 中对参数范围和北向夹角的校验逻辑,交由前端或其他服务处理。
简化设备绑定流程,去除分页相关无用代码,优化日志记录方式。
在 DevInfoService 及其实现中,将更新设备方法的参数类型从 DeviceBandingParams 改为 UpdateDeviceParams,使职责更清晰。同时调整 MQTT 发送消息的默认 QoS 级别为2,提升消息传递可靠性。
新增绑定设备操作日志类型 BIND_DEVICE。
hxd 4 weeks ago
parent
commit
8d450a2684

+ 4 - 47
portal-service-common/src/main/java/com/hfln/portal/common/request/device/DeviceBandingParams.java

@@ -5,8 +5,10 @@ import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 
-import javax.validation.constraints.*;
-import java.math.BigDecimal;
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotEmpty;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
 
 @EqualsAndHashCode(callSuper = true)
 @Data
@@ -25,19 +27,6 @@ public class DeviceBandingParams extends BaseVO {
     @Size(max = 10, message = "设备名称不能超过10个字符")
     private String devName;
 
-    @Schema(description = "安装高度: 200 - 370 cm 之内")
-//    @DecimalMax(value = "370", message = "height不能大于370")
-//    @DecimalMin(value = "200", message = "height不能小于200")
-    private BigDecimal height;
-
-    @Schema(description = "WIFI名称")
-    private String wifiName;
-
-    @Schema(description = "WIFI密码")
-    private String wifiPassword;
-
-    @Schema(description = "北向夹角 允许传值: 0,90,180,270")
-    private BigDecimal northAngle;
 
     @Schema(description = "安装方式:Wall-墙装,Ceiling-顶装")
     @NotBlank(message = "安装方式不能为空")
@@ -46,36 +35,4 @@ public class DeviceBandingParams extends BaseVO {
     @Schema(description = "安装位置:Toilet-卫生间,Bedroom-卧室,LivingRoom-客厅,Restaurant-餐厅")
     private String installPosition;
 
-    @Schema(description = "雷达监测范围x开始,范围:-200-200 cm之内")
-//    @DecimalMin(value = "-200", message = "xxStart不能小于-200")
-//    @DecimalMax(value = "200", message = "xxStart不能大于200")
-    private BigDecimal xxStart;
-
-    @Schema(description = "雷达监测范围x结束,范围:-200-200 cm之内,且xxEnd > xxStart")
-//    @DecimalMin(value = "-200", message = "xxEnd不能小于-200")
-//    @DecimalMax(value = "200", message = "xxEnd不能大于200")
-    private BigDecimal xxEnd;
-
-
-    @Schema(description = "雷达监测范围y开始,范围:-250-250 cm之内")
-//    @DecimalMin(value = "-250", message = "yyStart不能小于-250")
-//    @DecimalMax(value = "250", message = "yyStart不能大于250")
-    private BigDecimal yyStart;
-
-    @Schema(description = "雷达监测范围y结束,范围:-250-250 cm之内,且yyEnd > yyStart")
-//    @DecimalMin(value = "-250", message = "yyEnd不能小于-250")
-//    @DecimalMax(value = "250", message = "yyEnd不能大于250")
-    private BigDecimal yyEnd;
-
-
-    @Schema(description = "雷达监测范围z开始,范围:0-5 cm之内")
-//    @DecimalMin(value = "0", message = "zzStart不能小于0")
-//    @DecimalMax(value = "5", message = "zzStart不能大于5")
-    private BigDecimal zzStart;
-
-
-    @Schema(description = "雷达监测范围z结束,范围:200-300 cm之内")
-//    @DecimalMin(value = "200", message = "zzEnd不能小于200")
-//    @DecimalMax(value = "300", message = "zzEnd不能大于300")
-    private BigDecimal zzEnd;
 }

+ 0 - 5
portal-service-common/src/main/java/com/hfln/portal/common/request/device/UpdateDeviceParams.java

@@ -32,11 +32,6 @@ public class UpdateDeviceParams extends BaseVO {
 //    @DecimalMin(value = "200", message = "height不能小于200")
     private BigDecimal height;
 
-    @Schema(description = "WIFI名称")
-    private String wifiName;
-
-    @Schema(description = "WIFI密码")
-    private String wifiPassword;
 
     @Schema(description = "北向夹角 允许传值: 0,90,180,270")
     private BigDecimal northAngle;

+ 1 - 0
portal-service-domain/src/main/java/com/hfln/portal/domain/customer/OprLogType.java

@@ -26,6 +26,7 @@ public enum OprLogType {
     UPDATE_DEVICE("update_device","编辑设备"),
     UNBLIND_DEVICE("unbind_device", "解绑设备"),
     DEVICE_TRANSFER("transfer_device", "设备转移"),
+    BIND_DEVICE("bind_device", "绑定设备"),
 
     USER_LOGOFF("logoff_user", "用户注销"),
 

+ 11 - 65
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/DeviceGatewayImpl.java

@@ -7,12 +7,10 @@ import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.hfln.portal.common.constant.UserConstants;
 import com.hfln.portal.common.constant.mqtt.topic.TopicConstants;
 import com.hfln.portal.common.constant.redis.RedisCacheConstant;
 import com.hfln.portal.common.dto.data.device.DeviceDTO;
-import com.hfln.portal.common.dto.data.event.EventListDTO;
 import com.hfln.portal.common.dto.data.home.HomeInfoDTO;
 import com.hfln.portal.common.dto.data.oss.OssFileDTO;
 import com.hfln.portal.common.dto.data.room.RegionDTO;
@@ -26,7 +24,6 @@ import com.hfln.portal.common.request.event.AlarmPlanDelReq;
 import com.hfln.portal.common.request.room.RoomParam;
 import com.hfln.portal.common.request.room.SubRegionInfo;
 import com.hfln.portal.common.request.share.*;
-import com.hfln.portal.common.vo.PageRecord;
 import com.hfln.portal.domain.customer.OprLogType;
 import com.hfln.portal.domain.customer.OssBusiType;
 import com.hfln.portal.domain.customer.util.DevPosFixUtil;
@@ -280,24 +277,8 @@ public class DeviceGatewayImpl implements DeviceGateway {
     @Override
     @Transactional(rollbackFor = Exception.class)
     public DeviceDTO deviceBind(DeviceBandingParams request) {
-        // 1.参数范围大小校验
-        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());
-        }
-
-        List<BigDecimal> validAngles = Arrays.asList(
-                BigDecimal.ZERO,
-                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());
-        }
 
-        // 2. 检查设备是否存在
+        // 1. 检查设备是否存在
         DevInfo devInfo = devInfoService.getOne(
                 Wrappers.<DevInfo>lambdaQuery()
                         .eq(DevInfo::getClientId, request.getClientId())
@@ -309,14 +290,15 @@ public class DeviceGatewayImpl implements DeviceGateway {
             throw new BizException(ErrorEnum.DEVICE_IS_BINDING.getErrorCode(), ErrorEnum.DEVICE_IS_BINDING.getErrorMessage());
         }
 
-        // 3. 更新设备信息到数据库
-        boolean updateResult = devInfoService.updateDevice(devInfo.getClientId(), request);
-        if (!updateResult) {
-            log.warn("更新设备信息失败,clientId:{}", devInfo.getClientId());
-        }
+        // 2. 绑定设备信息到写入数据库
+        devInfo.setUserId(request.getUserId());
+        devInfo.setDevName(request.getDevName());
+        devInfo.setMountPlain(request.getMountPlain());
+        devInfo.setInstallPosition(request.getInstallPosition());
+        devInfo.setActiveTime(LocalDateTime.now());
+        devInfoService.updateById(devInfo);
 
-        //4.通过MQTT把信息参数发送到设备
-        mqttSend.sendDeviceParamsToMqtt(devInfo.getClientId(), request);
+        logService.saveLog(OprLogType.BIND_DEVICE.getCode(), String.format("用户 %d 绑定设备 %s", request.getUserId(), request.getClientId()));
 
         //5.返回绑定结果和设备主键id
         DeviceDTO result = new DeviceDTO();
@@ -344,28 +326,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
     }
 
 
-    // 填充PageRecord
-    private PageRecord<EventListDTO> getEventListDTOPageRecord(Page<EventList> eventPage, List<EventListDTO> targets) {
-        int totalPages = (int) Math.ceil((double) eventPage.getTotal() / eventPage.getSize());
-        PageRecord<EventListDTO> pageRecord = new PageRecord<>();
-        pageRecord.setRows(targets);
-        pageRecord.setTotal(eventPage.getTotal());
-        pageRecord.setPageNum((int) eventPage.getCurrent());
-        pageRecord.setPageSize((int) eventPage.getSize());
-        pageRecord.setOutTotalPageNum(eventPage.getCurrent() > totalPages);
-        pageRecord.setTotalPageNum(totalPages);
-        return pageRecord;
-    }
 
-    // 实体转换方法
-    private List<EventListDTO> convertToTargetVO(List<EventList> records) {
-        return records.stream().map(event -> {
-            EventListDTO vo = new EventListDTO();
-            // 使用BeanUtils简化属性拷贝
-            BeanUtils.copyProperties(event, vo);
-            return vo;
-        }).collect(Collectors.toList());
-    }
 
 
     @Override
@@ -482,29 +443,14 @@ public class DeviceGatewayImpl implements DeviceGateway {
         }
 
         //3.更新数据
-        devInfo.setDevName(params.getDevName());
-        devInfo.setHeight(params.getHeight());
-        devInfo.setWifiName(params.getWifiName());
-        devInfo.setWifiPassword(params.getWifiPassword());
-        devInfo.setNorthAngle(params.getNorthAngle());
-        devInfo.setMountPlain(params.getMountPlain());
-        devInfo.setInstallPosition(params.getInstallPosition());
-        devInfo.setXxStart(params.getXxStart());
-        devInfo.setXxEnd(params.getXxEnd());
-        devInfo.setYyStart(params.getYyStart());
-        devInfo.setYyEnd(params.getYyEnd());
-        devInfo.setZzStart(params.getZzStart());
-        devInfo.setZzEnd(params.getZzEnd());
-        devInfo.setLength(params.getXxEnd().subtract(params.getXxStart()));
-        devInfo.setWidth(params.getYyEnd().subtract(params.getYyStart()));
 
-        boolean updated = devInfoService.updateById(devInfo);
+        boolean updated = devInfoService.updateDevice(devInfo.getClientId(), params);
         if (!updated) {
             log.warn("更新设备信息失败,clientId:{}", devInfo.getClientId());
             throw new BizException(ErrorEnum.DEVICE_UPDATE_FAIL.getErrorCode(), ErrorEnum.DEVICE_UPDATE_FAIL.getErrorMessage());
         }
 
-        logService.saveLog(OprLogType.UPDATE_DEVICE.getCode(), devInfo);
+        logService.saveLog(OprLogType.UPDATE_DEVICE.getCode(), params);
 
         //4.通过MQTT把信息参数发送到设备
         mqttSend.sendDeviceParamsToMqtt(devInfo.getClientId(), params);

+ 2 - 2
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/mqtt/MqttClient.java

@@ -25,10 +25,10 @@ public class MqttClient {
 
     /**
      * 发送消息
-     * 默认QoS 0,适合大多数业务场景
+     * 默认QoS 2,适合大多数业务场景
      */
     public void sendMessage(String topic, String payload) {
-        sendMessage(topic, payload, 0, false);
+        sendMessage(topic, payload, 2, false);
     }
 
     /**

+ 2 - 2
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/DevInfoService.java

@@ -2,8 +2,8 @@ package com.hfln.portal.infrastructure.service;
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.IService;
-import com.hfln.portal.common.request.device.DeviceBandingParams;
 import com.hfln.portal.common.request.device.DeviceListQueryReq;
+import com.hfln.portal.common.request.device.UpdateDeviceParams;
 import com.hfln.portal.infrastructure.po.DevInfo;
 
 import java.math.BigDecimal;
@@ -20,7 +20,7 @@ public interface DevInfoService extends IService<DevInfo> {
 
     List<DevInfo> queryByIds(Collection<String> clientIds);
 
-    Boolean updateDevice(Object identifier, DeviceBandingParams request);
+    Boolean updateDevice(Object identifier, UpdateDeviceParams params);
 
     DevInfo queryByClientId(String clientId);
 

+ 18 - 30
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/DevInfoServiceImpl.java

@@ -5,8 +5,8 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.hfln.portal.common.request.device.DeviceBandingParams;
 import com.hfln.portal.common.request.device.DeviceListQueryReq;
+import com.hfln.portal.common.request.device.UpdateDeviceParams;
 import com.hfln.portal.domain.customer.util.UpdateWrapperBuilder;
 import com.hfln.portal.infrastructure.mapper.DevInfoMapper;
 import com.hfln.portal.infrastructure.mapper.GroupDevMapMapper;
@@ -94,46 +94,34 @@ public class DevInfoServiceImpl extends ServiceImpl<DevInfoMapper, DevInfo> impl
     }
 
     @Override
-    public Boolean updateDevice(Object identifier, DeviceBandingParams request) {
+    public Boolean updateDevice(Object identifier, UpdateDeviceParams params) {
         // 参数校验
         if (identifier == null) {
             throw new IllegalArgumentException("设备标识不能为空");
         }
 
-        //获取数据库中已有的设备信息
-        DevInfo devInfo = this.getOne(
-                Wrappers.<DevInfo>lambdaQuery()
-                        .eq(DevInfo::getClientId, request.getClientId()));
-
         // 构建更新条件
         UpdateWrapperBuilder<DevInfo> builder = UpdateWrapperBuilder.<DevInfo>create()
-                .setIfNotNull(DevInfo::getClientId, request.getClientId())
-                .setIfNotNull(DevInfo::getUserId, request.getUserId())
-                .setIfNotBlank(DevInfo::getDevName, request.getDevName())
+                .setIfNotNull(DevInfo::getClientId, params.getClientId())
+                .setIfNotNull(DevInfo::getUserId, params.getUserId())
+                .setIfNotBlank(DevInfo::getDevName, params.getDevName())
                 //监测监测X长度: xxEnd-xxStart
-                .setIfNotNull(DevInfo::getLength, (request.getXxEnd().subtract(request.getXxStart())))
+                .setIfNotNull(DevInfo::getLength, (params.getXxEnd().subtract(params.getXxStart())))
                 //监测监测Y长度: yyEnd-yyStart
-                .setIfNotNull(DevInfo::getWidth, (request.getYyEnd().subtract(request.getYyStart())))
-                .setIfNotNull(DevInfo::getHeight, request.getHeight())
-                .setIfNotNull(DevInfo::getWifiName, request.getWifiName())
-                .setIfNotNull(DevInfo::getWifiPassword, request.getWifiPassword())
-                .setIfNotNull(DevInfo::getNorthAngle, request.getNorthAngle())
-                .setIfNotNull(DevInfo::getMountPlain, request.getMountPlain())
-                .setIfNotNull(DevInfo::getInstallPosition, request.getInstallPosition())
-                .setIfNotNull(DevInfo::getXxStart, request.getXxStart())
-                .setIfNotNull(DevInfo::getXxEnd, request.getXxEnd())
-                .setIfNotNull(DevInfo::getYyStart, request.getYyStart())
-                .setIfNotNull(DevInfo::getYyEnd, request.getYyEnd())
-                .setIfNotNull(DevInfo::getZzStart, request.getZzStart())
-                .setIfNotNull(DevInfo::getZzEnd, request.getZzEnd())
+                .setIfNotNull(DevInfo::getWidth, (params.getYyEnd().subtract(params.getYyStart())))
+                .setIfNotNull(DevInfo::getHeight, params.getHeight())
+                .setIfNotNull(DevInfo::getNorthAngle, params.getNorthAngle())
+                .setIfNotNull(DevInfo::getMountPlain, params.getMountPlain())
+                .setIfNotNull(DevInfo::getInstallPosition, params.getInstallPosition())
+                .setIfNotNull(DevInfo::getXxStart, params.getXxStart())
+                .setIfNotNull(DevInfo::getXxEnd, params.getXxEnd())
+                .setIfNotNull(DevInfo::getYyStart, params.getYyStart())
+                .setIfNotNull(DevInfo::getYyEnd, params.getYyEnd())
+                .setIfNotNull(DevInfo::getZzStart, params.getZzStart())
+                .setIfNotNull(DevInfo::getZzEnd, params.getZzEnd())
                 .setIfNotNull(DevInfo::getFallingConfirm, 53)
                 // 无条件更新的字段
-                .set(DevInfo::getUpdateTime, LocalDateTime.now())
-                .set(DevInfo::getOnline, 1);
-        //激活时间的插入
-        if (devInfo.getActiveTime() == null) {
-            builder.set(DevInfo::getActiveTime, LocalDateTime.now());
-        }
+                .set(DevInfo::getUpdateTime, LocalDateTime.now());
 
         // 根据identifier类型决定使用哪个条件
         if (identifier instanceof Long) {