140 lines
3.0 KiB
C++
140 lines
3.0 KiB
C++
#include "schedule/reminder_service.h"
|
|
|
|
#include <esp_log.h>
|
|
#include <esp_timer.h>
|
|
|
|
namespace borla {
|
|
namespace {
|
|
|
|
constexpr const char* kTag = "ReminderService2";
|
|
constexpr uint32_t kMaximumDurationSeconds = 7U * 24U * 60U * 60U;
|
|
|
|
class Lock final {
|
|
public:
|
|
explicit Lock(SemaphoreHandle_t mutex) : mutex_(mutex)
|
|
{
|
|
xSemaphoreTake(mutex_, portMAX_DELAY);
|
|
}
|
|
~Lock() { xSemaphoreGive(mutex_); }
|
|
|
|
private:
|
|
SemaphoreHandle_t mutex_;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
bool ReminderService::Initialize(IEventPublisher& publisher)
|
|
{
|
|
if (running_.load()) {
|
|
return true;
|
|
}
|
|
publisher_ = &publisher;
|
|
mutex_ = xSemaphoreCreateMutexStatic(&mutex_control_);
|
|
if (mutex_ == nullptr) {
|
|
return false;
|
|
}
|
|
running_.store(true);
|
|
task_ = xTaskCreateStatic(
|
|
&ReminderService::TaskEntry,
|
|
"reminder2",
|
|
task_stack_.size(),
|
|
this,
|
|
4,
|
|
task_stack_.data(),
|
|
&task_control_);
|
|
if (task_ == nullptr) {
|
|
running_.store(false);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void ReminderService::Stop()
|
|
{
|
|
running_.store(false);
|
|
}
|
|
|
|
bool ReminderService::Add(uint32_t duration_seconds)
|
|
{
|
|
if (duration_seconds == 0 ||
|
|
duration_seconds > kMaximumDurationSeconds ||
|
|
mutex_ == nullptr) {
|
|
return false;
|
|
}
|
|
Lock lock(mutex_);
|
|
return schedule_.Add(
|
|
NowMs() +
|
|
static_cast<uint64_t>(duration_seconds) * 1000);
|
|
}
|
|
|
|
void ReminderService::Cancel(TimerCancelScope scope)
|
|
{
|
|
if (mutex_ == nullptr) {
|
|
return;
|
|
}
|
|
Lock lock(mutex_);
|
|
if (scope == TimerCancelScope::kAll) {
|
|
schedule_.CancelAll();
|
|
} else {
|
|
schedule_.CancelNext();
|
|
}
|
|
}
|
|
|
|
size_t ReminderService::count() const
|
|
{
|
|
if (mutex_ == nullptr) {
|
|
return 0;
|
|
}
|
|
Lock lock(mutex_);
|
|
return schedule_.count();
|
|
}
|
|
|
|
uint64_t ReminderService::NextWakeupDelayUs() const
|
|
{
|
|
if (mutex_ == nullptr) {
|
|
return 0;
|
|
}
|
|
Lock lock(mutex_);
|
|
const uint64_t deadline = schedule_.next_deadline_ms();
|
|
const uint64_t now = NowMs();
|
|
if (deadline == 0) {
|
|
return 0;
|
|
}
|
|
const uint64_t delay_ms = deadline > now ? deadline - now : 1;
|
|
return delay_ms * 1000ULL;
|
|
}
|
|
|
|
void ReminderService::TaskEntry(void* context)
|
|
{
|
|
static_cast<ReminderService*>(context)->Run();
|
|
}
|
|
|
|
void ReminderService::Run()
|
|
{
|
|
while (running_.load()) {
|
|
size_t due = 0;
|
|
{
|
|
Lock lock(mutex_);
|
|
due = schedule_.PopDue(NowMs());
|
|
}
|
|
if (due > 0 && publisher_ != nullptr) {
|
|
ESP_LOGI(kTag, "%u reminder(s) due",
|
|
static_cast<unsigned int>(due));
|
|
publisher_->Publish(AppEvent::Make(
|
|
AppEventType::kReminderDue,
|
|
0,
|
|
static_cast<int32_t>(due)));
|
|
}
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
|
}
|
|
task_ = nullptr;
|
|
vTaskDelete(nullptr);
|
|
}
|
|
|
|
uint64_t ReminderService::NowMs()
|
|
{
|
|
return static_cast<uint64_t>(esp_timer_get_time() / 1000);
|
|
}
|
|
|
|
} // namespace borla
|