* @Author: luozheng 1732036298@qq.com
* @Date: 2024-06-20 15:14:50
* @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-10-16 10:46:11
+ * @LastEditTime: 2024-10-16 18:36:51
* @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp
*/
#include "aivoice/include/AiVoiceModel.h"
/*跳转到AI聆听界面界面*/
bool AiVoiceModel::ChangeToAIVoiceListeningView(bool value)
{
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::ChangeToReturnResultTextView");
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::ChangeToAIVoiceListeningView");
AiVoicePresenter *Aipresenter_ = AiVoicePresenter::GetInstance();
if (Aipresenter_ == nullptr)
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::ChangeToReturnResultTextView fail!!");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::ChangeToAIVoiceListeningView fail!!");
return false;
}
Aipresenter_->ChangeToAIVoiceListeningView(value);
return;
}
- /*如果文件不存在就创建*/
- bool AiVoiceModel::CreateIfNotExists()
- {
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::CreateIfNotExists");
- const std::string filePath = "/user/userdata/AiRecording/AIAudio.pcm";
- // Check if file exists
- FILE *file = fopen(filePath.c_str(), "r");
- if (file != nullptr)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "File already exists: %s", filePath.c_str());
- fclose(file);
- return false;
- }
-
- // File does not exist, create it
- file = fopen(filePath.c_str(), "w");
- if (file == nullptr)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create file: %s", filePath.c_str());
- return false;
- }
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "File created: %s", filePath.c_str());
- fclose(file);
-
- return true;
- }
-
/*获取API Keys是否可用*/
bool AiVoiceModel::IsExistOpenAIAPIKeys()
{
return false;
}
- // 获取与AI模型的连接状态
- bool AiVoiceModel::GetAiVoiceConnectStatus(void)
- {
- return AiVoiceConnectStatus;
- }
-
- // 设置与AI模型的连接状态
- void AiVoiceModel::SetAiVoiceConnectStatus(bool state)
- {
- AiVoiceConnectStatus = state;
- }
-
/*查询用户选择的AI模型是否已经连接*/
void AiVoiceModel::QuerySelectedAiModelIsConnect()
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::QuerySelectedAiModelIsConnect");
auto it = selectAiModelToTextId.find(GetSelectAiModel());
if (it != selectAiModelToTextId.end())
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SelectAiModel = %d", it->second.second);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[QuerySelectedAiModelIsConnect] SelectAiModel = %d", it->second.second);
inquire_AI_is_ready(it->second.second);
}
}
/* -------------------------------------------------------------------------- */
userQuestionStr += str;
/* -------------------------------------------------------------------------- */
- // if (!UpdateUserQuestionStr())
- // {
- // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateUserQuestionStr fail!!");
- // }
SendMsgToUIService(UI_UPDATE_USER_QUESTION_STR, 0, nullptr, 0);
}
/*设置Ai回答的字符串*/
void AiVoiceModel::SetAiAnswerStrStr(std::string str)
{
- // if (!entryTextViewState)
- // {
- // SendMsgToUIService(UI_AI_RESPONSE_USERDATA, 0, nullptr, 0);
- // entryTextViewState = true; //进入返回结果界面的标志 避免重复发消息
- // }
- /* -------------------------------------------------------------------------- */
aiAnswerStr += str;
- /* -------------------------------------------------------------------------- */
- // if (!UpdateAiAnswerStr())
- // {
- // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateAiAnswerStr fail!!");
- // }
SendMsgToUIService(UI_UPDATE_AI_ANSWER_STR, 0, nullptr, 0);
}
SendMsgToUIService(UI_AI_JUMP_TO_RESPONDERR_VIEW, 0, (void *)errnote.c_str(), errnote.length());
return;
}
- else if(AiVoiceModel::AI_RESPONSE_END) //如果是AI 回应结束
+ else if (AiVoiceModel::AI_RESPONSE_END) // 如果是AI 回应结束
{
- if(errnote.empty()) //如果没有回应数据
+ if (errnote.empty()) // 如果没有回应数据
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::GetErrnote is empty!!");
AiVoiceModel::GetInstance().SetErrnote("response data is empty");
/*获取AI音频路径*/
std::string AiVoiceModel::GetAIAudioPath(void)
{
- // CreateIfNotExists();
return AIAIAudioPath;
}
}
}
- bool AiVoiceModel::GetEntryTimeoutFlag(void)
- {
- return entryTimeoutFlag;
- }
-
- void AiVoiceModel::SetEntryTimeoutFlag(bool flag)
- {
- entryTimeoutFlag = flag;
- }
-
void AiVoiceModel::StatusResume(void)
{
Time = THIRTY_TIME;
}
ChatContentListCount = 0;
entryTextViewState = false;
- entryTimeoutFlag = false;
- AIVoice_StopTimer();
ClearQuestionAndAiAnswerStr();
StopListenAndRecordingUserQuestion();
}
/*定时器到期时的处理*/
static void AIVoice_TimerCallbackProc(int data)
{
- if (!AiVoiceModel::GetInstance().GetAiVoiceConnectStatus()) // 查询AI模型是否可用的定时器
+ // 等待AI思考完成的定时器
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AIVoice_TimerCallbackProc,Time = %d", Time);
+ if (Time == 30) // 等待一秒,确保录音停止,发送录音准备就绪指令
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AIVoice_TimerCallbackProc,AiModelConnectTimeout = %d", AiModelConnectTimeout);
- AiModelConnectTimeout--;
- if (AiModelConnectTimeout < 0) // 模型连接失败有5秒超时
- {
- AiVoiceModel::GetInstance().AIVoice_StopTimer();
- AiVoiceView *view_ = AiVoiceView::GetInstance();
- AiVoicePresenter *p_AiVoicePresenter = AiVoicePresenter::GetInstance();
- if (view_ != nullptr && p_AiVoicePresenter != nullptr)
- {
- if ((view_->GetAiVoiceScrollView(AIVOICE_AIMODE_CONNECTING) != nullptr) &&
- (view_->GetAiVoiceScrollView(AIVOICE_AIMODE_CONNECTING)->IsVisible()))
- {
- p_AiVoicePresenter->ChangeViewAndLogicOperator(AIVOICE_AIMODE_CONNECTING, AIVOICE_AIMODEL_CONNECT_FAILED_VIEWID);
- }
- }
- }
+ AiVoiceModel::GetInstance().CheckFileAndSendAIIsReady();
}
- else // 等待AI思考完成的定时器
+ if (Time < 0) // 如果思考时间超时30s,进入连接超时界面
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AIVoice_TimerCallbackProc,Time = %d", Time);
- Time -= 1;
- if (Time == 30) //等待一秒,确保录音停止,发送录音准备就绪指令
- {
- AiVoiceModel::GetInstance().CheckFileAndSendAIIsReady();
- // AiVoiceModel::GetInstance().AIVoice_StopTimer();
- // return;
- }
- if (Time < 0) // 如果思考时间超时30s,进入连接超时界面
+ AiVoiceModel::GetInstance().AIVoice_StopTimer();
+ AiVoiceModel::GetInstance().SetErrnote("The connection timed out. Please restart the conversation");
+ std::string str = AiVoiceModel::GetInstance().GetErrnote();
+ SendMsgToUIService(UI_AI_JUMP_TO_RESPONDERR_VIEW, 0, (void *)str.c_str(), str.length());
+ }
+ else
+ {
+ // 4g中途断连待扩展
+
+ // 蓝牙的连接状态判断,如果AI思考过程中途断连,直接弹窗提示,不在硬等
+ if (get_ble_connect() == false)
{
- AiVoiceModel::GetInstance().AIVoice_StopTimer();
- AiVoiceModel::GetInstance().SetErrnote("The connection timed out. Please restart the conversation");
+ AiVoiceModel::GetInstance().SetErrnote("ble is disconnected");
std::string str = AiVoiceModel::GetInstance().GetErrnote();
SendMsgToUIService(UI_AI_JUMP_TO_RESPONDERR_VIEW, 0, (void *)str.c_str(), str.length());
}
- else
- {
- //4g中途断连待扩展
-
- //蓝牙的连接状态判断,如果AI思考过程中途断连,直接弹窗提示,不在硬等
- if(get_ble_connect() == false)
- {
- AiVoiceModel::GetInstance().SetErrnote("ble is disconnected");
- std::string str = AiVoiceModel::GetInstance().GetErrnote();
- SendMsgToUIService(UI_AI_JUMP_TO_RESPONDERR_VIEW, 0, (void *)str.c_str(), str.length());
- }
- }
}
return;
}
*/
static void AIVoice_TimerCallback(int data)
{
+ Time -= 1;
GraphicService::GetInstance()->PostGraphicEvent(std::bind(AIVoice_TimerCallbackProc, data));
}
// 声明定时器变量
static osTimerId_t g_screen60s_timer = nullptr;
- static uint16_t SCREEN_60S_TIME = 60;
+ static uint16_t SCREEN_60S_TIME = 60; // 60s
static void ai_start_screen60s_timer_callbackProc(int data)
{
LISTENING_TIMEOUT--;
if (LISTENING_TIMEOUT <= 0)
{
- AiVoiceModel::GetInstance().StopListenAndRecordingUserQuestion(); //停止聆听用户问题
- AiVoiceModel::GetInstance().stop_listening_timer(); //停止聆听定时器
- if (AiVoiceView::GetInstance() && AivoiceListeningView::GetInstance() )
+ AiVoiceModel::GetInstance().StopListenAndRecordingUserQuestion(); // 停止聆听用户问题
+ AiVoiceModel::GetInstance().stop_listening_timer(); // 停止聆听定时器
+ if (AiVoiceView::GetInstance() && AivoiceListeningView::GetInstance())
{
AiVoiceView::GetInstance()->ChangeView(AIVOICE_LISTENING_VIEWID, AIVOICE_THINKING_VIEWID); // 切换到思考中界面
AiVoiceModel::GetInstance().AIVoice_StartTimer();
AivoiceListeningView::GetInstance()->GetShowtimeoutLabel()->Invalidate();
}
}
+ return;
}
static void AIListeningCallback(int data)
{
return;
}
}
-
+
// 停止一个AI录音的定时器
void AiVoiceModel::stop_listening_timer(void)
{
LISTENING_TIMEOUT = 15;
}
}
+
+ static osTimerId_t g_ai_is_ready_Handle = nullptr; // 处理查询AI是否就绪定时器
+ static uint8_t AI_IS_READY_TIMEOUT = 5; // 5s钟超时
+ static void AI_IS_READY_CallbackProc(int data)
+ {
+ if (AI_IS_READY_TIMEOUT <= 0) // 模型连接失败有5秒超时
+ {
+ AiVoiceModel::GetInstance().stop_ai_isready_timer();
+ AiVoiceView *view_ = AiVoiceView::GetInstance();
+ AiVoicePresenter *p_AiVoicePresenter = AiVoicePresenter::GetInstance();
+ if (view_ != nullptr && p_AiVoicePresenter != nullptr)
+ {
+ if ((view_->GetAiVoiceScrollView(AIVOICE_AIMODE_CONNECTING) != nullptr) &&
+ (view_->GetAiVoiceScrollView(AIVOICE_AIMODE_CONNECTING)->IsVisible()))
+ {
+ p_AiVoicePresenter->ChangeViewAndLogicOperator(AIVOICE_AIMODE_CONNECTING, AIVOICE_AIMODEL_CONNECT_FAILED_VIEWID);
+ }
+ }
+ }
+ return;
+ }
+ static void AI_IS_READY_Callback(int data)
+ {
+ AI_IS_READY_TIMEOUT--;
+ GraphicService::GetInstance()->PostGraphicEvent(std::bind(AI_IS_READY_CallbackProc, data));
+ }
+
+ // 开启一个查询AI是否准备就绪的定时器
+ void AiVoiceModel::start_ai_isready_timer(uint32_t interval)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::start_ai_isready_timer");
+ /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */
+ if (g_ai_is_ready_Handle != nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer already exists!!");
+ return;
+ }
+ /* --------------------------------- 2.创建定时器 -------------------------------- */
+ g_ai_is_ready_Handle = osTimerNew((osTimerFunc_t)AI_IS_READY_Callback, osTimerPeriodic, nullptr, nullptr);
+ // 检查定时器创建是否成功
+ if (g_ai_is_ready_Handle == nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer create failed!!");
+ return;
+ }
+ /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 ---------------------------- */
+ int tick = osMs2Tick(interval);
+ /* --------------------------------- 4.启动定时器 -------------------------------- */
+ osStatus_t ret = osTimerStart(g_ai_is_ready_Handle, tick);
+ /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 nullptr -------------------- */
+ if (ret != osOK)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret);
+ if (g_ai_is_ready_Handle != nullptr)
+ {
+ ret = osTimerDelete(g_ai_is_ready_Handle);
+ }
+ g_ai_is_ready_Handle = nullptr;
+ return;
+ }
+ return;
+ }
+
+ // 停止一个查询AI是否准备就绪的定时器
+ void AiVoiceModel::stop_ai_isready_timer(void)
+ {
+ // 检查定时器是否存在
+ if (g_ai_is_ready_Handle != nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::stop_ai_isready_timer");
+ osTimerStop(g_ai_is_ready_Handle);
+ // 删除定时器并置为 nullptr
+ osStatus_t ret = osTimerDelete(g_ai_is_ready_Handle);
+ if (ret != osOK)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret);
+ }
+ g_ai_is_ready_Handle = nullptr;
+ AI_IS_READY_TIMEOUT = 5;
+ }
+ }
+
// 设置选择的ai模型
void AiVoiceModel::SetSelectAiModel(int index)
{