feat: mcp sleep
This commit is contained in:
@ -26,6 +26,12 @@ namespace {
|
||||
constexpr const char* kDirectWebsocketUrl = "ws://172.19.0.240:8080";
|
||||
constexpr int kDirectWebsocketVersion = 3;
|
||||
constexpr bool kUseDirectWebsocketWithoutOta = true;
|
||||
constexpr int kDefaultFatigueListeningTimeoutSec = 12;
|
||||
constexpr int kDefaultFatigueIdleTimeoutSec = 12;
|
||||
constexpr int kDefaultFatigueCooldownSec = 60;
|
||||
constexpr const char* kDefaultFatigueEmotion = "wakeup";
|
||||
constexpr const char* kDefaultFatigueMessage = "你是不是又要睡着啦?快醒醒,我还要给你跳舞呢~";
|
||||
constexpr const char* kDefaultFatigueStatus = "打起精神";
|
||||
|
||||
void StartDirectTimeSync() {
|
||||
setenv("TZ", "CST-8", 1);
|
||||
@ -283,6 +289,7 @@ void Application::Run() {
|
||||
clock_ticks_++;
|
||||
auto display = Board::GetInstance().GetDisplay();
|
||||
display->UpdateStatusBar();
|
||||
CheckFatigueReminder();
|
||||
|
||||
// Print debug info every 10 seconds
|
||||
if (clock_ticks_ % 10 == 0) {
|
||||
@ -706,6 +713,108 @@ void Application::DismissAlert() {
|
||||
}
|
||||
}
|
||||
|
||||
void Application::CheckFatigueReminder() {
|
||||
auto state = GetDeviceState();
|
||||
if (state != kDeviceStateListening) {
|
||||
fatigue_silence_seconds_ = 0;
|
||||
fatigue_reminder_triggered_in_listening_ = false;
|
||||
}
|
||||
if (state != kDeviceStateIdle) {
|
||||
fatigue_idle_seconds_ = 0;
|
||||
}
|
||||
|
||||
Settings settings("fatigue", false);
|
||||
if (!settings.GetBool("enabled", true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int64_t now_us = esp_timer_get_time();
|
||||
int cooldown_sec = settings.GetInt("cooldown_sec", kDefaultFatigueCooldownSec);
|
||||
if (cooldown_sec < 10) {
|
||||
cooldown_sec = 10;
|
||||
} else if (cooldown_sec > 3600) {
|
||||
cooldown_sec = 3600;
|
||||
}
|
||||
if (last_fatigue_reminder_time_us_ != 0 &&
|
||||
now_us - last_fatigue_reminder_time_us_ < static_cast<int64_t>(cooldown_sec) * 1000000) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == kDeviceStateIdle) {
|
||||
fatigue_idle_seconds_++;
|
||||
|
||||
int idle_timeout_sec = settings.GetInt("idle_timeout_sec", kDefaultFatigueIdleTimeoutSec);
|
||||
if (idle_timeout_sec < 3) {
|
||||
idle_timeout_sec = 3;
|
||||
} else if (idle_timeout_sec > 3600) {
|
||||
idle_timeout_sec = 3600;
|
||||
}
|
||||
if (fatigue_idle_seconds_ >= idle_timeout_sec) {
|
||||
fatigue_idle_seconds_ = 0;
|
||||
last_fatigue_reminder_time_us_ = now_us;
|
||||
TriggerFatigueReminder();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (state != kDeviceStateListening) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (audio_service_.IsVoiceDetected()) {
|
||||
fatigue_silence_seconds_ = 0;
|
||||
fatigue_reminder_triggered_in_listening_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
fatigue_silence_seconds_++;
|
||||
if (fatigue_reminder_triggered_in_listening_) {
|
||||
return;
|
||||
}
|
||||
|
||||
int timeout_sec = settings.GetInt("listening_timeout_sec", kDefaultFatigueListeningTimeoutSec);
|
||||
if (timeout_sec < 3) {
|
||||
timeout_sec = 3;
|
||||
} else if (timeout_sec > 300) {
|
||||
timeout_sec = 300;
|
||||
}
|
||||
if (fatigue_silence_seconds_ < timeout_sec) {
|
||||
return;
|
||||
}
|
||||
|
||||
fatigue_reminder_triggered_in_listening_ = true;
|
||||
last_fatigue_reminder_time_us_ = now_us;
|
||||
TriggerFatigueReminder();
|
||||
}
|
||||
|
||||
void Application::TriggerFatigueReminder() {
|
||||
Settings settings("fatigue", false);
|
||||
std::string emotion = settings.GetString("emotion", kDefaultFatigueEmotion);
|
||||
std::string message = settings.GetString("message", kDefaultFatigueMessage);
|
||||
std::string sound_asset = settings.GetString("sound_asset");
|
||||
|
||||
ESP_LOGW(TAG, "Fatigue reminder triggered: silence=%ds, emotion=%s",
|
||||
fatigue_silence_seconds_, emotion.c_str());
|
||||
|
||||
auto display = Board::GetInstance().GetDisplay();
|
||||
display->SetStatus(kDefaultFatigueStatus);
|
||||
display->SetEmotion(emotion.c_str());
|
||||
display->SetChatMessage("assistant", message.c_str());
|
||||
|
||||
if (!sound_asset.empty()) {
|
||||
void* ptr = nullptr;
|
||||
size_t size = 0;
|
||||
auto& assets = Assets::GetInstance();
|
||||
if (assets.partition_valid() && assets.GetAssetData(sound_asset, ptr, size)) {
|
||||
audio_service_.PlaySound(std::string_view(static_cast<const char*>(ptr), size));
|
||||
return;
|
||||
}
|
||||
ESP_LOGW(TAG, "Fatigue sound asset not found: %s", sound_asset.c_str());
|
||||
}
|
||||
|
||||
audio_service_.PlaySound(Lang::Sounds::OGG_POPUP);
|
||||
}
|
||||
|
||||
void Application::ToggleChatState() {
|
||||
xEventGroupSetBits(event_group_, MAIN_EVENT_TOGGLE_CHAT);
|
||||
}
|
||||
@ -919,11 +1028,12 @@ void Application::HandleStateChangedEvent() {
|
||||
break;
|
||||
case kDeviceStateConnecting:
|
||||
display->SetStatus(Lang::Strings::CONNECTING);
|
||||
display->SetEmotion("neutral");
|
||||
display->SetChatMessage("system", "");
|
||||
display->SetEmotion("neutral");
|
||||
break;
|
||||
case kDeviceStateListening:
|
||||
display->SetStatus(Lang::Strings::LISTENING);
|
||||
display->ClearChatMessages();
|
||||
display->SetEmotion("neutral");
|
||||
|
||||
// Make sure the audio processor is running
|
||||
|
||||
Reference in New Issue
Block a user