~aleteoryx/muditaos

f6b6cd9f8cd6718eb6ec5db8b232086d67fb1f83 — Kuba 4 years ago a696e57
[EGD-6986] Add Store timezone in settings db

Adds storing current timezone in settings database.
It allows to proper handling of timezone on system startup.
M image/user/db/settings_v2_002.sql => image/user/db/settings_v2_002.sql +3 -1
@@ 35,4 35,6 @@ INSERT OR IGNORE INTO settings_tab (path, value) VALUES
    ('\EventManager\\br_state', '0'),
    ('\EventManager\\br_auto_mode', '0'),
    ('\EventManager\\br_level', '50.0f'),
    ('keypad_light_state', '0');
    ('keypad_light_state', '0'),
    ('gs_current_timezone', '');


M module-services/service-db/agents/settings/SystemSettings.hpp => module-services/service-db/agents/settings/SystemSettings.hpp +1 -0
@@ 21,6 21,7 @@ namespace settings
        constexpr inline auto eulaAccepted             = "gs_eula_accepted";
        constexpr inline auto osCurrentVersion         = "gs_os_current_version";
        constexpr inline auto osUpdateVersion          = "gs_os_update_version";
        constexpr inline auto currentTimezone          = "gs_current_timezone";
    } // namespace SystemProperties
    namespace Bluetooth
    {

M module-services/service-time/ServiceTime.cpp => module-services/service-time/ServiceTime.cpp +57 -27
@@ 7,6 7,7 @@
#include "service-time/RTCCommand.hpp"
#include <service-time/internal/StaticData.hpp>
#include <service-time/TimeSettings.hpp>
#include <service-time/TimezoneHandler.hpp>

#include <BaseInterface.hpp>
#include <Common/Query.hpp>


@@ 16,10 17,12 @@
#include <service-db/DBNotificationMessage.hpp>
#include <service-db/QueryMessage.hpp>
#include <service-cellular/service-cellular/CellularMessage.hpp>
#include <service-cellular/service-cellular/ServiceCellular.hpp>
#include <module-utils/Utils.hpp>
#include <service-cellular/ServiceCellular.hpp>
#include <time/time_constants.hpp>
#include <time/time_conversion_factory.hpp>
#include <service-evtmgr/Constants.hpp>
#include <service-db/service-db/Settings.hpp>
#include <service-db/agents/settings/SystemSettings.hpp>

#include <memory>
#include <utility>


@@ 135,13 138,7 @@ namespace stm
    void ServiceTime::registerMessageHandlers()
    {
        connect(typeid(CellularTimeNotificationMessage), [&](sys::Message *request) -> sys::MessagePointer {
            auto message = static_cast<CellularTimeNotificationMessage *>(request);
            if (stm::api::isAutomaticDateAndTime()) {
                timeManager->handleCellularTimeUpdate(
                    message->getTime().value(),
                    std::chrono::minutes{message->getTimeZoneOffset().value() / utils::time::secondsInMinute});
            }
            return std::make_shared<sys::ResponseMessage>();
            return handleCellularTimeNotificationMessage(request);
        });

        connect(typeid(stm::message::SetAutomaticDateAndTimeRequest),


@@ 158,7 155,11 @@ namespace stm

        connect(typeid(stm::message::SetDateFormatRequest),
                [&](sys::Message *request) -> sys::MessagePointer { return handleSetDateFormatRequest(request); });

        connect(typeid(stm::message::SetTimezoneRequest),
                [&](sys::Message *request) -> sys::MessagePointer { return handleSetTimezoneRequest(request); });
    }

    auto ServiceTime::handleSetAutomaticDateAndTimeRequest(sys::Message *request)
        -> std::shared_ptr<sys::ResponseMessage>
    {


@@ 167,9 168,7 @@ namespace stm
            LOG_WARN("The selected value is already set. Ignore.");
            return std::shared_ptr<sys::ResponseMessage>();
        }
        settings->setValue(settings::SystemProperties::automaticDateAndTimeIsOn,
                           std::to_string(message->getValue()),
                           settings::SettingsScope::AppLocal);
        settings->setValue(settings::SystemProperties::automaticDateAndTimeIsOn, std::to_string(message->getValue()));
        stm::internal::StaticData::get().setAutomaticDateAndTime(message->getValue());

        bus.sendUnicast(std::make_shared<stm::message::AutomaticDateAndTimeChangedMessage>(message->getValue()),


@@ 184,9 183,7 @@ namespace stm
            LOG_WARN("The selected value is already set. Ignore.");
            return std::shared_ptr<sys::ResponseMessage>();
        }
        settings->setValue(settings::SystemProperties::automaticTimeZoneIsOn,
                           std::to_string(message->getValue()),
                           settings::SettingsScope::AppLocal);
        settings->setValue(settings::SystemProperties::automaticTimeZoneIsOn, std::to_string(message->getValue()));
        stm::internal::StaticData::get().setAutomaticTimezoneOn(message->getValue());
        return std::shared_ptr<sys::ResponseMessage>();
    }


@@ 199,8 196,7 @@ namespace stm
            return std::shared_ptr<sys::ResponseMessage>();
        }
        settings->setValue(settings::SystemProperties::timeFormat,
                           std::to_string(static_cast<unsigned>(message->getTimeFormat())),
                           settings::SettingsScope::AppLocal);
                           std::to_string(static_cast<unsigned>(message->getTimeFormat())));
        stm::internal::StaticData::get().setTimeFormat(message->getTimeFormat());
        return std::shared_ptr<sys::ResponseMessage>();
    }


@@ 213,28 209,62 @@ namespace stm
            return std::shared_ptr<sys::ResponseMessage>();
        }
        settings->setValue(settings::SystemProperties::dateFormat,
                           std::to_string(static_cast<unsigned>(message->getDateFormat())),
                           settings::SettingsScope::AppLocal);
                           std::to_string(static_cast<unsigned>(message->getDateFormat())));
        stm::internal::StaticData::get().setDateFormat(message->getDateFormat());
        return std::shared_ptr<sys::ResponseMessage>();
    }

    auto ServiceTime::handleSetTimezoneRequest(sys::Message *request) -> std::shared_ptr<sys::ResponseMessage>
    {
        auto message = static_cast<stm::message::SetTimezoneRequest *>(request);

        timeManager->handleTimezoneChangeRequest(message->getTimezone());
        settings->setValue(settings::SystemProperties::currentTimezone, message->getTimezone());
        stm::internal::StaticData::get().setTimezone(message->getTimezone());
        return std::shared_ptr<sys::ResponseMessage>();
    }

    auto ServiceTime::handleCellularTimeNotificationMessage(sys::Message *request)
        -> std::shared_ptr<sys::ResponseMessage>
    {
        auto message  = static_cast<CellularTimeNotificationMessage *>(request);
        auto timezone = TimezoneHandler(std::chrono::duration_cast<std::chrono::minutes>(
                                            std::chrono::seconds{message->getTimeZoneOffset().value()}))
                            .getTimezone();
        if (stm::api::isAutomaticDateAndTime()) {
            timeManager->handleCellularTimeUpdate(message->getTime().value(), timezone);
            settings->setValue(settings::SystemProperties::currentTimezone, timezone);
            stm::internal::StaticData::get().setTimezone(timezone);
        }
        else if (stm::api::isAutomaticTimezone()) {
            timeManager->handleTimezoneChangeRequest(timezone);
            settings->setValue(settings::SystemProperties::currentTimezone, timezone);
            stm::internal::StaticData::get().setTimezone(timezone);
        }

        return std::make_shared<sys::ResponseMessage>();
    }

    void ServiceTime::initStaticData()
    {
        stm::internal::StaticData::get().setAutomaticDateAndTime(utils::getNumericValue<bool>(settings->getValue(
            ::settings::SystemProperties::automaticDateAndTimeIsOn, settings::SettingsScope::AppLocal)));
        stm::internal::StaticData::get().setAutomaticTimezoneOn(utils::getNumericValue<bool>(settings->getValue(
            ::settings::SystemProperties::automaticTimeZoneIsOn, settings::SettingsScope::AppLocal)));
        auto dateFormat = magic_enum::enum_cast<utils::time::Locale::DateFormat>(utils::getNumericValue<unsigned int>(
            settings->getValue(::settings::SystemProperties::dateFormat, settings::SettingsScope::AppLocal)));
        stm::internal::StaticData::get().setAutomaticDateAndTime(
            utils::getNumericValue<bool>(settings->getValue(::settings::SystemProperties::automaticDateAndTimeIsOn)));
        stm::internal::StaticData::get().setAutomaticTimezoneOn(
            utils::getNumericValue<bool>(settings->getValue(::settings::SystemProperties::automaticTimeZoneIsOn)));
        auto dateFormat = magic_enum::enum_cast<utils::time::Locale::DateFormat>(
            utils::getNumericValue<unsigned int>(settings->getValue(::settings::SystemProperties::dateFormat)));
        if (dateFormat != std::nullopt) {
            stm::internal::StaticData::get().setDateFormat(dateFormat.value());
        }
        auto timeFormat = magic_enum::enum_cast<utils::time::Locale::TimeFormat>(utils::getNumericValue<unsigned int>(
            settings->getValue(::settings::SystemProperties::timeFormat, settings::SettingsScope::AppLocal)));
        auto timeFormat = magic_enum::enum_cast<utils::time::Locale::TimeFormat>(
            utils::getNumericValue<unsigned int>(settings->getValue(::settings::SystemProperties::timeFormat)));
        if (timeFormat != std::nullopt) {
            stm::internal::StaticData::get().setTimeFormat(timeFormat.value());
        }
        auto timezone =
            settings->getValue(settings::SystemProperties::currentTimezone, settings::SettingsScope::AppLocal);
        stm::internal::StaticData::get().setTimezone(timezone);
        timeManager->handleTimezoneChangeRequest(timezone);
    }

} /* namespace stm */

M module-services/service-time/ServiceTime.hpp => module-services/service-time/ServiceTime.hpp +2 -1
@@ 42,7 42,8 @@ namespace stm
        auto handleSetAutomaticTimezoneRequest(sys::Message *request) -> std::shared_ptr<sys::ResponseMessage>;
        auto handleSetTimeFormatRequest(sys::Message *request) -> std::shared_ptr<sys::ResponseMessage>;
        auto handleSetDateFormatRequest(sys::Message *request) -> std::shared_ptr<sys::ResponseMessage>;

        auto handleSetTimezoneRequest(sys::Message *request) -> std::shared_ptr<sys::ResponseMessage>;
        auto handleCellularTimeNotificationMessage(sys::Message *request) -> std::shared_ptr<sys::ResponseMessage>;
        void initStaticData();

      public:

M module-services/service-time/TimeManager.cpp => module-services/service-time/TimeManager.cpp +7 -4
@@ 4,12 4,10 @@
#include <service-time/TimeManager.hpp>
#include <service-time/TimezoneHandler.hpp>

#include <chrono>
#include "time.h"
#include <ctime>

void TimeManager::handleCellularTimeUpdate(struct tm time, std::chrono::minutes timezoneOffset)
void TimeManager::handleCellularTimeUpdate(struct tm time, const std::string &timezone)
{
    auto timezone = TimezoneHandler(timezoneOffset).getTimezone();
    rtcCommand->setTime(time);
    rtcCommand->setTimezone(timezone);
}


@@ 18,3 16,8 @@ void TimeManager::handleTimeChangeRequest(const time_t &time)
{
    rtcCommand->setTime(time);
}

void TimeManager::handleTimezoneChangeRequest(const std::string &timezone)
{
    rtcCommand->setTimezone(timezone);
}

M module-services/service-time/api/TimeSettingsApi.cpp => module-services/service-time/api/TimeSettingsApi.cpp +4 -0
@@ 31,4 31,8 @@ namespace stm::api
    {
        return stm::internal::StaticData::get().getTimeFormat() == utils::time::Locale::TimeFormat::FormatTime12H;
    }
    const std::string getCurrentTimezone()
    {
        return stm::internal::StaticData::get().getCurrentTimezone();
    }
} // namespace stm::api
\ No newline at end of file

M module-services/service-time/api/TimeSettingsApi.hpp => module-services/service-time/api/TimeSettingsApi.hpp +5 -0
@@ 32,4 32,9 @@ namespace stm::api
     * @return true when 12h format is set, false when 24h format is set
     */
    bool isTimeFormat12h();
    /**
     * Gets value corresponded to current timezone setting stored in DB
     * @return current timezone
     */
    const std::string getCurrentTimezone();
} // namespace stm::api

M module-services/service-time/internal/StaticData.cpp => module-services/service-time/internal/StaticData.cpp +8 -0
@@ 51,5 51,13 @@ namespace stm::internal
    {
        return timeFormat;
    }
    void StaticData::setTimezone(const std::string &newTimezone)
    {
        timezone = newTimezone;
    }
    std::string StaticData::getCurrentTimezone() const
    {
        return timezone;
    }

} // namespace stm::internal
\ No newline at end of file

M module-services/service-time/internal/StaticData.hpp => module-services/service-time/internal/StaticData.hpp +11 -0
@@ 17,6 17,7 @@ namespace stm::internal
        bool isAutomaticTimezoneOn                 = false;
        utils::time::Locale::DateFormat dateFormat = utils::time::Locale::DateFormat::DD_MM_YYYY;
        utils::time::Locale::TimeFormat timeFormat = utils::time::Locale::TimeFormat::FormatTime12H;
        std::string timezone;

        StaticData() = default;



@@ 68,5 69,15 @@ namespace stm::internal
         * @return actual setting value
         */
        [[nodiscard]] utils::time::Locale::TimeFormat getTimeFormat() const noexcept;
        /**
         * Sets value corresponded to current Timezone setting
         * @param timezone new timezone to set
         */
        void setTimezone(const std::string &newTimezone);
        /**
         * Gets value corresponded to current Timezone setting
         * @retrun actual timezone setting
         */
        [[nodiscard]] std::string getCurrentTimezone() const;
    };
} // namespace stm::internal
\ No newline at end of file

M module-services/service-time/service-time/TimeManager.hpp => module-services/service-time/service-time/TimeManager.hpp +7 -3
@@ 6,7 6,6 @@
#include "RTCCommandInterface.hpp"

#include <ctime>
#include <chrono>

class TimeManager
{


@@ 22,14 21,19 @@ class TimeManager
    /**
     * Handles GSM time update notification. Sets custom timezone.
     * @param time new UTC time
     * @param timezoneOffset timezone offset related to UTC time
     * @param timezone formatted timezone string
     */
    void handleCellularTimeUpdate(const struct tm time, std::chrono::minutes timezoneOffset);
    void handleCellularTimeUpdate(const struct tm time, const std::string &timezone);
    /**
     * Handles time change request.
     * @param time UTC time to set
     * **/
    void handleTimeChangeRequest(const time_t &time);
    /**
     * Handles timezone change request
     * @param timezone formatted timezone string
     */
    void handleTimezoneChangeRequest(const std::string &timezone);

  private:
    std::unique_ptr<RTCCommandInterface> rtcCommand;

M module-services/service-time/service-time/TimeMessage.hpp => module-services/service-time/service-time/TimeMessage.hpp +15 -0
@@ 182,4 182,19 @@ namespace stm::message
      private:
        utils::time::Locale::DateFormat dateFormat;
    };

    class SetTimezoneRequest : public sys::DataMessage
    {
      public:
        explicit SetTimezoneRequest(const std::string &timezoneRules)
            : sys::DataMessage(MessageType::MessageTypeUninitialized), timezone(timezoneRules)
        {}
        auto getTimezone() const -> std::string
        {
            return timezone;
        }

      private:
        std::string timezone;
    };
} // namespace stm::message