/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2013                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
	Classe haut niveau de gestion des sondes lazer.  Cette classe instancie les
	objets CLazerProbe selon ce qui est défini dans le fichier de configuration
	de la ZT.  Ensuite, elle les ajoute dans une liste et les rend disponible via
	la fonction GetLazerProbeHandle.

*/

/* ************************************************************************** */
/* Revision:
### 20130130 JFM
    Verision d'origine.

### YYYYMMDD Description du besoin ou du bug
    Description du changement.
 */

/* ************************************************************************** */
#include "LazerProbesMgr.h"
#include "ZTconfigmgr.h"
#include "EngLog.h"
#include "ZTLog.h"

CLazerProbesMgr::CLazerProbesMgr():
	mNbOpenedProbes(0)
{
}
CLazerProbesMgr::~CLazerProbesMgr()
{
	for(int i = 0; i < mLazerProbesList.size(); i++)
    {
		if(mLazerProbesList.at(i))
		{
			mLazerProbesList.at(i)->QuitProbeThread();
			mLazerProbesThread.at(i)->quit();
			mLazerProbesThread.at(i)->wait(1000);
			delete mLazerProbesThread.at(i);
			delete mLazerProbesList.at(i);
		}
    }

	mLazerProbesList.clear();
}

unsigned int CLazerProbesMgr::InitLazerProbes()
{

    //Open each module and make shure the configuration matches the slave address

	QList<stLazerProbesConfig_t> * ProbesList = 0;
    int i;

	//Parse list extracted from config file and create modules instances.
	ProbesList = CZTConfigMgr::instance()->GetLazerProbesConfigList();
	CLazerProbe *NewProbe;
	for(i = 0; i < ProbesList->size(); i++)
    {
		NewProbe = new CLazerProbe(ProbesList->at(i).ProbeID, ProbesList->at(i).ProbeType);

		unsigned int Ret = NewProbe->OpenPort(ProbesList->at(i).SerialPort);

		if(Ret != RET_OK)
		{
			CEngLog::instance()->AddLogString(QString().sprintf("Impossible d'ouvrir la sonde lazer.  ID:%d, Type:%d, Port:%s",ProbesList->at(i).ProbeID,ProbesList->at(i).ProbeType,ProbesList->at(i).SerialPort.toUtf8().data()),1);
			delete NewProbe;
			return LAZERPROBES_MGR_RET_PROBE_OPEN_ERROR;
		}

		mLazerProbesList.append(NewProbe);

        QThread *ProbeThread = new QThread;
        NewProbe->moveToThread(ProbeThread);
		connect(ProbeThread,SIGNAL(started()),NewProbe,SLOT(ProbeDataAvailable()));
		mLazerProbesThread.append(ProbeThread);

        ProbeThread->start(QThread::HighPriority);  //JFM 20150421 higher priority to avoid buffer overruns...
	}

	return LAZERPROBES_MGR_RET_OK;
}

CAbstractLazerProbe * CLazerProbesMgr::GetLazerProbeHandle(eLazerProbeType_t LazerProbeType, unsigned int LazerProbeID)
{
	for(int i = 0; i < mLazerProbesList.size(); i++)
    {
		if((int)mLazerProbesList.at(i)->GetType() == LazerProbeType && mLazerProbesList.at(i)->GetID() == LazerProbeID)
			return mLazerProbesList.at(i);
    }
    return NULL;
}
