~aleteoryx/muditaos

muditaos/module-services/service-desktop/endpoints/timeSync/TimeSyncHelper.cpp -rw-r--r-- 2.5 KiB
a405cad6Aleteoryx trim readme 7 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <endpoints/timeSync/TimeSyncHelper.hpp>
#include <endpoints/Context.hpp>
#include <endpoints/JsonKeyNames.hpp>
#include <service-time/ServiceTimeName.hpp>
#include <service-time/service-time/TimeMessage.hpp>
#include <Service/Service.hpp>
#include <log/log.hpp>

namespace sdesktop::endpoints
{
    auto TimeSyncHelper::processGet(Context &context) -> ProcessResult
    {
        const auto &body = context.getBody();

        /* Validate */
        if (body[json::timeSync::value] != json::timeSync::timestamp) {
            return {Sent::No, ResponseContext{.status = http::Code::BadRequest}};
        }

        std::time_t currentTimestamp;
        std::time(&currentTimestamp);

        json11::Json::object response({{json::timeSync::timestamp, currentTimestamp}});
        return {Sent::No, ResponseContext{.status = http::Code::OK, .body = std::move(response)}};
    }

    auto TimeSyncHelper::processPost(Context &context) -> ProcessResult
    {
        const auto &jsonTimestamp = context.getBody()[json::timeSync::timestamp];

        /* Validate */
        if (!jsonTimestamp.is_number()) {
            LOG_ERROR("Timestamp data type not a number!");
            return {Sent::No, ResponseContext{.status = http::Code::UnprocessableEntity}};
        }

        const auto timestamp = static_cast<std::time_t>(jsonTimestamp.number_value());
        logReceivedDateTime(timestamp);
        if (const auto success = sendTimeUpdateMessage(timestamp); !success) {
            LOG_ERROR("Failed to send time update message!");
            return {Sent::No, ResponseContext{.status = http::Code::InternalServerError}};
        }

        return {Sent::No, ResponseContext{.status = http::Code::OK}};
    }

    auto TimeSyncHelper::logReceivedDateTime(std::time_t timestamp) const noexcept -> void
    {
        const auto t = localtime(&timestamp);
        LOG_INFO("Received time set request: %d-%02d-%02d %02d:%02d:%02d",
                 t->tm_year + 1900,
                 t->tm_mon + 1,
                 t->tm_mday,
                 t->tm_hour,
                 t->tm_min,
                 t->tm_sec);
    }

    auto TimeSyncHelper::sendTimeUpdateMessage(std::time_t timestamp) const -> bool
    {
        auto msg = std::make_shared<stm::message::TimeChangeRequestMessage>(timestamp);
        return owner->bus.sendUnicast(std::move(msg), service::name::service_time);
    }
} // namespace sdesktop::endpoints