故事重连修改 3217/3
authorhcj <1224667075@qq.com>
Wed, 25 Feb 2026 04:14:49 +0000 (12:14 +0800)
committerhcj <1224667075@qq.com>
Wed, 25 Feb 2026 04:14:49 +0000 (12:14 +0800)
code/sdk/zhihui/leiniao/imsl_ln/LN_MQTT/ln_mqtt_api.c
code/sdk/zhihui/leiniao/imsl_ln/leiniao_api_at.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api.h
code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c

index 570d940..61db245 100644 (file)
@@ -37,7 +37,7 @@ void ln_mqtt_set_log_level(uint8_t level)
 
 /* 测试性录音数据保存到文件:1=启用,0=禁用。启用后每次开始录音创建新文件,停止时关闭。 */
 #ifndef LN_MQTT_RECORD_SAVE_TO_FILE_TEST
-#define LN_MQTT_RECORD_SAVE_TO_FILE_TEST 1
+#define LN_MQTT_RECORD_SAVE_TO_FILE_TEST 0
 #endif
 
 /* MQTT API 全局状态 */
index 95507e4..798f7f9 100644 (file)
@@ -369,7 +369,7 @@ static uint32_t ln_ws_force_reconnect_at(uint8_t *para, uint32_t paraLen, char *
     LN_UNUSED(paraLen); 
     LN_UNUSED(strCommonCmd);
     printf("[LN][WS_FORCE_RECONNECT]\n");
-    int ret =0;// ln_ws_force_reconnect();
+    int ret = ln_ws_reconnect();
     if (ret != 0)
     {
         printf("[LN][WS_FORCE_RECONNECT] failed: %d\n", ret);
index 67fdc5b..5ae2bb1 100644 (file)
@@ -575,6 +575,10 @@ bool ln_ws_is_audio_initialized(void);
 
 int ln_ws_vad_auth(void);
 
+bool ln_ws_send_story_continue_request(void);
+
+int ln_ws_reconnect(void);
+
 /* WebSocket状态查询接口 */
 int ln_ws_get_connection_state(void);
 const char* ln_ws_get_connection_state_string(void);
index 776e475..af0ab9f 100644 (file)
@@ -145,7 +145,7 @@ static void ln_ws_process_message_with_callback(const ws_data_msg_t *data_msg);
 static int ln_ws_close_with_status(int client_fd);
 static ws_close_status_t ln_ws_get_close_status(void);
 static int ln_ws_check_chat_id_match(const char *chat_id, cJSON *root);
-static int ln_ws_reconnect(void);
+int ln_ws_reconnect(void);
 static void ln_ws_reset_close_status(void);
 static bool ln_ws_check_binary_data_interrupt(void);
 static void ln_ws_reconnect_success_callback(void);
@@ -172,6 +172,7 @@ static bool check_playback_completion_by_timeout(void);
 
 static char story_theme_cache[64]={0};
 
+static uint32_t g_story_index=0;
 
 uint16_t send_audio=0;
 
@@ -1572,6 +1573,7 @@ void ln_ws_audio_deinit(void)
 
     g_audio_playback_active = false;
     g_audio_chat_ctx.finish_state = LN_CHAT_NOT_ENDED;
+    g_story_index=0;
 
     uapi_ws_wake_keep(0);
     
@@ -2415,6 +2417,10 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms
             if (finished && cJSON_IsBool(finished)) {
                 data_msg->finished = cJSON_IsTrue(finished);
             }
+             cJSON *index = cJSON_GetObjectItem(data_obj, "index");
+            if (index && cJSON_IsNumber(index)) {
+                g_story_index= index->valueint;
+            }
             
             // 解析chatId字段
             cJSON *chat_id = cJSON_GetObjectItem(data_obj, "chatId");
@@ -2432,11 +2438,18 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms
                     // 只有在对话ID匹配的情况下才更新结束状态
                     if (finished && cJSON_IsBool(finished) && data_msg->finished) {
                         g_audio_chat_ctx.finish_state = LN_CHAT_RECEIVED_END;
+
+                        g_story_index=0;
+
                     }
                     LN_WS_DEBUG("ChooseStoryResponse matched current chat, set is_reply=true, finished=%s\n",
                            finished && cJSON_IsBool(finished) ? (data_msg->finished ? "true" : "false") : "unknown");
                 }
             }
+
+           
+
+            
             
             // 解析data.data字段
             cJSON *data_data = cJSON_GetObjectItem(data_obj, "data");
@@ -2495,6 +2508,12 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms
             if (finished && cJSON_IsBool(finished)) {
                 data_msg->finished = cJSON_IsTrue(finished);
             }
+
+            cJSON *index = cJSON_GetObjectItem(data_obj, "index");
+            if (index && cJSON_IsNumber(index)) {
+                g_story_index= index->valueint;
+            }
+
             
             // 解析chatId字段
             cJSON *chat_id = cJSON_GetObjectItem(data_obj, "chatId");
@@ -2512,6 +2531,7 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms
                     // 只有在对话ID匹配的情况下才更新结束状态
                     if (finished && cJSON_IsBool(finished) && data_msg->finished) {
                         g_audio_chat_ctx.finish_state = LN_CHAT_RECEIVED_END;
+                        g_story_index=0;
                     }
                     LN_WS_DEBUG("RandomStoryResponse matched current chat, set is_reply=true, finished=%s\n",
                            finished && cJSON_IsBool(finished) ? (data_msg->finished ? "true" : "false") : "unknown");
@@ -3206,7 +3226,7 @@ static void ln_ws_process_incremental_base64_data(const char *new_data, size_t n
  * WebSocket重连函数
  * @return 0-重连成功,-1-重连失败
  */
-static int ln_ws_reconnect(void)
+int ln_ws_reconnect(void)
 {
     LN_WS_INFO("Starting WebSocket reconnection\n");
     
@@ -3273,6 +3293,11 @@ static void ln_ws_reconnect_success_callback(void)
         ln_ws_send_random_story_request(story_theme_cache);
         memset(story_theme_cache,0,sizeof(story_theme_cache));
     }
+
+    if(g_story_index>0)
+    {
+        ln_ws_send_story_continue_request();
+    }
     
     // 设备重新上线后,上报蓝牙状态
     // LN_WS_INFO("Device reconnected, reporting Bluetooth status\n");
@@ -4321,4 +4346,81 @@ static uint32_t ln_ws_convert_audio_format_string_to_enum(const char *format_str
         LN_WS_WARN("Unknown audio format: %s, using default MP3\n", format_str);
         return MP3;  // 默认值
     }
-}
\ No newline at end of file
+}
+
+
+bool ln_ws_send_story_continue_request(void)
+{
+    if (!g_audio_initialized) {
+        LN_WS_ERROR("Audio not initialized, please call ln_ws_audio_init() first\n");
+        return false;
+    }
+    
+    if (g_ws_connected != WS_STATE_CONNECTED) {
+        LN_WS_WARN("Cannot send story continue request, connection state: %d\n", g_ws_connected);
+        return false;
+    }
+
+    // 生成消息ID
+    char message_id[128];
+    generate_message_id(message_id, sizeof(message_id), g_audio_chat_ctx.chat_id, 1);
+    
+    // 构建JSON消息
+    cJSON *json = cJSON_CreateObject();
+    if (!json) {
+        LN_WS_ERROR("Failed to create JSON object\n");
+        return false;
+    }
+    
+    // 添加基本字段
+    cJSON_AddStringToObject(json, "deviceId", uapi_ln_get_device_id());
+    cJSON_AddNumberToObject(json, "timestamp", (double)time(NULL));
+    cJSON_AddStringToObject(json, "messageId", message_id);
+    cJSON_AddStringToObject(json, "command", "StoryContinueRequest");
+    
+    // 添加data字段
+    cJSON *data = cJSON_CreateObject();
+    if (!data) {
+        LN_WS_ERROR("Failed to create data JSON object\n");
+        cJSON_Delete(json);
+        return false;
+    }
+    
+    cJSON_AddStringToObject(data, "chatId", g_audio_chat_ctx.chat_id);
+    cJSON_AddNumberToObject(data, "index", g_story_index+1);
+    
+    cJSON_AddItemToObject(json, "data", data);
+    
+    // 转换为字符串
+    char *json_string = cJSON_Print(json);
+    if (!json_string) {
+        LN_WS_ERROR("Failed to convert JSON to string\n");
+        cJSON_Delete(json);
+        return false;
+    }
+    
+    LN_WS_ERROR("Sending StoryContinueRequest: %s\n", json_string);
+    
+    // 发送消息
+    int ret = uapi_ws_send_text(json_string);
+    cJSON_free(json_string);
+    cJSON_Delete(json);
+
+    if (ret != 0) {
+        LN_WS_ERROR("Failed to send StoryContinueRequest\n");
+
+        return false;
+    }
+    LN_WS_DEBUG("StoryContinueRequest sent successfully\n");
+    return true;
+}
+// {
+//   "deviceId": "DEVICE_001",
+//   "timestamp": 1640995200,
+//   "messageId": "story_continue_001",
+//   "command": "StoryContinueRequest",
+//   "data": {
+//     "chatId": "story_12345",
+//     "index": 2
+//   }
+// }
\ No newline at end of file