MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
commandparser.cpp
Go to the documentation of this file.
1//=============================================================================================================
37//=============================================================================================================
38// INCLUDES
39//=============================================================================================================
40
41#include "commandparser.h"
42#include "commandmanager.h"
43
44//=============================================================================================================
45// QT INCLUDES
46//=============================================================================================================
47
48#include <QDebug>
49#include <QStringList>
50#include <QJsonObject>
51#include <QJsonDocument>
52
53//=============================================================================================================
54// USED NAMESPACES
55//=============================================================================================================
56
57using namespace COMMUNICATIONLIB;
58
59//=============================================================================================================
60// DEFINE MEMBER METHODS
61//=============================================================================================================
62
64: QObject(parent)
65{
66}
67
68//=============================================================================================================
69
70bool CommandParser::exists(const QString& p_sCommand)
71{
72 Subject::t_Observers::Iterator itObservers;
73 for(itObservers = this->observers().begin(); itObservers != this->observers().end(); ++itObservers)
74 {
75 CommandManager* t_pCommandManager = static_cast<CommandManager*> (*itObservers);
76 if(t_pCommandManager->hasCommand(p_sCommand))
77 return true;
78 }
79 return false;
80}
81
82//=============================================================================================================
83
84bool CommandParser::parse(const QString &p_sInput, QStringList &p_qListCommandsParsed)
85{
86 if(p_sInput.size() <= 0)
87 return false;
88
89 p_qListCommandsParsed.clear();
90
91 //Check if JSON format;
92 bool isJson = false;
93 if(QString::compare(p_sInput.at(0), QString("{")) == 0)
94 isJson = true;
95
96 if(isJson)
97 {
98 qDebug() << "JSON command recognized";
99
100 QJsonObject t_jsonObjectCommand;
101 QJsonObject t_jsonObjectParameters;
102 QJsonDocument t_jsonDocument(QJsonDocument::fromJson(p_sInput.toUtf8()));
103
104 //Switch to command object
105 if(t_jsonDocument.isObject() && t_jsonDocument.object().value(QString("commands")) != QJsonValue::Undefined)
106 t_jsonObjectCommand = t_jsonDocument.object().value(QString("commands")).toObject();
107 else
108 return false;
109
110 //iterate over commands
111 QJsonObject::Iterator it;
112 QJsonObject::Iterator itParam;
113 for(it = t_jsonObjectCommand.begin(); it != t_jsonObjectCommand.end(); ++it)
114 {
115 //Print Command
116 printf("%s\r\n", it.key().toUtf8().constData());
117
118 if(exists(it.key()))
119 {
120 RawCommand t_rawCommand(it.key(), true);
121 m_rawCommand = t_rawCommand;
122 t_jsonObjectParameters = it.value().toObject();
123
124 // push command to processed commands
125 p_qListCommandsParsed.push_back(it.key());
126
127 //append the parameters
128 for(itParam= t_jsonObjectParameters.begin(); itParam != t_jsonObjectParameters.end(); ++itParam)
129 {
130 printf(" %s", itParam.value().toString().toUtf8().constData());
131 //ToDo do a cross check with the param naming and key
132 m_rawCommand.pValues().append(itParam.value().toString());
133// qDebug() << itParam.key() << " + " << itParam.value().toString();
134 }
135
136 //Notify attached command manager
137 notify();
138 }
139 printf("\r\n");
140 }
141 }
142 else
143 {
144 QStringList t_qCommandList = p_sInput.split(" ");
145
146 //Print command
147 printf("%s\r\n", t_qCommandList[0].toUtf8().constData());
148
149 if(!exists(t_qCommandList[0]))
150 {
151 printf("\r\n");
152 return false;
153 }
154
155 RawCommand t_rawCommand(t_qCommandList[0], false);
156 m_rawCommand = t_rawCommand;
157
158 // push command to processed commands
159 p_qListCommandsParsed.push_back(t_qCommandList[0]);
160
161 if(t_qCommandList.size() > 1) //Parameter parsing
162 {
163 //Parse Parameters
164 for(qint32 i = 1; i < t_qCommandList.size(); ++i)
165 {
166 printf(" %s", t_qCommandList[i].toUtf8().constData());
167 m_rawCommand.pValues().append(t_qCommandList[i]);
168 }
169 }
170 printf("\r\n");
171 notify();
172 }
173
174 return true;
175}
Declaration of the CommandParser Class.
bool hasCommand(const QString &p_sCommand) const
bool exists(const QString &p_sCommand)
bool parse(const QString &p_sInput, QStringList &p_qListCommandsParsed)
QList< QString > & pValues()
Definition rawcommand.h:184
t_Observers & observers()