Torc  0.1
torctriggernotification.cpp
Go to the documentation of this file.
1 /* Class TorcTriggerNotification
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 // Qt
21 #include <QLocale>
22 
23 // Torc
24 #include "torclogging.h"
25 #include "torcdevice.h"
26 #include "torcnotify.h"
28 
86  : TorcNotification(Details),
87  m_inputName(),
88  m_input(nullptr),
89  m_lastValue(0.0),
90  m_triggerHigh(true),
91  m_customData(),
92  m_references(),
93  m_referenceDevices()
94 {
95  if (uniqueId.isEmpty() || m_notifierNames.isEmpty() || m_body.isEmpty())
96  return;
97 
98  if (Details.contains(QStringLiteral("inputs")))
99  {
100  QVariantMap inputs = Details.value(QStringLiteral("inputs")).toMap();
101  QVariantMap::const_iterator it = inputs.constBegin();
102  for ( ; it != inputs.constEnd(); ++it)
103  {
104  if (it.key() == QStringLiteral("device"))
105  {
106  m_inputName = it.value().toString().trimmed();
107  m_customData.insert(QStringLiteral("inputname"), m_inputName);
108  m_customData.insert(QStringLiteral("name"), userName);
109  break;
110  }
111  }
112  }
113 
114  if (Details.contains(QStringLiteral("triggerlow")))
115  {
116  m_triggerHigh = false;
117  m_lastValue = 1.0;
118  }
119 
120  if (Details.contains(QStringLiteral("references")))
121  {
122  QVariantMap references = Details.value(QStringLiteral("references")).toMap();
123  QVariantMap::const_iterator it = references.constBegin();
124  for ( ; it != references.constEnd(); ++it)
125  {
126  if (it.key() == QStringLiteral("device"))
127  m_references.append(it.value().toString().trimmed());
128  }
129  }
130 }
131 
132 bool TorcTriggerNotification::IsKnownInput(const QString &UniqueId)
133 {
134  QMutexLocker locker(&lock);
135  return UniqueId == m_inputName;
136 }
137 
139 {
140  if (m_triggerHigh)
141  return QStringList() << tr("Trigger 0 to 1");
142  return QStringList() << tr("Trigger 1 to 0");
143 }
144 
145 void TorcTriggerNotification::Graph(QByteArray *Data)
146 {
147  if (!Data)
148  return;
149 
150  if (m_input)
151  Data->append(QStringLiteral(" \"%2\"->\"%1\"\r\n").arg(uniqueId, m_input->GetUniqueId()));
152 
153  foreach (TorcDevice *device, m_referenceDevices)
154  Data->append(QStringLiteral(" \"%1\"->\"%2\" [style=dashed]\r\n").arg(device->GetUniqueId(), uniqueId));
155 
156  foreach (TorcNotifier* notifier, m_notifiers)
157  Data->append(QStringLiteral(" \"%1\"->\"%2\"\r\n").arg(uniqueId, notifier->GetUniqueId()));
158 }
159 
161 {
162  // N.B. this should be thread safe as InputValueChanged is always triggered via a signal
163  TorcDevice* input = qobject_cast<TorcDevice*>(sender());
164 
165  if (input && (input == m_input))
166  {
167  if ((Value > 0.0 && m_lastValue <= 0.0 && m_triggerHigh) || // low to high
168  (Value <= 0.0 && m_lastValue > 0.0 && !m_triggerHigh)) // high to low
169  {
170  // add 'key'-'value' data for each reference (e.g. 'tanktemperature', '27.0')
171  foreach (TorcDevice* device, m_referenceDevices)
172  m_customData.insert(device->GetUniqueId(), QString::number(device->GetValue()));
173  QVariantMap message = TorcNotify::gNotify->SetNotificationText(m_title, m_body, m_customData);
174  emit Notify(message);
175  }
176 
177  m_lastValue = Value;
178  }
179  else
180  {
181  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("TriggerNotifiction '%1' signalled from unknown input").arg(uniqueId));
182  }
183 }
184 
190 {
191  if (m_inputName.isEmpty())
192  return false;
193 
194  // setup the input
195  TorcDevice* input = nullptr;
196  {
197  QMutexLocker locker(gDeviceListLock);
198  if (!m_inputName.isEmpty() && gDeviceList->contains(m_inputName))
199  input = gDeviceList->value(m_inputName);
200  }
201 
202  if (!input)
203  {
204  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Failed to find input '%1' for trigger notification '%2'").arg(m_inputName, uniqueId));
205  return false;
206  }
207 
209  m_input = input;
210 
211  // check for the existence of reference devices
212  foreach (QString reference, m_references)
213  {
214  QMutexLocker locker(gDeviceListLock);
215  if (!reference.isEmpty() && gDeviceList->contains(reference))
216  m_referenceDevices.append(gDeviceList->value(reference));
217  }
218 
219  return TorcNotification::Setup();
220 }
221 
223 {
224  TorcNotification* Create(const QString &Type, const QVariantMap &Details) override
225  {
226  if (Type == QStringLiteral("trigger") && Details.contains(QStringLiteral("inputs")))
227  return new TorcTriggerNotification(Details);
228  return nullptr;
229  }
virtual bool Setup(void) override
Finalise the notification.
QStringList m_notifierNames
void ValueChanged(double Value)
QStringList GetDescription(void) override
QString uniqueId
Definition: torcdevice.h:63
static QMutex * gDeviceListLock
Definition: torcdevice.h:69
static QHash< QString, TorcDevice * > * gDeviceList
Definition: torcdevice.h:68
VERBOSE_PREAMBLE true
TorcTriggerNotification(const QVariantMap &Details)
QList< TorcNotifier * > m_notifiers
QMutex lock
Definition: torcdevice.h:66
TorcTriggerNotificationFactory TorcTriggerNotificationFactory
double value
Definition: torcdevice.h:60
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
QVariantMap SetNotificationText(const QString &Title, const QString &Body, const QMap< QString, QString > &Custom)
Definition: torcnotify.cpp:102
virtual bool Setup(void)
QString userName
Definition: torcdevice.h:64
void Notify(const QVariantMap &Message)
double GetValue(void)
Definition: torcdevice.cpp:142
bool IsKnownInput(const QString &UniqueId) override
void Graph(QByteArray *Data) override
static TorcNotify * gNotify
Definition: torcnotify.h:19
QString GetUniqueId(void)
Definition: torcdevice.cpp:162