v2.0.0
Loading...
Searching...
No Matches
command_manager.cpp
Go to the documentation of this file.
1//=============================================================================================================
21
22//=============================================================================================================
23// Includes
24//=============================================================================================================
25
26#include "command_manager.h"
27#include "raw_command.h"
28
29//=============================================================================================================
30// Qt Includes
31//=============================================================================================================
32
33#include <QVector>
34#include <QDebug>
35#include <QJsonObject>
36#include <QStringList>
37
38//=============================================================================================================
39// USED NAMESPACES
40//=============================================================================================================
41
42using namespace COMLIB;
43
44//=============================================================================================================
45// DEFINE MEMBER METHODS
46//=============================================================================================================
47
48CommandManager::CommandManager(bool p_bIsActive, QObject* parent)
49: QObject(parent)
50, m_bIsActive(p_bIsActive)
51{
52 init();
53}
54
55//=============================================================================================================
56
57CommandManager::CommandManager(const QByteArray& p_qByteArrayJsonDoc, bool p_bIsActive, QObject* parent)
58: QObject(parent)
59, m_bIsActive(p_bIsActive)
60{
61 init();
62
63 m_jsonDocumentOrigin = QJsonDocument::fromJson(p_qByteArrayJsonDoc);
64
65 insert(m_jsonDocumentOrigin);
66}
67
68//=============================================================================================================
69
70CommandManager::CommandManager(const QJsonDocument& p_jsonDoc, bool p_bIsActive, QObject* parent)
71: QObject(parent)
72, m_bIsActive(p_bIsActive)
73, m_jsonDocumentOrigin(p_jsonDoc)
74{
75 init();
76
77 insert(m_jsonDocumentOrigin);
78}
79
80//=============================================================================================================
81
83{
84 //Disconnect all connections which are created with the help of this manager.
85 // this->disconnectAll();
86
87 //Remove commands which where inserted into the static command list
88}
89
90//=============================================================================================================
91
93{
94 m_qMapCommands.clear();
95}
96
97//=============================================================================================================
98
99void CommandManager::init()
100{
101}
102
103//=============================================================================================================
104//ToDo connect all commands inserted in this class by default.
105void CommandManager::insert(const QJsonDocument& p_jsonDocument)
106{
107 QJsonObject t_jsonObjectCommand;
108
109 //Switch to command object
110 if (p_jsonDocument.isObject() && p_jsonDocument.object().value(QString("commands")) != QJsonValue::Undefined)
111 t_jsonObjectCommand = p_jsonDocument.object().value(QString("commands")).toObject();
112 else
113 return;
114
115 QJsonObject::Iterator it;
116 for (it = t_jsonObjectCommand.begin(); it != t_jsonObjectCommand.end(); ++it) {
117 if (!m_qMapCommands.contains(it.key()))
118 m_qMapCommands.insert(it.key(), Command(it.key(), it.value().toObject(), true, this));
119 else
120 qWarning("Warning: CommandMap contains command %s already. Insertion skipped.\n", it.key().toUtf8().constData());
121 }
122
123 emit commandMapChanged();
124}
125
126//=============================================================================================================
127
128void CommandManager::insert(const QString& p_sKey, const QString& p_sDescription)
129{
130 Command t_command(p_sKey, p_sDescription, false, this);
131 insert(p_sKey, t_command);
132}
133
134//=============================================================================================================
135
136void CommandManager::insert(const QString& p_sKey, const Command& p_command)
137{
138 Command t_command(p_command);
139 t_command.setParent(this);
140 m_qMapCommands.insert(p_sKey, t_command);
141 emit commandMapChanged();
142}
143
144//=============================================================================================================
145
147{
148 // If Manager is not active do not parse commands
149 if (!m_bIsActive)
150 return;
151
152 CommandParser* t_pCommandParser = static_cast<CommandParser*>(p_pSubject);
153
154 RawCommand t_rawCommand(t_pCommandParser->getRawCommand());
155 QString t_sCommandName = t_rawCommand.command();
156
157 if (!this->hasCommand(t_sCommandName))
158 return;
159
160 // check if number of parameters is right
161 if (t_rawCommand.count() >= m_qMapCommands[t_sCommandName].count()) {
162 m_qMapCommands[t_sCommandName].isJson() = t_rawCommand.isJson();
163
164 //Parse Parameters
165 for (quint32 i = 0; i < m_qMapCommands[t_sCommandName].count(); ++i) {
166 QMetaType t_metaType = m_qMapCommands[t_sCommandName][i].metaType();
167
168 QVariant t_qVariantParam(t_rawCommand.pValues()[i]);
169
170 if (t_qVariantParam.canConvert(t_metaType) && t_qVariantParam.convert(t_metaType))
171 m_qMapCommands[t_sCommandName][i] = t_qVariantParam;
172 else
173 return;
174 }
175
176 m_qMapCommands[t_sCommandName].execute();
177 }
178}
179
180//=============================================================================================================
181
183{
184 return m_qMapCommands[key];
185}
186
187//=============================================================================================================
188
189const Command CommandManager::operator[](const QString& key) const
190{
191 return m_qMapCommands[key];
192}
Untyped intermediate produced by COMLIB::CommandParser before type information is bound to parameters...
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.
Named, parameterised command exchanged with mne_rt_server; supports both JSON and CLI serialisation.
Definition command.h:87
virtual void update(UTILSLIB::Subject *p_pSubject)
bool hasCommand(const QString &p_sCommand) const
CommandManager(bool p_bIsActive=true, QObject *parent=0)
Command & operator[](const QString &key)
void insert(const QJsonDocument &p_jsonDocument)
Observer-pattern subject that decodes JSON or CLI command strings into RawCommand and notifies attach...
RawCommand & getRawCommand()
Tokenised but unresolved command: a keyword plus a list of raw string arguments awaiting type binding...
Definition raw_command.h:71
bool isJson() const
QString command() const
QList< QString > & pValues()
quint32 count() const
The Subject class provides the base class of every subject of the observer design pattern.