~aleteoryx/muditaos

ref: 452135b30414899b65010069deee3d8b2dbb39ff muditaos/module-services/service-evtmgr/alarm/EventManagerAlarm.cpp -rw-r--r-- 2.4 KiB
452135b3 — Piotr Tanski [EGD-4373] Fix for changing the content of option window. (#1054) 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <service-evtmgr/EventManager.hpp>
#include <service-evtmgr/EVMessages.hpp>

#include <AlarmsRecord.hpp>
#include <MessageType.hpp>
#include <Service/Bus.hpp>
#include <SwitchData.hpp>
#include <service-appmgr/Controller.hpp>
#include <service-db/DBServiceAPI.hpp>     // for DBServiceAPI

#include <memory>
#include <string>
#include <sys/types.h>
#include <utility>

namespace sys
{
    class DataMessage;
} // namespace sys

void EventManager::HandleAlarmTrigger(sys::DataMessage *msgl)
{
    sevm::RtcMinuteAlarmMessage *msg = reinterpret_cast<sevm::RtcMinuteAlarmMessage *>(msgl);

    // Send message to update time in application
    auto message       = std::make_shared<sevm::RtcMinuteAlarmMessage>(MessageType::EVMMinuteUpdated);
    message->timestamp = msg->timestamp;

    if (targetApplication.empty() == false) {
        sys::Bus::SendUnicast(message, targetApplication, this);
    }

    time_t currentTime = message->timestamp;

    // it there is no valid alarm and DB is not empty
    if (alarmIsValid == false) {
        // check if its midnight
        if (currentTime % 86400 == 0) {
            alarmDBEmpty = false;
        }

        if (alarmDBEmpty == false)
            // get next alarm
            GetNextAlarmTimestamp(currentTime);
    }

    // there is valid alarm
    if (alarmIsValid == true) {
        // check if its time to trigger alarm
        // round time and compare to alaram timestamp
        if (alarmTimestamp == (currentTime % 86400)) {
            alarmIsValid = false;
            // run bell application
            std::unique_ptr<gui::SwitchData> switchMessage = std::make_unique<sevm::EVMAlarmSwitchData>(alarmID);
            app::manager::Controller::switchApplication(this, "bell", "main", std::move(switchMessage));
        }
        // check if alarm is not obsolete
        else if ((alarmTimestamp < (currentTime % 86400)) && ((currentTime + 60) % 86400) > (currentTime % 86400)) {
            alarmIsValid = false;
        }
    }
}

void EventManager::GetNextAlarmTimestamp(time_t timestamp)
{
    AlarmsRecord record;
    record = DBServiceAPI::AlarmGetNext(this, ((timestamp) % 86400));

    if (record.ID != 0) {
        alarmIsValid   = true;
        alarmTimestamp = record.time;
        alarmID        = record.ID;
    }
    else {
        alarmDBEmpty = true;
    }
}