// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#pragma once
#include "Constants.hpp"
#include <MessageType.hpp>
#include <Service/Common.hpp>
#include <Service/Message.hpp>
#include <Service/Service.hpp>
#include <Timers/TimerHandle.hpp>
#include <Service/Worker.hpp>
#include <bsp/common.hpp>
#include <bsp/keyboard/key_codes.hpp>
#include <bsp/keypad_backlight/keypad_backlight.hpp>
#include <screen-light-control/ScreenLightControl.hpp>
#include <vibra/Vibra.hpp>
#include <service-db/DBServiceName.hpp>
#include <cstdint>
#include <memory>
#include <string>
#include <sys/types.h>
class WorkerEvent;
class EventManager : public sys::Service
{
private:
static constexpr auto stackDepth = 4096;
void handleMinuteUpdate(time_t timestamp);
bool processKeypadBacklightRequest(bsp::keypad_backlight::Action action);
void startKeypadLightTimer();
bool processVibraRequest(bsp::vibrator::Action act,
std::chrono::milliseconds RepetitionTime = std::chrono::milliseconds{1000});
void toggleTorchOnOff();
void toggleTorchColor();
std::shared_ptr<settings::Settings> settings;
sys::TimerHandle loggerTimer;
sys::TimerHandle keypadLightTimer;
bsp::keypad_backlight::State keypadLightState{bsp::keypad_backlight::State::off};
static constexpr auto keypadLightTimerName = "KeypadLightTimer";
static constexpr auto keypadLightTimerTimeout = std::chrono::seconds(5);
protected:
std::unique_ptr<WorkerEvent> EventWorker;
// application where key events are sent. This is also only application that is allowed to change keyboard long
// press settings.
std::string targetApplication;
// alarm timestamp in seconds from midnight
uint32_t alarmTimestamp;
// ID of alarm waiting to trigger
uint32_t alarmID;
// flag set when there is no alarm to trigger in current day
bool alarmDBEmpty = false;
// flag set when there is alarm to handle
bool alarmIsValid = false;
std::unique_ptr<screen_light_control::ScreenLightControl> screenLightControl;
std::unique_ptr<vibra_handle::Vibra> Vibra;
public:
EventManager(const std::string &name = service::name::evt_manager);
~EventManager();
sys::MessagePointer DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *resp) override;
// Invoked during initialization
sys::ReturnCodes InitHandler() override;
sys::ReturnCodes DeinitHandler() override;
void ProcessCloseReason(sys::CloseReason closeReason) override;
sys::ReturnCodes SwitchPowerModeHandler(const sys::ServicePowerMode mode) override final;
void dumpLogsToFile();
/**
* @brief Sends request to application manager to switch from current application to specific window in application
* with specified name .
*/
static bool messageSetApplication(sys::Service *sender, const std::string &applicationName);
};
namespace sys
{
template <> struct ManifestTraits<EventManager>
{
static auto GetManifest() -> ServiceManifest
{
ServiceManifest manifest;
manifest.name = service::name::evt_manager;
manifest.dependencies = {service::name::db};
return manifest;
}
};
} // namespace sys