~aleteoryx/muditaos

ref: 2276ceed679b93a3a891e4f5739ade9e13991c5a muditaos/module-db/Interface/AlarmsRecord.cpp -rw-r--r-- 3.7 KiB
2276ceed — Radoslaw Wicik [EGD-3743] Update copyrights in fies 5 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

/*
 * AlarmsRecord.cpp
 *
 *  Created on: 15 lip 2019
 *      Author: kuba
 */

#include "AlarmsRecord.hpp"
#include "AlarmsTable.hpp"

AlarmsRecordInterface::AlarmsRecordInterface(AlarmsDB *alarmsDb) : alarmsDB(alarmsDb)
{}

AlarmsRecordInterface::~AlarmsRecordInterface()
{}

bool AlarmsRecordInterface::Add(const AlarmsRecord &rec)
{
    // Create alarm
    alarmsDB->alarms.add(
        AlarmsTableRow{.time = rec.time, .snooze = rec.snooze, .status = rec.status, .path = rec.path});

    // TODO: error check

    return true;
}

uint32_t AlarmsRecordInterface::GetCount()
{
    return alarmsDB->alarms.count();
}

std::unique_ptr<std::vector<AlarmsRecord>> AlarmsRecordInterface::GetLimitOffsetByField(uint32_t offset,
                                                                                        uint32_t limit,
                                                                                        AlarmsRecordField field,
                                                                                        const char *str)
{
    auto records = std::make_unique<std::vector<AlarmsRecord>>();

    std::vector<AlarmsTableRow> alarm;

    switch (field) {
    case AlarmsRecordField::Time:
        alarm = alarmsDB->alarms.getLimitOffsetByField(offset, limit, AlarmsTableFields::Time, str);
        break;
    case AlarmsRecordField::Snooze:
        alarm = alarmsDB->alarms.getLimitOffsetByField(offset, limit, AlarmsTableFields::Snooze, str);
        break;
    case AlarmsRecordField::Status:
        alarm = alarmsDB->alarms.getLimitOffsetByField(offset, limit, AlarmsTableFields::Status, str);
        break;
    default:
        return records;
    }
    return records;
}

std::unique_ptr<std::vector<AlarmsRecord>> AlarmsRecordInterface::GetLimitOffset(uint32_t offset, uint32_t limit)
{
    auto alarm = alarmsDB->alarms.getLimitOffset(offset, limit);

    auto records = std::make_unique<std::vector<AlarmsRecord>>();

    AlarmsRecordInterface alarmsInterface(alarmsDB);
    for (const auto &w : alarm) {

        auto alarmsRec = alarmsInterface.GetByID(w.ID);

        records->push_back({
            .ID     = w.ID,
            .time   = w.time,
            .snooze = w.snooze,
            .status = w.status,
            .path   = w.path,
        });
    }

    return records;
}

bool AlarmsRecordInterface::Update(const AlarmsRecord &rec)
{

    auto alarm = alarmsDB->alarms.getById(rec.ID);
    if (alarm.ID == 0) {
        return false;
    }

    alarmsDB->alarms.update(
        AlarmsTableRow{.ID = rec.ID, .time = rec.time, .snooze = rec.snooze, .status = rec.status, .path = rec.path});

    return true;
}

bool AlarmsRecordInterface::RemoveByID(uint32_t id)
{

    auto alarm = alarmsDB->alarms.getById(id);
    if (alarm.ID == 0) {
        return false;
    }

    // Remove alarm
    if (alarmsDB->alarms.removeById(id) == false) {
        return false;
    }

    return true;
}

bool AlarmsRecordInterface::RemoveByField(AlarmsRecordField field, const char *str)
{

    switch (field) {
    case AlarmsRecordField ::Time:
        return alarmsDB->alarms.removeByField(AlarmsTableFields::Time, str);

    default:
        return false;
    }
}

AlarmsRecord AlarmsRecordInterface::GetByID(uint32_t id)
{
    auto alarm = alarmsDB->alarms.getById(id);

    return AlarmsRecord{
        .ID = alarm.ID, .time = alarm.time, .snooze = alarm.snooze, .status = alarm.status, .path = alarm.path};
}

AlarmsRecord AlarmsRecordInterface::GetNext(time_t time)
{
    auto alarm = alarmsDB->alarms.next(time);

    return AlarmsRecord{
        .ID = alarm.ID, .time = alarm.time, .snooze = alarm.snooze, .status = alarm.status, .path = alarm.path};
}