/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Classe fourre-tout qui contient les différentes structures données utilisées
	un peu partout dans le programme.
	
*/

/* ************************************************************************** */
/* Revision:
### YYYMMDD JFM
	Verision d'origine.
	
### YYYYMMDD Description du besoin ou du bug
	Description du changement.
 */

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

#include "ZTData.h"

const char * CZTData::TrainTypeString[] = {"Inconnu","MR63/73", "MPM10"};
CZTData::CZTData()
{
}

const char * CZTData::ErrorString[] = {"Erreur de comptage S1-S2",
									   "Déclenchement frotteur négatif",
									   "Déclenchement pneu de guidage",
									   "Déclenchement pneu porteur intérieur",
									   "Déclenchement pneu porteur extérieur",

									   "Erreur de comptage ZT2",
                                       "Déclenchement pneu porteur intérieur ZT2",
									   "Déclenchement pneu porteur extérieur ZT2",

									   "Panne équipement ZT1",
									   "Panne équipement ZT2",

									  };

const char * CZTData::ShortErrorString[] = {"V00",
                                            "DFN",
											"DPG",
											"DPPI",
                                            "DPPE",

											"V002",
											"DPPI2",
											"DPPE2",

											"PEQ1",
											"PEQ2"};

const char* CZTData::GetErrorString(unsigned int ErrorID)
{
	if(ErrorID >= DETECTION_MAX_DETECTION_ID)
		ErrorID = 0;
	return ErrorString[ErrorID];
}

const char* CZTData::GetShortErrorString(unsigned int ErrorID)
{
	if(ErrorID >= DETECTION_MAX_DETECTION_ID)
		ErrorID = 0;
	return ShortErrorString[ErrorID];
}

const char* CZTData::GetTrainTypeString(unsigned int TrainType)
{
	if(TrainType >= TRAIN_TYPE_MAX_ID)
		TrainType = TRAIN_TYPE_UNKNOWN;
	return TrainTypeString[TrainType];
}

const char* CZTData::GetModbusTrainTypeString(unsigned int TrainType)
{
    switch(TrainType)
    {
        case MODBUS_CC_TRAIN_TYPE_MR63:
        {
            return "MR63";
            break;
        }
        case MODBUS_CC_TRAIN_TYPE_MR73:
        {
            return "MR73";
            break;
        }
        case MODBUS_CC_TRAIN_TYPE_MPM10:
        {
            return "MPM10";
            break;
        }
        case MODBUS_CC_TRAIN_TYPE_MAINTENANCE:
        {
            return "Vehicule Travaux";
            break;
        }
        case MODBUS_CC_TRAIN_TYPE_INVALID_ATS_OFFLINE:
        case MODBUS_CC_TRAIN_TYPE_INVALID_SERVICE_REBOOT:
        case MODBUS_CC_TRAIN_TYPE_INVALID_NOT_UPDATED:
        case MODBUS_CC_TRAIN_TYPE_INVALID_SACL_OFFLINE:
        default:
        {
            return "Inconnu";
            break;
        }
    }
}


CZT1LogData::CZT1LogData(CZT1ThreadData *ThreadData):
	mZT1ThreadData(ThreadData)
{
}
CZT1LogData::~CZT1LogData()
{
	if(mZT1ThreadData != 0)
		delete mZT1ThreadData;
}

CZT1ThreadData::~CZT1ThreadData()
{

}

void CZT1ThreadData::operator =(CZT1ThreadData& TD)
{
	mTimeStamp = TD.mTimeStamp;
	mDateTime = TD.mDateTime;
	mS1 = TD.mS1;
	mS2 = TD.mS2;
	mPInt = TD.mPInt;
	mPExt = TD.mPExt;
	mFN = TD.mFN;
	mPG = TD.mPG;

	mBogie = TD.mBogie;
	mRank = TD.mRank;
	mS1Count = TD.mS1Count;
	mS2Count = TD.mS2Count;
	mFNCount = TD.mFNCount;
	mPGExtValue = TD.mPGExtValue;
	mPGIntValue = TD.mPGIntValue;
	mTrainSpeed = TD.mTrainSpeed;
	mTrainType = TD.mTrainType;
}



QDataStream &operator<<(QDataStream &out, const CZT1FlagsData &source)
{
	out << source.mExtPGOffset
		<< source.mIntPGOffset
		<< source.mPGCalibrationON
		<< source.mPGTresholdValue
		<< source.mAnalogTracePresent
        << source.mIsProblematicPassage
        << source.mTrainCompo1
        << source.mTrainCompo2
        << source.mTrainCompo3
        << source.mModbusTrainType
        << source.mZT1ITI
        << source.mZTDetectionsConfig
        << source.mUnusedFlag14
        << source.mUnusedFlag15
        << source.mUnusedFlag16
        << source.mUnusedFlag17
        << source.mUnusedFlag18
        << source.mUnusedFlag19
        << source.mUnusedFlag20;

	return out;
}

QDataStream &operator>>(QDataStream &in, CZT1FlagsData &dest)
{
		in >> dest.mExtPGOffset
		   >> dest.mIntPGOffset
		   >> dest.mPGCalibrationON
		   >> dest.mPGTresholdValue
		   >> dest.mAnalogTracePresent
           >> dest.mIsProblematicPassage
           >> dest.mTrainCompo1
           >> dest.mTrainCompo2
           >> dest.mTrainCompo3
           >> dest.mModbusTrainType
           >> dest.mZT1ITI
           >> dest.mZTDetectionsConfig
           >> dest.mUnusedFlag14
           >> dest.mUnusedFlag15
           >> dest.mUnusedFlag16
           >> dest.mUnusedFlag17
           >> dest.mUnusedFlag18
           >> dest.mUnusedFlag19
           >> dest.mUnusedFlag20;

		return in;
}

QDataStream& operator<<(QDataStream &out, const CZT1LogData &source)
{
	quint32 ThreadDataValid = 0;
	if(source.mZT1ThreadData != 0)
		ThreadDataValid = 1;


	out << source.mDateTime
		<< source.mTimestamp
		<< source.mCIZT1
		<< source.mCDVApproach_ZT1
		<< source.mCDVARM_ZT1
		<< ThreadDataValid;

	if(ThreadDataValid == 1)
	{
		out << source.mZT1ThreadData->mS1
			<< source.mZT1ThreadData->mS2
			<< source.mZT1ThreadData->mFN
			<< source.mZT1ThreadData->mPInt
			<< source.mZT1ThreadData->mPExt
			<< source.mZT1ThreadData->mPG
			<< source.mZT1ThreadData->mTrainSpeed
			<< source.mZT1ThreadData->mBogie
			<< source.mZT1ThreadData->mRank
			<< source.mZT1ThreadData->mPGExtValue
			<< source.mZT1ThreadData->mPGIntValue
            << source.mZT1ThreadData->mTrainType
            << source.mZT1ThreadData->mS1Count
            << source.mZT1ThreadData->mS2Count;
	}

	return out;
}

QDataStream &operator>>(QDataStream &in, CZT1LogData &dest)
{
	quint32 ThreadValid;
	in >> dest.mDateTime
		  >> dest.mTimestamp
		  >> dest.mCIZT1
		  >> dest.mCDVApproach_ZT1
		  >> dest.mCDVARM_ZT1
		  >> ThreadValid;
	if(ThreadValid == 1)
	{
		dest.mZT1ThreadData = new CZT1ThreadData();
		in >> dest.mZT1ThreadData->mS1
		   >> dest.mZT1ThreadData->mS2
		   >> dest.mZT1ThreadData->mFN
		   >> dest.mZT1ThreadData->mPInt
		   >> dest.mZT1ThreadData->mPExt
		   >> dest.mZT1ThreadData->mPG
		   >> dest.mZT1ThreadData->mTrainSpeed
		   >> dest.mZT1ThreadData->mBogie
		   >> dest.mZT1ThreadData->mRank
		   >> dest.mZT1ThreadData->mPGExtValue
		   >> dest.mZT1ThreadData->mPGIntValue
           >> dest.mZT1ThreadData->mTrainType
           >>dest.mZT1ThreadData->mS1Count
           >>dest.mZT1ThreadData->mS2Count;
	}

	return in;
}


QDataStream &operator<<(QDataStream &out, const CZTDetectionData &source)
{
    out << source.mTimeStamp
		<< source.mRank
		<< source.mDetectionID;

	return out;
}

QDataStream &operator>>(QDataStream &in, CZTDetectionData &dest)
{
    in >> dest.mTimeStamp
	   >> dest.mRank
	   >> dest.mDetectionID;

	return in;
}

CZT2LogData::CZT2LogData(CZT2ThreadData *ThreadData):
    mZT2ThreadData(ThreadData)
{
}
CZT2LogData::~CZT2LogData()
{
    if(mZT2ThreadData != 0)
        delete mZT2ThreadData;
}
QDataStream &operator<<(QDataStream &out, const CZT2FlagsData &source)
{
    out << source.mIsProblematicPassage
        << source.mTrainCompo1
        << source.mTrainCompo2
        << source.mTrainCompo3
        << source.mModbusTrainType
        << source.mZTDetectionsConfig
        << source.mUnusedFlag7
        << source.mUnusedFlag8
        << source.mUnusedFlag9
        << source.mUnusedFlag10;

    return out;
}

QDataStream &operator>>(QDataStream &in, CZT2FlagsData &dest)
{
        in >> dest.mIsProblematicPassage
           >> dest.mTrainCompo1
           >> dest.mTrainCompo2
           >> dest.mTrainCompo3
           >> dest.mModbusTrainType
           >> dest.mZTDetectionsConfig
           >> dest.mUnusedFlag7
           >> dest.mUnusedFlag8
           >> dest.mUnusedFlag9
           >> dest.mUnusedFlag10;;

        return in;
}


QDataStream& operator<<(QDataStream &out, const CZT2LogData &source)
{
    quint32 ThreadDataValid = 0;
    if(source.mZT2ThreadData != 0)
        ThreadDataValid = 1;

    out << source.mDateTime
        << source.mTimestamp
        << source.mCIZT2
        << source.mCDVARM_ZT2
		<< source.mCDVApproach_ZT2
        << ThreadDataValid;

    if(ThreadDataValid == 1)
    {
        out << source.mZT2ThreadData->mS1
            << source.mZT2ThreadData->mPPInt
            << source.mZT2ThreadData->mPPExt
            << source.mZT2ThreadData->mBogie
            << source.mZT2ThreadData->mRank
            << source.mZT2ThreadData->mS1Count;
    }

    return out;
}

QDataStream &operator>>(QDataStream &in, CZT2LogData &dest)
{
    quint32 ThreadValid;
    in >> dest.mDateTime
          >> dest.mTimestamp
          >> dest.mCIZT2
          >> dest.mCDVARM_ZT2
		  >> dest.mCDVApproach_ZT2
          >> ThreadValid;
    if(ThreadValid == 1)
    {
        dest.mZT2ThreadData = new CZT2ThreadData();
        in >> dest.mZT2ThreadData->mS1
           >> dest.mZT2ThreadData->mPPInt
           >> dest.mZT2ThreadData->mPPExt
           >> dest.mZT2ThreadData->mBogie
           >> dest.mZT2ThreadData->mRank
           >> dest.mZT2ThreadData->mS1Count;
    }

    return in;
}

CZTDetectionFunctionConfig::CZTDetectionFunctionConfig()
{
	for(int i = 0; i < DETECTION_FCT_MAX_FCT; i++)
	{
		mZTDetectionConfig[i].AnalysisActive = false;
		mZTDetectionConfig[i].TKActive = false;
	}
}

void CZTDetectionFunctionConfig::operator =(CZTDetectionFunctionConfig& DF)
{
	for(int i = 0; i < DETECTION_FCT_MAX_FCT; i++)
	{
		mZTDetectionConfig[i].AnalysisActive = DF.mZTDetectionConfig[i].AnalysisActive;
		mZTDetectionConfig[i].TKActive = DF.mZTDetectionConfig[i].TKActive;
	}
}

QDataStream &operator<<(QDataStream &out, const CZTDetectionFunctionConfig &source)
{
    for(int i = 0; i < DETECTION_FCT_MAX_FCT; i++)
    {
        out << source.mZTDetectionConfig[i].AnalysisActive
            << source.mZTDetectionConfig[i].TKActive;
    }
    return out;
}

QDataStream &operator>>(QDataStream &in, CZTDetectionFunctionConfig &dest)
{
    for(int i = 0; i < DETECTION_FCT_MAX_FCT; i++)
    {
        in >> dest.mZTDetectionConfig[i].AnalysisActive
           >> dest.mZTDetectionConfig[i].TKActive;
    }
    return in;
}
CSettingsData::CSettingsData()
{

}
CZTSettingsData::CZTSettingsData()
{
    mDetectionFunctionSettings = new CZTDetectionFunctionConfig();
	mAutoExportZT1CSV = false;
	mAutoExportZT2CSV = false;
}

QDataStream &operator<<(QDataStream &out, const CZTNetworkConfig &source)
{
    out << source.mZTID
        << source.mIPAdress;

    return out;
}

QDataStream &operator>>(QDataStream &in, CZTNetworkConfig &dest)
{
    in >> dest.mZTID
       >> dest.mIPAdress;

    return in;
}
