v2.0.0
Loading...
Searching...
No Matches
command.cpp
Go to the documentation of this file.
1//=============================================================================================================
36
37//=============================================================================================================
38// Includes
39//=============================================================================================================
40
41#include "command.h"
42#include "command_manager.h"
43
44//=============================================================================================================
45// Qt Includes
46//=============================================================================================================
47
48#include <QVector>
49#include <QString>
50#include <QDebug>
51
52//=============================================================================================================
53// USED NAMESPACES
54//=============================================================================================================
55
56using namespace COMLIB;
57
58//=============================================================================================================
59// DEFINE MEMBER METHODS
60//=============================================================================================================
61
62Command::Command(bool p_bIsJson, QObject *parent)
63: QObject(parent)
64, m_sCommand("")
66, m_bIsJson(p_bIsJson)
67{
68}
69
70//=============================================================================================================
71
72Command::Command(const QString &p_sCommand, const QJsonObject &p_qCommandDescription, bool p_bIsJson, QObject *parent)
73: QObject(parent)
74, m_bIsJson(p_bIsJson)
75{
76 this->m_sCommand = p_sCommand;
77 this->m_sDescription = p_qCommandDescription.value(QString("description")).toString();
78
79 QJsonObject t_jsonObjectParameter = p_qCommandDescription.value(QString("parameters")).toObject();
80
81 QJsonObject::Iterator it;
82
83 for(it = t_jsonObjectParameter.begin(); it != t_jsonObjectParameter.end(); ++it)
84 {
85 QJsonValue t_jsonValueType = it.value().toObject().value(QString("type"));
86 QMetaType t_metaType = QMetaType::fromName(t_jsonValueType.toString().toUtf8().constData());
87
88 this->m_qListParamNames.push_back(it.key());
89 this->m_qListParamValues.push_back(QVariant(t_metaType));
90 this->m_qListParamDescriptions.push_back(it.value().toObject().value(QString("description")).toString());
91 }
92}
93
94//=============================================================================================================
95
96Command::Command(const QString &p_sCommand, const QString &p_sDescription, bool p_bIsJson, QObject *parent)
97: QObject(parent)
98, m_sCommand(p_sCommand)
99, m_sDescription(p_sDescription)
100, m_bIsJson(p_bIsJson)
101{
102}
103
104//=============================================================================================================
105
106Command::Command( const QString &p_sCommand, const QString &p_sDescription,
107 const QStringList &p_qListParamNames, const QList<QVariant> &p_qListParamValues, bool p_bIsJson, QObject *parent)
108: QObject(parent)
109, m_sCommand(p_sCommand)
110, m_sDescription(p_sDescription)
111, m_bIsJson(p_bIsJson)
112{
113 m_qListParamNames = p_qListParamNames;
114 m_qListParamValues = p_qListParamValues;
115
116 for(qint32 i = 0; i < p_qListParamValues.size(); ++i)
117 m_qListParamDescriptions.append("");
118}
119
120//=============================================================================================================
121
122Command::Command( const QString &p_sCommand, const QString &p_sDescription,
123 const QStringList &p_qListParamNames, const QList<QVariant> &p_qListParamValues, const QStringList &p_vecParameterDescriptions, bool p_bIsJson, QObject *parent)
124: QObject(parent)
125, m_sCommand(p_sCommand)
126, m_sDescription(p_sDescription)
127, m_bIsJson(p_bIsJson)
128{
129 if(p_qListParamNames.size() == p_qListParamValues.size())
130 {
131 if(p_qListParamValues.size() == p_vecParameterDescriptions.size())
132 {
133 m_qListParamNames = p_qListParamNames;
134 m_qListParamValues = p_qListParamValues;
135 m_qListParamDescriptions = p_vecParameterDescriptions;
136 }
137 }
138 else
139 {
140 qCritical("error: description vector hasn't the same size like parameter map.\n");
141 return;
142 }
143}
144
145//=============================================================================================================
146
147Command::Command(const Command &p_Command)
148: QObject(p_Command.parent())
149, m_sCommand(p_Command.m_sCommand)
154, m_bIsJson(p_Command.m_bIsJson)
155{
156}
157
158//=============================================================================================================
159
163
164//=============================================================================================================
165
167{
168 emit this->executed(*this);
169}
170
171//=============================================================================================================
172
173void Command::reply(const QString &p_sReply)
174{
175 CommandManager* t_commandManager = static_cast<CommandManager*> (this->parent());
176
177 if(t_commandManager) {
178 emit t_commandManager->response(p_sReply, *this);
179 }
180}
181
182//=============================================================================================================
183
185{
186 CommandManager* t_commandManager = static_cast<CommandManager*> (this->parent());
187
188 if(t_commandManager) {
189 emit t_commandManager->triggered(*this);
190 }
191}
192
193//=============================================================================================================
194
195QJsonObject Command::toJsonObject() const
196{
197 QJsonObject p_jsonCommandObject;
198 p_jsonCommandObject.insert("description", QJsonValue(m_sDescription));
199
200 QJsonObject t_jsonAllParametersObject;
201 for(qint32 i = 0; i < m_qListParamValues.size(); ++i)
202 {
203 QJsonObject t_jsonParameterObject;
204 t_jsonParameterObject.insert("description",QJsonValue(m_qListParamDescriptions[i]));
205 t_jsonParameterObject.insert("type",QString(m_qListParamValues[i].typeName()));
206 t_jsonAllParametersObject.insert(m_qListParamNames[i], QJsonValue(t_jsonParameterObject));
207 }
208 p_jsonCommandObject.insert("parameters", QJsonValue(t_jsonAllParametersObject));
209
210 return p_jsonCommandObject;
211}
212
213//=============================================================================================================
214
215QStringList Command::toStringList() const
216{
217 QStringList p_stringCommandList;
218
219 p_stringCommandList << m_sCommand;
220
221 QString t_sParameters;
222 for(qint32 i = 0; i < m_qListParamDescriptions.size(); ++i)
223 {
224 t_sParameters.append("[");
225 t_sParameters.append(m_qListParamDescriptions[i]);
226 t_sParameters.append("]");
227 }
228 p_stringCommandList << t_sParameters;
229
230 p_stringCommandList << m_sDescription;
231
232 return p_stringCommandList;
233}
234
235//=============================================================================================================
236
238{
239 QString p_stringCommand;
240
241 QString t_sParameters;
242 for(qint32 i = 0; i < m_qListParamNames.size(); ++i)
243 {
244// qDebug() << m_qListParamValues[i];
245 t_sParameters.append(QString("\"%1\":\"%2\"").arg(m_qListParamNames[i]).arg(m_qListParamValues[i].toString()));
246
247 if(i < m_qListParamNames.size()-1)
248 t_sParameters.append(",");
249 }
250
251 p_stringCommand.append(QString("\"%1\":{%2}").arg(m_sCommand).arg(t_sParameters));
252
253 return p_stringCommand;
254}
255
256//=============================================================================================================
257
259{
260 if (this != &rhs) // protect against invalid self-assignment
261 {
267 }
268 // to support chained assignment operators (a=b=c), always return *this
269 return *this;
270}
271
272//=============================================================================================================
273
274QVariant& Command::operator[] (const QString &key)
275{
276 if(m_qListParamNames.contains(key))
277 return m_qListParamValues[m_qListParamNames.indexOf(key)];
278 else
279 return defaultVariant;
280}
281
282//=============================================================================================================
283
284QVariant& Command::operator[] (qint32 idx)
285{
286 if(m_qListParamValues.size() > idx)
287 return m_qListParamValues[idx];
288 else
289 return defaultVariant;
290}
291
292//=============================================================================================================
293
294const QVariant Command::operator[] (const QString &key) const
295{
296 if(m_qListParamNames.contains(key))
297 return m_qListParamValues[m_qListParamNames.indexOf(key)];
298 else
299 return defaultVariant;
300}
Declaration of the CommandManager Class.
Declaration of the Command Class.
Real-time client/server communication (commands, raw data streaming).
QStringList toStringList() const
Definition command.cpp:215
QJsonObject toJsonObject() const
Definition command.cpp:195
QString m_sDescription
Definition command.h:308
void reply(const QString &p_sReply)
Definition command.cpp:173
QStringList m_qListParamDescriptions
Definition command.h:311
QStringList m_qListParamNames
Definition command.h:309
Command & operator=(const Command &rhs)
Definition command.cpp:258
Command(bool p_bIsJson=true, QObject *parent=0)
Definition command.cpp:62
QString m_sCommand
Definition command.h:307
QList< QVariant > m_qListParamValues
Definition command.h:310
void executed(Command p_command)
virtual ~Command()
Definition command.cpp:160
virtual void execute()
Definition command.cpp:166
QString toStringReadySend() const
Definition command.cpp:237
QVariant & operator[](const QString &key)
Definition command.cpp:274
Registry of available commands; dispatches parsed command strings to the matching Command handler.
void response(QString p_sReply, Command p_command)
void triggered(Command p_command)
void insert(const QJsonDocument &p_jsonDocument)