61 lines
1.8 KiB
C++
61 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include "ports/application_observer.h"
|
|
#include "ports/audio_engine.h"
|
|
#include "ports/event_publisher.h"
|
|
#include "ports/local_sequence_handler.h"
|
|
|
|
#include <cstdint>
|
|
#include <mutex>
|
|
|
|
#include <esp_timer.h>
|
|
|
|
namespace borla {
|
|
|
|
class ActivationReplayService final
|
|
: public ILocalSequenceHandler,
|
|
public IApplicationObserver {
|
|
public:
|
|
explicit ActivationReplayService(IAudioEngine& audio)
|
|
: audio_(audio)
|
|
{
|
|
}
|
|
|
|
bool Initialize(IEventPublisher& publisher);
|
|
bool StartLocalSequence(
|
|
const LocalPromptSequence& sequence) override;
|
|
void StopLocalSequence() override;
|
|
void OnApplicationEvent(
|
|
const AppEvent& event,
|
|
const RuntimeState& state) override;
|
|
|
|
private:
|
|
static constexpr uint64_t kReplayDelayUs =
|
|
6000ULL * 1000ULL;
|
|
|
|
static void TimerCallback(void* context);
|
|
void OnTimer();
|
|
void PlayCurrent(const RuntimeState& state, uint32_t generation);
|
|
bool CanPlay(const RuntimeState& state) const;
|
|
void StopLocked(PlaybackSessionId* session_to_cancel);
|
|
// The previous firmware logged every stop with its trigger
|
|
// (StopActivationReplay(reason)). Without it a device stuck reading
|
|
// the activation code cannot be told apart from one that simply never
|
|
// received the server's stop signal, which is the only thing that ends
|
|
// this loop — there is no device-side activation check in either
|
|
// firmware.
|
|
void StopWithReason(const char* reason);
|
|
|
|
IAudioEngine& audio_;
|
|
IEventPublisher* publisher_ = nullptr;
|
|
esp_timer_handle_t timer_ = nullptr;
|
|
mutable std::mutex mutex_;
|
|
LocalPromptSequence sequence_{};
|
|
PlaybackSessionId current_session_ = 0;
|
|
uint32_t generation_ = 0;
|
|
bool active_ = false;
|
|
bool repeating_ = false;
|
|
};
|
|
|
|
} // namespace borla
|