Torc  0.1
torcsystemnotification.cpp
Go to the documentation of this file.
1 /* Class TorcSystemNotification
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 "torccoreutils.h"
23 #include "torcnotify.h"
24 #include "torcsystemnotification.h"
25 
60  : TorcNotification(Details),
61  m_events()
62 {
63  if (uniqueId.isEmpty() || m_notifierNames.isEmpty() || m_body.isEmpty())
64  return;
65 
66  // NB event names are also validated in the xsd
67  if (Details.contains(QStringLiteral("inputs")))
68  {
69  QVariantMap inputs = Details.value(QStringLiteral("inputs")).toMap();
70  QVariantMap::const_iterator it = inputs.constBegin();
71  for ( ; it != inputs.constEnd(); ++it)
72  {
73  if (it.key() == QStringLiteral("event"))
74  {
75  int event = TorcCoreUtils::StringToEnum<Torc::Actions>(it.value().toString().trimmed());
76  if (event != -1)
77  m_events.append((Torc::Actions)event);
78  }
79  }
80  }
81 
82  if (m_events.isEmpty())
83  {
84  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("System notification '%1' has no events to listen for").arg(uniqueId));
85  }
86  else
87  {
89  SetValid(true);
90  }
91 }
92 
94 {
96 }
97 
99 {
100  QStringList result;
101  result.append(tr("System event"));
102  foreach (Torc::Actions event, m_events)
103  result.append(TorcCoreUtils::EnumToString<Torc::Actions>(event));
104  return result;
105 }
106 
107 void TorcSystemNotification::Graph(QByteArray *Data)
108 {
109  if (!Data)
110  return;
111 
112  foreach (TorcNotifier* notifier, m_notifiers)
113  Data->append(QStringLiteral(" \"%1\"->\"%2\"\r\n").arg(uniqueId, notifier->GetUniqueId()));
114 }
115 
117 bool TorcSystemNotification::event(QEvent *Event)
118 {
119  if (Event && (Event->type() == TorcEvent::TorcEventType))
120  {
121  TorcEvent* torcevent = dynamic_cast<TorcEvent*>(Event);
122  if (torcevent)
123  {
124  QMutexLocker locker(&lock);
125  Torc::Actions event = (Torc::Actions)torcevent->GetEvent();
126  if (m_events.contains(event))
127  {
128  QMap<QString,QString> custom;
129  custom.insert(QStringLiteral("event"), TorcCoreUtils::EnumToString<Torc::Actions>(event));
130  QVariantMap message = TorcNotify::gNotify->SetNotificationText(m_title, m_body, custom);
131  emit Notify(message);
132  }
133  }
134  }
135 
136  return TorcNotification::event(Event);
137 }
138 
141 {
142  TorcNotification* Create(const QString &Type, const QVariantMap &Details) override
143  {
144  if (Type == QStringLiteral("system") && Details.contains(QStringLiteral("inputs")))
145  return new TorcSystemNotification(Details);
146  return nullptr;
147  }
149 
QStringList m_notifierNames
TorcLocalContext * gLocalContext
TorcSystemNotification(const QVariantMap &Details)
QString uniqueId
Definition: torcdevice.h:63
virtual void SetValid(bool Valid)
Definition: torcdevice.cpp:101
bool event(QEvent *Event) override
Listen for system events (TorcEvent).
int GetEvent(void)
Return the Torc action associated with this event.
Definition: torcevent.cpp:65
void Graph(QByteArray *Data) override
QList< TorcNotifier * > m_notifiers
TorcSystemNotificationFactory TorcSystemNotificationFactory
QMutex lock
Definition: torcdevice.h:66
static Type TorcEventType
Register TorcEventType with QEvent.
Definition: torcevent.h:19
double value
Definition: torcdevice.h:60
A general purpose event object.
Definition: torcevent.h:9
QStringList GetDescription(void) override
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
void AddObserver(QObject *Observer)
brief Register the given object to receive events.
QVariantMap SetNotificationText(const QString &Title, const QString &Body, const QMap< QString, QString > &Custom)
Definition: torcnotify.cpp:102
Create instances of TorcSystemNotification.
void RemoveObserver(QObject *Observer)
brief Deregister the given object.
void Notify(const QVariantMap &Message)
static TorcNotify * gNotify
Definition: torcnotify.h:19
QString GetUniqueId(void)
Definition: torcdevice.cpp:162