#include "ModbusMaster.h"
#include <QTcpSocket>
#include "ProtocolDefs.h"

CModbusMaster::CModbusMaster(CModbusRepository *Repo):
    CModbusBackend(Repo)
{
   // connect(mModbusTCPSocketHandle,SIGNAL(readyRead()),this,SLOT(ModbusDataReady()));
    mModbusMode = MODBUS_MASTER_MODE;

    mModbusTCPSocketHandle = new QTcpSocket();
    connect(mModbusTCPSocketHandle,SIGNAL(readyRead()),this,SLOT(ModbusDataReady()));
    connect(mModbusTCPSocketHandle,SIGNAL(disconnected()),this,SLOT(SocketDisconnected()));
    connect(mModbusTCPSocketHandle,SIGNAL(connected()),this,SLOT(SocketConnected()));

}

CModbusMaster::~CModbusMaster()
{
    if(mModbusTCPSocketHandle->state() != QAbstractSocket::UnconnectedState)
    {
        mModbusTCPSocketHandle->disconnectFromHost();
        mModbusTCPSocketHandle->waitForDisconnected(1000);
    }
    delete mModbusTCPSocketHandle;
}

int CModbusMaster::ConnectToSlave(QString SlaveIP, int SlavePort)
{
    if(mModbusTCPSocketHandle->state() != QAbstractSocket::UnconnectedState)
    {
        return RET_ERROR;
    }

    mModbusTCPSocketHandle->connectToHost(SlaveIP,SlavePort);

    return RET_OK;
}

int CModbusMaster::DisconnectFromSlave()
{
    if(mModbusTCPSocketHandle->state() != QAbstractSocket::ConnectedState)
    {
        qDebug("Trying to disconnect a non connected socket");
        mModbusTCPSocketHandle->disconnectFromHost();
        return 0;
    }

    qDebug("Requesting Disconnection...");
    mModbusTCPSocketHandle->disconnectFromHost();
    return 1;
}

void CModbusMaster::SocketConnected()
{
    emit ModbusMasterConnected(this);
    qDebug("Master: Connection established with slave");
}

void CModbusMaster::SocketDisconnected()
{
    ModbusLinkDisconnected();
    emit ModbusMasterDisconnected(this);
    mModbusTCPSocketHandle->flush();
}

int CModbusMaster::ReadModbusRegisters()
{
    return SendReadHoldingRegistersRequest(MODBUS_ZT_DATA_BASE_REG,MODBUS_ZT_TABLE_DATA_SIZE);    //Read all registers from ZT (2000 - 2027)
}


void CModbusMaster::RegistersDatabaseUpdated(quint16 StartAddress, quint16 Length)
{
    emit ModbusMasterRepositoryUpdated();
//    qDebug("Database updated with ZT data...");
}

void CModbusMaster::ModbusResponseException(quint8 ExceptionCode, quint8 FctCode)
{
    qDebug("Modbus MASTER exception: code:%d  Fct:%d",ExceptionCode,FctCode);
}

int CModbusMaster::SendAN1ToZT()
{
    //return SendWriteHoldingRegistersRequest(CC_AN1_REGISTER_ADDRESS,1);
    return SendWriteSingleRegisterRequest(MODBUS_CC_AN_BASE_REG_ADD);
}

int CModbusMaster::SendAN2ToZT()
{
    //return SendWriteHoldingRegistersRequest(CC_AN2_REGISTER_ADDRESS,1);
    return SendWriteSingleRegisterRequest(MODBUS_CC_AN_BASE_REG_ADD);
}

int CModbusMaster::SendWatchdog()
{
    return SendWriteSingleRegisterRequest(MODBUS_CC_WATCHDOG_BASE_REG_ADD);
}
int CModbusMaster::SendDateTime()
{
    return SendWriteHoldingRegistersRequest(MODBUS_CC_CLK_UPDATE_BASE_REG_ADD,5);
}

int CModbusMaster::SendTrainData()
{
    return SendWriteHoldingRegistersRequest(MODBUS_CC_ZT1_TRAIN_TYPE_REG_ADD,8);
}

int CModbusMaster::SendZTInhibitionData()
{
    return SendWriteSingleRegisterRequest(MODBUS_CC_AN_BASE_REG_ADD);
}
