Torc  0.1
torcdevice.cpp
Go to the documentation of this file.
1 /* Class TorcDevice
2 *
3 * Copyright (C) Mark Kendall 2015-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 "torccentral.h"
23 #include "torcdevice.h"
24 
25 QHash<QString,TorcDevice*>* TorcDevice::gDeviceList = new QHash<QString,TorcDevice*>();
26 QMutex* TorcDevice::gDeviceListLock = new QMutex(QMutex::Recursive);
27 
28 TorcDevice::TorcDevice(bool Valid, double Value, double Default,
29  const QString &ModelId, const QVariantMap &Details)
30  : QObject(),
32  valid(Valid),
33  value(Value),
34  defaultValue(Default),
35  modelId(ModelId),
36  uniqueId(QStringLiteral("")),
37  userName(QStringLiteral("")),
38  userDescription(QStringLiteral("")),
39  lock(QMutex::Recursive),
40  wasInvalid(true)
41 {
42  if (!Details.contains(QStringLiteral("name")))
43  {
44  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Device has no <name> - THIS WILL NOT WORK"));
45  }
46  else
47  {
48  QMutexLocker locker(gDeviceListLock);
49  uniqueId = Details.value(QStringLiteral("name")).toString();
50  setObjectName(uniqueId);
51 
52  if (!uniqueId.isEmpty() && !gDeviceList->contains(uniqueId))
53  {
54  gDeviceList->insert(uniqueId, this);
55  LOG(VB_GENERAL, LOG_DEBUG, QStringLiteral("New device id: %1").arg(uniqueId));
56  }
57  else
58  {
59  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Device id '%1' already in use - THIS WILL NOT WORK").arg(uniqueId));
60  }
61  }
62 
63  if (Details.contains(QStringLiteral("username")))
64  userName = Details.value(QStringLiteral("username")).toString();
65  if (Details.contains(QStringLiteral("userdescription")))
66  userDescription = Details.value(QStringLiteral("userdescription")).toString();
67 }
68 
70 {
71  {
72  QMutexLocker locker(gDeviceListLock);
73 
74  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Device id: %1 removed").arg(uniqueId));
75  gDeviceList->remove(uniqueId);
76  }
77 }
78 
79 QStringList TorcDevice::GetDescription(void)
80 {
81  return QStringList();
82 }
83 
88 void TorcDevice::Start(void)
89 {
90 }
91 
96 void TorcDevice::Stop(void)
97 {
98  SetValid(false);
99 }
100 
101 void TorcDevice::SetValid(bool Valid)
102 {
103  QMutexLocker locker(&lock);
104 
105  if (Valid == valid)
106  return;
107 
108  if (Valid)
109  wasInvalid = true;
110 
111  valid = Valid;
112  emit ValidChanged(valid);
113 }
114 
115 void TorcDevice::SetValue(double Value)
116 {
117  QMutexLocker locker(&lock);
118 
119  // force an update if the last value was 'invalid'
120  if (wasInvalid)
121  {
122  wasInvalid = false;
123  }
124  else
125  {
126  // filter out unnecessary changes
127  if (qFuzzyCompare(Value + 1.0f, value + 1.0f))
128  return;
129  }
130 
131  value = Value;
132  emit ValueChanged(value);
133 }
134 
136 {
137  QMutexLocker locker(&lock);
138 
139  return valid;
140 }
141 
143 {
144  QMutexLocker locker(&lock);
145 
146  return value;
147 }
148 
150 {
151  QMutexLocker locker(&lock);
152 
153  return defaultValue;
154 }
155 
157 {
158  // no need to lock
159  return modelId;
160 }
161 
163 {
164  // no need to lock
165  return uniqueId;
166 }
167 
169 {
170  QMutexLocker locker(&lock);
171 
172  return userName;
173 }
174 
176 {
177  QMutexLocker locker(&lock);
178 
179  return userDescription;
180 }
QString GetUserName(void)
Definition: torcdevice.cpp:168
double GetDefaultValue(void)
Definition: torcdevice.cpp:149
virtual ~TorcDevice()
Definition: torcdevice.cpp:69
virtual void SetValid(bool Valid)
Definition: torcdevice.cpp:101
A reference counting implementation.
bool GetValid(void)
Definition: torcdevice.cpp:135
QString GetUserDescription(void)
Definition: torcdevice.cpp:175
static QMutex * gDeviceListLock
Definition: torcdevice.h:69
static QHash< QString, TorcDevice * > * gDeviceList
Definition: torcdevice.h:68
VERBOSE_PREAMBLE true
virtual void SetValue(double Value)
Definition: torcdevice.cpp:115
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
QString GetModelId(void)
Definition: torcdevice.cpp:156
double GetValue(void)
Definition: torcdevice.cpp:142
QString GetUniqueId(void)
Definition: torcdevice.cpp:162