* @Author: luozheng 1732036298@qq.com
* @Date: 2024-06-20 15:14:50
* @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-08-31 16:13:29
+ * @LastEditTime: 2024-09-07 12:04:07
* @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp
*/
#include "aivoice/include/AiVoiceModel.h"
namespace OHOS
{
// 声明定时器变量
- static osTimerId_t g_timer = NULL;
+ static osTimerId_t g_timer = nullptr;
static constexpr uint16_t THIRTY_TIME = 31;
static int Time = THIRTY_TIME;
static int AiModelConnectTimeout = 5;
const std::string filePath = "/user/userdata/AiRecording/AIAudio.pcm";
// Check if file exists
FILE *file = fopen(filePath.c_str(), "r");
- if (file != NULL)
+ if (file != nullptr)
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "File already exists: %s", filePath.c_str());
fclose(file);
// File does not exist, create it
file = fopen(filePath.c_str(), "w");
- if (file == NULL)
+ 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, "AiVoiceModel::AIVoice_StartTimer");
/* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */
- if (g_timer != NULL)
+ if (g_timer != nullptr)
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer already exists!!");
return;
}
/* --------------------------------- 2.创建定时器 -------------------------------- */
- g_timer = osTimerNew((osTimerFunc_t)AIVoice_TimerCallback, osTimerPeriodic, NULL, NULL);
+ g_timer = osTimerNew((osTimerFunc_t)AIVoice_TimerCallback, osTimerPeriodic, nullptr, nullptr);
// 检查定时器创建是否成功
- if (g_timer == NULL)
+ if (g_timer == nullptr)
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer create failed!!");
return;
/* --------------------------------- 4.启动定时器 -------------------------------- */
osStatus_t ret = osTimerStart(g_timer, tick);
- /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 NULL -------------------- */
+ /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 nullptr -------------------- */
if (ret != osOK)
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret);
- if (g_timer != NULL)
+ if (g_timer != nullptr)
{
ret = osTimerDelete(g_timer);
}
- g_timer = NULL;
+ g_timer = nullptr;
return;
}
}
void AiVoiceModel::AIVoice_StopTimer(void)
{
// 检查定时器是否存在
- if (g_timer != NULL)
+ if (g_timer != nullptr)
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::AIVoice_StopTimer");
- // 删除定时器并置为 NULL
+ // 删除定时器并置为 nullptr
osStatus_t ret = osTimerDelete(g_timer);
if (ret != osOK)
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret);
}
- g_timer = NULL;
+ g_timer = nullptr;
Time = THIRTY_TIME;
AiModelConnectTimeout = 5;
}
}
+ // 声明定时器变量
+ static osTimerId_t g_screen60s_timer = nullptr;
+ static uint16_t SCREEN_60S_TIME = 60;
+
+ static void ai_start_screen60s_timer_callbackProc(int data)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::ai_start_screen60s_timer_callbackProc,time = %d", SCREEN_60S_TIME);
+ SCREEN_60S_TIME -= 30;
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::ai_start_screen60s_timer_callbackProc,time = %d", SCREEN_60S_TIME);
+ if (SCREEN_60S_TIME <= 0)
+ {
+ // 60s后息屏停止定时
+ AiVoiceModel::GetInstance().ai_stop_screen60s_timer();
+ // 取消常亮屏
+ ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON);
+ }
+ }
+ static void ai_start_screen60s_timer_callback(int data)
+ {
+ GraphicService::GetInstance()->PostGraphicEvent(std::bind(ai_start_screen60s_timer_callbackProc, data));
+ }
+ void AiVoiceModel::ai_start_screen60s_timer(uint32_t interval)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::ai_start_screen60s_timer");
+ /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */
+ if (g_screen60s_timer != nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer already exists!!");
+ return;
+ }
+ /* --------------------------------- 2.创建定时器 -------------------------------- */
+ g_screen60s_timer = osTimerNew((osTimerFunc_t)ai_start_screen60s_timer_callback, osTimerPeriodic, nullptr, nullptr);
+ // 检查定时器创建是否成功
+ if (g_screen60s_timer == nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer create failed!!");
+ return;
+ }
+ /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 ---------------------------- */
+ int tick = osMs2Tick(interval);
+ /* --------------------------------- 4.启动定时器 -------------------------------- */
+ osStatus_t ret = osTimerStart(g_screen60s_timer, tick);
+ /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 nullptr -------------------- */
+ if (ret != osOK)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret);
+ if (g_screen60s_timer != nullptr)
+ {
+ ret = osTimerDelete(g_screen60s_timer);
+ }
+ g_screen60s_timer = nullptr;
+ return;
+ }
+ }
+ void AiVoiceModel::ai_stop_screen60s_timer(void)
+ {
+ // 检查定时器是否存在
+ if (g_screen60s_timer != nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::ai_stop_screen60s_timer");
+ // 删除定时器并置为 nullptr
+ osStatus_t ret = osTimerDelete(g_screen60s_timer);
+ if (ret != osOK)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret);
+ }
+ g_screen60s_timer = nullptr;
+ SCREEN_60S_TIME = 60;
+ }
+ }
+ void AiVoiceModel::reset_ai_screen60s_timer_time()
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "reset_ai_screen60s_timer_time");
+ SCREEN_60S_TIME = 60;
+ if (nullptr == g_screen60s_timer)
+ {
+ ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON); // 重新常亮屏
+ ai_start_screen60s_timer(); // 开启定时器
+ }
+ }
+
// 设置选择的ai模型
void AiVoiceModel::SetSelectAiModel(int index)
{
* @Author: luozheng 1732036298@qq.com
* @Date: 2024-06-20 15:11:17
* @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-08-31 16:13:34
+ * @LastEditTime: 2024-09-07 12:30:56
* @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp
*/
#include "AppViewIDs.h"
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoicePresenter::OnStart");
Presenter<AiVoiceView>::OnStart();
- // ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON);
+ ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON);
+ // 开启一个60s取消长亮屏timer
+ model->ai_start_screen60s_timer();
}
// 页面已经展示后的调用逻辑
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoicePresenter::OnStop");
Presenter<AiVoiceView>::OnStop();
- // ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON);
+ ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON);
model->StatusResume();
+ model->ai_stop_screen60s_timer();
}
/*调转到返回结果界面*/
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiModelsPresenter::index = %d", index);
model->SetSelectAiModel(index);
view_->ChangeAiModelAndUpdateComponentsStatus();
+ model->reset_ai_screen60s_timer_time();//重置60s取消常亮屏timer
return;
}
{
UNUSED(event);
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoicePresenter::OnClick");
+
+ model->reset_ai_screen60s_timer_time();//重置60s取消常亮屏timer
if (strcmp(view.GetViewId(), AIVOICE_IMGAE_VIEW_ID) == 0) /*点击的是Ai模型就切换模型*/
{
bool AiVoicePresenter::OnRelease(UIView &view, const ReleaseEvent &event)
{
UNUSED(event);
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoicePresenter::OnRelease");
std::string viewId = view.GetViewId();
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoicePresenter::OnRelease %s", viewId.c_str());
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoicePresenter::OnRelease %s", viewId.c_str());
+ /* ----------------------------- 重置60s取消常亮屏timer ---------------------------- */
+ model->reset_ai_screen60s_timer_time();
+ /* -------------------------------------------------------------------------- */
if ((viewId == AIVOICE_CHAT_BUTTON_VIEW_ID) && model->IsExistOpenAIAPIKeys() == false) // 如果点击的是(对话按钮)且未配置apikey
{
}
else
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Return false if viewId doesn't match any entry in the map");
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Return false if viewId doesn't match any entry in the map");
return false;
}
return true;
{
return false;
}
+
+ model->reset_ai_screen60s_timer_time();//重置60s取消常亮屏timer
+
if (event.GetDragDirection() == DragEvent::DIRECTION_RIGHT_TO_LEFT) /*左滑*/
{
DragRightToLetfChangeAIModel();