~aleteoryx/muditaos

ref: e338b69fabdaff2c885d517b559bb13a155241b4 muditaos/module-services/service-time/AlarmEventsDBRepository.cpp -rw-r--r-- 5.3 KiB
e338b69f — Paweł Joński [BH-642] Handle alarm messages in service-time 4 years 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "AlarmEventsDBRepository.hpp"
#include <service-db/DBServiceAPI.hpp>

#include <module-db/queries/alarm_events/QueryAlarmEventsAdd.hpp>
#include <module-db/queries/alarm_events/QueryAlarmEventsEdit.hpp>
#include <module-db/queries/alarm_events/QueryAlarmEventsGet.hpp>
#include <module-db/queries/alarm_events/QueryAlarmEventsGetBetweenDates.hpp>
#include <module-db/queries/alarm_events/QueryAlarmEventsGetLimited.hpp>
#include <module-db/queries/alarm_events/QueryAlarmEventsGetNext.hpp>
#include <module-db/queries/alarm_events/QueryAlarmEventsGetRecurringBetweenDates.hpp>
#include <module-db/queries/alarm_events/QueryAlarmEventsRemove.hpp>

namespace alarms
{
    AlarmEventsDBRepository::AlarmEventsDBRepository(sys::Service *service) : service{service}
    {}

    auto AlarmEventsDBRepository::getAlarmEvent(const std::uint32_t alarmId, const OnGetAlarmEventCallback &callback)
        -> void
    {
        sendQueryWithCallback<db::query::alarmEvents::Get, db::query::alarmEvents::GetResult, OnGetAlarmEventCallback>(
            callback, alarmId);
    }

    auto AlarmEventsDBRepository::getAlarmEvents(std::uint32_t offset,
                                                 std::uint32_t limit,
                                                 const OnGetAlarmEventsCallback &callback) -> void
    {
        sendQueryWithCallback<db::query::alarmEvents::GetLimited,
                              db::query::alarmEvents::GetLimitedResult,
                              OnGetAlarmEventsCallback>(callback, offset, limit);
    }

    auto AlarmEventsDBRepository::getAlarmEventsInRange(TimePoint start,
                                                        TimePoint end,
                                                        std::uint32_t offset,
                                                        std::uint32_t limit,
                                                        const OnGetAlarmEventsInRangeCallback &callback) -> void
    {
        sendQueryWithCallback<db::query::alarmEvents::GetBetweenDates,
                              db::query::alarmEvents::GetBetweenDatesResult,
                              OnGetAlarmEventsInRangeCallback>(callback, start, end, offset, limit);
    }

    auto AlarmEventsDBRepository::addAlarmEvent(const AlarmEventRecord &alarmEvent,
                                                const OnAddAlarmEventCallback &callback) -> void
    {
        sendQueryWithCallback<db::query::alarmEvents::Add, db::query::alarmEvents::AddResult, OnAddAlarmEventCallback>(
            callback, alarmEvent);
    }

    auto AlarmEventsDBRepository::updateAlarmEvent(const AlarmEventRecord &alarmEvent,
                                                   const OnUpdateAlarmEventCallback &callback) -> void
    {
        sendQueryWithCallback<db::query::alarmEvents::Edit,
                              db::query::alarmEvents::EditResult,
                              OnUpdateAlarmEventCallback>(callback, alarmEvent);
    }

    auto AlarmEventsDBRepository::removeAlarmEvent(const std::uint32_t id, const OnRemoveAlarmEventCallback &callback)
        -> void
    {
        sendQueryWithCallback<db::query::alarmEvents::Remove,
                              db::query::alarmEvents::RemoveResult,
                              OnRemoveAlarmEventCallback>(callback, id);
    }

    auto AlarmEventsDBRepository::getNext(TimePoint start,
                                          std::uint32_t offset,
                                          std::uint32_t limit,
                                          const OnGetNextCallback &callback) -> void
    {
        sendQueryWithCallback<db::query::alarmEvents::GetNext,
                              db::query::alarmEvents::GetNextResult,
                              OnGetNextCallback>(callback, start, offset, limit);
    }

    auto AlarmEventsDBRepository::getAlarmEventsRecurringInRange(TimePoint start,
                                                                 TimePoint end,
                                                                 std::uint32_t offset,
                                                                 std::uint32_t limit,
                                                                 const OnGetAlarmEventsRecurringInRange &callback)
        -> void
    {
        sendQueryWithCallback<db::query::alarmEvents::GetRecurringBetweenDates,
                              db::query::alarmEvents::GetRecurringBetweenDatesResult,
                              OnGetAlarmEventsRecurringInRange>(callback, start, end, offset, limit);
    }

    template <typename requestT, typename responseT, typename callbackT, typename... Types>
    void AlarmEventsDBRepository::sendQueryWithCallback(const callbackT &callback, Types... queryArgs)
    {
        auto query = std::make_unique<requestT>(queryArgs...);

        query->setQueryListener(db::QueryCallback::fromFunction([callback](auto response) {
            auto result = dynamic_cast<responseT *>(response);
            if (result == nullptr) {
                return false;
            }
            if (callback) {
                callback(result->getResult());
            }
            return true;
        }));
        DBServiceAPI::GetQuery(service, db::Interface::Name::AlarmEvents, std::move(query));
    }

} // namespace alarms