69 lines
1.7 KiB
C++
69 lines
1.7 KiB
C++
#pragma once
|
|
|
|
#include "ports/application_observer.h"
|
|
#include "ports/event_publisher.h"
|
|
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/task.h>
|
|
|
|
namespace borla {
|
|
|
|
struct IdleServiceConfig {
|
|
uint32_t check_after_ms = 3U * 60U * 1000U;
|
|
uint32_t soft_off_after_ms = 10U * 60U * 1000U;
|
|
uint32_t response_window_ms = 15U * 1000U;
|
|
uint32_t prompt_start_timeout_ms = 10U * 1000U;
|
|
};
|
|
|
|
enum class IdlePhase : uint8_t {
|
|
kStopped,
|
|
kRunning,
|
|
kPromptingCheck,
|
|
kAwaitingResponse,
|
|
kIdleSleeping,
|
|
kGoodbyeTriggered,
|
|
};
|
|
|
|
class IdleService final : public IApplicationObserver {
|
|
public:
|
|
explicit IdleService(
|
|
const IdleServiceConfig& config = IdleServiceConfig{})
|
|
: config_(config)
|
|
{
|
|
}
|
|
|
|
bool Initialize(IEventPublisher& publisher);
|
|
void Stop();
|
|
void OnApplicationEvent(
|
|
const AppEvent& event,
|
|
const RuntimeState& state) override;
|
|
|
|
IdlePhase phase() const { return phase_.load(); }
|
|
|
|
private:
|
|
static constexpr uint32_t kTaskStackDepth = 2048;
|
|
static void TaskEntry(void* context);
|
|
void Run();
|
|
void Reset(uint64_t now_ms);
|
|
static bool IsActivity(const AppEvent& event);
|
|
static uint64_t NowMs();
|
|
|
|
IdleServiceConfig config_;
|
|
IEventPublisher* publisher_ = nullptr;
|
|
std::atomic<IdlePhase> phase_{IdlePhase::kStopped};
|
|
std::atomic<uint64_t> idle_since_ms_{0};
|
|
std::atomic<uint64_t> phase_deadline_ms_{0};
|
|
std::atomic<uint32_t> prompt_session_id_{0};
|
|
std::atomic<bool> busy_{false};
|
|
std::atomic<bool> running_{false};
|
|
TaskHandle_t task_ = nullptr;
|
|
StaticTask_t task_control_{};
|
|
std::array<StackType_t, kTaskStackDepth> task_stack_{};
|
|
};
|
|
|
|
} // namespace borla
|