
/*******************************************************************************/
/*
    Description:
    Classe qui implémente les TC/TK avec la CC via le lien Modbus
    Gère l'émission des alarmes à la CC.
*/

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

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

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

#ifndef MODBUSTKTRANSPORT_H
#define MODBUSTKTRANSPORT_H

#include "TKTransportInterface.h"
#include "ModbusRepository.h"
#include <QTimer>
#include "ModbusCCDefs.h"
#include "ModbusSEIDefs.h"


class CModbusTKTransport : public CTKTransportInterface
{
    Q_OBJECT
public:
    enum eModbusTKSMStates
    {
        MODBUS_ZT1_TK_STANDBY_STATE,
        MODBUS_ZT1_TK_SHOW_STATE,
        MODBUS_ZT1_TK_WAIT_FOR_CLEAR_STATE,

        MODBUS_ZT2_TK_STANDBY_STATE,
        MODBUS_ZT2_TK_SHOW_STATE,
        MODBUS_ZT2_TK_WAIT_FOR_CLEAR_STATE
    };



    CModbusTKTransport(CModbusRepository *CCRepo, CModbusRepository *SEIRepo);
    ~CModbusTKTransport();


    int SendTKToCC(int ZT);
    int ClearTK(int ZT);
    unsigned int CancelAllTK();


    virtual bool IsZT2TKProcessing();
    virtual bool IsZT1TKProcessing();
    virtual bool IsTKProcessing();
    virtual int AddNewZT1Detection(CZTDetectionData Detection);
    virtual int AddNewZT2Detection(CZTDetectionData Detection);
    virtual unsigned int BeginTKEmission();
    virtual unsigned int SetInputStates(bool AN1State, bool ZT1CDVState, bool AN2State, bool ZT2CDVState);
    virtual unsigned int ClearTKForNewTrain();


    virtual unsigned int CancelMaintenanceCurrentTK();
    virtual unsigned int ExitMaintenance();
    virtual unsigned int EnterMaintenance();

private:
    CModbusRepository *mMCCodbusRepo,*mSEIModbusRepo;
    QList<CZTDetectionData> mZT1TKDataList,mZT2TKDataList;
    bool mZT1CDVState, mZT2CDVState;
    bool mMaintenanceMode, mForceZT1Clear, mForceZT2Clear;
    int mModbusTKZT1SMState,mModbusTKZT2SMState;
    QTimer *mZT1TKStateMachineTimer, *mZT2TKStateMachineTimer;
    bool mZT1CCClear, mZT2CCClear,mZT1SEIClear, mZT2SEIClear;
    bool mNewTrainIncoming;

    int RegOffset(qint16 Reg){return (int)Reg-MODBUS_ZT_DATA_BASE_REG;}

signals:
    void TKUpdated();

public slots:
    void ExecZT1SM();
    void ExecZT2SM();
    void ModbusCCUpdated();
    void ModbusSEIClearUpdate(bool ZT1Clear, bool ZT2Clear);
};

#endif // MODBUSTKTRANSPORT_H
