#include "clightdriver_wl.h"
#include "cmserialport.h"
#include "clight.h"
CLightDriver_WL::CLightDriver_WL(const QString &name, int32_t id)
	:CLightDriver(name,id)
{

}

CLightDriver_WL::~CLightDriver_WL()
{

}

int32_t CLightDriver_WL::setlight(int32_t iChid, int32_t ibright)
{
    CLight* pLight=(CLight*)FindChannel(iChid);
    Q_ASSERT(pLight);
    int isn=pLight->m_Variants.get_VData(Var_Base_SerialID_ID)->toInt();
    Q_ASSERT(isn>0&&isn<=4);
    byte bylight=ibright;
    char Command[32]={0};
    MakeCommand(Command, "SI", isn, bylight,true,3);
    char ch_response[RESPONSELENGTH]={0};
    SendCommand(Command,ch_response);
    checkResponse( iChid,ch_response);
    return CLightDriver::setlight(iChid,ibright);
}

int32_t CLightDriver_WL::freshlight(int32_t iChid)
{
    CLight* pLight=(CLight*)FindChannel(iChid);
    Q_ASSERT(pLight);
    int isn=pLight->m_Variants.get_VData(Var_Base_SerialID_ID)->toInt();
    Q_ASSERT(isn>0&&isn<=4);
    char Command[32]={0};
    MakeCommand(Command, "RI", isn,0,false,0);
    char ch_response[RESPONSELENGTH]={0};
    SendCommand(Command,ch_response);
    checkResponse( iChid,ch_response);
    QRegExp rx("\\{(.*)\\}");
    QString(ch_response).indexOf(rx);
    QString str= rx.cap(1);
    int ilight=str.toDouble();
    return CLightDriver::setlight(iChid,ilight);
}

int32_t CLightDriver_WL::setmultiplier(int32_t iChid, int32_t imultiplier)
{
    CLight* pLight=(CLight*)FindChannel(iChid);
    Q_ASSERT(pLight);
    int isn=pLight->m_Variants.get_VData(Var_Base_SerialID_ID)->toInt();
    Q_ASSERT(isn>0&&isn<=4);
    byte bymultiplier=imultiplier;
    char Command[32]={0};
    MakeCommand(Command, "SC", isn, bymultiplier,true,2);
    char ch_response[RESPONSELENGTH]={0};
    SendCommand(Command,ch_response);
    checkResponse( iChid,ch_response);
    CLightDriver::setmultiplier(  iChid,   imultiplier);

    return RTN_SUCCESS1;
}


int32_t CLightDriver_WL::init_driver()
{
    m_pSerialPort->open();
	return CLightDriver::init_driver();
}

int32_t CLightDriver_WL::exit_driver()
{

    int irtn= CLightDriver::exit_driver();
    m_pSerialPort->close();
    return irtn;
}

void CLightDriver_WL::SendCommand(const char *pCommand, char *pResponse)
{
    QMutexLocker locker(&m_CardMutex);
	int isize=strlen(pCommand);

	qDebug()<<pCommand;
	m_pSerialPort->send(pCommand,isize);
    int32_t irtn=m_pSerialPort->wait();
    m_pSerialPort->receive(pResponse,irtn);

//	MSleep(10);
}

void CLightDriver_WL::MakeCommand(char *pCommand, char* commid,
                                  byte byCh, byte byData, bool bwrite, int idatasize)
{
    if(bwrite)
    {
        char ch[32]={0};
        sprintf(ch,"@%%d%%s{%%0%dd}",idatasize);
        sprintf(pCommand,ch,byCh,commid,byData);
    }
    else
    {
        sprintf(pCommand,"@%d%s{}",byCh,commid);
    }
}

void CLightDriver_WL::checkResponse(int32_t iChid,char* pResponse)
{
	CLight* pLight=(CLight*)FindChannel(iChid);
	Q_ASSERT(pLight);
    qDebug()<<pResponse;
    if(!strcmp(pResponse,"Light:{ERR}"))
	{
		MQT_THROW_STR(QObject::trUtf8("Light %1: set Err")
                      .arg(pLight->m_sName));

	}
}

