* @Author: luozheng 1732036298@qq.com
* @Date: 2024-06-20 15:14:43
* @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-07-03 17:55:19
+ * @LastEditTime: 2024-07-03 19:20:55
* @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h
*/
#ifndef AI_VOICE_MODEL_H
#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);
~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
* @Author: luozheng 1732036298@qq.com
* @Date: 2024-06-20 15:14:50
* @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-07-03 18:08:04
+ * @LastEditTime: 2024-07-03 19:16:04
* @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp
*/
#include "aivoice/include/AiVoiceModel.h"
return true;
}
- // 如果文件不存在就创建
+ /*如果文件不存在就创建*/
bool AiVoiceModel::CreateIfNotExists()
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::CreateIfNotExists");
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)
{
return true;
}
- /*å\88\9då§\8bå\8c\96å\8e\86å\8f²å¯¹è¯\9dæ\95°æ\8d®*/
- bool AiVoiceModel::InitchatContentItems(void)
+ /*å\88\97表æ\95°é\87\8f*/
+ 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;
}
}
/*更新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();
}
chatTtems[i].isAiAnswer = false;
}
ChatContentListCount = 0;
-
+
entryTimeoutFlag = false;
AIVoice_StopTimer();
ClearQuestionAndAiAnswerStr();