#include "vst_wait_clean.h"
#include "logic.h"
#include "vst_work_clean.h"
VST_Wait_Clean::VST_Wait_Clean(int iID, const QString &sName,
                               VSM_Thread *pParent)
    :VST_Wait_Base(iID,sName,pParent)
{

}

//仅有清洗的过程
void VST_Wait_Clean::do_Msg(const V_Msg &msg)
{
    switch(msg.m_iID)
    {
    case VarMsg_Auto_StartClean:
    {
        CProcess_Clean* pClean=GET_PROCESS(PROCESS_CLEAN,
                                         SM_CLEAN,
                                         CProcess_Clean);
        QString strState=QObject::trUtf8("Auto Clean State");
        QString strMsg=QObject::trUtf8("Auto Clean Sig");
        COATCLEANSTEPVECTOR step= pClean->m_vCoatCleanStep;
        int iSize=step.size();
        int iStartStep=0;
        int iEndStep=iSize-1;
        if(!m_pNextState)
        {
            m_pNextState=new VST_Work_Clean(VarState_Work_CoatClean,
                                           strState, m_pParent,this,0);
            ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_CoatClean_BeforeAll, strMsg,
                                 QVariantList()<<NORMAL_ACTION);
            for(int i=iStartStep;i<=iEndStep;i++)
            {
				//清洗步骤依次添加
                ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_CoatClean_Step, strMsg,
                                     QVariantList()<<NORMAL_ACTION<<step[i].iType
                                     <<step[i].dTime<<step[i].iRPM<<i);
            }
            ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_CoatClean_AfterAll, strMsg,
                                 QVariantList()<<NORMAL_ACTION);
            ((VST_Work_Clean*)m_pNextState)->LoadMsgProcess();
        }
		m_gSystem.m_totalCleanTime.ClearTime();
		m_gSystem.m_totalCleanTime.StartTime();
        m_pParent->SetState(m_pNextState);
        QString strFinish=QObject::trUtf8("Auto Clean Finish");
        pushback_Msg(VarMsg_CoatClean_Finished,strFinish);
		g_SecsManager.SendEvent(EVENT_CleaningStarted);

        break;
    }
    case VarMsg_CoatClean_Finished:
    {
		m_gSystem.m_totalCleanTime.StopTime();
		//结束后发送VarMsg_Transfer_CheckState消息
        CProcess_Trans* pTrans=GET_PROCESS(PROCESS_TRANS,
                                           SM_TRANS,CProcess_Trans);

        QMutexLocker l(&pTrans->m_slotMutex);
        if(pTrans->m_pSlotClean)
        {
            pTrans->m_pSlotClean->WPstatus=WPCLEANFINISH;
            pTrans->m_pSlotClean->bCleanFinished = 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("Clean Finish")));*/

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

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

