/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
	Classe instantiable dérivée de la classe de base CStation.  Les paramètres
	physiques de la station sont définis et initialisés ici.  Ils sont ensuite
	accédés de manière générique à travers la classe CStation.
	Les paramètres dépend du câblage de l'armoire ZT et de la configuration
	particulière des CDVs de la station.
    
*/

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

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

#include "DuCollege.h"
#include <QtGlobal>
#ifndef WINDOWS_OS
#include "ExternalIOMgr.h"
#include "Seaio430driver.h"
#include "SwitchCDV.h"
#endif

//rydy
CDuCollegeStation::CDuCollegeStation()
{
    //All the inherited variables from base class must be initialized here
    //according to the physical implementation in the station.
    mNbExtInputModules430 = DUCOLLEGE_NB_EXTERNAL_INPUT_430_MODULES;
    mNbExtOutputModules440 = DUCOLLEGE_NB_EXTERNAL_OUTPUT_440_MODULES;
    mNbExtMixedModules470 = DUCOLLEGE_NB_EXTERNAL_MIXED_470_MODULES;
    mStationPhysicalKey = DUCOLLEGE_STATION_KEY;

	//Initialize mask structures
	//

    mStationInputMasks.InputZT1S1Mask = DUCOLLEGE_ZT1_S1_MASK;
    mStationInputMasks.InputZT1S2Mask = DUCOLLEGE_ZT1_S2_MASK;
    mStationInputMasks.InputZT1PIMask = DUCOLLEGE_ZT1_PI_MASK;
    mStationInputMasks.InputZT1PEMask = DUCOLLEGE_ZT1_PE_MASK;
    mStationInputMasks.InputZT1FNMask = DUCOLLEGE_ZT1_FN_MASK;
    mStationInputMasks.InputZT2S1Mask = DUCOLLEGE_ZT2_S1_MASK;
    mStationInputMasks.InputZT2PIMask = DUCOLLEGE_ZT2_PI_MASK;
    mStationInputMasks.InputZT2PEMask = DUCOLLEGE_ZT2_PE_MASK;

	//External (ethernet) input module masks
    mStationInputMasks.InputZT1ITIMask = DUCOLLEGE_IN_ZT1_V2_ITI_MASK;
    mStationInputMasks.InputZT1_AltITIMask = DUCOLLEGE_IN_ZT1_12_14_ITI_MASK;
    mStationInputMasks.InputZT1ANMask = DUCOLLEGE_IN_ZT1_AN_MASK;
    mStationInputMasks.InputZT1ARFMask = DUCOLLEGE_IN_ZT1_ARF_MASK;
    mStationInputMasks.InputZT2ITIMask = 0;
    mStationInputMasks.InputZT2ANMask = DUCOLLEGE_IN_ZT2_AN_MASK;
    mStationInputMasks.InputZT2ARFMask = DUCOLLEGE_IN_ZT2_ARF_MASK;
    mStationInputMasks.InputStationIDMask = DUCOLLEGE_IN_STATION_ID_MASK;

	//Logic input masks
    mStationInputMasks.InputCDVZT1ApproachMask = DUCOLLEGE_IN_CDV_12B_MASK;
    mStationInputMasks.InputCDVZT1Mask = DUCOLLEGE_IN_CDV_13A_MASK;
    mStationInputMasks.InputCDVZT2ApproachMask = 0;
    mStationInputMasks.InputCDVZT2Mask = 0;

	//Output masks
    mStationOutputMasks.OutputVP1Mask = DUCOLLEGE_OUT_VP1_MASK;
    mStationOutputMasks.OutputVP2Mask = DUCOLLEGE_OUT_VP2_MASK;
    mStationOutputMasks.OutputVP3Mask = DUCOLLEGE_OUT_VP3_MASK;
    mStationOutputMasks.OutputVP4Mask = DUCOLLEGE_OUT_VP4_MASK;
    mStationOutputMasks.OutputVP5Mask = DUCOLLEGE_OUT_VP5_MASK;
    mStationOutputMasks.OutputVP6Mask = DUCOLLEGE_OUT_VP6_MASK;
    mStationOutputMasks.OutputVF1Mask = DUCOLLEGE_OUT_VF1_MASK;
    mStationOutputMasks.OutputVF2Mask = DUCOLLEGE_OUT_VF2_MASK;
    mStationOutputMasks.OutputVF3Mask = DUCOLLEGE_OUT_VF3_MASK;
    mStationOutputMasks.OutputVF4Mask = DUCOLLEGE_OUT_VF4_MASK;
    mStationOutputMasks.OutputVF5Mask = DUCOLLEGE_OUT_VF5_MASK;
    mStationOutputMasks.OutputWP1Mask = DUCOLLEGE_OUT_WP1_MASK;
    mStationOutputMasks.OutputWP2Mask = DUCOLLEGE_OUT_WP2_MASK;
    mStationOutputMasks.OutputWP3Mask = DUCOLLEGE_OUT_WP3_MASK;
    mStationOutputMasks.OutputWP4Mask = DUCOLLEGE_OUT_WP4_MASK;
    mStationOutputMasks.OutputWP5Mask = DUCOLLEGE_OUT_WP5_MASK;
    mStationOutputMasks.OutputWP6Mask = DUCOLLEGE_OUT_WP6_MASK;
    mStationOutputMasks.OutputDPEMask = DUCOLLEGE_OUT_DPE_MASK;
    mStationOutputMasks.OutputDPIMask = DUCOLLEGE_OUT_DPI_MASK;
    mStationOutputMasks.OutputV00Mask = DUCOLLEGE_OUT_V00_MASK;
    mStationOutputMasks.OutputDPGMask = DUCOLLEGE_OUT_DPG_MASK;
    mStationOutputMasks.OutputDFRMask = DUCOLLEGE_OUT_DFR_MASK;
    mStationOutputMasks.OutputRFMask = DUCOLLEGE_OUT_RF_MASK;
    mStationOutputMasks.OutputRF2Mask = DUCOLLEGE_OUT_RF2_MASK;
    mStationOutputMasks.OutputVELMask = DUCOLLEGE_OUT_VEL_MASK;
    mStationOutputMasks.OutputVEL2Mask = DUCOLLEGE_OUT_VEL2_MASK;
    mStationOutputMasks.OutputDPI2Mask = DUCOLLEGE_OUT_DPI2_MASK;
    mStationOutputMasks.OutputDPE2Mask = DUCOLLEGE_OUT_DPE2_MASK;
    mStationOutputMasks.OutputV002Mask = DUCOLLEGE_OUT_V002_MASK;
    mStationOutputMasks.OutputPEQ1Mask = DUCOLLEGE_OUT_PEQ1_MASK;
    mStationOutputMasks.OutputPEQ2Mask = DUCOLLEGE_OUT_PEQ2_MASK;
    mStationOutputMasks.OutputWatchdogMask = DUCOLLEGE_OUT_WATCHDOG_MASK;

    mAnalogAcqChannels.SDFAcquisitionChannel = DUCOLLEGE_SDF_ACQUISITION_CHANNEL;

    mStationHasZT2 = false;
    mTextualStationName = "Du Collège";
    mTextualShortName = "COLG";

    mCurZT1Itinerary = DUCOLLEGE_ZT1_NO_ITINERARY;
}

unsigned int CDuCollegeStation::BuildStationCDVList()
{
	CCDV *NewCDV;
	mCDVList.empty();
    unsigned int SP2_V2_12_14_ItiMask = DUCOLLEGE_IN_ZT1_12_14_ITI_MASK | DUCOLLEGE_IN_ZT1_V2_ITI_MASK | DUCOLLEGE_IN_ZT1_SP2_ITI_MASK;

    NewCDV = new CCDV(SP2_V2_12_14_ItiMask,DUCOLLEGE_IN_CDV_14C_MASK,CDV_NORMAL_TYPE,"14C",1,1);
    mCDVList.append(NewCDV);

    NewCDV = new CCDV(SP2_V2_12_14_ItiMask,DUCOLLEGE_IN_CDV_14B_MASK,CDV_NORMAL_TYPE,"14B",1,2);
    mCDVList.append(NewCDV);

    NewCDV = new CCDV(SP2_V2_12_14_ItiMask,DUCOLLEGE_IN_CDV_14A_MASK,CDV_NORMAL_TYPE,"14A",1,3);
    mZT1AlarmAutoAcquireCDV = NewCDV;
    mCDVList.append(NewCDV);

    NewCDV = new CCDV(SP2_V2_12_14_ItiMask,DUCOLLEGE_IN_CDV_13B_MASK,CDV_NORMAL_TYPE,"13B",1,4);
    mZT1SubsequentCDV = NewCDV;
    mCDVList.append(NewCDV);

//    CSwitchCDV *NewSwitchCDV = new CSwitchCDV(DUCOLLEGE_IN_ZT1_12_14_ITI_MASK,DUCOLLEGE_IN_ZT1_V2_ITI_MASK | DUCOLLEGE_IN_ZT1_SP2_ITI_MASK,DUCOLLEGE_IN_CDV_13B_MASK | DUCOLLEGE_IN_CDV_23B_MASK,CDV_SWITCH_TYPE,"13B",1,4);
//    mCDVList.append((CCDV*)NewSwitchCDV);
//    mZT1SubsequentCDV = (CCDV*)NewSwitchCDV;

    NewCDV = new CCDV(SP2_V2_12_14_ItiMask,DUCOLLEGE_IN_CDV_13A_MASK,CDV_NORMAL_TYPE,"13A ZT",1,5);
    mZT1CDV = NewCDV;
	mCDVList.append(NewCDV);

    NewCDV = new CCDV(SP2_V2_12_14_ItiMask,DUCOLLEGE_IN_CDV_12B_MASK,CDV_NORMAL_TYPE,"12B",1,6);
    mCDVList.append(NewCDV);
    NewCDV->SetCDVDeck(true);

    NewCDV = new CCDV(SP2_V2_12_14_ItiMask,DUCOLLEGE_IN_CDV_12A_MASK,CDV_NORMAL_TYPE,"12A",1,7);
    NewCDV->SetCDVDeck(true);
    mZT1ApproachCDV = NewCDV;
    mCDVList.append(NewCDV);

    NewCDV = new CCDV(0,DUCOLLEGE_IN_CDV_11B_MASK,CDV_NORMAL_TYPE,"11B",1,8);
    mCDVList.append(NewCDV);

    NewCDV = new CCDV(DUCOLLEGE_IN_ZT1_V2_ITI_MASK | DUCOLLEGE_IN_ZT1_SP2_ITI_MASK,DUCOLLEGE_IN_CDV_23B_MASK,CDV_NORMAL_TYPE,"23B",2,4);
    mCDVList.append(NewCDV);

    NewCDV = new CCDV(DUCOLLEGE_IN_ZT1_V2_ITI_MASK | DUCOLLEGE_IN_ZT1_SP2_ITI_MASK,DUCOLLEGE_IN_CDV_23A_MASK,CDV_NORMAL_TYPE,"23A",2,5);
    mCDVList.append(NewCDV);

    NewCDV = new CCDV(DUCOLLEGE_IN_ZT1_V2_ITI_MASK | DUCOLLEGE_IN_ZT1_SP2_ITI_MASK,DUCOLLEGE_IN_CDV_22B_MASK,CDV_NORMAL_TYPE,"22B",2,6);
    mCDVList.append(NewCDV);

    NewCDV = new CCDV(DUCOLLEGE_IN_ZT1_V2_ITI_MASK | DUCOLLEGE_IN_ZT1_SP2_ITI_MASK,DUCOLLEGE_IN_CDV_22A_MASK,CDV_NORMAL_TYPE,"22A",2,7);
    mCDVList.append(NewCDV);

    return RET_OK;
}

unsigned int CDuCollegeStation::UpdateStationMasks(unsigned int ExtInputData)
{
    mStationInputMasks.InputZT1_StdITIMask = DUCOLLEGE_IN_ZT1_V2_ITI_MASK | DUCOLLEGE_IN_ZT1_SP2_ITI_MASK;
    if(((DUCOLLEGE_IN_ZT1_SP2_ITI_MASK & ExtInputData)) != 0 || ((DUCOLLEGE_IN_ZT1_V2_ITI_MASK & ExtInputData) != 0))
    {
        if(mCurZT1Itinerary != DUCOLLEGE_SP2_V2_ITINERARY)
        {	//Assign masks relative to 10/12 itinerary
            mStationInputMasks.InputZT1ITIMask = DUCOLLEGE_IN_ZT1_V2_ITI_MASK | DUCOLLEGE_IN_ZT1_SP2_ITI_MASK;;
            mCurZT1Itinerary = DUCOLLEGE_SP2_V2_ITINERARY;
            qDebug("Itinéraire SP2/V2 configuré");
        }

    }
//    else if((DUCOLLEGE_IN_ZT1_12_14_ITI_MASK & ExtInputData) != 0)
//    {
//        if(mCurZT1Itinerary != DUCOLLEGE_12_14_ITINERARY)
//        {	//Assign masks relative to 12/14 itinerary
//            mStationInputMasks.InputZT1ITIMask = DUCOLLEGE_IN_ZT1_12_14_ITI_MASK;
//            mCurZT1Itinerary = DUCOLLEGE_12_14_ITINERARY;
//              qDebug("Itinéraire 12/14 configuré");
//        }
//    }
    else
    {
        mCurZT1Itinerary = DUCOLLEGE_ZT1_NO_ITINERARY;
    }

    return RET_OK;
}

int CDuCollegeStation::GetCurrentITI()
{
    //Seulement l'itinéraire primaire est renvoyé à la CC à DuCollège.
    return ZT_PRIMARY_ITI;


//    if(mCurZT1Itinerary == DUCOLLEGE_ZT1_NO_ITINERARY || mCurZT1Itinerary == DUCOLLEGE_SP2_V2_ITINERARY)
//    {
//        return ZT_PRIMARY_ITI;
//    }
//    else if(mCurZT1Itinerary == DUCOLLEGE_12_14_ITINERARY)
//    {
//        return ZT_SECONDARY_ITI;
//    }
//    else
//    {
//        qDebug("Invalid mCurZT1Itinerary in CDuCollegeStation.  Weird stuff!");
//        return ZT_PRIMARY_ITI;
//    }
}


GenericInputMasks_t *CDuCollegeStation::GetInputMasks(void)
{
    return &mStationInputMasks;
}

