~aleteoryx/muditaos

4bf8623d4061d212b4ea090a4f2ad9db4aa744a0 — Maciej Janicki 4 years ago d2fcc30
[BH-685] Add alarm handler factory

This commit adds factory for alarm handlers with
basic handlers draft for Bell.
A module-services/service-alarm/AlarmHandlerFactory.cpp => module-services/service-alarm/AlarmHandlerFactory.cpp +25 -0
@@ 0,0 1,25 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <map>
#include <memory>

#include "include/service-alarm/AlarmHandler.hpp"
#include "include/service-alarm/AlarmHandlerFactory.hpp"

namespace alarms
{
    [[nodiscard]] auto AlarmHandlerFactory::getHandler(const AlarmType type) -> const std::shared_ptr<AlarmHandler>
    {
        if (handlers.count(type) != 0u) {
            return handlers.at(type);
        }
        LOG_ERROR("no such alarm type handler defined: %d", static_cast<int>(type));
        return nullptr;
    }

    auto AlarmHandlerFactory::addHandler(const AlarmType type, const std::shared_ptr<AlarmHandler> handler) -> void
    {
        handlers.insert({type, handler});
    }
} // namespace alarms

M module-services/service-alarm/CMakeLists.txt => module-services/service-alarm/CMakeLists.txt +8 -4
@@ 8,18 8,18 @@ target_sources(service-alarm
    PUBLIC
        include/service-alarm/AlarmMessage.hpp
        include/service-alarm/AlarmServiceAPI.hpp
        include/service-alarm/AlarmHandler.hpp
        include/service-alarm/AlarmHandlerFactory.hpp
    PRIVATE
        AlarmRepository.hpp
        AlarmHandlerFactory.cpp
)

target_include_directories(service-alarm
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    PRIVATE
        $<BUILD_INTERFACE:
            ${CMAKE_CURRENT_SOURCE_DIR}
            include
        >
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)

target_link_libraries(service-alarm


@@ 30,3 30,7 @@ target_link_libraries(service-alarm
        messagetype
        module-sys
)

if (${ENABLE_TESTS})
    add_subdirectory(tests)
endif ()

A module-services/service-alarm/include/service-alarm/AlarmHandler.hpp => module-services/service-alarm/include/service-alarm/AlarmHandler.hpp +25 -0
@@ 0,0 1,25 @@
// 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 <module-db/Interface/AlarmEventRecord.hpp>

namespace alarms
{
    enum class AlarmType
    {
        Clock,
        Calendar,
        EveningReminder,
        None
    };

    class AlarmHandler
    {
      public:
        virtual ~AlarmHandler() = default;

        virtual auto handle(const AlarmEventRecord &record) -> bool = 0;
    };
} // namespace alarms

A module-services/service-alarm/include/service-alarm/AlarmHandlerFactory.hpp => module-services/service-alarm/include/service-alarm/AlarmHandlerFactory.hpp +27 -0
@@ 0,0 1,27 @@
// 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 "AlarmHandler.hpp"

#include <module-db/Interface/AlarmEventRecord.hpp>

#include <map>

namespace alarms
{
    class AlarmHandlerFactory
    {
      private:
        std::map<const AlarmType, const std::shared_ptr<AlarmHandler>> handlers;

      public:
        AlarmHandlerFactory() = default;
        explicit AlarmHandlerFactory(const std::map<const AlarmType, const std::shared_ptr<AlarmHandler>> handlers)
            : handlers{handlers} {};

        [[nodiscard]] auto getHandler(const AlarmType type) -> const std::shared_ptr<AlarmHandler>;
        auto addHandler(const AlarmType type, const std::shared_ptr<AlarmHandler>) -> void;
    };
} // namespace alarms

M module-services/service-alarm/include/service-alarm/AlarmServiceAPI.hpp => module-services/service-alarm/include/service-alarm/AlarmServiceAPI.hpp +0 -2
@@ 1,5 1,3 @@
#pragma once

// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md


A module-services/service-alarm/tests/CMakeLists.txt => module-services/service-alarm/tests/CMakeLists.txt +11 -0
@@ 0,0 1,11 @@
add_catch2_executable(
    NAME
        service-alarm-tests
    SRCS
        tests-main.cpp
        tests-AlarmHandlerFactory.cpp
        MockAlarmHandler.hpp
    LIBS
        service-alarm
        module-db
)

A module-services/service-alarm/tests/MockAlarmHandler.hpp => module-services/service-alarm/tests/MockAlarmHandler.hpp +20 -0
@@ 0,0 1,20 @@
// 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 <service-alarm/AlarmHandler.hpp>

namespace alarms
{

    class MockAlarmHandler : public AlarmHandler
    {
      public:
        auto handle(const AlarmEventRecord &record) -> bool
        {
            LOG_DEBUG("MockAlarmHandler");
            return true;
        }
    };
} // namespace alarms

A module-services/service-alarm/tests/tests-AlarmHandlerFactory.cpp => module-services/service-alarm/tests/tests-AlarmHandlerFactory.cpp +30 -0
@@ 0,0 1,30 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "MockAlarmHandler.hpp"

#include <service-alarm/AlarmHandlerFactory.hpp>

#include <catch2/catch.hpp>

TEST_CASE("AlarmHandlerFactory Tests")
{
    using namespace alarms;

    SECTION("Create AlarmHandlerFactory")
    {
        AlarmHandlerFactory alarmHandlerFactory;
        REQUIRE(alarmHandlerFactory.getHandler(AlarmType::Clock) == nullptr);
    }

    SECTION("Add/get handler")
    {
        AlarmHandlerFactory alarmHandlerFactory;
        auto handler = std::make_shared<MockAlarmHandler>();
        alarmHandlerFactory.addHandler(AlarmType::Clock, handler);

        REQUIRE(alarmHandlerFactory.getHandler(AlarmType::Clock) != nullptr);
        REQUIRE(alarmHandlerFactory.getHandler(AlarmType::Clock) == handler);
        REQUIRE(alarmHandlerFactory.getHandler(AlarmType::Calendar) == nullptr);
    }
}

A module-services/service-alarm/tests/tests-main.cpp => module-services/service-alarm/tests/tests-main.cpp +5 -0
@@ 0,0 1,5 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include <catch2/catch.hpp>

M products/BellHybrid/CMakeLists.txt => products/BellHybrid/CMakeLists.txt +1 -0
@@ 84,3 84,4 @@ add_standalone_image(BellHybrid)
add_update_package(BellHybrid)

add_subdirectory(services)
add_subdirectory(alarms)

A products/BellHybrid/alarms/AlarmHandlerActions.cpp => products/BellHybrid/alarms/AlarmHandlerActions.cpp +28 -0
@@ 0,0 1,28 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "AlarmHandlerActions.hpp"

#include <service-audio/AudioServiceAPI.hpp>

namespace alarms
{

    auto playAlarmSound(const std::string &soundPath) -> bool
    {
        // playAlarmSound after it will be implemented [BH-660]
        return true;
    }

    auto displayAlarmPopup() -> bool
    {
        // displayAlarmPopup after it will be implemented [BH-566]
        return true;
    }

    auto turnOnFrontlight() -> bool
    {
        // turnOnFrontlight after it will be implemented [BH-756]
        return true;
    }
} // namespace alarms

A products/BellHybrid/alarms/BellAlarmHandler.cpp => products/BellHybrid/alarms/BellAlarmHandler.cpp +33 -0
@@ 0,0 1,33 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <BellAlarmHandler.hpp>
#include <AlarmHandlerActions.hpp>

namespace alarms
{

    auto BellAlarmClockHandler::handle(const AlarmEventRecord &record) -> bool
    {
        LOG_DEBUG("BellAlarmClockHandler");

        auto result = false;

        if (record.enabled) {
            result = playAlarmSound(record.musicTone);
            result = turnOnFrontlight();
            result = displayAlarmPopup();
            return result;
        }

        return result;
    }

    auto EveningReminderHandler::handle(const AlarmEventRecord &record) -> bool
    {
        LOG_DEBUG("EveningReminderHandler");
        // implement this alarm type handling here
        return true;
    }

} // namespace alarms

A products/BellHybrid/alarms/CMakeLists.txt => products/BellHybrid/alarms/CMakeLists.txt +21 -0
@@ 0,0 1,21 @@
add_library(alarms STATIC)

target_sources(alarms
    PRIVATE
        BellAlarmHandler.cpp
        AlarmHandlerActions.cpp
)

target_include_directories(alarms
    PRIVATE
        include
        
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

target_link_libraries(alarms
   PUBLIC
        module-db
        service-alarm
)

A products/BellHybrid/alarms/include/AlarmHandlerActions.hpp => products/BellHybrid/alarms/include/AlarmHandlerActions.hpp +15 -0
@@ 0,0 1,15 @@
// 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 <module-db/Interface/AlarmEventRecord.hpp>

namespace alarms
{

    auto playAlarmSound(const std::string &soundPath) -> bool;
    auto displayAlarmPopup() -> bool;
    auto turnOnFrontlight() -> bool;

} // namespace alarms

A products/BellHybrid/alarms/include/BellAlarmHandler.hpp => products/BellHybrid/alarms/include/BellAlarmHandler.hpp +22 -0
@@ 0,0 1,22 @@
// 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 <service-alarm/AlarmHandler.hpp>

namespace alarms
{

    class BellAlarmClockHandler : public AlarmHandler
    {
      public:
        auto handle(const AlarmEventRecord &record) -> bool;
    };

    class EveningReminderHandler : public AlarmHandler
    {
      public:
        auto handle(const AlarmEventRecord &record) -> bool;
    };
} // namespace alarms