MNE-CPP  beta 0.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mnertclient.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "mnertclient.h"
42 #include "mnertclientproducer.h"
43 
45 
46 #include <utils/ioutils.h>
47 
48 
49 //*************************************************************************************************************
50 //=============================================================================================================
51 // QT INCLUDES
52 //=============================================================================================================
53 
54 #include <QtCore/QtPlugin>
55 #include <QtCore/QTextStream>
56 #include <QtCore/QFile>
57 
58 #include <QList>
59 #include <QDebug>
60 
61 
62 //*************************************************************************************************************
63 //=============================================================================================================
64 // USED NAMESPACES
65 //=============================================================================================================
66 
67 using namespace MneRtClientPlugin;
68 using namespace UTILSLIB;
69 
70 
71 //*************************************************************************************************************
72 //=============================================================================================================
73 // DEFINE MEMBER METHODS
74 //=============================================================================================================
75 
77 : m_sMneRtClientClientAlias("mne-x")
78 , m_pRtCmdClient(NULL)
79 , m_bCmdClientIsConnected(false)
80 , m_sMneRtClientIP("127.0.0.1")//("172.21.16.88")//("127.0.0.1")
81 , m_pMneRtClientProducer(new MneRtClientProducer(this))
82 , m_iBufferSize(-1)
83 , m_pRawMatrixBuffer_In(0)
84 /*m_pRTMSA_MneRtClient(0)*/
85 {
86 
87 }
88 
89 
90 //*************************************************************************************************************
91 
93 {
94  if(this->isRunning())
95  stop();
96  if(m_pMneRtClientProducer->isRunning())
97  m_pMneRtClientProducer->stop();
98 }
99 
100 
101 //*************************************************************************************************************
102 
103 QSharedPointer<IPlugin> MneRtClient::clone() const
104 {
105  QSharedPointer<MneRtClient> pMneRtClientClone(new MneRtClient());
106  return pMneRtClientClone;
107 }
108 
109 
110 //*************************************************************************************************************
111 
113 {
114  // Start MneRtClientProducer
115  m_pMneRtClientProducer->start();
116 
117 
118 // //Convinience CMD connection timer --> ToDo get rid of that -> it interrupts acquistion when not connected
119 // connect(&m_cmdConnectionTimer, &QTimer::timeout, this, &MneRtClient::connectCmdClient);
120 
121  //init channels when fiff info is available
122  connect(this, &MneRtClient::fiffInfoAvailable, this, &MneRtClient::initConnector);
123 
124 // //Start convinience timer
125 // m_cmdConnectionTimer.start(5000);
126 
127  //Try to connect the cmd client on start up using localhost connection
128  this->connectCmdClient();
129 }
130 
131 
132 //*************************************************************************************************************
133 //=============================================================================================================
134 // Create measurement instances and config them
135 //=============================================================================================================
136 
137 void MneRtClient::initConnector()
138 {
139 
140 
141  qDebug() << "MneRtClient::init()";
142 
143 // if(m_pFiffInfo)
144 // {
146 // m_pRTMSA_MneRtClient = addProviderRealTimeMultiSampleArray_New(MSR_ID::MEGMNERTCLIENT_OUTPUT);
147 // m_pRTMSA_MneRtClient->initFromFiffInfo(m_pFiffInfo);
148 // m_pRTMSA_MneRtClient->setMultiArraySize(10);
149 // }
150 
151 
152  if(m_pFiffInfo)
153  {
154  m_pRTMSA_MneRtClient = PluginOutputData<NewRealTimeMultiSampleArray>::create(this, "RtClient", "MNE Rt Client");
155 
156  m_pRTMSA_MneRtClient->data()->initFromFiffInfo(m_pFiffInfo);
157  m_pRTMSA_MneRtClient->data()->setMultiArraySize(10);
158 
159  m_pRTMSA_MneRtClient->data()->setVisibility(true);
160 
161  m_outputConnectors.append(m_pRTMSA_MneRtClient);
162  }
163 
164 }
165 
166 
167 //*************************************************************************************************************
168 
169 void MneRtClient::changeConnector(qint32 p_iNewConnectorId)
170 {
171  if(p_iNewConnectorId != m_iActiveConnectorId)
172  {
173  // read meas info
174  (*m_pRtCmdClient)["selcon"].pValues()[0].setValue(p_iNewConnectorId);
175  (*m_pRtCmdClient)["selcon"].send();
176 
177  m_iActiveConnectorId = p_iNewConnectorId;
178 
179  // clear all and request everything new
180  clear();
181 
182  //
183  // request available commands
184  //
185  m_pRtCmdClient->requestCommands();
186 
187  //
188  // Read Info
189  //
190  if(!m_pFiffInfo)
191  requestInfo();
192 
193  //
194  // Read Buffer Size
195  //
196  m_iBufferSize = m_pRtCmdClient->requestBufsize();
197 
198  emit cmdConnectionChanged(m_bCmdClientIsConnected);
199  }
200 }
201 
202 
203 //*************************************************************************************************************
204 
206 {
207  m_pFiffInfo.reset();
208  m_iBufferSize = -1;
209 }
210 
211 
212 //*************************************************************************************************************
213 
215 {
216  if(m_pRtCmdClient.isNull())
217  m_pRtCmdClient = QSharedPointer<RtCmdClient>(new RtCmdClient);
218  else if(m_bCmdClientIsConnected)
219  this->disconnectCmdClient();
220 
221  m_pRtCmdClient->connectToHost(m_sMneRtClientIP);
222  m_pRtCmdClient->waitForConnected(1000);
223 
224  if(m_pRtCmdClient->state() == QTcpSocket::ConnectedState)
225  {
226  rtServerMutex.lock();
227 
228  //Stop convinience timer
229 // m_cmdConnectionTimer.stop();
230 
231  if(!m_bCmdClientIsConnected)
232  {
233  //
234  // request available commands
235  //
236  m_pRtCmdClient->requestCommands();
237 
238  //
239  // set cmd client is connected
240  //
241  m_bCmdClientIsConnected = true;
242 
243  //
244  // Read Info
245  //
246  if(!m_pFiffInfo)
247  requestInfo();
248 
249  //
250  // Read Connectors
251  //
252  if(m_qMapConnectors.size() == 0)
253  m_iActiveConnectorId = m_pRtCmdClient->requestConnectors(m_qMapConnectors);
254 
255  //
256  // Read Buffer Size
257  //
258  m_iBufferSize = m_pRtCmdClient->requestBufsize();
259 
260  emit cmdConnectionChanged(m_bCmdClientIsConnected);
261  }
262  rtServerMutex.unlock();
263  }
264 }
265 
266 
267 //*************************************************************************************************************
268 
270 {
271  if(m_bCmdClientIsConnected)
272  {
273  m_pRtCmdClient->disconnectFromHost();
274  m_pRtCmdClient->waitForDisconnected();
275  rtServerMutex.lock();
276  m_bCmdClientIsConnected = false;
277  rtServerMutex.unlock();
278  emit cmdConnectionChanged(m_bCmdClientIsConnected);
279  }
280 }
281 
282 
283 //*************************************************************************************************************
284 
286 {
287  if(m_pMneRtClientProducer->m_iDataClientId > -1 && m_bCmdClientIsConnected)
288  {
289  // read meas info
290  (*m_pRtCmdClient)["measinfo"].pValues()[0].setValue(m_pMneRtClientProducer->m_iDataClientId);
291  (*m_pRtCmdClient)["measinfo"].send();
292 
293  m_pMneRtClientProducer->producerMutex.lock();
294  m_pMneRtClientProducer->m_bFlagInfoRequest = true;
295  m_pMneRtClientProducer->producerMutex.unlock();
296  }
297  else
298  qWarning() << "MneRtClientProducer is not connected!";
299 }
300 
301 
302 //*************************************************************************************************************
303 
305 {
306  if(m_bCmdClientIsConnected && m_pFiffInfo)
307  {
308  // Initialize real time measurements
309  init();
310 
311  //Set buffer size
312  (*m_pRtCmdClient)["bufsize"].pValues()[0].setValue(m_iBufferSize);
313  (*m_pRtCmdClient)["bufsize"].send();
314 
315  // Buffer
316  m_pRawMatrixBuffer_In = QSharedPointer<RawMatrixBuffer>(new RawMatrixBuffer(8,m_pFiffInfo->nchan,m_iBufferSize));
317 
318  // Start threads
319  QThread::start();
320 
321  // Start Measurement at rt_Server
322  // start measurement
323  (*m_pRtCmdClient)["start"].pValues()[0].setValue(m_pMneRtClientProducer->m_iDataClientId);
324  (*m_pRtCmdClient)["start"].send();
325 
326  m_pMneRtClientProducer->producerMutex.lock();
327  m_pMneRtClientProducer->m_bFlagMeasuring = true;
328  m_pMneRtClientProducer->producerMutex.unlock();
329  m_pMneRtClientProducer->start();
330 
331  return true;
332  }
333  else
334  return false;
335 }
336 
337 
338 //*************************************************************************************************************
339 
341 {
342  if(m_bCmdClientIsConnected) //ToDo replace this with is running
343  {
344  // Stop Measurement at rt_Server
345  (*m_pRtCmdClient)["stop-all"].send();
346 
347  m_pMneRtClientProducer->producerMutex.lock();
348  m_pMneRtClientProducer->m_bFlagMeasuring = false;
349  m_pMneRtClientProducer->producerMutex.unlock();
350  }
351 
352  // Stop threads
353  QThread::terminate();
354  QThread::wait();
355 
356  //Clear Buffers
357  m_pRawMatrixBuffer_In->clear();
358 
359  return true;
360 }
361 
362 
363 //*************************************************************************************************************
364 
366 {
367  return _ISensor;
368 }
369 
370 
371 //*************************************************************************************************************
372 
373 QString MneRtClient::getName() const
374 {
375  return "RT Client";
376 }
377 
378 
379 //*************************************************************************************************************
380 
382 {
383  MneRtClientSetupWidget* widget = new MneRtClientSetupWidget(this);//widget is later distroyed by CentralWidget - so it has to be created everytime new
384 
385  //init dialog
386 
387  return widget;
388 }
389 
390 
391 //*************************************************************************************************************
392 
394 {
395 
396  MatrixXf matValue;
397  while(true)
398  {
399  //pop matrix
400  matValue = m_pRawMatrixBuffer_In->pop();
401 // std::cout << "matValue " << matValue.block(0,0,1,10) << std::endl;
402 
403  //emit values
404  for(qint32 i = 0; i < matValue.cols(); ++i)
405  m_pRTMSA_MneRtClient->data()->setValue(matValue.col(i).cast<double>());
406 // for(qint32 i = 0; i < matValue.cols(); i += 100)
407 // m_pRTMSA_MneRtClient->setValue(matValue.col(i).cast<double>());
408  }
409 }
OutputConnectorList m_outputConnectors
Definition: IPlugin.h:232
virtual QWidget * setupWidget()
The MneRtClientProducer class provides a Rt Client data producer for a given sampling rate...
Real-time command client.
Definition: rtcmdclient.h:86
Contains the declaration of the MneRtClientSetupWidget class.
Contains the declaration of the RTServerProducer class.
Contains the declaration of the MneRtClient class.
The MneRtClientSetupWidget class provides the ECG configuration window.
virtual QString getName() const
virtual QSharedPointer< IPlugin > clone() const
static QSharedPointer< PluginOutputData< T > > create(IPlugin *parent, const QString &name, const QString &descr)
void changeConnector(qint32 p_iNewConnectorId)
virtual IPlugin::PluginType getType() const
void cmdConnectionChanged(bool p_bStatus)
IOUtils class declaration.