bolar_2 backup
This commit is contained in:
167
main/power/idle_service.cc
Normal file
167
main/power/idle_service.cc
Normal file
@@ -0,0 +1,167 @@
|
||||
#include "power/idle_service.h"
|
||||
|
||||
#include "core/playback_types.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_timer.h>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "IdleService2";
|
||||
|
||||
} // namespace
|
||||
|
||||
bool IdleService::Initialize(IEventPublisher& publisher)
|
||||
{
|
||||
if (running_.load()) {
|
||||
return true;
|
||||
}
|
||||
if (config_.check_after_ms == 0 ||
|
||||
config_.soft_off_after_ms <= config_.check_after_ms ||
|
||||
config_.response_window_ms == 0) {
|
||||
return false;
|
||||
}
|
||||
publisher_ = &publisher;
|
||||
running_.store(true);
|
||||
task_ = xTaskCreateStatic(
|
||||
&IdleService::TaskEntry,
|
||||
"idle_service2",
|
||||
task_stack_.size(),
|
||||
this,
|
||||
3,
|
||||
task_stack_.data(),
|
||||
&task_control_);
|
||||
if (task_ == nullptr) {
|
||||
running_.store(false);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void IdleService::Stop()
|
||||
{
|
||||
running_.store(false);
|
||||
phase_.store(IdlePhase::kStopped);
|
||||
}
|
||||
|
||||
void IdleService::OnApplicationEvent(
|
||||
const AppEvent& event,
|
||||
const RuntimeState& state)
|
||||
{
|
||||
busy_.store(
|
||||
state.call != CallState::kInactive ||
|
||||
state.conversation == ConversationState::kSpeaking ||
|
||||
state.playback != PlaybackState::kIdle ||
|
||||
state.alert_ringing);
|
||||
|
||||
if (state.lifecycle != LifecycleState::kRunning) {
|
||||
phase_.store(IdlePhase::kStopped);
|
||||
return;
|
||||
}
|
||||
if (event.type == AppEventType::kRealtimeConnected &&
|
||||
phase_.load() == IdlePhase::kStopped) {
|
||||
Reset(NowMs());
|
||||
return;
|
||||
}
|
||||
if (IsActivity(event)) {
|
||||
Reset(NowMs());
|
||||
return;
|
||||
}
|
||||
if (event.type == AppEventType::kLocalPromptStarted &&
|
||||
event.value ==
|
||||
static_cast<int32_t>(LocalPrompt::kIdleCheck) &&
|
||||
phase_.load() == IdlePhase::kPromptingCheck) {
|
||||
prompt_session_id_.store(event.correlation_id);
|
||||
return;
|
||||
}
|
||||
if ((event.type == AppEventType::kPlaybackDrained ||
|
||||
event.type == AppEventType::kPlaybackFailed) &&
|
||||
prompt_session_id_.load() != 0 &&
|
||||
event.correlation_id == prompt_session_id_.load() &&
|
||||
phase_.load() == IdlePhase::kPromptingCheck) {
|
||||
prompt_session_id_.store(0);
|
||||
phase_.store(IdlePhase::kAwaitingResponse);
|
||||
phase_deadline_ms_.store(
|
||||
NowMs() + config_.response_window_ms);
|
||||
}
|
||||
}
|
||||
|
||||
void IdleService::TaskEntry(void* context)
|
||||
{
|
||||
static_cast<IdleService*>(context)->Run();
|
||||
}
|
||||
|
||||
void IdleService::Run()
|
||||
{
|
||||
while (running_.load()) {
|
||||
const uint64_t now_ms = NowMs();
|
||||
const IdlePhase phase = phase_.load();
|
||||
const uint64_t idle_since = idle_since_ms_.load();
|
||||
|
||||
if (phase == IdlePhase::kRunning &&
|
||||
idle_since != 0 &&
|
||||
now_ms - idle_since >= config_.check_after_ms &&
|
||||
!busy_.load()) {
|
||||
phase_.store(IdlePhase::kPromptingCheck);
|
||||
phase_deadline_ms_.store(
|
||||
now_ms + config_.prompt_start_timeout_ms);
|
||||
publisher_->Publish(
|
||||
AppEvent::Make(AppEventType::kIdleCheckDue));
|
||||
} else if (phase == IdlePhase::kPromptingCheck &&
|
||||
phase_deadline_ms_.load() <= now_ms &&
|
||||
prompt_session_id_.load() == 0) {
|
||||
// Missing/rejected prompt still receives the normal response
|
||||
// window instead of immediately sleeping.
|
||||
phase_.store(IdlePhase::kAwaitingResponse);
|
||||
phase_deadline_ms_.store(
|
||||
now_ms + config_.response_window_ms);
|
||||
} else if (phase == IdlePhase::kAwaitingResponse &&
|
||||
phase_deadline_ms_.load() <= now_ms) {
|
||||
phase_.store(IdlePhase::kIdleSleeping);
|
||||
publisher_->Publish(AppEvent::Make(
|
||||
AppEventType::kIdleResponseExpired));
|
||||
} else if (phase == IdlePhase::kIdleSleeping &&
|
||||
idle_since != 0 &&
|
||||
now_ms - idle_since >=
|
||||
config_.soft_off_after_ms) {
|
||||
phase_.store(IdlePhase::kGoodbyeTriggered);
|
||||
publisher_->Publish(AppEvent::Make(
|
||||
AppEventType::kIdleGoodbyeDue));
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
task_ = nullptr;
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
void IdleService::Reset(uint64_t now_ms)
|
||||
{
|
||||
idle_since_ms_.store(now_ms);
|
||||
phase_deadline_ms_.store(0);
|
||||
prompt_session_id_.store(0);
|
||||
phase_.store(IdlePhase::kRunning);
|
||||
}
|
||||
|
||||
bool IdleService::IsActivity(const AppEvent& event)
|
||||
{
|
||||
switch (event.type) {
|
||||
case AppEventType::kButtonClicked:
|
||||
case AppEventType::kButtonLongPressed:
|
||||
case AppEventType::kWakeWordDetected:
|
||||
case AppEventType::kUserActivityDetected:
|
||||
case AppEventType::kTtsStarted:
|
||||
case AppEventType::kIncomingCall:
|
||||
case AppEventType::kCallConnected:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t IdleService::NowMs()
|
||||
{
|
||||
return static_cast<uint64_t>(esp_timer_get_time() / 1000);
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user