#include "vst_wait_coat.h"
#include "logic.h"
#include "vsm_thread.h"
#include "vst_work_coat.h"
VST_Wait_Coat::VST_Wait_Coat(int iID, const QString &sName,
                             VSM_Thread *pParent)
    :VST_Wait_Base(  iID,  sName, pParent)
{

}
//仅有涂敷的过程 
void VST_Wait_Coat::do_Msg(const V_Msg &msg)
{
    switch(msg.m_iID)
    {
    case VarMsg_Auto_StartCoat:
    {
		//开始涂敷，由Transfer转发了消息
        CProcess_Coat* pCoat=GET_PROCESS(PROCESS_COAT,
                                         SM_COAT,
                                         CProcess_Coat);

        QString strState=QObject::trUtf8("Auto Coat State");
        QString strMsg=QObject::trUtf8("Auto Coat Sig");
        COATCLEANSTEPVECTOR step= pCoat->m_vCoatCleanStep;
        int iSize=step.size();
        int iStartStep=0;
        int iEndStep=iSize-1;
        if(!m_pNextState)
        {
            m_pNextState=new VST_Work_Coat(VarState_Work_CoatClean,
                                           strState,
                                           m_pParent,this,0);
            ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_CoatClean_BeforeAll, strMsg,
                                 QVariantList()<<NORMAL_ACTION);
            for(int i=iStartStep;i<=iEndStep;i++)
            {
				//涂敷步骤依次添加
                ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_CoatClean_Step, strMsg,
                                     QVariantList()<<NORMAL_ACTION<<step[i].iType
                                     <<step[i].dTime<<step[i].iRPM<<i<<step[i].dSpayNum<<step[i].dSpaySpd
									 <<step[i].dCenterSpeed<<step[i].dEageSpeed<<step[i].dCenterWaitTime
									 <<step[i].dCoatPreSpyNum<<step[i].iCoatPreSpyWaitTime
									 <<step[i].dBumpNum<<step[i].dPreSpayNum);
            }
            ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_CoatClean_AfterAll, strMsg,
                                 QVariantList()<<NORMAL_ACTION);
            ((VST_Work_Coat*)m_pNextState)->LoadMsgProcess();
        }
		m_gSystem.m_totalCoatTime.ClearTime();
		m_gSystem.m_totalCoatTime.StartTime();
        m_pParent->SetState(m_pNextState);
        QString strFinish=QObject::trUtf8("Auto Coat Finish");
        pushback_Msg(VarMsg_CoatClean_Finished,strFinish);
		g_SecsManager.SendEvent(EVENT_CoatingStarted);
        break;
    }
    case VarMsg_CoatClean_Finished:
    {
		//涂敷结束后发送VarMsg_Transfer_CheckState消息
        CProcess_Trans* pTrans=GET_PROCESS(PROCESS_TRANS,
                                           SM_TRANS,CProcess_Trans);
        QMutexLocker l(&pTrans->m_slotMutex);
        if(pTrans->m_pSlotCoat)
        {
            pTrans->m_pSlotCoat->WPstatus=WPCOATFINISH;
            pTrans->m_pSlotCoat->bCoatFinished = true;
        }
        l.unlock();
        /*VSM_Thread* pSM=(VSM_Thread*)m_gSystem.FindSM(SM_TRANS);
        pSM->pushBack_Msg_toState(pSM->m_pState_Wait,
                                  V_Msg(VarMsg_Transfer_CheckState,
                                  QObject::trUtf8("Coat Finish")));*/

		m_gSystem.m_totalCoatTime.StopTime();

		m_gSystem.AddProductInfo(PROCESS_COAT, QString("Coat Finish at %2 TotalSpendTime: %1S").arg(m_gSystem.m_totalCoatTime.GetTime()).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz")));
		g_SecsManager.SendEvent(EVENT_CoatingComplete);

		pTrans->WriteRecoveredData();
        break;
    }
    }
    VST_Wait_Base::do_Msg(msg);
}

