From 17f18bac1afe83c8a2e12f93ff1cc77e48386ea5 Mon Sep 17 00:00:00 2001 From: hcj <1224667075@qq.com> Date: Wed, 20 Aug 2025 11:59:46 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E6=94=B9=E9=98=9F=E5=88=97=E5=88=9D?= =?utf8?q?=E5=A7=8B=E5=8C=96=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../slip/cat1Service/cat1_ai_server.cpp | 134 +++++++++++++----- 1 file changed, 101 insertions(+), 33 deletions(-) diff --git a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp index d022000f8..34a115c62 100644 --- a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp +++ b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp @@ -401,7 +401,8 @@ static int ws_queue_init(ws_message_queue_t* queue,uint32_t queue_size) static void ws_queue_deinit(ws_message_queue_t* queue) { if (!queue || !queue->initialized) return; - + queue->initialized=false; + if (queue->queue_id != 0) { osal_msg_queue_delete(queue->queue_id); queue->queue_id = 0; @@ -934,6 +935,11 @@ void bk_websocket_demo(void *data) { is_bk_websocket_chat = 1; AI_UNLOCK(); + if (chat_msg_queue.initialized ) { + WEARABLE_LOGW(WEARABLE_LOG_MODULE_APP, "WebSocket queues already initialized, deinitializing first"); + ws_queue_deinit(&chat_msg_queue); + } + ws_queue_init(&chat_msg_queue,256); char_reconnet: ws_queue_cleanup(&chat_msg_queue); @@ -1173,6 +1179,7 @@ void bk_websocket_demo(void *data) { cleanup_and_exit: AI_LOCK(); + ws_queue_deinit(&chat_msg_queue); is_bk_websocket_chat = 0; chat_client_ping = 0; AI_UNLOCK(); @@ -1489,6 +1496,12 @@ void bk_websocket_asr(void *data) { WebSocketMessageType type; bool state = true; +if (asr_msg_queue.initialized ) { + WEARABLE_LOGW(WEARABLE_LOG_MODULE_APP, "WebSocket queues already initialized, deinitializing first"); + ws_queue_deinit(&asr_msg_queue); + } + ws_queue_init(&asr_msg_queue,256); + ASR_SATRT: AI_LOCK(); is_bk_websocket_asr = 1; @@ -1728,8 +1741,11 @@ WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "等待ASR WebSocket握手完成... (%d m asr_cleanup_and_exit: AI_LOCK(); is_bk_websocket_asr = 0; + ws_queue_deinit(&asr_msg_queue); + AI_UNLOCK(); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "ASR WebSocket ended"); return; } @@ -1830,11 +1846,6 @@ void uapi_start_tts(void) void uapi_stop_tts(void) { - if (tts_is_ready!=1) - { - return; - } - char *jsonString = " { \ \"header\": { \ @@ -2052,6 +2063,13 @@ uint8_t is_bk_websocket_tts=0; void bk_websocket_tts(void *data) { uint32_t start_time = (uint32_t)osKernelGetTickCount(); uint32_t retry_count = 0; + + if (tts_msg_queue.initialized ) { + WEARABLE_LOGW(WEARABLE_LOG_MODULE_APP, "WebSocket queues already initialized, deinitializing first"); + ws_queue_deinit(&tts_msg_queue); + } + ws_queue_init(&tts_msg_queue,0); + TTS_SATRT: uapi_cat1_keep_wakeup(1); @@ -2273,6 +2291,7 @@ tts_cleanup_and_exit: AI_LOCK(); is_bk_websocket_tts = 0; tts_client_ping = 0; + ws_queue_deinit(&tts_msg_queue); AI_UNLOCK(); WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "TTS WebSocket ended"); @@ -2493,40 +2512,40 @@ bool init_ai_task(uint8_t type) disconnect_server = 0; - g_timer_time_sync = osTimerNew((osTimerFunc_t)time_sync_func, osTimerPeriodic, NULL, NULL); - if (g_timer_time_sync == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "g_timer_time_sync create failed"); - ai_cleanup_resources(); - return false; - } + // g_timer_time_sync = osTimerNew((osTimerFunc_t)time_sync_func, osTimerPeriodic, NULL, NULL); + // if (g_timer_time_sync == NULL) { + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "g_timer_time_sync create failed"); + // ai_cleanup_resources(); + // return false; + // } type_ai = type; // 初始化TTS播放 if (uapi_tts_paly_init() != ERRCODE_SUCC) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to initialize TTS playback"); - ai_cleanup_resources(); - return false; + // ai_cleanup_resources(); + // return false; } // 初始化TTS服务器 if (tts_server_init() != ERRCODE_SUCC) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to initialize TTS server"); - ai_cleanup_resources(); - return false; + // ai_cleanup_resources(); + // return false; } // 初始化聊天服务器 if (chat_server_init() != ERRCODE_SUCC) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to initialize chat server"); - ai_cleanup_resources(); - return false; + // ai_cleanup_resources(); + // return false; } // 初始化ASR服务器 if (asr_server_init() != ERRCODE_SUCC) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to initialize asr server"); - ai_cleanup_resources(); - return false; + // ai_cleanup_resources(); + // return false; } WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI task initialized successfully, type: %d", type); } @@ -3098,10 +3117,10 @@ void ai_cleanup_resources(void) { } // 清理定时器 - if (g_timer_time_sync) { - osTimerDelete(g_timer_time_sync); - g_timer_time_sync = NULL; - } + // if (g_timer_time_sync) { + // osTimerDelete(g_timer_time_sync); + // g_timer_time_sync = NULL; + // } // 重置内存使用统计 g_ai_memory_usage = 0; @@ -3140,15 +3159,15 @@ errcode_t ai_system_init(void) { g_ai_memory_usage = 0; // 初始化WebSocket消息队列 - if (ws_queues_init_all() != 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to initialize all WebSocket queues"); - if (g_ai_mutex) { - osMutexDelete(g_ai_mutex); - g_ai_mutex = NULL; - } - tcp_simple_manager_deinit(); // 清理TCP管理器 - return ERRCODE_FAIL; - } + // if (ws_queues_init_all() != 0) { + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to initialize all WebSocket queues"); + // if (g_ai_mutex) { + // osMutexDelete(g_ai_mutex); + // g_ai_mutex = NULL; + // } + // tcp_simple_manager_deinit(); // 清理TCP管理器 + // return ERRCODE_FAIL; + // } WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI system initialized successfully"); @@ -3189,6 +3208,55 @@ bool ai_system_health_check(void) { return true; } +bool uapi_get_server_state_is_end() +{ + bool ret=true; + + int osThreadGetState_t= osThreadGetState(chat_taskcfg.task_handle); + + const char *buf= osThreadGetName(chat_taskcfg.task_handle); + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"chat osThreadGetState_t %d!\r\n",osThreadGetState_t); + if(strcmp("chat_server_init",buf)==0) + { + if(osThreadGetState_t!=osThreadReady && osThreadGetState_t!=osThreadError && osThreadGetState_t !=osThreadInactive) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"osThreadGetState is %d!\r\n",osThreadGetState_t); + ret=false; + } + } + + osThreadGetState_t= osThreadGetState(tts_taskcfg.task_handle); + + buf= osThreadGetName(tts_taskcfg.task_handle); + + if(strcmp("tts_server_init",buf)==0) + { + if(osThreadGetState_t!=osThreadReady && osThreadGetState_t!=osThreadError && osThreadGetState_t !=osThreadInactive) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"osThreadGetState is %d!\r\n",osThreadGetState_t); + ret=false; + } + } + osThreadGetState_t= osThreadGetState(asr_taskcfg.task_handle); + + buf= osThreadGetName(asr_taskcfg.task_handle); + + if(strcmp("asr_server_init",buf)==0) + { + if(osThreadGetState_t!=osThreadReady && osThreadGetState_t!=osThreadError && osThreadGetState_t !=osThreadInactive) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"osThreadGetState is %d!\r\n",osThreadGetState_t); + ret=false; + } + } + + return ret; +} + + + + #ifdef __cplusplus } #endif /* __cplusplus */ -- 2.17.1