7 #include "commandmanager.h"
21 using namespace COMMUNICATIONLIB;
31 , m_bIsJson(p_bIsJson)
37 Command::Command(
const QString &p_sCommand,
const QJsonObject &p_qCommandDescription,
bool p_bIsJson, QObject *parent)
39 , m_bIsJson(p_bIsJson)
41 this->m_sCommand = p_sCommand;
42 this->m_sDescription = p_qCommandDescription.value(QString(
"description")).toString();
44 QJsonObject t_jsonObjectParameter = p_qCommandDescription.value(QString(
"parameters")).toObject();
46 QJsonObject::Iterator it;
48 for(it = t_jsonObjectParameter.begin(); it != t_jsonObjectParameter.end(); ++it)
50 QJsonValue t_jsonValueType = it.value().toObject().value(QString(
"type"));
51 QVariant::Type t_type = QVariant::nameToType(t_jsonValueType.toString().toUtf8().constData());
53 this->m_qListParamNames.push_back(it.key());
54 this->m_qListParamValues.push_back(QVariant(t_type));
55 this->m_qListParamDescriptions.push_back(it.value().toObject().value(QString(
"description")).toString());
61 Command::Command(
const QString &p_sCommand,
const QString &p_sDescription,
bool p_bIsJson, QObject *parent)
63 , m_sCommand(p_sCommand)
64 , m_sDescription(p_sDescription)
65 , m_bIsJson(p_bIsJson)
72 const QStringList &p_qListParamNames,
const QList<QVariant> &p_qListParamValues,
bool p_bIsJson, QObject *parent)
74 , m_sCommand(p_sCommand)
75 , m_sDescription(p_sDescription)
76 , m_bIsJson(p_bIsJson)
78 m_qListParamNames = p_qListParamNames;
79 m_qListParamValues = p_qListParamValues;
81 for(qint32 i = 0; i < p_qListParamValues.size(); ++i)
82 m_qListParamDescriptions.append(
"");
88 const QStringList &p_qListParamNames,
const QList<QVariant> &p_qListParamValues,
const QStringList &p_vecParameterDescriptions,
bool p_bIsJson, QObject *parent)
90 , m_sCommand(p_sCommand)
91 , m_sDescription(p_sDescription)
92 , m_bIsJson(p_bIsJson)
94 if(p_qListParamNames.size() == p_qListParamValues.size())
96 if(p_qListParamValues.size() == p_vecParameterDescriptions.size())
98 m_qListParamNames = p_qListParamNames;
99 m_qListParamValues = p_qListParamValues;
100 m_qListParamDescriptions = p_vecParameterDescriptions;
105 printf(
"error: description vector hasn't the same size like parameter map.\n");
113 : QObject(p_Command.parent())
114 , m_sCommand(p_Command.m_sCommand)
115 , m_sDescription(p_Command.m_sDescription)
116 , m_qListParamNames(p_Command.m_qListParamNames)
117 , m_qListParamValues(p_Command.m_qListParamValues)
118 , m_qListParamDescriptions(p_Command.m_qListParamDescriptions)
119 , m_bIsJson(p_Command.m_bIsJson)
142 if(t_commandManager) {
143 emit t_commandManager->
response(p_sReply, *
this);
153 if(t_commandManager) {
162 QJsonObject p_jsonCommandObject;
163 p_jsonCommandObject.insert(
"description", QJsonValue(m_sDescription));
165 QJsonObject t_jsonAllParametersObject;
166 for(qint32 i = 0; i < m_qListParamValues.size(); ++i)
168 QJsonObject t_jsonParameterObject;
169 t_jsonParameterObject.insert(
"description",QJsonValue(m_qListParamDescriptions[i]));
170 t_jsonParameterObject.insert(
"type",QString(m_qListParamValues[i].typeName()));
171 t_jsonAllParametersObject.insert(m_qListParamNames[i], QJsonValue(t_jsonParameterObject));
173 p_jsonCommandObject.insert(
"parameters", QJsonValue(t_jsonAllParametersObject));
175 return p_jsonCommandObject;
182 QStringList p_stringCommandList;
184 p_stringCommandList << m_sCommand;
186 QString t_sParameters;
187 for(qint32 i = 0; i < m_qListParamDescriptions.size(); ++i)
189 t_sParameters.append(
"[");
190 t_sParameters.append(m_qListParamDescriptions[i]);
191 t_sParameters.append(
"]");
193 p_stringCommandList << t_sParameters;
195 p_stringCommandList << m_sDescription;
197 return p_stringCommandList;
204 QString p_stringCommand;
206 QString t_sParameters;
207 for(qint32 i = 0; i < m_qListParamNames.size(); ++i)
210 t_sParameters.append(QString(
"\"%1\":\"%2\"").arg(m_qListParamNames[i]).arg(m_qListParamValues[i].toString()));
212 if(i < m_qListParamNames.size()-1)
213 t_sParameters.append(
",");
216 p_stringCommand.append(QString(
"\"%1\":{%2}").arg(m_sCommand).arg(t_sParameters));
218 return p_stringCommand;
227 m_sCommand = rhs.m_sCommand;
228 m_sDescription = rhs.m_sDescription;
229 m_qListParamNames = rhs.m_qListParamNames;
230 m_qListParamValues = rhs.m_qListParamValues;
231 m_qListParamDescriptions = rhs.m_qListParamDescriptions;
241 if(m_qListParamNames.contains(key))
242 return m_qListParamValues[m_qListParamNames.indexOf(key)];
244 return defaultVariant;
251 if(m_qListParamValues.size() > idx)
252 return m_qListParamValues[idx];
254 return defaultVariant;
261 if(m_qListParamNames.contains(key))
262 return m_qListParamValues[m_qListParamNames.indexOf(key)];
264 return defaultVariant;