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 QJsonValue t_jsonValueType = it.value().toObject().value(QString("type"));
70 QMetaType t_metaType = QMetaType::fromName(t_jsonValueType.toString().toUtf8().constData());
71
72 this->m_qListParamNames.push_back(it.key());
73 this->m_qListParamValues.push_back(QVariant(t_metaType));
74 this->m_qListParamDescriptions.push_back(it.value().toObject().value(QString("description")).toString());
75 }
76}
77
78//=============================================================================================================
79
80Command::Command(const QString& p_sCommand, const QString& p_sDescription, bool p_bIsJson, QObject* parent)
81: QObject(parent)
82, m_sCommand(p_sCommand)
83, m_sDescription(p_sDescription)
84, m_bIsJson(p_bIsJson)
85{
86}
87
88//=============================================================================================================
89
90Command::Command(const QString& p_sCommand, const QString& p_sDescription,
91 const QStringList& p_qListParamNames, const QList<QVariant>& p_qListParamValues, bool p_bIsJson, QObject* parent)
92: QObject(parent)
93, m_sCommand(p_sCommand)
94, m_sDescription(p_sDescription)
95, m_bIsJson(p_bIsJson)
96{
97 m_qListParamNames = p_qListParamNames;
98 m_qListParamValues = p_qListParamValues;
99
100 for (qint32 i = 0; i < p_qListParamValues.size(); ++i)
101 m_qListParamDescriptions.append("");
102}
103
104//=============================================================================================================
105
106Command::Command(const QString& p_sCommand, const QString& p_sDescription,
107 const QStringList& p_qListParamNames, const QList<QVariant>& p_qListParamValues, const QStringList& p_vecParameterDescriptions, 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 if (p_qListParamNames.size() == p_qListParamValues.size()) {
114 if (p_qListParamValues.size() == p_vecParameterDescriptions.size()) {
115 m_qListParamNames = p_qListParamNames;
116 m_qListParamValues = p_qListParamValues;
117 m_qListParamDescriptions = p_vecParameterDescriptions;
118 }
119 } else {
120 qCritical("error: description vector hasn't the same size like parameter map.\n");
121 return;
122 }
123}
124
125//=============================================================================================================
126
127Command::Command(const Command& p_Command)
128: QObject(p_Command.parent())
129, m_sCommand(p_Command.m_sCommand)
134, m_bIsJson(p_Command.m_bIsJson)
135{
136}
137
138//=============================================================================================================
139
143
144//=============================================================================================================
145
147{
148 emit this->executed(*this);
149}
150
151//=============================================================================================================
152
153void Command::reply(const QString& p_sReply)
154{
155 CommandManager* t_commandManager = static_cast<CommandManager*>(this->parent());
156
157 if (t_commandManager) {
158 emit t_commandManager->response(p_sReply, *this);
159 }
160}
161
162//=============================================================================================================
163
165{
166 CommandManager* t_commandManager = static_cast<CommandManager*>(this->parent());
167
168 if (t_commandManager) {
169 emit t_commandManager->triggered(*this);
170 }
171}
172
173//=============================================================================================================
174
175QJsonObject Command::toJsonObject() const
176{
177 QJsonObject p_jsonCommandObject;
178 p_jsonCommandObject.insert("description", QJsonValue(m_sDescription));
179
180 QJsonObject t_jsonAllParametersObject;
181 for (qint32 i = 0; i < m_qListParamValues.size(); ++i) {
182 QJsonObject t_jsonParameterObject;
183 t_jsonParameterObject.insert("description", QJsonValue(m_qListParamDescriptions[i]));
184 t_jsonParameterObject.insert("type", QString(m_qListParamValues[i].typeName()));
185 t_jsonAllParametersObject.insert(m_qListParamNames[i], QJsonValue(t_jsonParameterObject));
186 }
187 p_jsonCommandObject.insert("parameters", QJsonValue(t_jsonAllParametersObject));
188
189 return p_jsonCommandObject;
190}
191
192//=============================================================================================================
193
194QStringList Command::toStringList() const
195{
196 QStringList p_stringCommandList;
197
198 p_stringCommandList << m_sCommand;
199
200 QString t_sParameters;
201 for (qint32 i = 0; i < m_qListParamDescriptions.size(); ++i) {
202 t_sParameters.append("[");
203 t_sParameters.append(m_qListParamDescriptions[i]);
204 t_sParameters.append("]");
205 }
206 p_stringCommandList << t_sParameters;
207
208 p_stringCommandList << m_sDescription;
209
210 return p_stringCommandList;
211}
212
213//=============================================================================================================
214
216{
217 QString p_stringCommand;
218
219 QString t_sParameters;
220 for (qint32 i = 0; i < m_qListParamNames.size(); ++i) {
221 // qDebug() << m_qListParamValues[i];
222 t_sParameters.append(QString("\"%1\":\"%2\"").arg(m_qListParamNames[i]).arg(m_qListParamValues[i].toString()));
223
224 if (i < m_qListParamNames.size() - 1)
225 t_sParameters.append(",");
226 }
227
228 p_stringCommand.append(QString("\"%1\":{%2}").arg(m_sCommand).arg(t_sParameters));
229
230 return p_stringCommand;
231}
232
233//=============================================================================================================
234
236{
237 if (this != &rhs) // protect against invalid self-assignment
238 {
244 }
245 // to support chained assignment operators (a=b=c), always return *this
246 return *this;
247}
248
249//=============================================================================================================
250
251QVariant& Command::operator[](const QString& key)
252{
253 if (m_qListParamNames.contains(key))
254 return m_qListParamValues[m_qListParamNames.indexOf(key)];
255 else
256 return defaultVariant;
257}
258
259//=============================================================================================================
260
261QVariant& Command::operator[](qint32 idx)
262{
263 if (m_qListParamValues.size() > idx)
264 return m_qListParamValues[idx];
265 else
266 return defaultVariant;
267}
268
269//=============================================================================================================
270
271const QVariant Command::operator[](const QString& key) const
272{
273 if (m_qListParamNames.contains(key))
274 return m_qListParamValues[m_qListParamNames.indexOf(key)];
275 else
276 return defaultVariant;
277}
Typed, parameter-bearing command object that round-trips between mne-cpp clients and mne_rt_server.
Per-component registry of COMLIB::Command schemas; receives COMLIB::RawCommand from the parser and di...
Real-time client/server communication primitives for talking to mne_rt_server.
QStringList toStringList() const
Definition command.cpp:194
QJsonObject toJsonObject() const
Definition command.cpp:175
QString m_sDescription
Definition command.h:319
void reply(const QString &p_sReply)
Definition command.cpp:153
QStringList m_qListParamDescriptions
Definition command.h:322
QStringList m_qListParamNames
Definition command.h:320
Command & operator=(const Command &rhs)
Definition command.cpp:235
Command(bool p_bIsJson=true, QObject *parent=0)
Definition command.cpp:47
QString m_sCommand
Definition command.h:318
QList< QVariant > m_qListParamValues
Definition command.h:321
void executed(Command p_command)
virtual ~Command()
Definition command.cpp:140
virtual void execute()
Definition command.cpp:146
QString toStringReadySend() const
Definition command.cpp:215
QVariant & operator[](const QString &key)
Definition command.cpp:251
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)