hxd 2 månader sedan
förälder
incheckning
6d5699594b

+ 13 - 59
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/DeviceGatewayImpl.java

@@ -31,6 +31,7 @@ import com.hfln.portal.domain.customer.util.WxOfficeAccountClient;
 import com.hfln.portal.domain.exception.ErrorEnum;
 import com.hfln.portal.domain.gateway.DeviceGateway;
 import com.hfln.portal.infrastructure.mqtt.MqttClient;
+import com.hfln.portal.infrastructure.mqtt.MqttSubHandle;
 import com.hfln.portal.infrastructure.oss.OssClient;
 import com.hfln.portal.infrastructure.oss.OssUtils;
 import com.hfln.portal.infrastructure.po.*;
@@ -99,6 +100,9 @@ public class DeviceGatewayImpl implements DeviceGateway {
     @Autowired
     private TblTenantService tblTenantService;
 
+    @Autowired
+    private MqttSubHandle mqttSubHandle;
+
 
     @Override
     public HomeInfoDTO queryHomeInfo(Long userId) {
@@ -575,72 +579,22 @@ public class DeviceGatewayImpl implements DeviceGateway {
         // 更新dev_room表
         devRoomService.saveOrUpdate(param);
 
-        //发送家具信息到算法
-        String topic = String.format(TopicConstants.TOPIC_DAS_SET_FURNITURE_PARAM, devInfo.getClientId());
-        JSONObject msg = new JSONObject();
-        msg.put("dev_id", devInfo.getClientId());
-        if (mqttClient != null) {
-            try{
-            mqttClient.sendMessage(topic, msg.toJSONString());
-            log.info("Message sent successfully to topic: {}", topic); // 记录消息发送成功的日志
-            } catch (Exception e){
-                log.error("Failed to send message to topic: {}", topic, e);  // 记录发送失败的错误日志
-            }
-        } else {
-            log.warn("MQTT client is not available, message not sent to topic: {}", topic);  // 记录客户端不可用的警告日志
+        // 发送家具信息到算法
+        if (param.getFurnitures() != null && !param.getFurnitures().isEmpty()) {
+            mqttSubHandle.sendMqttMessage(TopicConstants.TOPIC_DAS_SET_FURNITURE_PARAM, devInfo.getClientId());
         }
 
-        //发送屏蔽子区域信息到算法
-        String subTopic = String.format(TopicConstants.TOPIC_DAS_SET_SUB_REGION_PARAM, devInfo.getClientId());
-        JSONObject subRegionMsg = new JSONObject();
-        msg.put("dev_id", devInfo.getClientId());
-        if (mqttClient != null) {
-            try{
-                mqttClient.sendMessage(subTopic, subRegionMsg.toJSONString());
-                log.info("Message sent successfully to topic: {}", subTopic); // 记录消息发送成功的日志
-            } catch (Exception e){
-                log.error("Failed to send message to topic: {}", subTopic, e);  // 记录发送失败的错误日志
-            }
-        } else {
-            log.warn("MQTT client is not available, message not sent to topic: {}", subTopic);  // 记录客户端不可用的警告日志
+        // 发送屏蔽子区域信息到算法
+        if (subRegions != null && !subRegions.isEmpty()) {
+            mqttSubHandle.sendMqttMessage(TopicConstants.TOPIC_DAS_SET_SUB_REGION_PARAM, devInfo.getClientId());
         }
 
-        //发送子区域信息到设备
+        // 发送子区域信息到设备
         if (subRegions != null && !subRegions.isEmpty()) {
-            JSONArray extSubRegions = new JSONArray();
-            for (SubRegionInfo region : subRegions) {
-                JSONObject base = new JSONObject();
-                base.put("x_cm_start", region.getStartXx());
-                base.put("x_cm_stop", region.getStopXx());
-                base.put("y_cm_start", region.getStartYy());
-                base.put("y_cm_stop", region.getStopYy());
-                base.put("presence_enter_duration", region.getPresenceEnterDuration());
-                base.put("presence_exit_duration", region.getPresenceExitDuration());
-                base.put("track_presence", region.getTrackPresence());
-                base.put("exclude_falling", region.getExcludeFalling());
-
-                JSONObject regionJson = new JSONObject();
-                regionJson.put("base", base);
-                regionJson.put("z_cm_start", region.getStartZz());
-                regionJson.put("z_cm_stop", region.getStopZz());
-                regionJson.put("is_low_snr", region.getIsLowSnr());
-                regionJson.put("is_door", region.getIsDoor());
-
-                extSubRegions.add(regionJson);
-            }
-            JSONObject infoMsg = new JSONObject();
-            infoMsg.put("ext_sub_regions", extSubRegions);
-
-            String topicDev = String.format(TopicConstants.TOPIC_SET_DEVICE_PARAM, devInfo.getClientId());
-            try {
-                mqttClient.sendMessage(topicDev, infoMsg.toJSONString());
-                log.info("SubRegions message sent successfully to topic: {}", topicDev);
-            } catch (Exception e) {
-                log.error("Failed to send subRegions message to topic: {}", topicDev, e);
-            }
+            mqttSubHandle.sendSubRegionToDevice(devInfo.getClientId(), subRegions);
         }
 
-        return null;
+        return true;
     }
 
     @Override

+ 60 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/mqtt/MqttSubHandle.java

@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.hfln.portal.common.constant.mqtt.topic.TopicConstants;
 import com.hfln.portal.common.constant.redis.RedisCacheConstant;
 import com.hfln.portal.common.dto.data.user.SendMsgUserDto;
+import com.hfln.portal.common.request.room.SubRegionInfo;
 import com.hfln.portal.domain.customer.util.MsgClient;
 import com.hfln.portal.domain.customer.util.WxOfficeAccountClient;
 import com.hfln.portal.infrastructure.po.*;
@@ -519,4 +520,63 @@ public class MqttSubHandle {
             redisUtil.sRemove(RedisCacheConstant.MQTT_CLIENT_USERID, deviceType + "_" + userId);
         }
     }
+
+
+    /**
+     * 调用房间家具保存接口的时候如果有更新发送MQTT消息通知算法
+     */
+    public void sendMqttMessage(String topicFormat, String devClientId) {
+        String topic = String.format(topicFormat, devClientId);
+        JSONObject msg = new JSONObject();
+        msg.put("dev_id", devClientId);
+
+        if (mqttClient != null) {
+            try {
+                mqttClient.sendMessage(topic, msg.toJSONString());
+                log.info("Message sent successfully to topic: {}", topic);
+            } catch (Exception e) {
+                log.error("Failed to send message to topic: {}", topic, e);
+            }
+        } else {
+            log.warn("MQTT client is not available, message not sent to topic: {}", topic);
+        }
+    }
+
+    /**
+     * 调用房间家具保存接口的时候如果子区域有更新发送MQTT消息通知设备
+     */
+    public void sendSubRegionToDevice(String devClientId, List<SubRegionInfo> subRegions) {
+        JSONArray extSubRegions = new JSONArray();
+        for (SubRegionInfo region : subRegions) {
+            JSONObject base = new JSONObject();
+            base.put("x_cm_start", region.getStartXx());
+            base.put("x_cm_stop", region.getStopXx());
+            base.put("y_cm_start", region.getStartYy());
+            base.put("y_cm_stop", region.getStopYy());
+            base.put("presence_enter_duration", region.getPresenceEnterDuration());
+            base.put("presence_exit_duration", region.getPresenceExitDuration());
+            base.put("track_presence", region.getTrackPresence());
+            base.put("exclude_falling", region.getExcludeFalling());
+
+            JSONObject regionJson = new JSONObject();
+            regionJson.put("base", base);
+            regionJson.put("z_cm_start", region.getStartZz());
+            regionJson.put("z_cm_stop", region.getStopZz());
+            regionJson.put("is_low_snr", region.getIsLowSnr());
+            regionJson.put("is_door", region.getIsDoor());
+
+            extSubRegions.add(regionJson);
+        }
+
+        JSONObject infoMsg = new JSONObject();
+        infoMsg.put("ext_sub_regions", extSubRegions);
+
+        String topicDev = String.format(TopicConstants.TOPIC_SET_DEVICE_PARAM, devClientId);
+        try {
+            mqttClient.sendMessage(topicDev, infoMsg.toJSONString());
+            log.info("SubRegions message sent successfully to topic: {}", topicDev);
+        } catch (Exception e) {
+            log.error("Failed to send subRegions message to topic: {}", topicDev, e);
+        }
+    }
 }

+ 11 - 7
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/DevRoomServiceImpl.java

@@ -10,7 +10,6 @@ import com.hfln.portal.infrastructure.po.BasePO;
 import com.hfln.portal.infrastructure.po.DevRoom;
 import com.hfln.portal.infrastructure.service.DevRoomService;
 import org.springframework.stereotype.Service;
-import org.springframework.util.CollectionUtils;
 
 @Service
 public class DevRoomServiceImpl extends ServiceImpl<DevRoomMapper, DevRoom> implements DevRoomService {
@@ -37,19 +36,24 @@ public class DevRoomServiceImpl extends ServiceImpl<DevRoomMapper, DevRoom> impl
             this.baseMapper.insert(room);
         }
 
+        // 构建更新条件
         UpdateWrapperBuilder<DevRoom> builder = new UpdateWrapperBuilder<>();
-        if (CollectionUtils.isEmpty(param.getFurnitures())) {
-            builder.set(DevRoom::getFurnitures, null);
-        } else {
+
+        // 处理 furnitures 字段,只有在非空时才更新
+        if (param.getFurnitures() != null) {
             builder.set(DevRoom::getFurnitures, JSON.toJSONString(param.getFurnitures()));
         }
-        if (CollectionUtils.isEmpty(param.getSubRegions())) {
-            builder.set(DevRoom::getSubRegions, null);
-        } else {
+
+        // 处理 subRegions 字段,只有在非空时才更新
+        if (param.getSubRegions() != null) {
             builder.set(DevRoom::getSubRegions, JSON.toJSONString(param.getSubRegions()));
         }
+
+        // 设置更新条件
         builder.eq(DevRoom::getDevId, param.getDevId());
         builder.eq(DevRoom::getIsDeleted, BasePO.DeleteFlag.NOT_DELETED);
+
+        // 执行更新操作
         this.baseMapper.update(null, builder.build());
     }
 }