From 9ab127ec8f96a5a549a2ed4b68906f322c8bdc79 Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Wed, 20 Aug 2025 16:45:25 +0800 Subject: [PATCH] =?utf8?q?1.=E5=85=B3=E9=97=AD=E5=A4=9A=E4=BD=99=E6=89=93?= =?utf8?q?=E5=8D=B0=202.4g=E6=9C=8D=E5=8A=A1=E5=99=A8=E8=BF=9E=E6=8E=A5?= =?utf8?q?=E4=B8=AD=E4=BC=98=E5=8C=96=EF=BC=8C=E8=A7=A3=E5=86=B3deinit?= =?utf8?q?=E5=90=8E=EF=BC=8C=E7=AB=8B=E9=A9=ACinit=E5=AF=BC=E8=87=B44Gai?= =?utf8?q?=E8=BF=9E=E6=8E=A5=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/aivoice/src/AiVoiceModel.cpp | 10 +- .../nativeui/aivoice/src/AiVoicePresenter.cpp | 6 +- .../slip/cat1Service/cat1_ai_server.cpp | 101 ++++++++++++++---- .../peripheral/slip/cat1Service/cat1_ws.cpp | 8 +- .../peripheral/slip/cat1Service/cat1_ws.h | 2 + .../slip/tcpmanager/tcp_simple_manager.c | 4 +- test_ws_parser/websocket_parser_test.cpp | 8 +- 7 files changed, 106 insertions(+), 33 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp index 4e425ef86..814f9241e 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp @@ -1475,6 +1475,12 @@ namespace OHOS { if (view_ != nullptr && p_AiVoicePresenter != nullptr) { + if(uapi_get_server_state_is_end()) + { + init_ai_task(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType()); + return; + } + if (AiVoiceModel::GetInstance().GetInit_4gai_task()) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "4gAi服务初始化成功"); @@ -2015,11 +2021,11 @@ namespace OHOS int textId = 0; selectAiModelToTextId = { {0, {STR_DEEPSEEK, 4}}, - {1, {STR_AIMODES_CHATGPT, 1}}, + {1, {STR_JIUTIAN, 5}}, {2, {STR_AIMODELS_DOUBAO, 3}}, {3, {STR_AIMODES_WENXINYIYAN, 0}}, {4, {STR_AIMODES_KIMI, 2}}, - {5, {STR_IFLYTEK_SPARK, 5}}, + {5, {STR_AIMODES_CHATGPT, 1}}, }; textId = STR_DEEPSEEK; diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp index 42062b2dc..c9540abef 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp @@ -471,7 +471,7 @@ namespace OHOS } if (!model->GetInit_4gai_task()) { - init_ai_task(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType()); + // init_ai_task(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType()); model->start_ai4g_connect_timer(); ChangeViewAndLogicOperator(AIVOICE_STARTCHAT_VIEWID, AIVOICE_4GSERVICECON_VIEW); // LOADING } @@ -503,7 +503,7 @@ namespace OHOS } if (!model->GetInit_4gai_task()) { - init_ai_task(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType()); + // init_ai_task(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType()); model->start_ai4g_connect_timer(); ChangeViewAndLogicOperator(AIVOICE_STARTCHAT_VIEWID, AIVOICE_4GSERVICECON_VIEW); // LOADING } @@ -556,7 +556,7 @@ namespace OHOS } if (!model->GetInit_4gai_task()) { - init_ai_task(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType()); + // init_ai_task(AiVoiceModel::GetInstance().GetCurUsing4GAiModelType()); model->start_ai4g_connect_timer(); ChangeViewAndLogicOperator(model->GetCurView(), AIVOICE_4GSERVICECON_VIEW); // LOADING } 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 34a115c62..96d70541c 100644 --- a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp +++ b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp @@ -679,7 +679,7 @@ bool send_ai_txt(uint8_t type,char *msg) if (send_websocket_message(chat_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0) { chat_reconnect=0; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); cJSON_free(jsonString); return true; } @@ -1122,7 +1122,7 @@ void bk_websocket_demo(void *data) { { recv_buf[len] = '\0'; } - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "bk_websocket_demo len %d\n",len); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "bk_websocket_demo len %d\n",len); if (len == -1 || type == WS_CLOSE) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WebSocket connection closed"); break; @@ -1274,7 +1274,7 @@ void uapi_start_asr(void) \"3fa6b9e6a30b407ea38ad7ae69e6c291\"},\"payload\":{\"format\":\"MP3\",\"sample_rate\":16000}}"; if (send_websocket_message(asr_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0) - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); else { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message erro\n"); @@ -1297,7 +1297,7 @@ void uapi_stop_asr(void) char *jsonString ="{\"header\":{\"event\":\"StopTranscription\",\"trace_id\":\"3fa6b9e6a30b407ea38ad7ae69e6c291\"}}"; if (send_websocket_message(asr_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0) - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); else { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message erro\n"); @@ -1585,7 +1585,7 @@ ASR_SATRT: if (asr_client == NULL) { bool state = false; - SendMsgToUIService(UI_AI_JUMP_TO_LISTENING_VIEW, 0, &state, sizeof(bool)); + // SendMsgToUIService(UI_AI_JUMP_TO_LISTENING_VIEW, 0, &state, sizeof(bool)); goto asr_cleanup_and_exit; } @@ -1838,7 +1838,7 @@ void uapi_start_tts(void) { memset_s(tts_task_id_buf,128,0,128); tts_is_ready=0; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); } } @@ -1859,7 +1859,7 @@ void uapi_stop_tts(void) if (send_websocket_message(tts_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0) { tts_is_ready=0; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); } } @@ -1887,13 +1887,14 @@ void uapi_start_tts_single(void) printf("uapi_start_tts : %s\r\n",jsonString); if (send_websocket_message(tts_client,(uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0) - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); } bool uapi_start_tts_single_txt(char *buf) { - + + #if 0 if(tts_is_ready!=1) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"uapi_start_tts_single_txt no tts_is_ready \n"); @@ -1933,12 +1934,73 @@ bool uapi_start_tts_single_txt(char *buf) if (send_websocket_message(tts_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0) { tts_reconnect=0; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); free(jsonString); return true; } free(jsonString); return false; + #else + if (tts_is_ready != 1) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uapi_start_tts_single_txt no tts_is_ready \n"); + return false; + } + // 创建根JSON对象 + cJSON* dataJSON = cJSON_CreateObject(); + if (dataJSON == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error creating root JSON object\n"); + return false; + } + + // 创建header对象并添加字段 + cJSON* header = cJSON_CreateObject(); + if (header == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error creating header JSON object\n"); + cJSON_Delete(dataJSON); // 释放已分配的内存 + return false; + } + cJSON_AddStringToObject(header, "event", "RunSynthesis"); + cJSON_AddStringToObject(header, "trace_id", tts_task_id_buf); + + // 创建payload对象并添加字段 + cJSON* payload = cJSON_CreateObject(); + if (payload == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error creating payload JSON object\n"); + cJSON_Delete(header); // 释放已分配的内存 + cJSON_Delete(dataJSON); + return false; + } + cJSON_AddStringToObject(payload, "text", buf); + + // 将header和payload添加到根对象dataJSON中 + cJSON_AddItemToObject(dataJSON, "header", header); + cJSON_AddItemToObject(dataJSON, "payload", payload); + + // 将JSON对象转换为字符串 + char* jsonString = cJSON_PrintUnformatted(dataJSON); + if (jsonString == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error converting JSON to string\n"); + cJSON_Delete(dataJSON); // 释放JSON对象 + return false; + } + + // 发送WebSocket消息 + bool result = false; + if (send_websocket_message(tts_client, (uint8_t*)jsonString, strlen(jsonString), WS_TEXT) == 0) { + tts_reconnect = 0; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "send_websocket_message succ 0\n"); + result = true; + } + else + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "send_websocket_message fail -1\n"); + } + + // 释放资源 + cJSON_Delete(dataJSON); // 释放JSON对象(会自动释放header和payload) + free(jsonString); // 释放cJSON_Print生成的字符串 + return result; + #endif } // char *tts_txt_str= @@ -1960,7 +2022,7 @@ void send_tts_txt(char *msg) if (send_websocket_message(tts_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0) - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); else WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message fail 1\n"); } @@ -2239,7 +2301,7 @@ TTS_SATRT: bk_websocket_tts_tick = (uint32_t)osKernelGetTickCount(); } send_msg_data_into_Ai_msg_queue((uint8_t *)tts_recv_buf, len, 200); - WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "TTS binary data received: %d bytes", len); + // WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "TTS binary data received: %d bytes", len); msleep(1); } else if (type == WS_TEXT) { WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "TTS text data received: %s", tts_recv_buf); @@ -2261,7 +2323,10 @@ TTS_SATRT: msleep(1); // 避免过度占用CPU } - + + + tts_is_ready=0; + bk_websocket_tts_tick = 0; AI_LOCK(); @@ -2612,13 +2677,13 @@ uint8_t is_bk_websocket_voice_chat=0; void send_ai_bin(uint8_t *buf,uint32_t len) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_ai_bin %d\n",len); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_ai_bin %d\n",len); // voice_chat_client // if (send_websocket_message(asr_client,( uint8_t *)buf,len,WS_BINARY)==0) if (send_websocket_message(asr_client,( uint8_t *)buf,len,WS_BINARY)==0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); msleep(100); } else @@ -2799,7 +2864,7 @@ void start_send_voice(void) if (send_websocket_message(voice_chat_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0) { startAudioCaptureTask(); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); } @@ -2822,7 +2887,7 @@ void back_voice_RequestToSpeak(void) printf("uapi_start_tts : %s\r\n",jsonString); if (send_websocket_message(voice_chat_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0) - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n"); free(jsonString); } @@ -3208,7 +3273,7 @@ bool ai_system_health_check(void) { return true; } -bool uapi_get_server_state_is_end() +bool uapi_get_server_state_is_end(void) { bool ret=true; diff --git a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.cpp b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.cpp index 5491fdd33..05852feb7 100644 --- a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.cpp +++ b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.cpp @@ -710,7 +710,7 @@ void _tcp_on_disconnect_cb(tcp_handle_t handle) } void _tcp_on_data_received_cb(tcp_handle_t handle, uint8_t* data, uint16_t datalen) { - WS_LOG_INFO("tcp_on_data_received_cb 0x%0X %d", handle, datalen); + // WS_LOG_INFO("tcp_on_data_received_cb 0x%0X %d", handle, datalen); // 参数验证 if (data == NULL || datalen == 0) { @@ -743,7 +743,7 @@ void _tcp_on_data_received_cb(tcp_handle_t handle, uint8_t* data, uint16_t datal return; } - WS_LOG_WARNING("Processed %d bytes %s %s of WebSocket data", processed, client->host, client->path); + // WS_LOG_WARNING("Processed %d bytes %s %s of WebSocket data", processed, client->host, client->path); } #if TEST_AI_WS @@ -1055,7 +1055,7 @@ int ws_process_received_data(WebSocketClient *client, const uint8_t *data, size_ // ========== 新增:打印收到的所有原始数据 ========== // printf("\n=== WebSocket Raw Data Received ===\n"); // printf("Client: %s:%s\n", client->host, client->path); - WS_LOG_WARNING("Total bytes: %zu", len); + // WS_LOG_WARNING("Total bytes: %zu", len); // printf("Raw data (hex): "); // // 打印所有数据的十六进制表示 // for (size_t i = 0; i < len; i++) { @@ -1115,7 +1115,7 @@ int ws_process_received_data(WebSocketClient *client, const uint8_t *data, size_ // bool valid_rsv = (rsv == 0); // printf(" * Valid RSV: %s\n", valid_rsv ? "YES" : "NO"); // } - WS_LOG_WARNING("====================================="); + // WS_LOG_WARNING("====================================="); ws_parser_t* parser = client->parser; int total_processed = 0; diff --git a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.h b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.h index 12acb3ea1..cb69e3387 100644 --- a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.h +++ b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.h @@ -22,6 +22,8 @@ bool uapi_start_tts_single_txt(char *buf); void uapi_UI_stop_start_asr_timeout(void); +bool uapi_get_server_state_is_end(void); + typedef enum { AI_TYPE_0=0, diff --git a/code/sdk/drivers/peripheral/slip/tcpmanager/tcp_simple_manager.c b/code/sdk/drivers/peripheral/slip/tcpmanager/tcp_simple_manager.c index 0602f75bf..a3b569511 100644 --- a/code/sdk/drivers/peripheral/slip/tcpmanager/tcp_simple_manager.c +++ b/code/sdk/drivers/peripheral/slip/tcpmanager/tcp_simple_manager.c @@ -678,10 +678,10 @@ static bool process_send_queue(tcp_connection_info_t* conn) } /* 尝试发送数据 */ - printf("process_send_queue start\r\n"); + // printf("process_send_queue start\r\n"); ssize_t sent = send(conn->sockfd, item->data, item->length, MSG_NOSIGNAL); - printf("process_send_queue %d %d\r\n",sent, item->length); + // printf("process_send_queue %d %d\r\n",sent, item->length); if (sent == item->length) { /* 发送成功 */ diff --git a/test_ws_parser/websocket_parser_test.cpp b/test_ws_parser/websocket_parser_test.cpp index f1f9160f9..ed92cf59f 100644 --- a/test_ws_parser/websocket_parser_test.cpp +++ b/test_ws_parser/websocket_parser_test.cpp @@ -98,10 +98,10 @@ void decode_data(ws_parser_t* parser,uint8_t* data, size_t len) { - printf("\n=== Multi-Frame Parsing Complete ===\n"); - printf("Total frames parsed: %d\n", frame_count); - printf("Total bytes processed: %zu\n", offset); - printf("Remaining bytes: %zu\n", len - offset); + // printf("\n=== Multi-Frame Parsing Complete ===\n"); + // printf("Total frames parsed: %d\n", frame_count); + // printf("Total bytes processed: %zu\n", offset); + // printf("Remaining bytes: %zu\n", len - offset); } -- 2.17.1