#include "LoraModuleIFMasterCtrlInterface.h"
#include "LoraModuleInterface.h"
#include "LoraModuleInterfaceData.h"

CLoraModuleIFMasterCtrlInterface::CLoraModuleIFMasterCtrlInterface(CLoraModuleInterface *ProgramHandle)
{
    mMyDeviceID = ID_LORA_INTERFACE_INTERFACE;
    mNetworkPort = 2182;
    mMasterCtrlIPAddress = "127.0.0.1";
    mNetworkCommSocket = 0;
    mDeviceAddress = 1;

    mProgramHandle = ProgramHandle;
}

int CLoraModuleIFMasterCtrlInterface::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 LORA_MODULE_IF_INTERFACE_ACK:
            {
                qDebug("Chalet Interface ACK received");
                break;
            }
            case LORA_MODULE_IF_INTERFACE_GENERAL_STATUS_RESPONSE:
            {
//                qDebug("Rx Lora Module IF status");
                CLoraModuleInterfaceStatus Status;
                QDataStream Strm(Data);
                Strm >> Status;
                mProgramHandle->NewLoraModuleStatusData(Status);
                break;
            }
            case LORA_MODULE_IF_INTERFACE_GENERAL_STATUS_REQUEST:
            default:
            {
                qDebug("CLoraModuleIFMasterCtrlInterface: invalid command received");
                break;
            }

        }
    }


    return RET_OK;

}

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

    }
    else
        return RET_ERROR;

    return RET_OK;
}
