/*******************************************************************************
*                                                                              *
* 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:
### 20121220 JFM
    Verision d'origine.
    
### YYYYMMDD Description du besoin ou du bug
    Description du changement.
 */

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

#include "CoteVertu.h"
#include <QtGlobal>
#include "SwitchCDV.h"

CCoteVertuStation::CCoteVertuStation()
{
    //All the inherited variables from base class must be initialized here
    //according to the physical implementation in the station.
    mNbExtInputModules430 = COTEVERTU_NB_EXTERNAL_INPUT_430_MODULES;
    mNbExtOutputModules440 = COTEVERTU_NB_EXTERNAL_OUTPUT_440_MODULES;
    mNbExtMixedModules470 = COTEVERTU_NB_EXTERNAL_MIXED_470_MODULES;
    mStationPhysicalKey = COTEVERTU_STATION_KEY;
    //Initialize mask structures
    //


    mStationInputMasks.InputZT1S1Mask = COTEVERTU_ZT1_S1_MASK;
    mStationInputMasks.InputZT1S2Mask = COTEVERTU_ZT1_S2_MASK;
    mStationInputMasks.InputZT1PIMask = COTEVERTU_ZT1_PI_MASK;
    mStationInputMasks.InputZT1PEMask = COTEVERTU_ZT1_PE_MASK;
    mStationInputMasks.InputZT1FNMask = COTEVERTU_ZT1_FN_MASK;
    mStationInputMasks.InputZT2S1Mask = COTEVERTU_ZT2_S1_MASK;
    mStationInputMasks.InputZT2PIMask = COTEVERTU_ZT2_PI_MASK;
    mStationInputMasks.InputZT2PEMask = COTEVERTU_ZT2_PE_MASK;

    //External (ethernet) input module masks
    mStationInputMasks.InputZT1ITIMask = COTEVERTU_IN_ZT1_10_12_ITI_MASK;
    mStationInputMasks.InputZT1_AltITIMask = COTEVERTU_IN_ZT1_10_22_ITI_MASK;
    mStationInputMasks.InputZT2ITIMask = COTEVERTU_IN_ZT2_ITI_MASK;
    mStationInputMasks.InputZT1ANMask = COTEVERTU_IN_ZT1_AN_MASK;
    mStationInputMasks.InputZT2ANMask = COTEVERTU_IN_ZT2_AN_MASK;
    mStationInputMasks.InputZT1ARFMask = COTEVERTU_IN_ZT1_ARF_MASK;
    mStationInputMasks.InputZT2ARFMask = COTEVERTU_IN_ZT2_ARF_MASK;
    mStationInputMasks.InputStationIDMask = COTEVERTU_IN_STATION_ID_MASK;

    //Logic input masks
    mStationInputMasks.InputCDVZT1ApproachMask = COTEVERTU_IN_CDV_2S177_179_MASK;
    mStationInputMasks.InputCDVZT1Mask = COTEVERTU_IN_CDV_10B_MASK;
	mStationInputMasks.InputCDVZT2ApproachMask = COTEVERTU_IN_CDV_12B_MASK;
    mStationInputMasks.InputCDVZT2Mask = COTEVERTU_IN_CDV_13A_MASK;

    //Output masks
    mStationOutputMasks.OutputVP1Mask = COTEVERTU_OUT_VP1_MASK;
    mStationOutputMasks.OutputVP2Mask = COTEVERTU_OUT_VP2_MASK;
    mStationOutputMasks.OutputVP3Mask = COTEVERTU_OUT_VP3_MASK;
    mStationOutputMasks.OutputVP4Mask = COTEVERTU_OUT_VP4_MASK;
    mStationOutputMasks.OutputVP5Mask = COTEVERTU_OUT_VP5_MASK;
    mStationOutputMasks.OutputVP6Mask = COTEVERTU_OUT_VP6_MASK;
    mStationOutputMasks.OutputVF1Mask = COTEVERTU_OUT_VF1_MASK;
    mStationOutputMasks.OutputVF2Mask = COTEVERTU_OUT_VF2_MASK;
    mStationOutputMasks.OutputVF3Mask = COTEVERTU_OUT_VF3_MASK;
    mStationOutputMasks.OutputVF4Mask = COTEVERTU_OUT_VF4_MASK;
    mStationOutputMasks.OutputVF5Mask = COTEVERTU_OUT_VF5_MASK;
    mStationOutputMasks.OutputWP1Mask = COTEVERTU_OUT_WP1_MASK;
    mStationOutputMasks.OutputWP2Mask = COTEVERTU_OUT_WP2_MASK;
    mStationOutputMasks.OutputWP3Mask = COTEVERTU_OUT_WP3_MASK;
    mStationOutputMasks.OutputWP4Mask = COTEVERTU_OUT_WP4_MASK;
    mStationOutputMasks.OutputWP5Mask = COTEVERTU_OUT_WP5_MASK;
    mStationOutputMasks.OutputWP6Mask = COTEVERTU_OUT_WP6_MASK;
    mStationOutputMasks.OutputDPEMask = COTEVERTU_OUT_DPE_MASK;
    mStationOutputMasks.OutputDPIMask = COTEVERTU_OUT_DPI_MASK;
    mStationOutputMasks.OutputV00Mask = COTEVERTU_OUT_V00_MASK;
    mStationOutputMasks.OutputDPGMask = COTEVERTU_OUT_DPG_MASK;
    mStationOutputMasks.OutputDFRMask = COTEVERTU_OUT_DFR_MASK;
    mStationOutputMasks.OutputRFMask = COTEVERTU_OUT_RF_MASK;
    mStationOutputMasks.OutputRF2Mask = COTEVERTU_OUT_RF2_MASK;
    mStationOutputMasks.OutputVELMask = COTEVERTU_OUT_VEL_MASK;
    mStationOutputMasks.OutputVEL2Mask = COTEVERTU_OUT_VEL2_MASK;
    mStationOutputMasks.OutputDPI2Mask = COTEVERTU_OUT_DPI2_MASK;
    mStationOutputMasks.OutputDPE2Mask = COTEVERTU_OUT_DPE2_MASK;
    mStationOutputMasks.OutputV002Mask = COTEVERTU_OUT_V002_MASK;
    mStationOutputMasks.OutputPEQ1Mask = COTEVERTU_OUT_PEQ1_MASK;
    mStationOutputMasks.OutputPEQ2Mask = COTEVERTU_OUT_PEQ2_MASK;
    mStationOutputMasks.OutputWatchdogMask = COTEVERTU_OUT_WATCHDOG_MASK;

	mAnalogAcqChannels.SDFAcquisitionChannel = COTEVERTU_SDF_ACQUISITION_CHANNEL;

    mStationHasZT2 = true;
    mCurZT1Itinerary = COTEVERTU_ZT1_NO_ITINERARY;
    mTextualStationName = "Côte Vertu";
	mTextualShortName = "CVRT";
}

unsigned int CCoteVertuStation::UpdateStationMasks(unsigned int ExtInputData)
{
    mStationInputMasks.InputZT1_StdITIMask = COTEVERTU_IN_ZT1_10_12_ITI_MASK;
    if((COTEVERTU_IN_ZT1_10_12_ITI_MASK & ExtInputData) != 0)
    {
        if(mCurZT1Itinerary != COTEVERTU_ZT1_10_12_ITINERARY)
        {	//Assign masks relative to 10/12 itinerary
            mStationInputMasks.InputZT1ITIMask = COTEVERTU_IN_ZT1_10_12_ITI_MASK;
            mStationInputMasks.InputZT1ANMask = COTEVERTU_IN_ZT1_AN_MASK;       //We use Modbus in C-V.  AN input is obsolete but we still assign it
            mCurZT1Itinerary = COTEVERTU_ZT1_10_12_ITINERARY;
            mZT1AlarmAutoAcquireCDV = m10_12AlarmAutoAcquireCDV_2;  //Not used but it's better to assign it.
            mZT1SubsequentCDV = m10_12SubsequentCDV;
            qDebug("Itinéraire 10/12 configuré");
        }

    }
    else if((COTEVERTU_IN_ZT1_10_22_ITI_MASK & ExtInputData) != 0)
    {
        if(mCurZT1Itinerary != COTEVERTU_ZT1_10_22_ITINERARY)
        {	//Assign masks relative to 10/22 itinerary
            mStationInputMasks.InputZT1ITIMask = COTEVERTU_IN_ZT1_10_22_ITI_MASK;
            mStationInputMasks.InputZT1ANMask = COTEVERTU_IN_ZT1_AN_MASK;  //We use Modbus in C-V.  AN input is obsolete but we still assign it
            mCurZT1Itinerary = COTEVERTU_ZT1_10_22_ITINERARY;
            mZT1AlarmAutoAcquireCDV = m10_22AlarmAutoAcquireCDV_2; //Not used but it's better to assign it.
            mZT1SubsequentCDV = m10_22SubsequentCDV;
            qDebug("Itinéraire 10/22 configuré");
        }
    }
    else
    {
        mCurZT1Itinerary = COTEVERTU_ZT1_NO_ITINERARY;
    }

    return RET_OK;
}

int CCoteVertuStation::GetCurrentITI()
{
    if(mCurZT1Itinerary == COTEVERTU_ZT1_NO_ITINERARY || mCurZT1Itinerary == COTEVERTU_ZT1_10_12_ITINERARY)
    {
        return ZT_PRIMARY_ITI;
    }
    else if(mCurZT1Itinerary == COTEVERTU_ZT1_10_22_ITINERARY)
    {
        return ZT_SECONDARY_ITI;
    }
    else
    {
        qDebug("Invalid mCurZT1Itinerary in CCoteVertuStation.  Weird stuff!");
        return ZT_PRIMARY_ITI;
    }
}

bool CCoteVertuStation::IsZT1AlarmAutoAcquireCDVOccupied()
{
    if(GetCurrentITI() == ZT_PRIMARY_ITI)   // itinéraire 10/12
    {
        if(m10_12AlarmAutoAcquireCDV_1->IsOccupied())
        {
            return true;
        }
        else if(m10_12AlarmAutoAcquireCDV_2->IsOccupied())
        {
            return true;
        }

        return false;
    }
    else        // Itinéraire 10/22
    {
        if(m10_22AlarmAutoAcquireCDV_1->IsOccupied())
        {
            return true;
        }
        else if(m10_22AlarmAutoAcquireCDV_2->IsOccupied())
        {
            return true;
        }

        return false;
    }
}

unsigned int CCoteVertuStation::BuildStationCDVList()
{
    CCDV *NewCDV;
    mCDVList.empty();


	NewCDV = new CCDV(COTEVERTU_IN_ZT2_ITI_MASK,COTEVERTU_IN_CDV_14B_MASK,CDV_NORMAL_TYPE,"14B",1,0);
    mCDVList.append(NewCDV);


	NewCDV = new CCDV(COTEVERTU_IN_ZT2_ITI_MASK,COTEVERTU_IN_CDV_14A_MASK,CDV_NORMAL_TYPE,"14A",1,1);
    mCDVList.append(NewCDV);

	NewCDV = new CCDV(COTEVERTU_IN_ZT2_ITI_MASK,COTEVERTU_IN_CDV_13B_MASK,CDV_NORMAL_TYPE,"13B",1,2);
	mZT2AlarmAutoAcquireCDV = NewCDV;

    mCDVList.append(NewCDV);


	NewCDV = new CCDV(COTEVERTU_IN_ZT2_ITI_MASK,COTEVERTU_IN_CDV_13A_MASK,CDV_NORMAL_TYPE,"13A (ZT2)",1,3);
	mZT2CDV = NewCDV;
   // mZT2ApproachCDV = NewCDV;   //JFM 2021-08-25 Suite à la MES du SEI, le CDV 13A s'occupe plus tard et on a des pannes équipement.  Déplacé le CDV d'approche sur le 12B
	mCDVList.append(NewCDV);

    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_12_ITI_MASK,COTEVERTU_IN_CDV_12B_MASK,CDV_NORMAL_TYPE,"12B",1,4);
    mCDVList.append(NewCDV);
    mZT2ApproachCDV = NewCDV;   //JFM 2021-08-25 Suite à la MES du SEI, le CDV 13A s'occupe plus tard et on a des pannes équipement.  Déplacé le CDV d'approche sur le 12B
    NewCDV->SetCDVDeck(true);


    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_12_ITI_MASK,COTEVERTU_IN_CDV_12A_MASK,CDV_NORMAL_TYPE,"12A",1,5);
	mZT1AlarmAutoAcquireCDV = NewCDV;
    mCDVList.append(NewCDV);
    NewCDV->SetCDVDeck(true);
    m10_12AlarmAutoAcquireCDV_2 = NewCDV;

    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_12_ITI_MASK,COTEVERTU_IN_CDV_11C_MASK,CDV_NORMAL_TYPE,"11C",1,6);
    mCDVList.append(NewCDV);
    m10_12AlarmAutoAcquireCDV_1 = NewCDV;


    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_12_ITI_MASK,COTEVERTU_IN_CDV_11B_MASK,CDV_NORMAL_TYPE,"11B",1,7);
    mCDVList.append(NewCDV);


//    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_12_ITI_MASK,COTEVERTU_IN_CDV_11A_MASK,CDV_NORMAL_TYPE,"11A",1,8);
//	mZT1ApproachCDV = NewCDV;
//    mZT1SubsequentCDV = NewCDV;
//    mCDVList.append(NewCDV);

    CSwitchCDV *NewSwitchCDV = new CSwitchCDV(COTEVERTU_IN_ZT1_10_12_ITI_MASK,COTEVERTU_IN_ZT1_10_22_ITI_MASK,COTEVERTU_IN_CDV_11A_MASK,CDV_SWITCH_TYPE,"11A ",1,8);
    mCDVList.append((CCDV*)NewSwitchCDV);
    mZT1SubsequentCDV = (CCDV*)NewSwitchCDV;
    m10_12SubsequentCDV = NewSwitchCDV;
    m10_22SubsequentCDV = NewCDV;


    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_12_ITI_MASK  | COTEVERTU_IN_ZT1_10_22_ITI_MASK,COTEVERTU_IN_CDV_10B_MASK,CDV_NORMAL_TYPE,"10B (ZT1)",1,9);
    mCDVList.append(NewCDV);
    mZT1CDV = NewCDV;

    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_12_ITI_MASK | COTEVERTU_IN_ZT1_10_22_ITI_MASK,COTEVERTU_IN_CDV_2S177_179_MASK,CDV_NORMAL_TYPE,"10A",1,10);
    mCDVList.append(NewCDV);
    mZT1ApproachCDV = NewCDV;

    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_22_ITI_MASK,COTEVERTU_IN_CDV_22B_MASK,CDV_NORMAL_TYPE,"22B",2,4);
    mCDVList.append(NewCDV);


    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_22_ITI_MASK,COTEVERTU_IN_CDV_22A_MASK,CDV_NORMAL_TYPE,"22A",2,5);
    mCDVList.append(NewCDV);
    m10_22AlarmAutoAcquireCDV_2 = NewCDV; //Memorize it.


    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_22_ITI_MASK,COTEVERTU_IN_CDV_21C_MASK,CDV_NORMAL_TYPE,"21C",2,6);
    mCDVList.append(NewCDV);
    m10_22AlarmAutoAcquireCDV_1 = NewCDV; //Memorize it.

    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_22_ITI_MASK,COTEVERTU_IN_CDV_21B_MASK,CDV_NORMAL_TYPE,"21B",2,7);
    mCDVList.append(NewCDV);

//    NewCDV = new CCDV(COTEVERTU_IN_ZT1_10_22_ITI_MASK,COTEVERTU_IN_CDV_21A_MASK,CDV_NORMAL_TYPE,"21A",2,8);
//    mCDVList.append(NewCDV);
//    m10_22SubsequentCDV = NewCDV;




    return RET_OK;
}

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