v2.0.0
Loading...
Searching...
No Matches
command.cpp
Go to the documentation of this file.
1//=============================================================================================================
21
22//=============================================================================================================
23// Includes
24//=============================================================================================================
25
26#include "command.h"
27#include "command_manager.h"
28
29//=============================================================================================================
30// Qt Includes
31//=============================================================================================================
32
33#include <QVector>
34#include <QString>
35#include <QDebug>
36
37//=============================================================================================================
38// USED NAMESPACES
39//=============================================================================================================
40
41using namespace COMLIB;
42
43//=============================================================================================================
44// DEFINE MEMBER METHODS
45//=============================================================================================================
46
47Command::Command(bool p_bIsJson, QObject *parent)
48: QObject(parent)
49, m_sCommand("")
51, m_bIsJson(p_bIsJson)
52{
53}
54
55//=============================================================================================================
56
57Command::Command(const QString &p_sCommand, const QJsonObject &p_qCommandDescription, bool p_bIsJson, QObject *parent)
58: QObject(parent)
59, m_bIsJson(p_bIsJson)
60{
61 this->m_sCommand = p_sCommand;
62 this->m_sDescription = p_qCommandDescription.value(QString("description")).toString();
63
64 QJsonObject t_jsonObjectParameter = p_qCommandDescription.value(QString("parameters")).toObject();
65
66 QJsonObject::Iterator it;
67
68 for(it = t_jsonObjectParameter.begin(); it != t_jsonObjectParameter.end(); ++it)
69 {
70 QJsonValue t_jsonValueType = it.value().toObject().value(QString("type"));
71 QMetaType t_metaType = QMetaType::fromName(t_jsonValueType.toString().toUtf8().constData());
72
73 this->m_qListParamNames.push_back(it.key());
74 this->m_qListParamValues.push_back(QVariant(t_metaType));
75 this->m_qListParamDescriptions.push_back(it.value().toObject().value(QString("description")).toString());
76 }
77}
78
79//=============================================================================================================
80
81Command::Command(const QString &p_sCommand, const QString &p_sDescription, bool p_bIsJson, QObject *parent)
82: QObject(parent)
83, m_sCommand(p_sCommand)
84, m_sDescription(p_sDescription)
85, m_bIsJson(p_bIsJson)
86{
87}
88
89//=============================================================================================================
90
91Command::Command( const QString &p_sCommand, const QString &p_sDescription,
92 const QStringList &p_qListParamNames, const QList<QVariant> &p_qListParamValues, bool p_bIsJson, QObject *parent)
93: QObject(parent)
94, m_sCommand(p_sCommand)
95, m_sDescription(p_sDescription)
96, m_bIsJson(p_bIsJson)
97{
98 m_qListParamNames = p_qListParamNames;
99 m_qListParamValues = p_qListParamValues;
100
101 for(qint32 i = 0; i < p_qListParamValues.size(); ++i)
102 m_qListParamDescriptions.append("");
103}
104
105//=============================================================================================================
106
107Command::Command( const QString &p_sCommand, const QString &p_sDescription,
108 const QStringList &p_qListParamNames, const QList<QVariant> &p_qListParamValues, const QStringList &p_vecParameterDescriptions, bool p_bIsJson, QObject *parent)
109: QObject(parent)
110, m_sCommand(p_sCommand)
111, m_sDescription(p_sDescription)
112, m_bIsJson(p_bIsJson)
113{
114 if(p_qListParamNames.size() == p_qListParamValues.size())
115 {
116 if(p_qListParamValues.size() == p_vecParameterDescriptions.size())
117 {
118 m_qListParamNames = p_qListParamNames;
119 m_qListParamValues = p_qListParamValues;
120 m_qListParamDescriptions = p_vecParameterDescriptions;
121 }
122 }
123 else
124 {
125 qCritical("error: description vector hasn't the same size like parameter map.\n");
126 return;
127 }
128}
129
130//=============================================================================================================
131
132Command::Command(const Command &p_Command)
133: QObject(p_Command.parent())
134, m_sCommand(p_Command.m_sCommand)
139, m_bIsJson(p_Command.m_bIsJson)
140{
141}
142
143//=============================================================================================================
144
148
149//=============================================================================================================
150
152{
153 emit this->executed(*this);
154}
155
156//=============================================================================================================
157
158void Command::reply(const QString &p_sReply)
159{
160 CommandManager* t_commandManager = static_cast<CommandManager*> (this->parent());
161
162 if(t_commandManager) {
163 emit t_commandManager->response(p_sReply, *this);
164 }
165}
166
167//=============================================================================================================
168
170{
171 CommandManager* t_commandManager = static_cast<CommandManager*> (this->parent());
172
173 if(t_commandManager) {
174 emit t_commandManager->triggered(*this);
175 }
176}
177
178//=============================================================================================================
179
180QJsonObject Command::toJsonObject() const
181{
182 QJsonObject p_jsonCommandObject;
183 p_jsonCommandObject.insert("description", QJsonValue(m_sDescription));
184
185 QJsonObject t_jsonAllParametersObject;
186 for(qint32 i = 0; i < m_qListParamValues.size(); ++i)
187 {
188 QJsonObject t_jsonParameterObject;
189 t_jsonParameterObject.insert("description",QJsonValue(m_qListParamDescriptions[i]));
190 t_jsonParameterObject.insert("type",QString(m_qListParamValues[i].typeName()));
191 t_jsonAllParametersObject.insert(m_qListParamNames[i], QJsonValue(t_jsonParameterObject));
192 }
193 p_jsonCommandObject.insert("parameters", QJsonValue(t_jsonAllParametersObject));
194
195 return p_jsonCommandObject;
196}
197
198//=============================================================================================================
199
200QStringList Command::toStringList() const
201{
202 QStringList p_stringCommandList;
203
204 p_stringCommandList << m_sCommand;
205
206 QString t_sParameters;
207 for(qint32 i = 0; i < m_qListParamDescriptions.size(); ++i)
208 {
209 t_sParameters.append("[");
210 t_sParameters.append(m_qListParamDescriptions[i]);
211 t_sParameters.append("]");
212 }
213 p_stringCommandList << t_sParameters;
214
215 p_stringCommandList << m_sDescription;
216
217 return p_stringCommandList;
218}
219
220//=============================================================================================================
221
223{
224 QString p_stringCommand;
225
226 QString t_sParameters;
227 for(qint32 i = 0; i < m_qListParamNames.size(); ++i)
228 {
229// qDebug() << m_qListParamValues[i];
230 t_sParameters.append(QString("\"%1\":\"%2\"").arg(m_qListParamNames[i]).arg(m_qListParamValues[i].toString()));
231
232 if(i < m_qListParamNames.size()-1)
233 t_sParameters.append(",");
234 }
235
236 p_stringCommand.append(QString("\"%1\":{%2}").arg(m_sCommand).arg(t_sParameters));
237
238 return p_stringCommand;
239}
240
241//=============================================================================================================
242
244{
245 if (this != &rhs) // protect against invalid self-assignment
246 {
252 }
253 // to support chained assignment operators (a=b=c), always return *this
254 return *this;
255}
256
257//=============================================================================================================
258
259QVariant& Command::operator[] (const QString &key)
260{
261 if(m_qListParamNames.contains(key))
262 return m_qListParamValues[m_qListParamNames.indexOf(key)];
263 else
264 return defaultVariant;
265}
266
267//=============================================================================================================
268
269QVariant& Command::operator[] (qint32 idx)
270{
271 if(m_qListParamValues.size() > idx)
272 return m_qListParamValues[idx];
273 else
274 return defaultVariant;
275}
276
277//=============================================================================================================
278
279const QVariant Command::operator[] (const QString &key) const
280{
281 if(m_qListParamNames.contains(key))
282 return m_qListParamValues[m_qListParamNames.indexOf(key)];
283 else
284 return defaultVariant;
285}
Per-component registry of COMLIB::Command schemas; receives COMLIB::RawCommand from the parser and di...
Typed, parameter-bearing command object that round-trips between mne-cpp clients and mne_rt_server.
Real-time client/server communication primitives for talking to mne_rt_server.
QStringList toStringList() const
Definition command.cpp:200
QJsonObject toJsonObject() const
Definition command.cpp:180
QString m_sDescription
Definition command.h:317
void reply(const QString &p_sReply)
Definition command.cpp:158
QStringList m_qListParamDescriptions
Definition command.h:320
QStringList m_qListParamNames
Definition command.h:318
Command & operator=(const Command &rhs)
Definition command.cpp:243
Command(bool p_bIsJson=true, QObject *parent=0)
Definition command.cpp:47
QString m_sCommand
Definition command.h:316
QList< QVariant > m_qListParamValues
Definition command.h:319
void executed(Command p_command)
virtual ~Command()
Definition command.cpp:145
virtual void execute()
Definition command.cpp:151
QString toStringReadySend() const
Definition command.cpp:222
QVariant & operator[](const QString &key)
Definition command.cpp:259
Observer-side registry of Command schemas a subsystem accepts from the shared CommandParser.
void response(QString p_sReply, Command p_command)
void triggered(Command p_command)
void insert(const QJsonDocument &p_jsonDocument)