MNE-CPP  beta 1.0
command.cpp
1 
2 //*************************************************************************************************************
3 //=============================================================================================================
4 // Includes
5 //=============================================================================================================
6 
7 #include "command.h"
8 #include "commandmanager.h"
9 
10 
11 //*************************************************************************************************************
12 //=============================================================================================================
13 // Qt Includes
14 //=============================================================================================================
15 
16 #include <QVector>
17 #include <QString>
18 #include <QDebug>
19 
20 
21 //*************************************************************************************************************
22 //=============================================================================================================
23 // USED NAMESPACES
24 //=============================================================================================================
25 
26 using namespace RTCOMMANDLIB;
27 
28 
29 //*************************************************************************************************************
30 //=============================================================================================================
31 // DEFINE MEMBER METHODS
32 //=============================================================================================================
33 
34 Command::Command(bool p_bIsJson, QObject *parent)
35 : QObject(parent)
36 , m_sCommand("")
37 , m_sDescription("")
38 , m_bIsJson(p_bIsJson)
39 {
40 
41 }
42 
43 
44 //*************************************************************************************************************
45 
46 Command::Command(const QString &p_sCommand, const QJsonObject &p_qCommandDescription, bool p_bIsJson, QObject *parent)
47 : QObject(parent)
48 , m_bIsJson(p_bIsJson)
49 {
50  this->m_sCommand = p_sCommand;
51  this->m_sDescription = p_qCommandDescription.value(QString("description")).toString();
52 
53  QJsonObject t_jsonObjectParameter = p_qCommandDescription.value(QString("parameters")).toObject();
54 
55  QJsonObject::Iterator it;
56 
57  for(it = t_jsonObjectParameter.begin(); it != t_jsonObjectParameter.end(); ++it)
58  {
59  QJsonValue t_jsonValueType = it.value().toObject().value(QString("type"));
60  QVariant::Type t_type = QVariant::nameToType(t_jsonValueType.toString().toLatin1().constData());
61 
62  this->m_qListParamNames.push_back(it.key());
63  this->m_qListParamValues.push_back(QVariant(t_type));
64  this->m_qListParamDescriptions.push_back(it.value().toObject().value(QString("description")).toString());
65  }
66 }
67 
68 
69 //*************************************************************************************************************
70 
71 Command::Command(const QString &p_sCommand, const QString &p_sDescription, bool p_bIsJson, QObject *parent)
72 : QObject(parent)
73 , m_sCommand(p_sCommand)
74 , m_sDescription(p_sDescription)
75 , m_bIsJson(p_bIsJson)
76 {
77 
78 }
79 
80 
81 //*************************************************************************************************************
82 
83 Command::Command( const QString &p_sCommand, const QString &p_sDescription,
84  const QStringList &p_qListParamNames, const QList<QVariant> &p_qListParamValues, bool p_bIsJson, QObject *parent)
85 : QObject(parent)
86 , m_sCommand(p_sCommand)
87 , m_sDescription(p_sDescription)
88 , m_bIsJson(p_bIsJson)
89 {
90  m_qListParamNames = p_qListParamNames;
91  m_qListParamValues = p_qListParamValues;
92 
93  for(qint32 i = 0; i < p_qListParamValues.size(); ++i)
94  m_qListParamDescriptions.append("");
95 }
96 
97 
98 //*************************************************************************************************************
99 
100 Command::Command( const QString &p_sCommand, const QString &p_sDescription,
101  const QStringList &p_qListParamNames, const QList<QVariant> &p_qListParamValues, const QStringList &p_vecParameterDescriptions, bool p_bIsJson, QObject *parent)
102 : QObject(parent)
103 , m_sCommand(p_sCommand)
104 , m_sDescription(p_sDescription)
105 , m_bIsJson(p_bIsJson)
106 {
107  if(p_qListParamNames.size() == p_qListParamValues.size())
108  {
109  if(p_qListParamValues.size() == p_vecParameterDescriptions.size())
110  {
111  m_qListParamNames = p_qListParamNames;
112  m_qListParamValues = p_qListParamValues;
113  m_qListParamDescriptions = p_vecParameterDescriptions;
114  }
115  }
116  else
117  {
118  printf("error: description vector hasn't the same size like parameter map.\n");
119  return;
120  }
121 
122 }
123 
124 //*************************************************************************************************************
125 
126 Command::Command(const Command &p_Command)
127 : QObject(p_Command.parent())
128 , m_sCommand(p_Command.m_sCommand)
129 , m_sDescription(p_Command.m_sDescription)
130 , m_qListParamNames(p_Command.m_qListParamNames)
131 , m_qListParamValues(p_Command.m_qListParamValues)
132 , m_qListParamDescriptions(p_Command.m_qListParamDescriptions)
133 , m_bIsJson(p_Command.m_bIsJson)
134 {
135 
136 }
137 
138 
139 //*************************************************************************************************************
140 
142 {
143 }
144 
145 
146 //*************************************************************************************************************
147 
149 {
150  emit this->executed(*this);
151 }
152 
153 
154 //*************************************************************************************************************
155 
156 void Command::reply(const QString &p_sReply)
157 {
158  CommandManager* t_commandManager = static_cast<CommandManager*> (this->parent());
159 
160  if(t_commandManager)
161  emit t_commandManager->response(p_sReply, *this);
162 }
163 
164 
165 //*************************************************************************************************************
166 
168 {
169  CommandManager* t_commandManager = static_cast<CommandManager*> (this->parent());
170  if(t_commandManager)
171  emit t_commandManager->triggered(*this);
172 }
173 
174 
175 //*************************************************************************************************************
176 
177 QJsonObject Command::toJsonObject() const
178 {
179  QJsonObject p_jsonCommandObject;
180  p_jsonCommandObject.insert("description", QJsonValue(m_sDescription));
181 
182  QJsonObject t_jsonAllParametersObject;
183  for(qint32 i = 0; i < m_qListParamValues.size(); ++i)
184  {
185  QJsonObject t_jsonParameterObject;
186  t_jsonParameterObject.insert("description",QJsonValue(m_qListParamDescriptions[i]));
187  t_jsonParameterObject.insert("type",QString(m_qListParamValues[i].typeName()));
188  t_jsonAllParametersObject.insert(m_qListParamNames[i], QJsonValue(t_jsonParameterObject));
189  }
190  p_jsonCommandObject.insert("parameters", QJsonValue(t_jsonAllParametersObject));
191 
192  return p_jsonCommandObject;
193 }
194 
195 
196 //*************************************************************************************************************
197 
198 QStringList Command::toStringList() const
199 {
200  QStringList p_stringCommandList;
201 
202  p_stringCommandList << m_sCommand;
203 
204  QString t_sParameters;
205  for(qint32 i = 0; i < m_qListParamDescriptions.size(); ++i)
206  {
207  t_sParameters.append("[");
208  t_sParameters.append(m_qListParamDescriptions[i]);
209  t_sParameters.append("]");
210  }
211  p_stringCommandList << t_sParameters;
212 
213  p_stringCommandList << m_sDescription;
214 
215  return p_stringCommandList;
216 }
217 
218 
219 //*************************************************************************************************************
220 
222 {
223  QString p_stringCommand;
224 
225  QString t_sParameters;
226  for(qint32 i = 0; i < m_qListParamNames.size(); ++i)
227  {
228 // qDebug() << m_qListParamValues[i];
229  t_sParameters.append(QString("\"%1\":\"%2\"").arg(m_qListParamNames[i]).arg(m_qListParamValues[i].toString()));
230 
231  if(i < m_qListParamNames.size()-1)
232  t_sParameters.append(",");
233  }
234 
235  p_stringCommand.append(QString("\"%1\":{%2}").arg(m_sCommand).arg(t_sParameters));
236 
237  return p_stringCommand;
238 }
239 
240 
241 //*************************************************************************************************************
242 
244 {
245  if (this != &rhs) // protect against invalid self-assignment
246  {
247  m_sCommand = rhs.m_sCommand;
248  m_sDescription = rhs.m_sDescription;
249  m_qListParamNames = rhs.m_qListParamNames;
250  m_qListParamValues = rhs.m_qListParamValues;
251  m_qListParamDescriptions = rhs.m_qListParamDescriptions;
252  }
253  // to support chained assignment operators (a=b=c), always return *this
254  return *this;
255 }
256 
257 
258 //*************************************************************************************************************
259 
260 QVariant& Command::operator[] (const QString &key)
261 {
262  if(m_qListParamNames.contains(key))
263  return m_qListParamValues[m_qListParamNames.indexOf(key)];
264  else
265  return defaultVariant;
266 }
267 
268 
269 //*************************************************************************************************************
270 
271 QVariant& Command::operator[] (qint32 idx)
272 {
273  if(m_qListParamValues.size() > idx)
274  return m_qListParamValues[idx];
275  else
276  return defaultVariant;
277 }
278 
279 
280 //*************************************************************************************************************
281 
282 const QVariant Command::operator[] (const QString &key) const
283 {
284  if(m_qListParamNames.contains(key))
285  return m_qListParamValues[m_qListParamNames.indexOf(key)];
286  else
287  return defaultVariant;
288 }
QJsonObject toJsonObject() const
Definition: command.cpp:177
Declaration of the Command Class.
void response(QString p_sReply, Command p_command)
Command(bool p_bIsJson=true, QObject *parent=0)
Definition: command.cpp:34
QVariant & operator[](const QString &key)
Definition: command.cpp:260
void triggered(Command p_command)
void reply(const QString &p_sReply)
Definition: command.cpp:156
QStringList toStringList() const
Definition: command.cpp:198
virtual void execute()
Definition: command.cpp:148
QString toStringReadySend() const
Definition: command.cpp:221
void executed(Command p_command)
Command & operator=(const Command &rhs)
Definition: command.cpp:243