/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Classe qui contient la logique et la machine à états pour l'émission des
	alarmes (TK) au PCC via le module de sorties externes.  Lorsqu'une nouvelle
	détection arrive via la fonction ProcessDetectionsTK(), la classe vérifie
	si la fonction est désactivée et ajoute la TK dans une liste.  Une machine
	à état s'occupe d'émettre les TK en fonction des entrées d'annulation AN1 ou
	AN2 ou lors de l'acquitement automatique.

*/

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

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

/* ************************************************************************** */

#include "TKGenerator.h"
#include "EngLog.h"
#include "ZTLog.h"
#include <QTextStream>
#include "ZTData.h"
#include <QTextCodec>
#include "TKTransportInterface.h"

CTKGenerator::CTKGenerator(CTKTransportInterface *IF)
{
//    mOutputMasks = 0;
//	mOutputModule = 0;

    mTKInterface = IF;

//	mMaintenanceMode = false;
//    mForceZT1Clear = false;
//    mForceZT2Clear = false;


//    //mClearZT1AlarmMask = mClearZT2AlarmMask = 0;
//    mAN1State = mZT1CDVState = mAN2State = mZT2CDVState = false;

//    mZT1TKGeneratorState = TK_GENERATOR_ZT1_STANDBY_STATE;
//    mZT2TKGeneratorState = TK_GENERATOR_ZT2_STANDBY_STATE;

 //   mZT1TKBuffer.clear();
//    mZT1TKDataList.clear();
 //   mZT2TKBuffer.clear();
 //   mZT2TKDataList.clear();
//    mZT1CurTKIndex = 0;
//    mZT2CurTKIndex = 0;

//	mZT1TKStateMachineTimer = new QTimer();
//	mZT1TKStateMachineTimer->setSingleShot(false);
//	connect(mZT1TKStateMachineTimer,SIGNAL(timeout()),this,SLOT(ExecZT1SM()));

//	mZT2TKStateMachineTimer = new QTimer();
//	mZT2TKStateMachineTimer->setSingleShot(false);
//	connect(mZT2TKStateMachineTimer,SIGNAL(timeout()),this,SLOT(ExecZT2SM()));

    ResetAlarmCount(ZTGLOBAL_TYPE_ID);
}

CTKGenerator::~CTKGenerator()
{
//    delete mZT1TKStateMachineTimer;
//    delete mZT2TKStateMachineTimer;
}

//void CTKGenerator::BindPointers(GenericOutputMasks_t *OutputMasks, COutputModule *OutputModule)
//{
//    mOutputMasks = OutputMasks;
//	mOutputModule = OutputModule;



//    mClearZT1AlarmMask = mOutputMasks->OutputVP1Mask | mOutputMasks->OutputVP2Mask | mOutputMasks->OutputVP3Mask | mOutputMasks->OutputVP4Mask |\
//                         mOutputMasks->OutputVP5Mask | mOutputMasks->OutputVP6Mask | mOutputMasks->OutputVF1Mask | mOutputMasks->OutputVF2Mask |\
//                         mOutputMasks->OutputVF3Mask | mOutputMasks->OutputVF4Mask | mOutputMasks->OutputVF5Mask | mOutputMasks->OutputDPEMask |\
//                         mOutputMasks->OutputDPIMask | mOutputMasks->OutputV00Mask | mOutputMasks->OutputDPGMask | mOutputMasks->OutputDFRMask |\
//                         mOutputMasks->OutputPEQ1Mask;

//    mClearZT2AlarmMask = mOutputMasks->OutputWP1Mask | mOutputMasks->OutputWP2Mask | mOutputMasks->OutputWP3Mask | mOutputMasks->OutputWP4Mask |\
//                         mOutputMasks->OutputWP5Mask | mOutputMasks->OutputWP6Mask | mOutputMasks->OutputDPI2Mask | mOutputMasks->OutputDPE2Mask |\
//                         mOutputMasks->OutputV002Mask | mOutputMasks->OutputPEQ2Mask;
//}

//void CTKGenerator::BindTransportInterface(CTKTransportInterface *IF)
//{
// //   mTKInterface = IF;
//}

bool CTKGenerator::UpdateDetectionConfig(CZTDetectionFunctionConfig *Config)
{
    //if(mZT2TKGeneratorState != TK_GENERATOR_ZT2_STANDBY_STATE || mZT1TKGeneratorState != TK_GENERATOR_ZT1_STANDBY_STATE)  //JFM Modbus
    if(mTKInterface->IsZT2TKProcessing() || mTKInterface->IsZT1TKProcessing())  //JFM Modbus
    {
        return false;
    }

    mZTDetectionConfig = Config;
    return true;
}

unsigned int CTKGenerator::ProcessDetectionsTK(CZTDetectionData *NewDetection)
{

 //   quint32 TKFlags = 0;
  //  quint32 Rank = NewDetection->mRank;// DetectionsList->at(i)->mRank;
    quint32 DetectionID = NewDetection->mDetectionID; //DetectionsList->at(i)->mDetectionID;

    CZTDetectionData DetectionData;
    DetectionData.mDetectionID = NewDetection->mDetectionID;;
    DetectionData.mRank = NewDetection->mRank;

    switch(DetectionID)
    {
        case DETECTION_MAGNETIC_SENSOR_COUNT:
        {
			if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_ZT1].TKActive)
			{
				if(mAlarmCount[DETECTION_MAGNETIC_SENSOR_COUNT] < TK_GENERATOR_MAX_V00X)
				{
                    //mZT1TKDataList.append(DetectionData);
                    mTKInterface->AddNewZT1Detection(DetectionData);
					mAlarmCount[DETECTION_MAGNETIC_SENSOR_COUNT]++;
				}
			}
            break;
        }
        case DETECTION_FN_DETECTION:
        {
            if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_FN].TKActive)
            {
				if(mAlarmCount[DETECTION_FN_DETECTION] < TK_GENERATOR_MAX_ALARMS)
				{
                    //mZT1TKDataList.append(DetectionData);
                    mTKInterface->AddNewZT1Detection(DetectionData);
					mAlarmCount[DETECTION_FN_DETECTION]++;
				}
            }
            break;
        }
		case DETECTION_PG_DETECTION:
        {
            if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_PG].TKActive)
            {
				if(mAlarmCount[DETECTION_PG_DETECTION] < TK_GENERATOR_MAX_ALARMS)
				{
                    //mZT1TKDataList.append(DetectionData);
                    mTKInterface->AddNewZT1Detection(DetectionData);
					mAlarmCount[DETECTION_PG_DETECTION]++;
				}
            }
            break;
        }
        case DETECTION_PPI_DETECTION:
        {
			if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_PP].TKActive)
            {
				if(mAlarmCount[DETECTION_PPI_DETECTION] < TK_GENERATOR_MAX_ALARMS)
				{
                   //mZT1TKDataList.append(DetectionData);
                    mTKInterface->AddNewZT1Detection(DetectionData);
					mAlarmCount[DETECTION_PPI_DETECTION]++;
				}
            }
            break;
        }
        case DETECTION_PPE_DETECTION:
        {
            if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_PP].TKActive)
            {
				if(mAlarmCount[DETECTION_PPE_DETECTION] < TK_GENERATOR_MAX_ALARMS)
				{
                    //mZT1TKDataList.append(DetectionData);
                    mTKInterface->AddNewZT1Detection(DetectionData);
					mAlarmCount[DETECTION_PPE_DETECTION]++;
				}
            }
            break;
        }
		case DETECTION_PEQ1_DETECTION:
        {

			if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_ZT1].TKActive)
			{
				if(mAlarmCount[DETECTION_PEQ1_DETECTION] < TK_GENERATOR_MAX_PEQ)
				{
                    //mZT1TKDataList.append(DetectionData);
                    mTKInterface->AddNewZT1Detection(DetectionData);
					mAlarmCount[DETECTION_PEQ1_DETECTION]++;
				}
			}
            break;
        }
		case DETECTION_PEQ2_DETECTION:
        {
			if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_ZT2].TKActive)
			{
				if(mAlarmCount[DETECTION_PEQ2_DETECTION] < TK_GENERATOR_MAX_PEQ)
				{
                   // mZT2TKDataList.append(DetectionData);
                    mTKInterface->AddNewZT2Detection(DetectionData);
					mAlarmCount[DETECTION_PEQ2_DETECTION]++;
				}
			}
            break;
        }
        case DETECTION_ZT2_MAGNETIC_SENSOR_COUNT:
        {
			if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_ZT2].TKActive)
			{
                if(mAlarmCount[DETECTION_ZT2_MAGNETIC_SENSOR_COUNT] < TK_GENERATOR_MAX_V00X)
				{
                    //mZT2TKDataList.append(DetectionData);
                    mTKInterface->AddNewZT2Detection(DetectionData);
                    mAlarmCount[DETECTION_ZT2_MAGNETIC_SENSOR_COUNT]++;
				}
			}
            break;
        }
        case DETECTION_ZT2_PPI_DETECTION:
        {
            if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_PP2].TKActive)
            {
				if(mAlarmCount[DETECTION_ZT2_PPI_DETECTION] < TK_GENERATOR_MAX_ALARMS)
				{
                    //mZT2TKDataList.append(DetectionData);
                    mTKInterface->AddNewZT2Detection(DetectionData);
					mAlarmCount[DETECTION_ZT2_PPI_DETECTION]++;
				}
            }
            break;
        }
        case DETECTION_ZT2_PPE_DETECTION:
        {
            if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_PP2].TKActive)
            {
				if(mAlarmCount[DETECTION_ZT2_PPE_DETECTION] < TK_GENERATOR_MAX_ALARMS)
				{
                    //mZT2TKDataList.append(DetectionData);
                    mTKInterface->AddNewZT2Detection(DetectionData);
					mAlarmCount[DETECTION_ZT2_PPE_DETECTION]++;
				}
            }
            break;
        }
    }

    return RET_OK;
}

//unsigned int CTKGenerator::BeginTKEmission()
//{
//    return mTKInterface->BeginTKEmission();

////	if(mTKInterface->IsTKProcessing())
////		return RET_ERROR;




////    if(mZT1TKDataList.size() > 0)   //JFM Modbus
////    {
////        if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_ZT1].TKActive == false)
////        {
////            mZT1TKDataList.clear();
////        }
////        else if(mZT1TKGeneratorState == TK_GENERATOR_ZT1_STANDBY_STATE)
////        {
////            mZT1TKGeneratorState = TK_ZT1_GENERATOR_SHOW_TK_STATE;
////            mTKInterface->ClearTK(ZT1_TYPE_ID);    //JFM Modbus
////            mZT1TKStateMachineTimer->start(0);
////        }
////    }


////        if(mZT2TKDataList.size() > 0)   //JFM Modbus
////		{
////			if(mZTDetectionConfig->mZTDetectionConfig[DETECTION_FCT_ZT2].TKActive == false)
////			{
////                mZT2TKDataList.clear();
////			}
////			else if(mZT2TKGeneratorState == TK_GENERATOR_ZT2_STANDBY_STATE)
////			{
////				mZT2TKGeneratorState = TK_ZT2_GENERATOR_SHOW_TK_STATE;
////                mTKInterface->ClearTK(ZT2_TYPE_ID); //JFM Modbus
////				mZT2TKStateMachineTimer->start(0);
////			}
////		}
////	return RET_OK;
//}

//unsigned int CTKGenerator::SetInputStates(bool AN1State, bool ZT1CDVState, bool AN2State, bool ZT2CDVState)
//{

//    return mTKInterface->SetInputStates(AN1State,ZT1CDVState,AN2State,ZT2CDVState);
////    mAN1State = AN1State;
////    mZT1CDVState = ZT1CDVState;
////    mAN2State = AN2State;
////    mZT2CDVState = ZT2CDVState;

////    return RET_OK;
//}







//void CTKGenerator::ExecZT1SM()
//{
//    switch(mZT1TKGeneratorState)
//    {
//        case TK_ZT1_GENERATOR_SHOW_TK_STATE:
//        {
////            if(mZT1CurTKIndex >= mZT1TKBuffer.size()) //JFM Modbus
//            if(mZT1CurTKIndex >= mZT1TKDataList.size())
//            {
//                mZT1TKGeneratorState = TK_GENERATOR_ZT1_STANDBY_STATE;
//            }
//            else
//            {
////				mOutputModule->SetOutputFlags(mZT1TKBuffer.at(mZT1CurTKIndex++));
//                mTKInterface->SendTKToCC(mZT1TKDataList.at(mZT1CurTKIndex++));  //JFM Modbus
//				mZT1TKGeneratorState = TK_ZT1_GENERATOR_WAIT_FOR_AN1_STATE;

//                CZTLog::instance()->AddLogString("Émission d'une TK ZT1 au PCC",true);
//                LogTK(mZT1TKDataList.at(mZT1CurTKIndex-1));


//                if(mMaintenanceMode)
//                {
//                    emit TKOutputStatesChanged(true,false);
//                }
//            }

//            break;
//        }
//        case TK_ZT1_GENERATOR_WAIT_FOR_AN1_STATE:
//        {
//            if(mAN1State == true || (mMaintenanceMode == true && mForceZT1Clear == true))
//            {
//                //clear the TK bits
//                //mOutputModule->ClearOutputFlags(mClearZT1AlarmMask);
//                mTKInterface->ClearTK(ZT1_TYPE_ID); //JFM Modbus
//                mZT1RefTimer.start();
//                mZT1TKGeneratorState = TK_ZT1_GENERATOR_SHOW_ZERO_STATE;

//                if(mMaintenanceMode == true)
//                {
//                    if(mForceZT1Clear == true)
//                    {
//                        mForceZT1Clear = false;
//                        CZTLog::instance()->AddLogString("Acquitement manuel de la TK ZT1",true);
//                    }
//                    else
//                    {
//                        CZTLog::instance()->AddLogString("Acquitement AN1 détectée",true);
//                    }

//                    emit TKOutputStatesChanged(false,false);

//                }
//                else
//                {
//                    CZTLog::instance()->AddLogString("Acquitement AN1 détectée",true);
//                }
//            }
//			else if(mZT1CDVState ==  false && mMaintenanceMode == false)
//            {
//                //Enter the auto acquitment TK generation mode...
//                //A TK is already output, make shure it's there long enough.
//                CZTLog::instance()->AddLogString("Libération du CDV de quai ZT1.  Début de l'acquitement automatique",true);
//                mZT1RefTimer.start();
//                mZT1TKGeneratorState = TK_ZT1_GENERATOR_AUTO_WAIT_FOR_TIMEOUT_STATE;
//            }

//            break;
//        }
//        case TK_ZT1_GENERATOR_SHOW_ZERO_STATE:
//        {
//            if(mZT1RefTimer.elapsed() >= TK_GENERATOR_SHOW_ZERO_TIMEOUT)
//            {
//                mZT1TKGeneratorState = TK_ZT1_GENERATOR_SHOW_TK_STATE;
//            }
//            break;
//        }
//        case TK_ZT1_GENERATOR_AUTO_SHOW_TK_AUTO_STATE:
//        {
//            //if(mZT1CurTKIndex >= mZT1TKBuffer.size())
//            if(mZT1CurTKIndex >= mZT1TKDataList.size()) //JF Modbus
//            {
//                mZT1TKGeneratorState = TK_GENERATOR_ZT1_STANDBY_STATE;
//            }
//            else
//            {
////				mOutputModule->SetOutputFlags(mZT1TKBuffer.at(mZT1CurTKIndex++));
//                mTKInterface->SendTKToCC(mZT1TKDataList.at(mZT1CurTKIndex++));  //JFM Modbus
//                mZT1RefTimer.start();
//                mZT1TKGeneratorState = TK_ZT1_GENERATOR_AUTO_WAIT_FOR_TIMEOUT_STATE;
//                CZTLog::instance()->AddLogString("Émission d'une TK ZT1 au PCC",true);
//                LogTK(mZT1TKDataList.at(mZT1CurTKIndex-1));
//            }
//            break;
//        }
//        case TK_ZT1_GENERATOR_AUTO_WAIT_FOR_TIMEOUT_STATE:
//        {
//            if(mZT1RefTimer.elapsed() >= TK_GENERATOR_AUTOMATIC_SHOW_TK_TIMEOUT)
//            {
//                //mOutputModule->ClearOutputFlags(mClearZT1AlarmMask);
//                mTKInterface->ClearTK(ZT1_TYPE_ID); //JFM  Modbus
//                mZT1RefTimer.start();
//                mZT1TKGeneratorState = TK_ZT1_GENERATOR_AUTO_SHOW_ZERO_STATE;
//                CZTLog::instance()->AddLogString("Acquitement automatique TK ZT1",true);
//            }
//            break;
//        }
//        case TK_ZT1_GENERATOR_AUTO_SHOW_ZERO_STATE:
//        {
//            if(mZT1RefTimer.elapsed() >= TK_GENERATOR_SHOW_ZERO_TIMEOUT)
//            {
//                //emit next TK
//                mZT1TKGeneratorState = TK_ZT1_GENERATOR_AUTO_SHOW_TK_AUTO_STATE;
//            }
//            break;
//        }

//        case TK_GENERATOR_ZT1_STANDBY_STATE:
//        {
//  //          mZT1TKBuffer.clear();
//            mZT1TKDataList.clear();
//            mZT1CurTKIndex = 0;
//            mZT1TKStateMachineTimer->stop();
//			ResetAlarmCount();

//			//In maintenance mode, it is possible to have ZT2 events at the same time
//			//than ZT1.  So if any ZT2 events are waiting in the qeue, send them...
//			if(mMaintenanceMode)
//			{
////				if(mZT2TKBuffer.size() > 0)
//                if(mZT2TKDataList.size() > 0)   //JFM Modbus
//				{
//					BeginTKEmission();
//				}
//			}

//            break;
//        }
//    }
//}

//void CTKGenerator::ExecZT2SM()
//{
//	switch(mZT2TKGeneratorState)
//	{
//		case TK_ZT2_GENERATOR_SHOW_TK_STATE:
//		{
////			if(mZT2CurTKIndex >= mZT2TKBuffer.size())
//            if(mZT2CurTKIndex >= mZT2TKDataList.size()) //JFM Modbus
//			{
//				mZT2TKGeneratorState = TK_GENERATOR_ZT2_STANDBY_STATE;
//			}
//			else
//			{
////				mOutputModule->SetOutputFlags(mZT2TKBuffer.at(mZT2CurTKIndex++));
//                mTKInterface->SendTKToCC(mZT2TKDataList.at(mZT2CurTKIndex++));//JFM Modbus
//				mZT2TKGeneratorState = TK_ZT2_GENERATOR_WAIT_FOR_AN2_STATE;
//                CZTLog::instance()->AddLogString("Émission d'une TK ZT2 au PCC",true);
//                LogTK(mZT2TKDataList.at(mZT2CurTKIndex-1));
//                if(mMaintenanceMode)
//                {
//                    emit TKOutputStatesChanged(false,true);
//                }
//			}
//			break;
//		}
//		case TK_ZT2_GENERATOR_WAIT_FOR_AN2_STATE:
//		{
//            if(mAN2State == true || (mMaintenanceMode == true && mForceZT2Clear == true))
//			{
//				//clear the TK bits
//                //mOutputModule->ClearOutputFlags(mClearZT2AlarmMask);
//                mTKInterface->ClearTK(ZT2_TYPE_ID); //JFM Modbus
//				mZT2RefTimer.start();
//				mZT2TKGeneratorState = TK_ZT2_GENERATOR_SHOW_ZERO_STATE;
//                if(mMaintenanceMode == true)
//                {
//                    if(mForceZT2Clear == true)
//                    {
//                        mForceZT2Clear = false;
//                        CZTLog::instance()->AddLogString("Acquitement manuel de la TK ZT2",true);
//                    }
//                    else
//                    {
//                        CZTLog::instance()->AddLogString("Acquitement AN2 détectée",true);
//                    }

//                    emit TKOutputStatesChanged(false,false);
//                }
//                else
//                {
//                    CZTLog::instance()->AddLogString("Acquitement AN2 détectée",true);
//                }
//			}
//			else if(mZT2CDVState ==  false && mMaintenanceMode == false)
//			{
//				//Enter the auto acquitment TK generation mode...
//				//A TK is already output, make shure it's there long enough.
//                CZTLog::instance()->AddLogString("Libération du CDV ZT2.  Début de l'acquitement automatique",true);
//				mZT2RefTimer.start();
//				mZT2TKGeneratorState = TK_ZT2_GENERATOR_AUTO_WAIT_FOR_TIMEOUT_STATE;
//			}
//			break;
//		}
//		case TK_ZT2_GENERATOR_SHOW_ZERO_STATE:
//		{
//			if(mZT2RefTimer.elapsed() >= TK_GENERATOR_SHOW_ZERO_TIMEOUT)
//			{
//				mZT2TKGeneratorState = TK_ZT2_GENERATOR_SHOW_TK_STATE;
//			}
//			break;
//		}
//		case TK_ZT2_GENERATOR_AUTO_SHOW_TK_AUTO_STATE:
//		{
//            //if(mZT2CurTKIndex >= mZT2TKBuffer.size())
//            if(mZT2CurTKIndex >= mZT2TKDataList.size()) //JFM Modbus
//			{
//				mZT2TKGeneratorState = TK_GENERATOR_ZT2_STANDBY_STATE;
//			}
//			else
//			{
//                //mOutputModule->SetOutputFlags(mZT2TKBuffer.at(mZT2CurTKIndex++));
//                mTKInterface->SendTKToCC(mZT2TKDataList.at(mZT2CurTKIndex++));  //JFM Modbus
//				mZT2RefTimer.start();
//				mZT2TKGeneratorState = TK_ZT2_GENERATOR_AUTO_WAIT_FOR_TIMEOUT_STATE;
//                CZTLog::instance()->AddLogString("Émission d'une TK ZT2 au PCC",true);
//                //LogTK(mZT2TKBuffer.at(mZT2CurTKIndex-1));
//                LogTK(mZT2TKDataList.at(mZT2CurTKIndex-1));
//			}
//			break;
//		}
//		case TK_ZT2_GENERATOR_AUTO_WAIT_FOR_TIMEOUT_STATE:
//		{
//			if(mZT2RefTimer.elapsed() >= TK_GENERATOR_AUTOMATIC_SHOW_TK_TIMEOUT)
//			{
//                //mOutputModule->ClearOutputFlags(mClearZT2AlarmMask);
//                mTKInterface->ClearTK(ZT2_TYPE_ID); //JFM Modbus
//				mZT2RefTimer.start();
//				mZT2TKGeneratorState = TK_ZT2_GENERATOR_AUTO_SHOW_ZERO_STATE;
//                CZTLog::instance()->AddLogString("Acquitement automatique ZT2",true);
//			}
//			break;
//		}
//		case TK_ZT2_GENERATOR_AUTO_SHOW_ZERO_STATE:
//		{
//			if(mZT2RefTimer.elapsed() >= TK_GENERATOR_SHOW_ZERO_TIMEOUT)
//			{
//				//emit next TK
//				mZT2TKGeneratorState = TK_ZT2_GENERATOR_AUTO_SHOW_TK_AUTO_STATE;
//			}
//			break;
//		}

//		case TK_GENERATOR_ZT2_STANDBY_STATE:
//		{
////			mZT2TKBuffer.clear();   //JFM Modbus
//            mZT2TKDataList.clear();
//			mZT2CurTKIndex = 0;
//			mZT2TKStateMachineTimer->stop();
//			ResetAlarmCount();

//			//In maintenance mode, it is possible to have ZT2 events at the same time
//			//than ZT1.  So if any ZT1 events are waiting in the qeue, send them...
//			if(mMaintenanceMode)
//			{
//                //if(mZT1TKBuffer.size() > 0)
//                if(mZT1TKDataList.size() > 0)   //JFM Modbus
//				{
//					BeginTKEmission();
//				}
//			}

//			break;
//		}
//	}
//}

void CTKGenerator::ResetAlarmCount(int ZT)
{
    if(ZT == ZT1_TYPE_ID)
    {
        mAlarmCount[DETECTION_MAGNETIC_SENSOR_COUNT] = 0;
        mAlarmCount[DETECTION_FN_DETECTION] = 0;
        mAlarmCount[DETECTION_PG_DETECTION] = 0;
        mAlarmCount[DETECTION_PPI_DETECTION] = 0;
        mAlarmCount[DETECTION_PPE_DETECTION] = 0;
        mAlarmCount[DETECTION_PEQ1_DETECTION] = 0;

    }
    else if(ZT == ZT2_TYPE_ID)
    {
        mAlarmCount[DETECTION_ZT2_MAGNETIC_SENSOR_COUNT] = 0;
        mAlarmCount[DETECTION_ZT2_PPI_DETECTION] = 0;
        mAlarmCount[DETECTION_ZT2_PPE_DETECTION] = 0;
        mAlarmCount[DETECTION_PEQ2_DETECTION] = 0;
    }
    else if(ZT == ZTGLOBAL_TYPE_ID)
    {
        for(int i = 0; i < DETECTION_MAX_DETECTION_ID; i++)
        {
            mAlarmCount[i] = 0;
        }
    }
}

//unsigned int CTKGenerator::CancelAllTK()
//{
//    mTKInterface->ClearTK(ZT1_TYPE_ID); //JFM Modbus
//    mTKInterface->ClearTK(ZT2_TYPE_ID);

//	mZT1TKGeneratorState = TK_GENERATOR_ZT1_STANDBY_STATE;
//	mZT2TKGeneratorState = TK_GENERATOR_ZT2_STANDBY_STATE;

////	mZT1TKBuffer.clear();
//    mZT1TKDataList.clear();
////	mZT2TKBuffer.clear();
//    mZT2TKDataList.clear();


//	//exec the SM once
//	mZT1TKStateMachineTimer->start();
//	mZT2TKStateMachineTimer->start();
//	ResetAlarmCount();

//	return RET_OK;
//}

//unsigned int CTKGenerator::CancelMaintenanceCurrentTK()
//{

//    if(mZT1TKGeneratorState == TK_ZT1_GENERATOR_WAIT_FOR_AN1_STATE)
//    {
//        mForceZT1Clear = true;
//        return RET_OK;
//    }
//    else if(mZT2TKGeneratorState == TK_ZT2_GENERATOR_WAIT_FOR_AN2_STATE)
//    {
//        mForceZT2Clear = true;
//        return RET_OK;
//    }
//    return RET_ERROR;
//}


//unsigned int CTKGenerator::ExitMaintenance()
//{

//	mMaintenanceMode = false;
//    mForceZT1Clear = false;
//    mForceZT2Clear = false;
//	CancelAllTK();

//	return RET_OK;
//}

//unsigned int CTKGenerator::EnterMaintenance()
//{

//    return mTKInterface->E
//    if(mTKInterface->IsTKProcessing())
//	{
//		return RET_ERROR;
//	}


//	mMaintenanceMode = true;
//    mForceZT1Clear = false;
//    mForceZT2Clear = false;

//	return RET_OK;
//}

//bool CTKGenerator::IsTKProcessing()
//{
//	if(mZT1TKGeneratorState == TK_GENERATOR_ZT1_STANDBY_STATE &&
//	   mZT2TKGeneratorState == TK_GENERATOR_ZT2_STANDBY_STATE)
//	{
//		return false;
//	}

//	return true;
//}

//bool CTKGenerator::IsZT1TKProcessing()
//{
//	if(mZT1TKGeneratorState == TK_GENERATOR_ZT1_STANDBY_STATE)
//	{
//		return false;
//	}

//	return true;
//}

//bool CTKGenerator::IsZT2TKProcessing()
//{
//	if(mZT2TKGeneratorState == TK_GENERATOR_ZT2_STANDBY_STATE)
//	{
//		return false;
//	}

//	return true;
//}

void CTKGenerator::LogTK(CZTDetectionData DetectionData)
{
    QString str = "TK envoyée: ";
    QTextStream stream(&str);
    stream.setCodec("UTF-8");

    stream << QString::fromUtf8(CZTData::GetErrorString(DetectionData.mDetectionID));
    stream << " | Rang:  " << DetectionData.mRank;
    CZTLog::instance()->AddLogString(str,true);
}

CZTDetectionFunctionConfig *CTKGenerator::GetDetectionConfig()
{
    return mZTDetectionConfig;
}
