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);
static char story_theme_cache[64]={0};
+static uint32_t g_story_index=0;
uint16_t send_audio=0;
g_audio_playback_active = false;
g_audio_chat_ctx.finish_state = LN_CHAT_NOT_ENDED;
+ g_story_index=0;
uapi_ws_wake_keep(0);
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");
// 只有在对话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");
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");
// 只有在对话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");
* WebSocket重连函数
* @return 0-重连成功,-1-重连失败
*/
-static int ln_ws_reconnect(void)
+int ln_ws_reconnect(void)
{
LN_WS_INFO("Starting WebSocket reconnection\n");
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");
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