Torc  0.1
torcinput.cpp
Go to the documentation of this file.
1 /* Class TorcInput
2 *
3 * Copyright (C) Mark Kendall 2014-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 <QMutex>
22 
23 // Torc
24 #include "torclogging.h"
25 #include "torccoreutils.h"
26 #include "torcinputs.h"
27 #include "torcinput.h"
28 
29 #define BLACKLIST QStringLiteral("SetValue,SetValid")
30 
40 TorcInput::TorcInput(TorcInput::Type Type, double Value, double RangeMinimum, double RangeMaximum,
41  const QString &ModelId, const QVariantMap &Details)
42  : TorcDevice(false, Value, Value, ModelId, Details),
43  TorcHTTPService(this,
44  QStringLiteral("%1/%2/%3").arg(INPUTS_DIRECTORY, TorcCoreUtils::EnumToLowerString<TorcInput::Type>(Type), Details.value(QStringLiteral("name")).toString()),
45  Details.value(QStringLiteral("name")).toString(), TorcInput::staticMetaObject,
46  ModelId.startsWith(QStringLiteral("Network")) ? QStringLiteral("") : BLACKLIST),
47  operatingRangeMin(RangeMinimum),
48  operatingRangeMax(RangeMaximum),
49  outOfRangeLow(true),
50  outOfRangeHigh(false)
51 {
52  // guard against stupidity
54  {
55  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("Input has invalid operating ranges - adjusting"));
57  }
58 
59  // register this input - the owner must DownRef AND call RemoveInput to ensure it is deleted.
60  // N.B. gInputs is a static class
61  if (!uniqueId.isEmpty())
62  TorcInputs::gInputs->AddInput(this);
63 }
64 
65 void TorcInput::Start(void)
66 {
67  // emit value first, as this will indicate a valid value.
68  // then emit valid to force its state.
69  emit ValueChanged(value);
70  emit ValidChanged(valid);
71 }
72 
73 QString TorcInput::GetUIName(void)
74 {
75  QMutexLocker locker(&lock);
76  if (userName.isEmpty())
77  return uniqueId;
78  return userName;
79 }
80 
81 void TorcInput::SubscriberDeleted(QObject *Subscriber)
82 {
84 }
85 
90 void TorcInput::SetValue(double Value)
91 {
92  QMutexLocker locker(&lock);
93 
94  // a call to SetValue implies there is a valid reading
95  if (!valid)
96  {
97  SetValid(true);
98  }
99  else
100  {
101  // ignore status quo if already valid
102  if (wasInvalid)
103  {
104  wasInvalid = false;
105  }
106  else
107  {
108  if (qFuzzyCompare(Value + 1.0f, value + 1.0f))
109  return;
110  }
111  }
112 
113  // update value and valueScaled
114  value = Value;
115  emit ValueChanged(value);
116 
117  // check for out of operating range - this works with default/unscaled values
118  // N.B. use >= to allow for binary operation (e.g. a switch is either 0 or 1)
119  if (value >= operatingRangeMax)
120  {
121  // signal changes - cannot be both states at once
122  if (outOfRangeLow)
123  emit OutOfRangeLowChanged(false);
124  outOfRangeLow = false;
125 
126  if (!outOfRangeHigh)
127  emit OutOfRangeHighChanged(true);
128  outOfRangeHigh = true;
129  }
130  else if (value <= operatingRangeMin)
131  {
132  // as above
133  if (outOfRangeHigh)
134  emit OutOfRangeHighChanged(false);
135  outOfRangeHigh = false;
136 
137  if (!outOfRangeLow)
138  emit OutOfRangeLowChanged(true);
139  outOfRangeLow = true;
140  }
141 }
142 
143 void TorcInput::SetValid(bool Valid)
144 {
145  QMutexLocker locker(&lock);
146 
147  if (!Valid)
149 
150  TorcDevice::SetValid(Valid);
151 }
152 
154 {
155  // no need to lock
156  return operatingRangeMin;
157 }
158 
160 {
161  // no need to lock
162  return operatingRangeMax;
163 }
164 
166 {
167  QMutexLocker locker(&lock);
168 
169  return outOfRangeLow;
170 }
171 
173 {
174  QMutexLocker locker(&lock);
175 
176  return outOfRangeHigh;
177 }
bool GetOutOfRangeHigh(void)
Definition: torcinput.cpp:172
bool outOfRangeLow
Definition: torcinput.h:20
void ValueChanged(double Value)
bool GetOutOfRangeLow(void)
Definition: torcinput.cpp:165
double GetOperatingRangeMin(void)
Definition: torcinput.cpp:153
double GetOperatingRangeMax(void)
Definition: torcinput.cpp:159
QString uniqueId
Definition: torcdevice.h:63
virtual void SetValid(bool Valid)
Definition: torcdevice.cpp:101
#define BLACKLIST
Definition: torcinput.cpp:29
void SubscriberDeleted(QObject *Subscriber)
Definition: torcinput.cpp:81
void OutOfRangeLowChanged(bool Value)
void SetValid(bool Valid) final
Definition: torcinput.cpp:143
double operatingRangeMin
Definition: torcinput.h:18
void OutOfRangeHighChanged(bool Value)
double defaultValue
Definition: torcdevice.h:61
VERBOSE_PREAMBLE true
void SetValue(double Value) override
Update the inputs value.
Definition: torcinput.cpp:90
QMutex lock
Definition: torcdevice.h:66
TorcInput(TorcInput::Type Type, double Value, double RangeMinimum, double RangeMaximum, const QString &ModelId, const QVariantMap &Details)
Definition: torcinput.cpp:40
double value
Definition: torcdevice.h:60
void HandleSubscriberDeleted(QObject *Subscriber)
bool outOfRangeHigh
Definition: torcinput.h:21
QString GetUIName(void) override
Definition: torcinput.cpp:73
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
#define INPUTS_DIRECTORY
Definition: torcinput.h:12
QString EnumToLowerString(T Value)
Definition: torccoreutils.h:18
QString userName
Definition: torcdevice.h:64
void ValidChanged(bool Valid)
double operatingRangeMax
Definition: torcinput.h:19
bool wasInvalid
Definition: torcdevice.h:71
bool valid
Definition: torcdevice.h:24
virtual void Start(void) override
Definition: torcinput.cpp:65