2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "AlarmClock.h"
11 #include "ServiceBroker.h"
12 #include "dialogs/GUIDialogKaiToast.h"
13 #include "events/EventLog.h"
14 #include "events/NotificationEvent.h"
15 #include "guilib/LocalizeStrings.h"
17 #include "messaging/ApplicationMessenger.h"
18 #include "utils/StringUtils.h"
23 using namespace std::chrono_literals
;
25 CAlarmClock::CAlarmClock() : CThread("AlarmClock")
29 CAlarmClock::~CAlarmClock() = default;
31 void CAlarmClock::Start(const std::string
& strName
, float n_secs
, const std::string
& strCommand
, bool bSilent
/* false */, bool bLoop
/* false */)
33 // make lower case so that lookups are case-insensitive
34 std::string
lowerName(strName
);
35 StringUtils::ToLower(lowerName
);
37 SAlarmClockEvent event
;
38 event
.m_fSecs
= static_cast<double>(n_secs
);
39 event
.m_strCommand
= strCommand
;
48 uint32_t labelAlarmClock
;
49 uint32_t labelStarted
;
50 if (StringUtils::EqualsNoCase(strName
, "shutdowntimer"))
52 labelAlarmClock
= 20144;
57 labelAlarmClock
= 13208;
61 EventPtr
alarmClockActivity(new CNotificationEvent(
63 StringUtils::Format(g_localizeStrings
.Get(labelStarted
), static_cast<int>(event
.m_fSecs
) / 60,
64 static_cast<int>(event
.m_fSecs
) % 60)));
66 auto eventLog
= CServiceBroker::GetEventLog();
70 eventLog
->Add(alarmClockActivity
);
72 eventLog
->AddWithNotification(alarmClockActivity
);
75 event
.watch
.StartZero();
76 std::unique_lock
<CCriticalSection
> lock(m_events
);
77 m_event
.insert(make_pair(lowerName
,event
));
78 CLog::Log(LOGDEBUG
, "started alarm with name: {}", lowerName
);
81 void CAlarmClock::Stop(const std::string
& strName
, bool bSilent
/* false */)
83 std::unique_lock
<CCriticalSection
> lock(m_events
);
85 std::string
lowerName(strName
);
86 StringUtils::ToLower(lowerName
); // lookup as lowercase only
87 std::map
<std::string
,SAlarmClockEvent
>::iterator iter
= m_event
.find(lowerName
);
89 if (iter
== m_event
.end())
92 uint32_t labelAlarmClock
;
93 if (StringUtils::EqualsNoCase(strName
, "shutdowntimer"))
94 labelAlarmClock
= 20144;
96 labelAlarmClock
= 13208;
98 std::string strMessage
;
101 if (iter
->second
.watch
.IsRunning())
102 elapsed
= iter
->second
.watch
.GetElapsedSeconds();
104 if (elapsed
> static_cast<float>(iter
->second
.m_fSecs
))
105 strMessage
= g_localizeStrings
.Get(13211);
108 float remaining
= static_cast<float>(iter
->second
.m_fSecs
) - elapsed
;
109 strMessage
= StringUtils::Format(g_localizeStrings
.Get(13212), static_cast<int>(remaining
) / 60,
110 static_cast<int>(remaining
) % 60);
113 if (iter
->second
.m_strCommand
.empty() || static_cast<float>(iter
->second
.m_fSecs
) > elapsed
)
115 EventPtr
alarmClockActivity(new CNotificationEvent(labelAlarmClock
, strMessage
));
116 auto eventLog
= CServiceBroker::GetEventLog();
120 eventLog
->Add(alarmClockActivity
);
122 eventLog
->AddWithNotification(alarmClockActivity
);
127 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_EXECUTE_BUILT_IN
, -1, -1, nullptr,
128 iter
->second
.m_strCommand
);
129 if (iter
->second
.m_loop
)
131 iter
->second
.watch
.Reset();
136 iter
->second
.watch
.Stop();
140 void CAlarmClock::Process()
146 std::unique_lock
<CCriticalSection
> lock(m_events
);
147 for (std::map
<std::string
,SAlarmClockEvent
>::iterator iter
=m_event
.begin();iter
!= m_event
.end(); ++iter
)
148 if (iter
->second
.watch
.IsRunning() &&
149 iter
->second
.watch
.GetElapsedSeconds() >= static_cast<float>(iter
->second
.m_fSecs
))
152 if ((iter
= m_event
.find(strLast
)) == m_event
.end())
156 strLast
= iter
->first
;
158 CThread::Sleep(100ms
);