Torc  0.1
torcinputs.cpp
Go to the documentation of this file.
1 /* Class TorcInputs
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2014-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 "torcadminthread.h"
27 #include "torccentral.h"
28 #include "torcnetworkpwminput.h"
29 #include "torcnetworkswitchinput.h"
31 #include "torcnetworkphinput.h"
32 #include "torcnetworkbuttoninput.h"
34 #include "torcinputs.h"
35 
36 #define BLACKLIST QStringLiteral("")
37 
38 TorcInputs* TorcInputs::gInputs = new TorcInputs();
39 
78  : QObject(),
79  TorcHTTPService(this, INPUTS_DIRECTORY, QStringLiteral("inputs"), TorcInputs::staticMetaObject, BLACKLIST),
81  inputList(),
82  inputTypes(),
83  m_createdInputs()
84 {
85 }
86 
87 QString TorcInputs::GetUIName(void)
88 {
89  return tr("Inputs");
90 }
91 
92 void TorcInputs::Graph(QByteArray *Data)
93 {
94  if (!Data)
95  return;
96 
97  QString start =
98  QStringLiteral(" subgraph cluster_0 {\r\n"
99  " label = \"%1\";\r\n"
100  " style=filled;\r\n"
101  " fillcolor = \"grey95\";\r\n").arg(tr("Inputs"));
102  Data->append(start);
103 
104  foreach(TorcInput *input, inputList)
105  {
106  QString id = input->GetUniqueId();
107  QString label = input->GetUserName();
108  QString desc;
109  QStringList source = input->GetDescription();
110  foreach (const QString &item, source)
111  if (!item.isEmpty())
112  desc.append(QString(DEVICE_LINE_ITEM).arg(item));
113  desc.append(QString(DEVICE_LINE_ITEM).arg(tr("Default %1").arg(input->GetDefaultValue())));
114  desc.append(QString(DEVICE_LINE_ITEM).arg(tr("Valid %1").arg(input->GetValid())));
115  desc.append(QString(DEVICE_LINE_ITEM).arg(tr("Value %1").arg(input->GetValue())));
116 
117  if (label.isEmpty())
118  label = id;
119  Data->append(QStringLiteral(" \"%1\" [shape=record id=\"%1\" label=<<B>%2</B>%3>];\r\n").arg(id, label, desc));
120  }
121 
122  Data->append(" }\r\n\r\n");
123 }
124 
125 void TorcInputs::SubscriberDeleted(QObject *Subscriber)
126 {
128 }
129 
136 QVariantMap TorcInputs::GetInputList(void)
137 {
138  QVariantMap result;
139  QReadLocker locker(&m_httpServiceLock);
140 
141  // iterate over our list for each input type
142  for (int type = TorcInput::Unknown; type < TorcInput::MaxType; type++)
143  {
144  QStringList inputsfortype;
145  foreach (TorcInput *input, inputList)
146  if (input->GetType() == type)
147  inputsfortype.append(input->GetUniqueId());
148 
149  if (!inputsfortype.isEmpty())
150  result.insert(TorcCoreUtils::EnumToLowerString<TorcInput::Type>(static_cast<TorcInput::Type>(type)), inputsfortype);
151  }
152  return result;
153 }
154 
155 QStringList TorcInputs::GetInputTypes(void)
156 {
157  return TorcCoreUtils::EnumList<TorcInput::Type>();
158 }
159 
161 {
162  QWriteLocker locker(&m_httpServiceLock);
163  if (!Input)
164  return;
165 
166  if (inputList.contains(Input))
167  {
168  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("Already have an input named %1 - ignoring").arg(Input->GetUniqueId()));
169  return;
170  }
171 
172  Input->UpRef();
173  inputList.append(Input);
174  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Registered input '%1'").arg(Input->GetUniqueId()));
175  emit InputsChanged();
176 }
177 
179 {
180  QWriteLocker locker(&m_httpServiceLock);
181  if (!Input)
182  return;
183 
184  if (!inputList.contains(Input))
185  {
186  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("Input %1 not recognised - cannot remove").arg(Input->GetUniqueId()));
187  return;
188  }
189 
190  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Input %1 de-registered").arg(Input->GetUniqueId()));
191  Input->DownRef();
192  inputList.removeOne(Input);
193  emit InputsChanged();
194 }
195 
196 void TorcInputs::Create(const QVariantMap &Details)
197 {
198  QWriteLocker locker(&m_handlerLock);
199 
200  QVariantMap::const_iterator i = Details.constBegin();
201  for ( ; i != Details.constEnd(); ++i)
202  {
203  if (i.key() != INPUTS_DIRECTORY)
204  continue;
205 
206  QVariantMap devices = i.value().toMap();
207  QVariantMap::const_iterator j = devices.constBegin();
208  for ( ; j != devices.constEnd(); ++j)
209  {
210  // look for <network> or <constant>
211  QString group = j.key();
212  bool network = group == NETWORK_DEVICE_STRING;
213  bool constant = group == CONSTANT_DEVICE_STRING;
214 
215  if (!network && !constant)
216  continue;
217 
218  QVariantMap details = j.value().toMap();
219  QVariantMap::const_iterator it = details.constBegin();
220  for ( ; it != details.constEnd(); ++it)
221  {
222  // iterate over known types <pwm>, <switch> etc
223  for (int type = TorcInput::Unknown; type != TorcInput::MaxType; type++)
224  {
225  if (it.key() == TorcCoreUtils::EnumToLowerString<TorcInput::Type>(static_cast<TorcInput::Type>(type)))
226  {
227  QVariantMap input = it.value().toMap();
228  QString defaultvalue = network ? input.value(QStringLiteral("default")).toString() : input.value(QStringLiteral("value")).toString();
229  QString uniqueid = input.value(QStringLiteral("name"), QStringLiteral("Error")).toString();
230 
231  bool ok = false;
232  double defaultdouble = defaultvalue.toDouble(&ok);
233  if (!ok)
234  defaultdouble = 0.0;
235 
236  TorcInput* newinput = nullptr;
237  switch (type)
238  {
239  case TorcInput::PWM:
240  newinput = network ? new TorcNetworkPWMInput(defaultdouble, input) : new TorcPWMInput(defaultdouble, DEVICE_CONSTANT + TorcCoreUtils::EnumToLowerString<TorcInput::Type>(TorcInput::PWM), input);
241  break;
242  case TorcInput::Switch:
243  newinput = network ? new TorcNetworkSwitchInput(defaultdouble, input) : new TorcSwitchInput(defaultdouble, DEVICE_CONSTANT + TorcCoreUtils::EnumToLowerString<TorcInput::Type>(TorcInput::Switch), input);
244  break;
246  newinput = network ? new TorcNetworkTemperatureInput(defaultdouble, input) : new TorcTemperatureInput(defaultdouble, -1000, 1000, DEVICE_CONSTANT + TorcCoreUtils::EnumToLowerString<TorcInput::Type>(TorcInput::Temperature), input);
247  break;
248  case TorcInput::pH:
249  newinput = network ? new TorcNetworkpHInput(defaultdouble, input) : new TorcpHInput(defaultdouble, DEVICE_CONSTANT + TorcCoreUtils::EnumToLowerString<TorcInput::Type>(TorcInput::pH), input);
250  break;
251  case TorcInput::Integer:
252  newinput = network ? new TorcNetworkIntegerInput(defaultdouble, input) : new TorcIntegerInput(defaultdouble, DEVICE_CONSTANT + TorcCoreUtils::EnumToLowerString<TorcInput::Type>(TorcInput::Integer), input);
253  break;
254  case TorcInput::Button:
255  if (constant)
256  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Cannot create constant button input"));
257  else
258  newinput = new TorcNetworkButtonInput(defaultdouble, input);
259  default: break;
260  }
261 
262  if (newinput)
263  m_createdInputs.insert(uniqueid, newinput);
264  }
265  }
266  }
267  }
268  }
269 }
270 
272 {
273  QWriteLocker locker(&m_handlerLock);
274 
275  QMap<QString,TorcInput*>::const_iterator it = m_createdInputs.constBegin();
276  for ( ; it != m_createdInputs.constEnd(); ++it)
277  {
278  it.value()->DownRef();
279  RemoveInput(it.value());
280  }
281  m_createdInputs.clear();
282 }
void AddInput(TorcInput *Input)
Definition: torcinputs.cpp:160
QString GetUserName(void)
Definition: torcdevice.cpp:168
double GetDefaultValue(void)
Definition: torcdevice.cpp:149
#define NETWORK_DEVICE_STRING
Definition: torcdevice.h:14
QVariantMap inputList
Definition: torcinputs.h:18
bool GetValid(void)
Definition: torcdevice.cpp:135
#define DEVICE_LINE_ITEM
Definition: torcdevice.h:16
virtual bool DownRef(void)
#define DEVICE_CONSTANT
Definition: torcdevice.h:17
#define BLACKLIST
Definition: torcinputs.cpp:36
QStringList GetInputTypes(void)
Definition: torcinputs.cpp:155
void Create(const QVariantMap &Details) override
Definition: torcinputs.cpp:196
void HandleSubscriberDeleted(QObject *Subscriber)
QReadWriteLock m_httpServiceLock
A network input that mimics the behaviour of a mechanical push button.
QVariantMap GetInputList(void)
Return a map of known inputs.
Definition: torcinputs.cpp:136
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
void SubscriberDeleted(QObject *Subscriber)
Definition: torcinputs.cpp:125
QReadWriteLock m_handlerLock
#define INPUTS_DIRECTORY
Definition: torcinput.h:12
TorcInputs()
TorcInputs contains the master record of known inputs.
Definition: torcinputs.cpp:77
void RemoveInput(TorcInput *Input)
Definition: torcinputs.cpp:178
QString GetUIName(void) override
Definition: torcinputs.cpp:87
void Graph(QByteArray *Data)
Definition: torcinputs.cpp:92
virtual TorcInput::Type GetType(void)=0
#define CONSTANT_DEVICE_STRING
Definition: torcdevice.h:15
void Destroy(void) override
Definition: torcinputs.cpp:271
double GetValue(void)
Definition: torcdevice.cpp:142
void InputsChanged(void)
QString GetUniqueId(void)
Definition: torcdevice.cpp:162