lifang 3 weeks ago
parent
commit
0c8488dae0

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

@@ -1,5 +1,7 @@
 package com.nb.core.controller;
 
+import cn.hutool.core.util.HexUtil;
+import cn.hutool.json.JSONUtil;
 import com.nb.core.entity.MqttMessage;
 import com.nb.core.result.R;
 import com.nb.core.service.MqttMessageProcessService;
@@ -8,6 +10,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 +34,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 +46,80 @@ public class MqttMessageController {
             if(MqttClientUtil.CLIENT_ID.equals(clientId)){
                 return R.fail("本节点发出的数据不予处理");
             }
+            // 打印评价人和评价信息日志
+            logEvaluationInfo(message);
+            message.setPayload(HexUtil.decodeHexStr(message.getPayload()));
+            if(!JSONUtil.isJson(message.getPayload())){
+                throw new RuntimeException("格式错误");
+            }
             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;
+    }
 }