From 2f2cada83b39e16cd0bf541fea474bac8bd504de Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Wed, 3 Jul 2024 19:28:30 +0800 Subject: [PATCH] =?utf8?q?AI=E8=AF=AD=E9=9F=B3Model=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/aivoice/include/AiVoiceModel.h | 42 +++-- .../nativeui/aivoice/src/AiVoiceModel.cpp | 146 +++++++++++------- 2 files changed, 118 insertions(+), 70 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h b/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h index 248a49871..d593c3acf 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-20 15:14:43 * @LastEditors: SnickerSun snickersun@163.com - * @LastEditTime: 2024-07-05 10:13:59 + * @LastEditTime: 2024-07-05 10:32:12 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h */ #ifndef AI_VOICE_MODEL_H @@ -15,37 +15,52 @@ #include "ChatContentItem.h" namespace OHOS { + class AiVoiceModel { public: - static AiVoiceModel &GetInstance(void); + static AiVoiceModel &GetInstance(void); // 获取单例实例 bool ChangeToReturnResultTextView(void); + // 初始化和状态管理 bool CreateIfNotExists(); + void StatusResume(void); + // 获取设备状态 bool GetBluetoothIsOpen(void); bool GetMobileDataIsOpen(void); bool GetAiVoiceConnectStatus(void); + // 聊天内容管理 bool InitchatContentItems(void); - bool AddChatContentToItemArray(ChatContentItem &item); + bool AddUserQuestionToChatItems(); + bool AddAiAnswerToChatItems(); + bool AddChatContentToItemArray(const ChatContentItem &item); uint8_t GetChatContentListCount(void); - void CheckFileAndSendAIIsReady(); - bool GetEntryTimeoutFlag(void); - void SetEntryTimeoutFlag(bool flag); - void StatusResume(void); + // 更新和清空问题及回答 bool UpdateUserQuestionStr(void); bool UpdateAiAnswerStr(void); + void ClearQuestionAndAiAnswerStr(void); + + // 获取和设置问题及回答 std::string GetUserQuestionStr(void); void SetUserQuestionStr(std::string str); std::string GetAiAnswerStr(void); void SetAiAnswerStrStr(std::string str); - void ClearQuestionAndAiAnswerStr(void); + + // 文件检查和AI状态管理 + void CheckFileAndSendAIIsReady(); + bool GetEntryTimeoutFlag(void); + void SetEntryTimeoutFlag(bool flag); + + // AI录音路径管理 std::string GetAIAudioPath(void); + // 启动和停止录音 void StartListenAndRecordingUserQuestion(void); void StopListenAndRecordingUserQuestion(void); + // 定时器管理 void AIVoice_StartTimer(uint32_t interval = 1000); void AIVoice_StopTimer(void); @@ -54,14 +69,17 @@ namespace OHOS ~AiVoiceModel(); AiVoiceModel(const AiVoiceModel &) = delete; AiVoiceModel &operator=(const AiVoiceModel &) = delete; - std::string AIAIAudioPath = "/user/userdata/AiRecording/AIAudio.pcm"; // ai录音的文件名 - std::string userQuestionStr; // 存储用户提出的问题 - std::string aiAnswerStr; // 存储ai回答的问题 + + // 私有成员变量 + std::string AIAIAudioPath = "/user/userdata/AiRecording/AIAudio.pcm"; // AI录音的文件路径 + std::string userQuestionStr; // 用户提出的问题 + std::string aiAnswerStr; // AI回答的问题 bool entryTimeoutFlag{false}; // 进入超时界面标志 uint8_t ChatContentListCount{0}; // 列表数量 static const uint16_t MaxChatContentListCount = 2; // 对话列表最大数量 - ChatContentItem chatTtems[MaxChatContentListCount] = {0, "", false}; + ChatContentItem chatTtems[MaxChatContentListCount] = {0, "", false}; // 对话项数组 }; + } #endif \ No newline at end of file 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 8e5d3909d..22a28298b 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-20 15:14:50 * @LastEditors: SnickerSun snickersun@163.com - * @LastEditTime: 2024-07-05 10:13:25 + * @LastEditTime: 2024-07-05 10:32:00 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp */ #include "aivoice/include/AiVoiceModel.h" @@ -50,7 +50,7 @@ namespace OHOS return true; } - // 如果文件不存在就创建 + /*如果文件不存在就创建*/ bool AiVoiceModel::CreateIfNotExists() { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::CreateIfNotExists"); @@ -77,35 +77,6 @@ namespace OHOS return true; } - uint8_t AiVoiceModel::GetChatContentListCount(void) - { - return ChatContentListCount; - } - - bool AiVoiceModel::AddChatContentToItemArray(ChatContentItem &item) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::InitchatContentItems"); - AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance(); - if (p_AivoiceReturnResultTextView == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!! "); - return false; - } - if (ChatContentListCount < MaxChatContentListCount) - { - chatTtems[ChatContentListCount].index = item.index; - chatTtems[ChatContentListCount].text = item.text; - chatTtems[ChatContentListCount].isAiAnswer = item.isAiAnswer; - ChatContentListCount += 1; - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ChatContentListCount out of bounds!!"); - return false; - } - return true; - } - // 获取蓝牙是否打开 bool AiVoiceModel::GetBluetoothIsOpen(void) { @@ -124,44 +95,97 @@ namespace OHOS return true; } - /*初始化历史对话数据*/ - bool AiVoiceModel::InitchatContentItems(void) + /*列表数量*/ + uint8_t AiVoiceModel::GetChatContentListCount(void) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateUserQuestionAndAiAnswerStr"); - AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance(); - if (p_AivoiceReturnResultTextView == nullptr) + return ChatContentListCount; + } + + /*添加聊天内容项到数组*/ + bool AiVoiceModel::AddChatContentToItemArray(const ChatContentItem &item) + { + // 检查 ChatContentListCount 是否超出范围 + if (ChatContentListCount >= MaxChatContentListCount) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!! "); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ChatContentListCount out of bounds!!"); return false; } - /* -------------------------------------------------------------------------- */ + + chatTtems[ChatContentListCount].index = item.index; + chatTtems[ChatContentListCount].text = item.text; + chatTtems[ChatContentListCount].isAiAnswer = item.isAiAnswer; + ChatContentListCount++; + + return true; + } + + /*添加用户问题项*/ + bool AiVoiceModel::AddUserQuestionToChatItems() + { ChatContentItem UserQuestionStrItem; UserQuestionStrItem.index = GetChatContentListCount(); UserQuestionStrItem.text = GetUserQuestionStr(); UserQuestionStrItem.isAiAnswer = false; - AddChatContentToItemArray(UserQuestionStrItem); - /* -------------------------------------------------------------------------- */ + return AddChatContentToItemArray(UserQuestionStrItem); + } + + /*添加AI回答项*/ + bool AiVoiceModel::AddAiAnswerToChatItems() + { ChatContentItem AiAnswerStrItem; AiAnswerStrItem.index = GetChatContentListCount(); AiAnswerStrItem.text = GetAiAnswerStr(); AiAnswerStrItem.isAiAnswer = true; - AddChatContentToItemArray(AiAnswerStrItem); - /* -------------------------------------------------------------------------- */ + return AddChatContentToItemArray(AiAnswerStrItem); + } + + /*初始化历史对话数据*/ + bool AiVoiceModel::InitchatContentItems(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::InitchatContentItems"); + + // 获取单例对象 + AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance(); + if (p_AivoiceReturnResultTextView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!!"); + return false; + } + + // 添加用户问题项到数组 + if (!AddUserQuestionToChatItems()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AddUserQuestionToChatItems fail!!"); + return false; + } + + // 添加AI回答项到数组 + if (!AddAiAnswerToChatItems()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AddAiAnswerToChatItems fail!!"); + return false; + } + + // 将聊天内容添加到显示列表中 for (uint8_t i = 0; i < ChatContentListCount; i++) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "chatTtems[%d]", i); p_AivoiceReturnResultTextView->AddChatContentToList(chatTtems[i]); } - /* -------------------------------------------------------------------------- */ - if (p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter() != nullptr && p_AivoiceReturnResultTextView->GetChatContentList() != nullptr) + + // 更新显示列表 + auto chatContentListAdapter = p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter(); + auto chatContentList = p_AivoiceReturnResultTextView->GetChatContentList(); + if (chatContentListAdapter != nullptr && chatContentList != nullptr) { - p_AivoiceReturnResultTextView->GetChatContentList()->SetAdapter(p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter()); - p_AivoiceReturnResultTextView->GetChatContentList()->RefreshList(); + chatContentList->SetAdapter(chatContentListAdapter); + chatContentList->RefreshList(); } else { return false; } + return true; } @@ -174,35 +198,41 @@ namespace OHOS } /*更新Ai答案字符串*/ - bool AiVoiceModel::UpdateAiAnswerStr(void) + bool AiVoiceModel::UpdateAiAnswerStr() { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateAiAnswerStr"); + + // 获取 ChatContentListCount int index = GetChatContentListCount() - 1; - if (index < 0) + if (index < 0 || index >= MaxChatContentListCount) { - index = 0; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid index: %d", index); + return false; } + + // 更新 chatTtems 中的 AI 回答内容 chatTtems[index].index = index; chatTtems[index].text = GetAiAnswerStr(); chatTtems[index].isAiAnswer = true; + // 更新显示列表 AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance(); if (p_AivoiceReturnResultTextView == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!! "); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!!"); return false; } - if (p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter() != nullptr) + AivoiceChatContentListAdapter *adapter = p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter(); + if (adapter != nullptr && !adapter->GetChatContentItem().empty()) { - if (!p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter()->GetChatContentItem().empty()) + // 如果最后一条是 AI 回答,则移除 + if (adapter->GetChatContentItem().back().isAiAnswer) { - if (p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter()->GetChatContentItem().back().isAiAnswer == true) // 如果是ai答案 - { - p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter()->GetChatContentItem().pop_back(); - } + adapter->GetChatContentItem().pop_back(); } - p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter()->GetChatContentItem().push_back(chatTtems[index]); + // 将更新后的 AI 回答内容添加到列表 + adapter->GetChatContentItem().push_back(chatTtems[index]); p_AivoiceReturnResultTextView->GetChatContentList()->RefreshList(); } @@ -348,7 +378,7 @@ namespace OHOS chatTtems[i].isAiAnswer = false; } ChatContentListCount = 0; - + entryTimeoutFlag = false; AIVoice_StopTimer(); ClearQuestionAndAiAnswerStr(); -- 2.17.1