#include "IspindelInterface.h"
#include "IspindelData.h"
#include "Ispindel.h"
#include "ProtocolDefs.h"

CIspindelInterface::CIspindelInterface(CIspindel *ProgramHandle)
{
    mMyDeviceID = ID_ISPINDEL_INTERFACE;
    mNetworkPort = 2182;
    mMasterCtrlIPAddress = "127.0.0.1";
    mNetworkCommSocket = 0;
    mDeviceAddress = 1;

    mProgramHandle = ProgramHandle;
}

int CIspindelInterface::DeviceConnectedToMaster(bool Connected)
{
    if(Connected)
    {
        qDebug("Ispindel Interface connected to Master.");
        mProgramHandle->ConnectedToMaster(Connected);

    }
    else
        return RET_ERROR;

    return RET_OK;
}

int CIspindelInterface::DeviceFrameReceived(int TargetDeviceID, int TargetDeviceAddress, int SenderID, int SenderAddress, int MessageID, int DataSize, QByteArray Data)
{
    Q_UNUSED(DataSize)
    Q_UNUSED(SenderID)
    Q_UNUSED(SenderAddress)

    if(TargetDeviceID == mMyDeviceID && (TargetDeviceAddress == BROADCAST_VALUE || TargetDeviceAddress == mDeviceAddress))
    {
        switch(MessageID)
        {
            case ISPINDEL_GET_FULL_DATA_BUFFER_RESPONSE:
            {
                mProgramHandle->IspindelFullBufferReceived(&Data);
                break;
            }
            case ISPINDLE_LATEST_DATA_RESPONSE:
            {
                mProgramHandle->IspindelLastFrameReceived(Data);
                break;
            }
            case ISPINDEL_DELETE_SAMPLE_RESPONSE:
            {
                mProgramHandle->DeleteSampleResponseReceived(Data);
                break;
            }
            case ISPINDEL_GET_FULL_DATA_BUFFER_REQUEST:
            case ISPINDLE_LATEST_DATA_REQUEST:
            case ISPINDEL_DELETE_SAMPLE_REQUEST:
            default:
            {
                qDebug("Ispindel: Invalid Ethernet Msg received from MasterCtrl: %d",MessageID);
                break;
            }
        }

    }

    return RET_OK;

}
