//---------------------------------------------------------------------------


#pragma hdrstop

#include "TVmsCtlrF.h"
#include "AppGlobalF.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

//---------------------------------------------------------------------------
TVmsCtlr::TVmsCtlr()
{
    InitializeCriticalSection(&FCS);

    VMS_MAX_PHSE_NUM      = INT_MAX_VMS_FORM;   // NUMBER(2)    Y            VMS 최대 표출면 개수

    VMS_PHSE_CHNG_CYCL    = 5;    // NUMBER(3)      Y VMS 표출면 변경 주기
    VMS_CMNC_ERR_BASS_VAL = 127;  // NUMBER(3)      Y VMS 통신 오류 기본 값(모듈 전원이 꺼지는 온도값(℃))
    FAN_MTNS_TMPR         = 35;   // NUMBER(6,3)    Y 팬 동작 온도
    HETR_MTNS_TMPR        = 0;    // NUMBER(6,3)    Y 히터 동작 온도
    VMS_MODL_ERR_RATE     = 20;   // NUMBER(3)      Y VMS 모듈 오류 율(Options)

    VMS_NGHT_BRGH_STEP    = 48;   // NUMBER(2)    Y            VMS 야간 휘도 단계
    VMS_WEEK_BRGH_STEP    = 64;   // NUMBER(2)    Y            VMS 주간 휘도 단계
    CMNCFAIL_SLOT_NMBR    = 0;    // NUMBER(4)    Y    0        통신장애 SLOT 번호
    PWER_FAIL_SLOT_NMBR   = 0;    // NUMBER(4)    Y    0        전원 장애 SLOT 번호
    VMS_MODL_CHK_CYCL     = 30;

    FDispLog = false;
    FSLog    = NULL;
    FCLog    = NULL;

    memset(&DSTATE, 0x00, sizeof(DSTATE));
    DSTATE.Comm = vms_error;
    DSTATE.Wcomm = vms_error;
    VComm_VmsInitStatus(&DSTATE);
    DSTATE.Comm = vms_error;
    DSTATE.Wcomm = vms_error;

    memset(&RSTATE, 0x00, sizeof(RSTATE));
    RSTATE.Comm = vms_error;
    RSTATE.Wcomm = vms_error;
    VComm_VmsInitStatus(&RSTATE);
    RSTATE.Comm = vms_error;
    RSTATE.Wcomm = vms_error;

    FSaveState = false;
    FOldState  = vms_normal;

    FProvide = false;
    FModeChange = false;

    FIsOldProtocol    = false;
    FIsFtpDownload    = false;
    FIsProtolFontName = false;
    FIsStatusAuto     = true;
    FBmpDownload      = false;

    FSymbolDownload   = false;

    RowNo = -1;
}
//---------------------------------------------------------------------------

TVmsCtlr::~TVmsCtlr()
{
    SAFE_DELETE(pPackQueue); /* 패킷 버퍼 */
    SAFE_DELETE(FSLog);
    SAFE_DELETE(FCLog);

    SAFE_DELETE(DOWNLOAD);
    SAFE_DELETE(CTLMODE);

    DeleteCriticalSection(&FCS);
}
//---------------------------------------------------------------------------
void TVmsCtlr::Lock()
{
    EnterCriticalSection(&FCS);
}
//---------------------------------------------------------------------------
void TVmsCtlr::UnLock()
{
    LeaveCriticalSection(&FCS);
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TVmsCtlrManager *VmsManager = NULL;
//---------------------------------------------------------------------------

TVmsCtlrManager::TVmsCtlrManager()
{
    Total  = 0;
    Error  = 0;
    Normal = 0;
    Module = 0;
}
//---------------------------------------------------------------------------
TVmsCtlrManager::~TVmsCtlrManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------

void TVmsCtlrManager::InitProvide(bool AProvide/*=false*/)
{
    try
    {
        FLists.Lock();
        FOR_STL(TVmsCtlr*, pObj, VmsManager->FLists)
        {
            pObj->FProvide = AProvide;;
        }
    }
    __finally
    {
        FLists.UnLock();
    }
}
//---------------------------------------------------------------------------


bool ChangedState(INT_VMS_STATE *DSTATE, INT_VMS_STATE *RSTATE)
{
    if (DSTATE->OprMode != RSTATE->OprMode) return true;
	//if (DSTATE->Comm    != RSTATE->Comm) return true;
	if (DSTATE->Wcomm   != RSTATE->Wcomm) return true;

	if (DSTATE->DoorStatus        != RSTATE->DoorStatus) return true;
	if (DSTATE->ModulePowerStatus != RSTATE->ModulePowerStatus) return true;
	if (DSTATE->FanStatus         != RSTATE->FanStatus) return true;
	if (DSTATE->HeaterStatus      != RSTATE->HeaterStatus) return true;
	if (DSTATE->BodyTemp          != RSTATE->BodyTemp) return true;
	if (DSTATE->LuminanceStatus   != RSTATE->LuminanceStatus) return true;

    if (DSTATE->ExternalLightStatus != RSTATE->ExternalLightStatus) return true;
    if (DSTATE->AlarmLightStatus    != RSTATE->AlarmLightStatus) return true;
    if (DSTATE->SpeakerStatus       != RSTATE->SpeakerStatus) return true;

	//if (!memcmp(DSTATE->ControllerCurrentTime, RSTATE->ControllerCurrentTime, INT_VMS_MAX_DATETIME)) return true;

    if (DSTATE->Voltage != RSTATE->Voltage) return true;

	if (DSTATE->ModuleState      != RSTATE->ModuleState) return true;
	if (DSTATE->ModuleVertical   != RSTATE->ModuleVertical) return true;
	if (DSTATE->ModuleHorizontal != RSTATE->ModuleHorizontal) return true;
	if (memcmp(DSTATE->ModuleStatus, RSTATE->ModuleStatus, INT_VMS_MAX_MODULE_BIT)) return true;

	if (DSTATE->PowerCount != RSTATE->PowerCount) return true;
	if (memcmp(DSTATE->PowerStatus, RSTATE->PowerStatus, INT_VMS_MAX_POWER_BIT)) return true;

	//if (DSTATE->ScheduledMessageOperatingTime   != RSTATE->ScheduledMessageOperatingTime) return true;
	if (DSTATE->ModuleOperatingTemperature      != RSTATE->ModuleOperatingTemperature) return true;
	if (DSTATE->FanOperatingTemperature         != RSTATE->FanOperatingTemperature) return true;
	if (DSTATE->HeaterOperatingTemperature      != RSTATE->HeaterOperatingTemperature) return true;
	if (DSTATE->ExternalLightOperatingLuminance != RSTATE->ExternalLightOperatingLuminance) return true;
	if (DSTATE->ModuleBasicFailureRate          != RSTATE->ModuleBasicFailureRate) return true;
	if (DSTATE->MaximumRetry                    != RSTATE->MaximumRetry) return true;
	if (DSTATE->ResponseTimeOut                 != RSTATE->ResponseTimeOut) return true;
	if (DSTATE->BlinkingCycleTime               != RSTATE->BlinkingCycleTime) return true;

    return false;
}
//---------------------------------------------------------------------------

void InitDownLoadInfo(VMS_DOWNLOAD_INFO *AInfo)
{
    AInfo->Enable   = vms_download_disable;
    AInfo->SaveFlag = -1;
    AInfo->dTimer   = Now();
    AInfo->Count    = 0;
}
//---------------------------------------------------------------------------

void InitCtlModeInfo(VMS_CONTROL_MODE *AInfo)
{
    AInfo->SaveFlag = -1;
    AInfo->pTimer   = Now();
}
//---------------------------------------------------------------------------

void EventInit(VMS_EVENT_INFO *AInfo)
{
    AInfo->Count = 0;
    for (int ii = 0; ii < VMS_MAX_EVENT; ii++)
    {
        AInfo->Unit[ii].LINK_ID               = "";
        AInfo->Unit[ii].IFSC_ID               = "";
        AInfo->Unit[ii].VMS_IFSC_ID           = "";
        AInfo->Unit[ii].VMS_IFSC_NM           = "";
        AInfo->Unit[ii].DSPL_STRT_NODE_NM     = "";
        AInfo->Unit[ii].DSPL_END_NODE_NM      = "";
        AInfo->Unit[ii].DETR_ID               = "";
        AInfo->Unit[ii].DSPL_PRRT             = "";
        AInfo->Unit[ii].INCD_OCRR_ID          = "";
        AInfo->Unit[ii].STRT_LCTN_NM          = "";
        AInfo->Unit[ii].END_LCTN_NM           = "";
        AInfo->Unit[ii].OCRR_LCTN_NM          = "";
        AInfo->Unit[ii].VMS_INCD_TYPE_NM      = "";
        AInfo->Unit[ii].VMS_INCD_TYPE_CD      = "";
        AInfo->Unit[ii].VMS_INCD_DETL_NM      = "";
        AInfo->Unit[ii].VMS_INCD_DETL_TYPE_CD = "";
        AInfo->Unit[ii].CMTR_GRAD_NM          = "";
        AInfo->Unit[ii].CMTR_GRAD_CD          = "";
        AInfo->Unit[ii].INCD_CLSR_LANE        = "";
        AInfo->Unit[ii].INCD_STRT_DT          = "";
        AInfo->Unit[ii].INCD_END_PRAR_DT      = "";
    }
}
//---------------------------------------------------------------------------

void ScheduleInit(VMS_SCHEDULE_INFO *AInfo)
{
    AInfo->Count = 0;
    for (int ii = 0; ii < VMS_MAX_SCHEDULE; ii++)
    {
        AInfo->Unit[ii].TRFC_SITU_TYPE_CD = 0;
        AInfo->Unit[ii].PHASE             = 0;
        AInfo->Unit[ii].VMS_FORM_ID       = "";
        AInfo->Unit[ii].FRST_VMS_IFSC_ID  = "";
        AInfo->Unit[ii].SECD_VMS_IFSC_ID  = "";
        AInfo->Unit[ii].THIR_VMS_IFSC_ID  = "";
        AInfo->Unit[ii].FOUR_VMS_IFSC_ID  = "";
        AInfo->Unit[ii].FRST_IMG_IFSC_ID  = "";
        AInfo->Unit[ii].SECD_IMG_IFSC_ID  = "";
        AInfo->Unit[ii].THIR_IMG_IFSC_ID  = "";
        AInfo->Unit[ii].FOUR_IMG_IFSC_ID  = "";
        AInfo->Unit[ii].DSPL_HH           = 0;
        AInfo->Unit[ii].VMS_FORM_PRRT_CD  = "";

        AInfo->Unit[ii].VMS_IFSC_ID       = "";

        AInfo->Unit[ii].EVENT             = NULL;
    }
}
//---------------------------------------------------------------------------

