Browse Source

add
mqtt信息发送

lifang 3 weeks ago
parent
commit
3a4efaa675

+ 6 - 6
nb-admin/src/main/resources/application-dev.yml

@@ -109,12 +109,12 @@ spring:
               user: root
               password: 123456
               validationQuery: SELECT 1
-  rabbitmq:
-    host: 192.168.100.32
-    port: 5672
-    username: guest
-    password: guest
-    virtual-host: /
+#  rabbitmq:
+#    host: 192.168.100.32
+#    port: 5672
+#    username: guest
+#    password: guest
+#    virtual-host: /
   # redis 配置
   redis:
     # 地址

+ 8 - 1
nb-common/config-common/src/main/java/com/nb/common/config/WebAppMvcConfig.java

@@ -16,6 +16,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
 import org.springframework.context.annotation.*;
 import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.http.converter.StringHttpMessageConverter;
 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@@ -23,6 +24,7 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
@@ -51,6 +53,9 @@ public class WebAppMvcConfig implements WebMvcConfigurer {
 
     @Override
     public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
+        // 添加UTF-8编码的StringHttpMessageConverter作为第一个转换器
+        converters.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
+        
         MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
         // 时间格式化
         SerializerProvider serializerProvider = objectMapper.getSerializerProvider();
@@ -62,6 +67,8 @@ public class WebAppMvcConfig implements WebMvcConfigurer {
         });
         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
         objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
+        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
+        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
 
         SimpleModule booleanSimpleModule = new SimpleModule();
         booleanSimpleModule.addSerializer(Boolean.class, new BooleanToIntegerSerializer());
@@ -107,7 +114,7 @@ public class WebAppMvcConfig implements WebMvcConfigurer {
         objectMapper.registerModule(booleanSimpleModule);
         // 设置格式化内容
         converter.setObjectMapper(objectMapper);
-        converters.add(0, converter);
+        converters.add(1, converter);
     }
 
     @Override

+ 71 - 1
nb-core/src/main/java/com/nb/core/controller/MqttMessageController.java

@@ -8,6 +8,8 @@ import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.*;
 
+import java.nio.charset.StandardCharsets;
+
 /**
  * MQTT消息控制器
  * 提供REST API用于测试和管理MQTT消息处理
@@ -30,7 +32,7 @@ public class MqttMessageController {
      * @param message MQTT消息
      * @return 处理结果
      */
-    @PostMapping("/process")
+    @PostMapping(value = "/process", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
     public R<String> processMessage(@RequestBody MqttMessage message) {
         try {
             //订阅和发布只能同时存在一个
@@ -42,10 +44,78 @@ public class MqttMessageController {
             if(MqttClientUtil.CLIENT_ID.equals(clientId)){
                 return R.fail("本节点发出的数据不予处理");
             }
+            
+            // 打印评价人和评价信息日志
+            logEvaluationInfo(message);
+            
             messageProcessService.processMessage(message);
             return R.success("消息处理成功");
         } catch (Exception e) {
+            log.error("处理MQTT消息失败", e);
             return R.fail("处理MQTT消息失败"+ e.getMessage());
         }
     }
+    
+    /**
+     * 打印评价人和评价信息
+     * @param message MQTT消息
+     */
+    private void logEvaluationInfo(MqttMessage message) {
+        try {
+            String payload = message.getPayload();
+            if (payload != null && payload.contains("evaluator")) {
+                // 解析评价人信息
+                String evaluator = extractFieldValue(payload, "evaluator");
+                String remark = extractFieldValue(payload, "remark");
+                
+                log.info("收到评价信息 - 评价人: {}, 评价内容: {}", evaluator, remark);
+            }
+        } catch (Exception e) {
+            log.warn("解析评价信息失败: {}", e.getMessage());
+        }
+    }
+    
+    /**
+     * 从JSON字符串中提取字段值
+     * @param json JSON字符串
+     * @param fieldName 字段名
+     * @return 字段值
+     */
+    private String extractFieldValue(String json, String fieldName) {
+        try {
+            String pattern = "\"" + fieldName + "\":";
+            int startIndex = json.indexOf(pattern);
+            if (startIndex == -1) {
+                return null;
+            }
+            
+            startIndex += pattern.length();
+            // 处理可能的空格
+            while (startIndex < json.length() && json.charAt(startIndex) == ' ') {
+                startIndex++;
+            }
+            
+            // 处理值被引号包围的情况
+            boolean quoted = startIndex < json.length() && json.charAt(startIndex) == '"';
+            if (quoted) {
+                startIndex++; // 跳过开始引号
+                int endIndex = json.indexOf('"', startIndex);
+                if (endIndex != -1) {
+                    return json.substring(startIndex, endIndex);
+                }
+            } else {
+                // 处理非引号包围的值(数字、布尔值等)
+                int endIndex = json.indexOf(',', startIndex);
+                if (endIndex == -1) {
+                    endIndex = json.indexOf('}', startIndex);
+                }
+                if (endIndex != -1) {
+                    return json.substring(startIndex, endIndex).trim();
+                }
+            }
+        } catch (Exception e) {
+            log.warn("提取字段 {} 失败: {}", fieldName, e.getMessage());
+        }
+        return null;
+    }
 }

+ 1 - 1
nb-service/iot-service/src/main/java/com/nb/aliyun/service/process/DeviceMsgHandler.java

@@ -70,7 +70,7 @@ public class DeviceMsgHandler {
         if(StrUtil.isNullOrUndefined(tenantId)){
             return;
         }
-        HospitalResult hospital = hospitalClient.findById(tenantId);
+//        HospitalResult hospital = hospitalClient.findById(tenantId);
 //        rabbitTemplate.convertAndSend(AliIotConstant.Exchange,hospital.getCode(),msg);
     }
 

+ 18 - 0
nb-service/web-service/src/main/java/com/nb/web/service/bus/listener/BusEvaluationMessageListener.java

@@ -57,6 +57,10 @@ public class BusEvaluationMessageListener extends AbstractMqttMessageHandler {
         try {
             // 解析消息内容为BusEvaluationEntity对象
             BusEvaluationEntity evaluation = JSON.parseObject(message.getPayload(), BusEvaluationEntity.class);
+            
+            // 打印评价人和评价信息日志
+            logEvaluationDetails(evaluation);
+            
             evaluation.setPatientId(null);
             evaluation.setInfusionId(null);
             evaluation.setClinicId(null);
@@ -66,6 +70,7 @@ public class BusEvaluationMessageListener extends AbstractMqttMessageHandler {
                     .eq(BusPatientEntity::getOriginCode, evaluation.getPatientCode())
                     .last("limit 1"));
             if(ObjectUtil.isNull(patient)){
+                log.warn("未找到对应患者信息,住院号: {}", evaluation.getPatientCode());
                 return;
             }
             evaluation.setPatientId(patient.getId());
@@ -84,4 +89,17 @@ public class BusEvaluationMessageListener extends AbstractMqttMessageHandler {
             log.error("处理评价信息时发生错误: ", e);
         }
     }
+    
+    /**
+     * 打印评价详细信息
+     * @param evaluation 评价实体
+     */
+    private void logEvaluationDetails(BusEvaluationEntity evaluation) {
+        if (evaluation != null) {
+            log.info("评价信息详情 - 评价人: {}, 评价时间: {}, 备注: {}", 
+                evaluation.getEvaluator(),
+                evaluation.getEvaluateTime(),
+                evaluation.getRemark());
+        }
+    }
 }

+ 5 - 5
nb-service/web-service/src/main/java/com/nb/web/service/bus/listener/DeviceInfoListener.java

@@ -112,10 +112,10 @@ public class DeviceInfoListener implements IIotMsgHandler {
      */
     private final HospitalManagerRegister hospitalManagerRegister;
 
-    /**
-     * 设备定制化服务,用于处理设备特定逻辑
-     */
-    private final DeviceCustomizedService deviceCustomizedService;
+//    /**
+//     * 设备定制化服务,用于处理设备特定逻辑
+//     */
+//    private final DeviceCustomizedService deviceCustomizedService;
     
     /**
      * 设备数据批量处理器,用于异步批量处理设备数据
@@ -156,7 +156,7 @@ public class DeviceInfoListener implements IIotMsgHandler {
             // 初始化设备数据
             deviceOperator.refreshHospitalCode(device.getUserId());
             device.setTenantId(deviceOperator.getTenantId());
-            deviceCustomizedService.customized(device);
+//            deviceCustomizedService.customized(device);
             init(device);
             
             // 缓存操作列表,用于事务提交后执行