Torc  0.1
torcnotification.cpp
Go to the documentation of this file.
1 /* Class TorcNotification
2 *
3 * Copyright (C) Mark Kendall 2016-18
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 // Torc
21 #include "torclogging.h"
22 #include "torcnotify.h"
23 #include "torcnotification.h"
24 
32 TorcNotification::TorcNotification(const QVariantMap &Details)
33  : TorcDevice(false, 0, 0, QStringLiteral("Notification"), Details),
34  m_notifierNames(),
35  m_notifiers(),
36  m_title(),
37  m_body()
38 {
39  if (Details.contains(QStringLiteral("outputs")) && Details.contains(QStringLiteral("message")))
40  {
41  QVariantMap message = Details.value(QStringLiteral("message")).toMap();
42  if (message.contains(QStringLiteral("body")))
43  {
44  m_body = message.value(QStringLiteral("body")).toString();
45  // title is optional
46  if (message.contains(QStringLiteral("title")))
47  m_title = message.value(QStringLiteral("title")).toString();
48 
49  QVariantMap outputs = Details.value(QStringLiteral("outputs")).toMap();
50  QVariantMap::const_iterator it = outputs.constBegin();
51  for (; it != outputs.constEnd(); ++it)
52  if (it.key() == QStringLiteral("notifier"))
53  m_notifierNames.append(it.value().toString().trimmed());
54  }
55  }
56 
57  if (m_notifierNames.isEmpty())
58  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Notifier is incomplete"));
59 }
60 
62 {
63  QMutexLocker locker(&lock);
64 
65  if (uniqueId.isEmpty())
66  return false;
67 
68  foreach (QString notifiername, m_notifierNames)
69  {
70  TorcNotifier* notifier = TorcNotify::gNotify->FindNotifierByName(notifiername);
71  if (notifier)
72  {
73  connect(this,&TorcNotification::Notify, notifier, &TorcNotifier::Notify);
74  m_notifiers.append(notifier);
75  }
76  else
77  {
78  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Notification '%1' failed to find notifier '%2'").arg(uniqueId, notifiername));
79  }
80  }
81 
82  return true;
83 }
84 
85 bool TorcNotification::IsKnownInput(const QString &UniqueId)
86 {
87  (void)UniqueId;
88  return false;
89 }
90 
92 
94  : nextTorcNotificationFactory(gTorcNotificationFactory)
95 {
97 }
98 
100 {
102 }
103 
105 {
107 }
108 
109 
QStringList m_notifierNames
TorcNotification(const QVariantMap &Details)
QString uniqueId
Definition: torcdevice.h:63
TorcNotificationFactory * nextTorcNotificationFactory
QList< TorcNotifier * > m_notifiers
QMutex lock
Definition: torcdevice.h:66
double value
Definition: torcdevice.h:60
TorcNotificationFactory * NextFactory(void) const
virtual void Notify(const QVariantMap &Notification)=0
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
TorcNotifier * FindNotifierByName(const QString &Name)
Definition: torcnotify.cpp:54
virtual bool Setup(void)
static TorcNotificationFactory * gTorcNotificationFactory
void Notify(const QVariantMap &Message)
virtual bool IsKnownInput(const QString &UniqueId)
static TorcNotify * gNotify
Definition: torcnotify.h:19
static TorcNotificationFactory * GetTorcNotificationFactory(void)