93 lines
2.5 KiB
C++
93 lines
2.5 KiB
C++
#include "app/application.h"
|
|
|
|
#include "core/state_reducer.h"
|
|
|
|
#include <esp_log.h>
|
|
|
|
namespace borla
|
|
{
|
|
namespace
|
|
{
|
|
|
|
constexpr const char *kTag = "Application2";
|
|
|
|
} // namespace
|
|
|
|
Application::Application(IEffectSink &effects)
|
|
: effects_(effects)
|
|
{
|
|
}
|
|
|
|
[[noreturn]] void Application::Run()
|
|
{
|
|
ESP_LOGI(kTag, "control-plane event loop starting");
|
|
|
|
ProcessEvent(AppEvent::Make(AppEventType::kBootRequested));
|
|
|
|
while (true)
|
|
{
|
|
AppEvent event;
|
|
if (events_.Receive(&event))
|
|
{
|
|
ProcessEvent(event);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Application::Publish(const AppEvent &event)
|
|
{
|
|
if (events_.Publish(event))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
ESP_LOGE(kTag, "event queue full, dropped event=%s pending=%u",
|
|
ToString(event.type),
|
|
static_cast<unsigned>(events_.Pending()));
|
|
return false;
|
|
}
|
|
|
|
void Application::ProcessEvent(const AppEvent &event)
|
|
{
|
|
const TransitionResult transition = ReduceState(state_, event);
|
|
const StateValidation validation = ValidateRuntimeState(transition.next);
|
|
if (!validation.valid)
|
|
{
|
|
ESP_LOGE(kTag, "rejected invalid transition event=%s reason=%s",
|
|
ToString(event.type), validation.reason);
|
|
return;
|
|
}
|
|
|
|
state_ = transition.next;
|
|
LogState(event);
|
|
|
|
for (size_t i = 0; i < transition.effect_count; ++i)
|
|
{
|
|
effects_.Handle(transition.effects[i], state_);
|
|
}
|
|
for (size_t i = 0; i < observer_count_; ++i)
|
|
{
|
|
observers_[i]->OnApplicationEvent(event, state_);
|
|
}
|
|
}
|
|
|
|
void Application::LogState(const AppEvent &event) const
|
|
{
|
|
ESP_LOGI(kTag,
|
|
"event=%s lifecycle=%s net=%s realtime=%s conversation=%s "
|
|
"call=%s playback=%s session=%lu pending_start=%d "
|
|
"pending_soft_off=%d",
|
|
ToString(event.type),
|
|
ToString(state_.lifecycle),
|
|
ToString(state_.connectivity),
|
|
ToString(state_.realtime),
|
|
ToString(state_.conversation),
|
|
ToString(state_.call),
|
|
ToString(state_.playback),
|
|
static_cast<unsigned long>(state_.playback_session_id),
|
|
state_.pending_conversation_start ? 1 : 0,
|
|
state_.pending_soft_off_after_playback ? 1 : 0);
|
|
}
|
|
|
|
} // namespace borla
|