Torc  0.1
torcoutput.cpp
Go to the documentation of this file.
1 /* Class TorcOutput
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2015-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Torc
24 #include "torclogging.h"
25 #include "torccoreutils.h"
26 #include "torcoutputs.h"
27 #include "torcoutput.h"
28 
29 #define BLACKLIST QStringLiteral("SetValue,SetValid")
30 
31 // N.B. We need to pass the staticMetaObject to TorcHTTPService as the object is not yet complete.
32 // If we pass 'this' during construction, this->metaObject() only contains details of the super class.
33 TorcOutput::TorcOutput(TorcOutput::Type Type, double Value, const QString &ModelId, const QVariantMap &Details)
34  : TorcDevice(true, Value, Value, ModelId, Details),
35  TorcHTTPService(this, QStringLiteral("%1/%2%3").arg(OUTPUTS_DIRECTORY, TorcCoreUtils::EnumToLowerString<TorcOutput::Type>(Type), Details.value(QStringLiteral("name")).toString()),
36  Details.value(QStringLiteral("name")).toString(), TorcOutput::staticMetaObject, BLACKLIST),
37  m_owner(nullptr)
38 {
39  TorcOutputs::gOutputs->AddOutput(this);
40 }
41 
42 TorcOutput::TorcOutput(TorcOutput::Type Type, double Value, const QString &ModelId, const QVariantMap &Details,
43  QObject *Output, const QMetaObject &MetaObject, const QString &Blacklist)
44  : TorcDevice(true, Value, Value, ModelId, Details),
45  TorcHTTPService(Output, QStringLiteral("%1/%2/%3").arg(OUTPUTS_DIRECTORY, TorcCoreUtils::EnumToLowerString<TorcOutput::Type>(Type), Details.value(QStringLiteral("name")).toString()),
46  Details.value(QStringLiteral("name")).toString(), MetaObject, BLACKLIST + "," + Blacklist),
47  m_owner(nullptr)
48 {
49  TorcOutputs::gOutputs->AddOutput(this);
50 }
51 
53 {
54  QMutexLocker locker(&lock);
55  return m_owner != nullptr;
56 }
57 
58 QString TorcOutput::GetUIName(void)
59 {
60  QMutexLocker locker(&lock);
61  if (userName.isEmpty())
62  return uniqueId;
63  return userName;
64 }
65 
66 bool TorcOutput::SetOwner(QObject *Owner)
67 {
68  QMutexLocker locker(&lock);
69 
70  if (!Owner)
71  {
72  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Cannot set NULL output owner for %1").arg(uniqueId));
73  return false;
74  }
75 
76  if (m_owner && m_owner != Owner)
77  {
78  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Cannot set different output owner for %1").arg(uniqueId));
79  return false;
80  }
81 
82  m_owner = Owner;
83 
84  return true;
85 }
86 
87 void TorcOutput::SubscriberDeleted(QObject *Subscriber)
88 {
90 }
91 
92 void TorcOutput::Graph(QByteArray *Data)
93 {
94  if (!Data)
95  return;
96 
97  QString id = GetUniqueId();
98  QString label = GetUserName();
99  QString url = GetPresentationURL();
100  QString desc;
101  QStringList source = GetDescription();
102  foreach (const QString &item, source)
103  if (!item.isEmpty())
104  desc.append(QString(DEVICE_LINE_ITEM).arg(item));
105  desc.append(QString(DEVICE_LINE_ITEM).arg(tr("Default %1").arg(GetDefaultValue())));
106  desc.append(QString(DEVICE_LINE_ITEM).arg(tr("Valid %1").arg(GetValid())));
107  desc.append(QString(DEVICE_LINE_ITEM).arg(tr("Value %1").arg(GetValue())));
108 
109  if (label.isEmpty())
110  label = id;
111  QString link = url.isEmpty() ? QString() : QStringLiteral(" href=\"%1\"").arg(url);
112  Data->append(QStringLiteral(" \"%1\" [shape=record id=\"%1\" label=<<B>%2</B>%3>%4];\r\n").arg(id, label, desc, link));
113 }
virtual void Graph(QByteArray *Data)
Definition: torcoutput.cpp:92
QString GetUserName(void)
Definition: torcdevice.cpp:168
double GetDefaultValue(void)
Definition: torcdevice.cpp:149
QString uniqueId
Definition: torcdevice.h:63
bool GetValid(void)
Definition: torcdevice.cpp:135
#define DEVICE_LINE_ITEM
Definition: torcdevice.h:16
void SubscriberDeleted(QObject *Subscriber)
Definition: torcoutput.cpp:87
#define OUTPUTS_DIRECTORY
Definition: torcoutput.h:13
#define BLACKLIST
Definition: torcoutput.cpp:29
VERBOSE_PREAMBLE true
virtual QString GetPresentationURL(void)
QMutex lock
Definition: torcdevice.h:66
double value
Definition: torcdevice.h:60
void HandleSubscriberDeleted(QObject *Subscriber)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
QString EnumToLowerString(T Value)
Definition: torccoreutils.h:18
QString userName
Definition: torcdevice.h:64
bool HasOwner(void)
Definition: torcoutput.cpp:52
QString GetUIName(void) override
Definition: torcoutput.cpp:58
TorcOutput(TorcOutput::Type Type, double Value, const QString &ModelId, const QVariantMap &Details)
Definition: torcoutput.cpp:33
double GetValue(void)
Definition: torcdevice.cpp:142
bool SetOwner(QObject *Owner)
Definition: torcoutput.cpp:66
QString GetUniqueId(void)
Definition: torcdevice.cpp:162