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

#pragma hdrstop

#include "CDSVmsCtlrF.h"
#include "ITSUtilF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
TVmsIfscRltn::TVmsIfscRltn()
{
}
//---------------------------------------------------------------------------
TVmsIfscRltn::~TVmsIfscRltn()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TVmsMsg::TVmsMsg()
{
    for (int ii = 0; ii < INT_VMS_MAX_FORM; ii++)
    {
        PHASE[ii].pVmsBmp = NULL;
    }
    Init();
};
//---------------------------------------------------------------------------
TVmsMsg::~TVmsMsg()
{
    for (int ii = 0; ii < INT_VMS_MAX_FORM; ii++)
    {
        if (PHASE[ii].pVmsBmp)
        {
            PHASE[ii].pVmsBmp->FreeImage();
            PHASE[ii].pVmsBmp->Assign(NULL);
            delete PHASE[ii].pVmsBmp;
            PHASE[ii].pVmsBmp = NULL;
        }
    }
};
//---------------------------------------------------------------------------

void TVmsMsg::Init()
{
    Total    = 0;
    Curr     = 0;
    OFFER_DT = "19990101000000";
    OFFER_YN = false;
    for (int ii = 0; ii < INT_VMS_MAX_FORM; ii++)
    {
        PHASE[ii].DSPL_HH = 4;        //표출시간
        PHASE[ii].DNLD_YN = "N";      //다운로드성공여부
        PHASE[ii].DSPL_CD = "";       //표출방법
        PHASE[ii].VMS_SCH_FORM_TYPE = 0;
        PHASE[ii].VMS_DSPL_MSG_TXT  = "";
    }
};
//---------------------------------------------------------------------------

void VmsInitStatus(INT_VMS_STATE *AState)
{
    int ii;
	if (AState->Comm != vms_comm_normal && AState->Wcomm != vms_wcomm_normal)
	{
        AState->DoorStatus          = vms_door_unknown; /* 도어상태정보코드,     0:열림 1:닫힘 2:알수없음 */
        AState->ModulePowerStatus   = vms_module_power_unknown; /* 모듈전원상태정보코드, 0:켜짐 1:꺼짐 2:알수없음 */
        AState->BodyTemp            = 0;           /* 함체온도값(℃), 범위(-128~127) */
        AState->LuminanceStatus     = 0;           /* 화면의 밝기값 (최대 휘도값을 100으로 했을 때의 백분율 값), 범위(0~100) */
        AState->FanStatus           = vms_fan_unknown; /* Fan 동작상태정보코드,    0:켜짐, 1:꺼짐 2:알수없음 */
        AState->HeaterStatus        = vms_heater_unknown; /* Heater 동작상태정보코드, 0:켜짐, 1:꺼짐 2:알수없음 */

        AState->ExternalLightStatus = vms_unknown; /* 선택 외부조명 동작상태정보코드 0:켜짐, 1:꺼짐, 2:자동(공단은 미사용) */
        AState->AlarmLightStatus    = vms_unknown; /* 선택 경광등 동작상태정보코드   0:켜짐, 1:꺼짐 */
        AState->SpeakerStatus       = vms_unknown; /* 선택 스피커 동작상태정보코드   0:켜짐, 1:꺼짐 */
        //memset(&AState->ControllerCurrentTime, 0x00, sizeof(AState->ControllerCurrentTime));  /*선택 제어기 시간 (YYYYMMDDHHMMSS) */

        AState->Voltage = 0;                       /* 전압, 범위(0~255), 사용안함 */

//        AState->ModuleState      = vms_unknown;    /* 모듈 상태, 0:정상 1:장애 2:알수없음 */
#if 0
        //AState->ModuleHorizontal = 0;              /* 가로 모듈수 */
        //AState->ModuleVertical   = 0;              /* 세로 모듈수 */
        for (ii = 0; ii < INT_VMS_MAX_MODULE_BIT; ii++)
            AState->ModuleStatus[ii] = 0xFF;//vms_unknown; /* 모듈 개별 상태, 0:정상 1:장애 2:알수없음 */
        //AState->PowerCount       = 0;               /* 전원 갯수 */
        for (ii = 0; ii < INT_VMS_MAX_POWER_BIT; ii++)
            AState->PowerStatus[ii] = 0xFF;//vms_unknown; /* 전원 개별 상태, 0:켜짐 1:꺼짐 2:알수없음 */
#endif

#if 0
        AState->ScheduledMessageOperatingTime   = 0; /* 필수 계획된 메시지의 동작시간(초) */
        AState->ModuleOperatingTemperature      = 0; /* 필수 모듈 전원이 꺼지는 온도값(℃) */
        AState->FanOperatingTemperature         = 0; /* 필수 Fan 동작 기준 온도값(℃) */
        AState->HeaterOperatingTemperature      = 0; /* 필수 Heater 동작 기준 온도값(℃) */
        AState->ExternalLightOperatingLuminance = 0; /* 선택 외부전등 동작 기준 휘도값 */
        AState->ModuleBasicFailureRate          = 0; /* 선택 모듈 장애율 (한 개의 모듈을 장애로 처리하기 위한 픽셀의 백분율값) */
        AState->MaximumRetry                    = 0; /* 선택 최대 재시도 횟수(회) */
        AState->ResponseTimeOut                 = 0; /* 선택 최대응답대기시간 (초) */
        AState->BlinkingCycleTime               = 0; /* 선택 점멸시간 주기 ( 1/10초단위) */
#endif
    }

    for (ii = 0; ii < INT_VMS_MAX_MODULE_BIT; ii++)
        AState->ModuleStatus[ii] = 0xFF;//vms_unknown; /* 모듈 개별 상태, 0:정상 1:장애 2:알수없음 */
    //AState->PowerCount       = 0;               /* 전원 갯수 */
    for (ii = 0; ii < INT_VMS_MAX_POWER_BIT; ii++)
        AState->PowerStatus[ii] = 0xFF;//vms_unknown; /* 전원 개별 상태, 0:켜짐 1:꺼짐 2:알수없음 */
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TVmsCtlr::TVmsCtlr()
{
    memset(&STATE, 0x00, sizeof(STATE));
    STATE.Comm = vms_comm_error;
    VmsInitStatus(&STATE);
    STATE.Comm = vms_comm_error;

    InitFormCount();

    VmsMsg = NULL;
    VmsMsg = new TVmsMsg();
    VmsMsg->Init();

    RunState = state_error;
}
//---------------------------------------------------------------------------

TVmsCtlr::~TVmsCtlr()
{
    FIfscRltn.RemoveAll();
}
//---------------------------------------------------------------------------
void TVmsCtlr::CheckRunState()
{
    RunState = STATE.Comm;
    if (STATE.Comm == vms_comm_normal && STATE.Wcomm != vms_wcomm_normal)
    {
        STATE.DoorStatus        = vms_door_unknown;
        STATE.ModulePowerStatus = vms_module_power_unknown;
        STATE.BodyTemp          = 0;
        STATE.LuminanceStatus   = 0;
        STATE.FanStatus         = vms_fan_unknown;
        STATE.HeaterStatus      = vms_heater_unknown;
    }
}
//---------------------------------------------------------------------------


TVmsSize::TVmsSize()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TVmsCtlrManager *VmsCtlrManager = NULL;
//---------------------------------------------------------------------------

TVmsCtlrManager::TVmsCtlrManager()
{
    FImgStream = NULL;
}
//---------------------------------------------------------------------------
TVmsCtlrManager::~TVmsCtlrManager()
{
    FVmsSize.RemoveAll();
    FLists.RemoveAll();
    if (FImgStream) delete FImgStream;
    FImgStream = NULL;
}
//---------------------------------------------------------------------------
void TVmsCtlrManager::InitFormCount()
{
    FLists.Lock();
    try
    {
        FOR_STL(TVmsCtlr*, pObj, FLists)
        {
            pObj->InitFormCount();
        }
    }
    __finally
    {
        FLists.UnLock();
    }
};
//---------------------------------------------------------------------------

bool TVmsCtlrManager::LoadVmsSize(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    FVmsSize.Lock();
    try
    {
        FVmsSize.RemoveAll();
    }
    __finally
    {
        FVmsSize.UnLock();
    }

    sQry = "SELECT VMS_TYPE_CD, VMS_TYPE_NM,                        \r\n"
           "       VMS_WDTH, VMS_HGHT, MODL_ROW_NUM, MODL_COL_NUM   \r\n"
           "  FROM TB_VMS_TYPE                                      \r\n"
           " WHERE USE_YN = 'Y'                                     \r\n";

    FVmsSize.Lock();
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
				TVmsSize *pObj = new TVmsSize();

                pObj->TypeCd = pADO->FieldByName("VMS_TYPE_CD")->AsString;
                pObj->TypeNm = pADO->FieldByName("VMS_TYPE_NM")->AsString;
                pObj->Width  = pADO->FieldByName("VMS_WDTH")->AsInteger;
                pObj->Height = pADO->FieldByName("VMS_HGHT")->AsInteger;

				FVmsSize.Push(pObj->TypeCd, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::LoadVmsSize", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::LoadVmsSize", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FVmsSize.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    FMinWidth = 1000;
    FMinHeight= 1000;
    FMaxWidth = 0;
    FMaxHeight= 0;

    sQry = "SELECT A.*,                               \r\n"
           "       B.VMS_TYPE_NM,                     \r\n"
           "       B.VMS_WDTH,     B.VMS_HGHT,        \r\n"
           "       B.ROW_NUM,      B.COL_NUM,         \r\n"
           "       B.MODL_ROW_NUM, B.MODL_COL_NUM,    \r\n"
           "       B.POWR_ROW_NUM, B.POWR_COL_NUM     \r\n"
           "  FROM TB_VMS_CTLR A,                     \r\n"
           "       TB_VMS_TYPE B                      \r\n"
           " WHERE A.VMS_TYPE_CD = B.VMS_TYPE_CD      \r\n"
           "   AND A.DEL_YN      = 'N'                \r\n";

    FLists.Lock();
    try
    {
        FLists.RemoveAll();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
				TVmsCtlr *pObj = new TVmsCtlr();

                pObj->VMS_CTLR_NMBR      = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;           //	N	NUMBER(10)	    N			VMS 제어기 번호
                pObj->VMS_CTLR_ID        = pADO->FieldByName("VMS_CTLR_ID")->AsString.Trim();       //	N	VARCHAR2(10)	Y			VMS 제어기 ID
                pObj->VMS_NM             = pADO->FieldByName("VMS_NM")->AsString.Trim();            //	N	VARCHAR2(40)	Y			VMS 명칭
                pObj->VMS_CTLR_IP        = pADO->FieldByName("VMS_CTLR_IP")->AsString.Trim();       //	N	VARCHAR2(20)	Y			VMS 제어기 아이피
#if 0
                pObj->VMS_CTLR_PORT      = pADO->FieldByName("VMS_CTLR_PORT")->AsInteger;           //	N	NUMBER(5)	    Y	0		VMS 제어기 포트
                pObj->VMS_CTLR_LOCAL_NO  = pADO->FieldByName("VMS_CTLR_LOCAL_NO")->AsInteger;       //	N	NUMBER(5)	    Y	0		VMS 제어기 지역번호
                pObj->DEF_PHSE_CHNG_CYCL = pADO->FieldByName("DEF_PHSE_CHNG_CYCL")->AsInteger;      //	N	NUMBER(3)	    Y	5		기본메시지주기
                pObj->FAN_MODE           = pADO->FieldByName("FAN_MODE")->AsInteger;                //	N	NUMBER(3)	Y	2		FAN 동작모드(0x00:꺼짐,0x01:켜짐,0x02:자동,0x09:알수없음)
                pObj->FAN_RUN_TMPR       = pADO->FieldByName("FAN_RUN_TMPR")->AsInteger;            //	N	NUMBER(3)	Y	30		팬 동작 온도
                pObj->HETR_MODE          = pADO->FieldByName("HETR_MODE")->AsInteger;               //	N	NUMBER(3)	Y	2		히터 동작모드(0x00:꺼짐,0x01:켜짐,0x02:자동,0x09:알수없음)
                pObj->HETR_RUN_TMPR      = pADO->FieldByName("HETR_RUN_TMPR")->AsInteger;           //	N	NUMBER(3)	Y	0		히터 동작 온도
                pObj->BRGH_CURR_STEP     = pADO->FieldByName("BRGH_CURR_STEP")->AsInteger;          //	N	NUMBER(3)	Y	40		휘도 현재 단계(0~100)
                pObj->BRGH_WEEK_STEP     = pADO->FieldByName("BRGH_WEEK_STEP")->AsInteger;          //	N	NUMBER(3)	Y	64		휘도 주간 단계(0~100)
                pObj->BRGH_NGHT_STEP     = pADO->FieldByName("BRGH_NGHT_STEP")->AsInteger;          //	N	NUMBER(3)	Y	48		휘도 야간 단계(0~100)
                pObj->MODL_ERR_RATE      = pADO->FieldByName("MODL_ERR_RATE")->AsInteger;           //	N	NUMBER(3)	Y	10		VMS 모듈 오류 율
                pObj->CMNC_FAIL_RATE     = pADO->FieldByName("CMNC_FAIL_RATE")->AsInteger;          //	N	NUMBER(3)	Y	60		VMS 통신 오류 기본 값
                pObj->WEB_CMRA_CMPY      = pADO->FieldByName("WEB_CMRA_CMPY")->AsString.Trim();     //	N	VARCHAR2(50)	Y			웹카메라제조사(삼성카메라'Samsung')
                pObj->WEB_CMRA_MDL       = pADO->FieldByName("WEB_CMRA_MDL")->AsString.Trim();      //	N	VARCHAR2(126)	Y			웹카메라모델
                pObj->ISTL_LCTN_ADDR     = pADO->FieldByName("ISTL_LCTN_ADDR")->AsString.Trim();    //	N	VARCHAR2(200)	Y			설치 위치 주소
#else
                pObj->VMS_CTLR_PORT      = pADO->FieldByName("VMS_CMNC_PORT")->AsInteger;           //	N	NUMBER(5)	    Y	0		VMS 제어기 포트
#endif
                pObj->VMS_TYPE_CD        = pADO->FieldByName("VMS_TYPE_CD")->AsString.Trim();       //	N	VARCHAR2(7)	    Y			VMS 유형 코드
                pObj->ISTL_LCTN_NM       = pADO->FieldByName("ISTL_LCTN_NM")->AsString.Trim();      //	N	VARCHAR2(40)	Y			설치 위치 명
                pObj->X_CRDN             = pADO->FieldByName("X_CRDN")->AsFloat;                    //	N	NUMBER(11,8)	Y			X 좌표
                pObj->Y_CRDN             = pADO->FieldByName("Y_CRDN")->AsFloat;                    //	N	NUMBER(10,8)	Y			Y 좌표
                pObj->DEL_YN             = pADO->FieldByName("DEL_YN")->AsString.Trim();            //	N	CHAR(1)	        Y	'N'		삭제 여부
                pObj->WEB_CMRA_IP        = pADO->FieldByName("WEB_CMRA_IP")->AsString.Trim();       //	N	VARCHAR2(20)	Y			웹 카메라 아이피
                pObj->WEB_CMRA_PORT      = pADO->FieldByName("WEB_CMRA_PORT")->AsString.Trim();     //	N	NUMBER(5)	    Y			웹카메라 포트
                pObj->WEB_CMRA_ID        = pADO->FieldByName("WEB_CMRA_ID")->AsString.Trim();       //	N	VARCHAR2(20)	Y			웹카메라 ID
                pObj->WEB_CMRA_PWD       = pADO->FieldByName("WEB_CMRA_PWD")->AsString.Trim();      //	N	VARCHAR2(20)	Y			웹카메라 비밀번호
                pObj->STRM_SESN_NM       = pADO->FieldByName("STRM_SESN_NM")->AsString.Trim();      //	N	VARCHAR2(200)	Y			스트리밍 세션 명
                pObj->OPER_MODE          = pADO->FieldByName("OPER_MODE")->AsString.Trim();         //	N	VARCHAR2(7)	    Y	'A'		운영 모드
                pObj->VMS_MAX_PHSE_NUM   = pADO->FieldByName("VMS_MAX_PHSE_NUM")->AsInteger;        //	N	NUMBER(2)	    Y	14		VMS 최대 표출면 개수
                pObj->PANL_ON_TIME       = pADO->FieldByName("PANL_ON_TIME")->AsString.Trim();      //	N	VARCHAR2(4)	    Y	'0400'	전광판 ON TIME
                pObj->PANL_OFF_TIME      = pADO->FieldByName("PANL_OFF_TIME")->AsString.Trim();     //	N	VARCHAR2(4)	    Y	'0200'	전광판 OFF TIME
                pObj->PANL_PWER_MODE     = pADO->FieldByName("PANL_PWER_MODE")->AsInteger;          //	N	NUMBER(3)	Y	2		전광판 전원 모드(0x00:꺼짐,0x01:켜짐,0x02:자동,0x09:알수없음)
                pObj->BRGH_MODE          = pADO->FieldByName("BRGH_MODE")->AsInteger;               //	N	NUMBER(3)	Y	2		휘도 모드(0x00:주간,0x01:야간,0x00:자동,0x09:알수없음)

                pObj->VMS_TYPE_NM        = pADO->FieldByName("VMS_TYPE_NM")->AsString;


                pObj->WIDTH              = pADO->FieldByName("VMS_WDTH")->AsString.Trim().ToIntDef(576);
                pObj->HEIGHT             = pADO->FieldByName("VMS_HGHT")->AsString.Trim().ToIntDef(320);
                pObj->ROW_NUM            = pADO->FieldByName("ROW_NUM")->AsInteger;
                pObj->COL_NUM            = pADO->FieldByName("COL_NUM")->AsInteger;
                pObj->MODL_ROW_NUM       = pADO->FieldByName("MODL_ROW_NUM")->AsInteger;
                pObj->MODL_COL_NUM       = pADO->FieldByName("MODL_COL_NUM")->AsInteger;
                pObj->POWR_ROW_NUM       = pADO->FieldByName("POWR_ROW_NUM")->AsInteger;
                pObj->POWR_COL_NUM       = pADO->FieldByName("POWR_COL_NUM")->AsInteger;

                if (pObj->WIDTH  > FMaxWidth)  FMaxWidth  = pObj->WIDTH;
                if (pObj->WIDTH  < FMinWidth)  FMinWidth  = pObj->WIDTH;
                if (pObj->HEIGHT > FMaxHeight) FMaxHeight = pObj->HEIGHT;
                if (pObj->HEIGHT < FMinHeight) FMinHeight = pObj->HEIGHT;

#if 0
                pObj->STATE.ScheduledMessageOperatingTime = pObj->VMS_PHSE_CHNG_CYCL;    /* 필수 계획된 메시지의 동작시간(초) */
                pObj->STATE.ModuleOperatingTemperature    = pObj->VMS_CMNC_ERR_BASS_VAL; /* 필수 모듈 전원이 꺼지는 온도값(℃) */
                pObj->STATE.FanOperatingTemperature       = pObj->FAN_MTNS_TMPR;         /* 필수 Fan 동작 기준 온도값(℃) */
                pObj->STATE.HeaterOperatingTemperature    = pObj->HETR_MTNS_TMPR;        /* 필수 Heater 동작 기준 온도값(℃) */
                pObj->STATE.ModuleBasicFailureRate        = pObj->VMS_MODL_ERR_RATE;     /* 선택 모듈 장애율 (한 개의 모듈을 장애로 처리하기 위한 픽셀의 백분율값) */
#endif
				FLists.Push(pObj->VMS_CTLR_NMBR, pObj);
                pObj->Completed  = true;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::LoadFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::LoadStatusFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

#if 0
//CMNC_STTS_CD
1	CMS0	CMS	정상	connected					Y
2	CMS1	CMS	통신두절	Not connected					Y
3	CMS2	CMS	정보없음						Y

//COBXDOOR_OPEN_STTS_CD
1	CDS0	CDS	닫힘	a door closed					Y
2	CDS1	CDS	열림	a door opened					Y
3	CDS2	CDS	정보없음						Y

1	CRS0	CRS	가동	operated					Y
2	CRS1	CRS	중지	stopped					Y

1	CPS0	CPS	ON	Power On					Y
2	CPS1	CPS	OFF	Power Off					Y

//HETR_STTS_CD
1	HTS0	HTS	가동	operated					Y
2	HTS1	HTS	중지	stopped					Y
3	HTS2	HTS	정보없음						Y

//PWER_STTS_CD
1	PWS0	PWS	정상	normal					Y
2	PWS1	PWS	오류	abnormal					Y

//FAN_STTS_CD
1	PAS0	PAS	가동	operated					Y
2	PAS1	PAS	중지	stopped					Y
3	PAS2	PAS	정보없음						Y

//모듈
1	MOS0	MOS	정상	normal					Y
2	MOS1	MOS	오류	abnormal					Y
#endif

#if 0
    sQry = "SELECT *                                                        \r\n"
           "  FROM TB_VMS_CTLR_STTS A, TB_VMS_CTLR B                        \r\n"
           " WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR                        \r\n"
           "   AND A.UPDT_DT >= TO_CHAR(SYSDATE-5/1440, 'YYYYMMDDHH24MISS') \r\n";
#else
    sQry = "SELECT *                                                        \r\n"
           "  FROM TB_VMS_STTS_PRST A, TB_VMS_CTLR B                        \r\n"
           " WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR                        \r\n"
           "   AND A.RGST_DT >= TO_CHAR(SYSDATE-5/1440, 'YYYYMMDDHH24MISS') \r\n";
#endif

    FLists.Lock();
    try
    {
        TVmsCtlr *pObj = NULL;

#if 0
        FOR_STL(TVmsCtlr*, pTmp, FLists)
        {
            pTmp->STATE.Comm = vms_comm_error;
            VmsInitStatus(&pTmp->STATE);
            pTmp->RunState = state_error;
        }
#endif

        try
        {
            int nORD;

            pADO = new TADOQuery(NULL);
            pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
            pADO->SQL->Clear();
            pADO->SQL->Text = sQry;

            pADO->Prepared = true;
            pADO->Open();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
                String sVmsNmbr = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;
                pObj = FLists.Find(sVmsNmbr);
                if (!pObj) continue;

                String UPDT_DT      = pADO->FieldByName("UPDT_DT")->AsString;
                String CMNC_STTS_CD = pADO->FieldByName("CMNC_STTS_CD")->AsString;
                if (CMNC_STTS_CD != "CMS0")
                {
                    //통신장애
                    continue;
                }
                pObj->STATE.Comm = vms_comm_normal;

                String CBOXDOOR_OPEN_STTS_CD = pADO->FieldByName("CBOX_DOOR_STTS_CD")->AsString;
                if      (CBOXDOOR_OPEN_STTS_CD == "CDS0") pObj->STATE.DoorStatus = vms_door_close;
                else if (CBOXDOOR_OPEN_STTS_CD == "CDS1") pObj->STATE.DoorStatus = vms_door_open;

                String CBOXFAN_STTS_CD = pADO->FieldByName("FAN_STTS_CD")->AsString;
                if      (CBOXFAN_STTS_CD == "MOS0") pObj->STATE.ModulePowerStatus = vms_module_power_on;
                else if (CBOXFAN_STTS_CD == "MOS1") pObj->STATE.ModulePowerStatus = vms_module_power_off;

                String CBOX_TMPR = pADO->FieldByName("CBOX_TMPR")->AsString;
                pObj->STATE.BodyTemp = CBOX_TMPR.ToIntDef(0);

                String CBOX_HMDT = pADO->FieldByName("BRGH_VAL")->AsString;
                pObj->STATE.LuminanceStatus = CBOX_HMDT.ToIntDef(0);

                String FAN_STTS_CD = pADO->FieldByName("FAN_STTS_CD")->AsString;
                if      (FAN_STTS_CD == "PAS0") pObj->STATE.FanStatus = vms_fan_on;
                else if (FAN_STTS_CD == "PAS1") pObj->STATE.FanStatus = vms_fan_off;

                String HETR_STTS_CD = pADO->FieldByName("HETR_STTS_CD")->AsString;
                if      (HETR_STTS_CD == "HTS0") pObj->STATE.HeaterStatus = vms_heater_on;
                else if (HETR_STTS_CD == "HTS1") pObj->STATE.HeaterStatus = vms_heater_off;

                String COMM_STTS_CD = pADO->FieldByName("COMM_STTS_CD")->AsString;
                if      (COMM_STTS_CD == "CMS0") pObj->STATE.Wcomm = vms_wcomm_normal;
                else if (COMM_STTS_CD == "CMS1") pObj->STATE.Wcomm = vms_wcomm_error;


                pObj->CheckRunState();
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::LoadStatusFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::LoadStatusFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::DeleteVms(TVmsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_VMS_CTLR          \r\n"
           "   SET DEL_YN = 'Y'         \r\n"
           " WHERE VMS_CTLR_NMBR = :p01 \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->VMS_CTLR_NMBR;
			pADO->ExecSQL();

        sQry = "DELETE TB_FCLT_INFR      \r\n"
               " WHERE FCLT_ID = :p01    \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->VMS_CTLR_ID;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::DeleteVms", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::DeleteVms", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::InsertVms(TVmsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_VMS_CTLR         \r\n"
           "          (                     \r\n"
           "           VMS_CTLR_NMBR,       \r\n"
           "           VMS_CTLR_ID,         \r\n"
           "           VMS_NM,              \r\n"
           "           VMS_CTLR_IP,         \r\n"
           "           VMS_CTLR_PORT,       \r\n"
           "           VMS_CTLR_LOCAL_NO,   \r\n"
           "           VMS_TYPE_CD,         \r\n"
           "           ISTL_LCTN_NM,        \r\n"
           "           X_CRDN,              \r\n"
           "           Y_CRDN,              \r\n"
           "           DEL_YN,              \r\n"
           "           WEB_CMRA_IP,         \r\n"
           "           WEB_CMRA_PORT,       \r\n"
           "           WEB_CMRA_ID,         \r\n"
           "           WEB_CMRA_PWD,        \r\n"
           "           STRM_SESN_NM,        \r\n"
           "           VMS_MAX_PHSE_NUM,    \r\n"
           "           PANL_ON_TIME,        \r\n"
           "           PANL_OFF_TIME,       \r\n"
           "           ISTL_LCTN_ADDR       \r\n"
           "          )                     \r\n"
           "   VALUES (:p01,                \r\n"
           "           :p02,                \r\n"
           "           :p03,                \r\n"
           "           :p04,                \r\n"
           "           :p05,                \r\n"
           "           :p06,                \r\n"
           "           :p07,                \r\n"
           "           :p08,                \r\n"
           "           :p09,                \r\n"
           "           :p10,                \r\n"
           "           :p11,                \r\n"
           "           :p12,                \r\n"
           "           :p13,                \r\n"
           "           :p14,                \r\n"
           "           :p15,                \r\n"
           "           :p16,                \r\n"
           "           :p17,                \r\n"
           "           :p18,                \r\n"
           "           :p19,                \r\n"
           "           :p20)                \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Parameters->ParamByName("p01")->Value = AObj->VMS_CTLR_NMBR;
            pADO->Parameters->ParamByName("p02")->Value = AObj->VMS_CTLR_ID;
            pADO->Parameters->ParamByName("p03")->Value = AObj->VMS_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->VMS_CTLR_IP;
            pADO->Parameters->ParamByName("p05")->Value = AObj->VMS_CTLR_PORT;
            pADO->Parameters->ParamByName("p06")->Value = AObj->VMS_CTLR_LOCAL_NO;
            pADO->Parameters->ParamByName("p07")->Value = AObj->VMS_TYPE_CD;
            pADO->Parameters->ParamByName("p08")->Value = AObj->ISTL_LCTN_NM;
            pADO->Parameters->ParamByName("p09")->Value = AObj->X_CRDN;
            pADO->Parameters->ParamByName("p10")->Value = AObj->Y_CRDN;
            pADO->Parameters->ParamByName("p11")->Value = AObj->DEL_YN;
            pADO->Parameters->ParamByName("p12")->Value = AObj->WEB_CMRA_IP;
            pADO->Parameters->ParamByName("p13")->Value = AObj->WEB_CMRA_PORT;
            pADO->Parameters->ParamByName("p14")->Value = AObj->WEB_CMRA_ID;
            pADO->Parameters->ParamByName("p15")->Value = AObj->WEB_CMRA_PWD;
            pADO->Parameters->ParamByName("p16")->Value = AObj->STRM_SESN_NM;
            pADO->Parameters->ParamByName("p17")->Value = AObj->VMS_MAX_PHSE_NUM;
            pADO->Parameters->ParamByName("p18")->Value = AObj->PANL_ON_TIME;
            pADO->Parameters->ParamByName("p19")->Value = AObj->PANL_OFF_TIME;
            pADO->Parameters->ParamByName("p20")->Value = AObj->ISTL_LCTN_ADDR;

			pADO->ExecSQL();

            return MergeFcltInfo(AObj, ADbConn);
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::InsertVms", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::InsertVms", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::UpdateVms(TVmsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    return MergeVms(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::MergeVms(TVmsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_VMS_CTLR L                   \r\n"
           "USING (SELECT :p01 AS VMS_CTLR_NMBR,       \r\n"
           "              :p02 AS VMS_CTLR_ID,         \r\n"
           "              :p03 AS VMS_NM,              \r\n"
           "              :p04 AS VMS_CTLR_IP,         \r\n"
           "              :p05 AS VMS_CTLR_PORT,       \r\n"
           "              :p06 AS VMS_CTLR_LOCAL_NO,   \r\n"
           "              :p07 AS VMS_TYPE_CD,         \r\n"
           "              :p08 AS ISTL_LCTN_NM,        \r\n"
           "              :p09 AS X_CRDN,              \r\n"
           "              :p10 AS Y_CRDN,              \r\n"
           "              :p11 AS DEL_YN,              \r\n"
           "              :p12 AS WEB_CMRA_IP,         \r\n"
           "              :p13 AS WEB_CMRA_PORT,       \r\n"
           "              :p14 AS WEB_CMRA_ID,         \r\n"
           "              :p15 AS WEB_CMRA_PWD,        \r\n"
           "              :p16 AS STRM_SESN_NM,        \r\n"
           "              :p17 AS VMS_MAX_PHSE_NUM,    \r\n"
           "              :p18 AS PANL_ON_TIME,        \r\n"
           "              :p19 AS PANL_OFF_TIME,       \r\n"
           "              :p20 AS ISTL_LCTN_ADDR       \r\n"
           "         FROM DUAL) M                                     \r\n"
           "        ON (L.VMS_CTLR_NMBR = M.VMS_CTLR_NMBR)            \r\n"
           "WHEN MATCHED THEN                                         \r\n"
           "   UPDATE SET L.VMS_CTLR_ID       = M.VMS_CTLR_ID,        \r\n"
           "              L.VMS_NM            = M.VMS_NM,             \r\n"
           "              L.VMS_CTLR_IP       = M.VMS_CTLR_IP,        \r\n"
           "              L.VMS_CTLR_PORT     = M.VMS_CTLR_PORT,      \r\n"
           "              L.VMS_CTLR_LOCAL_NO = M.VMS_CTLR_LOCAL_NO,  \r\n"
           "              L.VMS_TYPE_CD       = M.VMS_TYPE_CD,        \r\n"
           "              L.ISTL_LCTN_NM      = M.ISTL_LCTN_NM,       \r\n"
           "              L.X_CRDN            = M.X_CRDN,             \r\n"
           "              L.Y_CRDN            = M.Y_CRDN,             \r\n"
           "              L.DEL_YN            = M.DEL_YN,             \r\n"
           "              L.WEB_CMRA_IP       = M.WEB_CMRA_IP,        \r\n"
           "              L.WEB_CMRA_PORT     = M.WEB_CMRA_PORT,      \r\n"
           "              L.WEB_CMRA_ID       = M.WEB_CMRA_ID,        \r\n"
           "              L.WEB_CMRA_PWD      = M.WEB_CMRA_PWD,       \r\n"
           "              L.STRM_SESN_NM      = M.STRM_SESN_NM,       \r\n"
           "              L.VMS_MAX_PHSE_NUM  = M.VMS_MAX_PHSE_NUM,   \r\n"
           "              L.PANL_ON_TIME      = M.PANL_ON_TIME,       \r\n"
           "              L.PANL_OFF_TIME     = M.PANL_OFF_TIME,      \r\n"
           "              L.ISTL_LCTN_ADDR    = M.ISTL_LCTN_ADDR      \r\n"
           "WHEN NOT MATCHED THEN                                     \r\n"
           "   INSERT (                     \r\n"
           "           VMS_CTLR_NMBR,       \r\n"
           "           VMS_CTLR_ID,         \r\n"
           "           VMS_NM,              \r\n"
           "           VMS_CTLR_IP,         \r\n"
           "           VMS_CTLR_PORT,       \r\n"
           "           VMS_CTLR_LOCAL_NO,   \r\n"
           "           VMS_TYPE_CD,         \r\n"
           "           ISTL_LCTN_NM,        \r\n"
           "           X_CRDN,              \r\n"
           "           Y_CRDN,              \r\n"
           "           DEL_YN,              \r\n"
           "           WEB_CMRA_IP,         \r\n"
           "           WEB_CMRA_PORT,       \r\n"
           "           WEB_CMRA_ID,         \r\n"
           "           WEB_CMRA_PWD,        \r\n"
           "           STRM_SESN_NM,        \r\n"
           "           VMS_MAX_PHSE_NUM,    \r\n"
           "           PANL_ON_TIME,        \r\n"
           "           PANL_OFF_TIME,       \r\n"
           "           ISTL_LCTN_ADDR       \r\n"
           "          )                     \r\n"
           "   VALUES (                     \r\n"
           "           M.VMS_CTLR_NMBR,     \r\n"
           "           M.VMS_CTLR_ID,       \r\n"
           "           M.VMS_NM,            \r\n"
           "           M.VMS_CTLR_IP,       \r\n"
           "           M.VMS_CTLR_PORT,     \r\n"
           "           M.VMS_CTLR_LOCAL_NO, \r\n"
           "           M.VMS_TYPE_CD,       \r\n"
           "           M.ISTL_LCTN_NM,      \r\n"
           "           M.X_CRDN,            \r\n"
           "           M.Y_CRDN,            \r\n"
           "           M.DEL_YN,            \r\n"
           "           M.WEB_CMRA_IP,       \r\n"
           "           M.WEB_CMRA_PORT,     \r\n"
           "           M.WEB_CMRA_ID,       \r\n"
           "           M.WEB_CMRA_PWD,      \r\n"
           "           M.STRM_SESN_NM,      \r\n"
           "           M.VMS_MAX_PHSE_NUM,  \r\n"
           "           M.PANL_ON_TIME,      \r\n"
           "           M.PANL_OFF_TIME,     \r\n"
           "           M.ISTL_LCTN_ADDR     \r\n"
           "          )                     \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Parameters->ParamByName("p01")->Value = AObj->VMS_CTLR_NMBR;
            pADO->Parameters->ParamByName("p02")->Value = AObj->VMS_CTLR_ID;
            pADO->Parameters->ParamByName("p03")->Value = AObj->VMS_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->VMS_CTLR_IP;
            pADO->Parameters->ParamByName("p05")->Value = AObj->VMS_CTLR_PORT;
            pADO->Parameters->ParamByName("p06")->Value = AObj->VMS_CTLR_LOCAL_NO;
            pADO->Parameters->ParamByName("p07")->Value = AObj->VMS_TYPE_CD;
            pADO->Parameters->ParamByName("p08")->Value = AObj->ISTL_LCTN_NM;
            pADO->Parameters->ParamByName("p09")->Value = AObj->X_CRDN;
            pADO->Parameters->ParamByName("p10")->Value = AObj->Y_CRDN;
            pADO->Parameters->ParamByName("p11")->Value = AObj->DEL_YN;
            pADO->Parameters->ParamByName("p12")->Value = AObj->WEB_CMRA_IP;
            pADO->Parameters->ParamByName("p13")->Value = AObj->WEB_CMRA_PORT;
            pADO->Parameters->ParamByName("p14")->Value = AObj->WEB_CMRA_ID;
            pADO->Parameters->ParamByName("p15")->Value = AObj->WEB_CMRA_PWD;
            pADO->Parameters->ParamByName("p16")->Value = AObj->STRM_SESN_NM;
            pADO->Parameters->ParamByName("p17")->Value = AObj->VMS_MAX_PHSE_NUM;
            pADO->Parameters->ParamByName("p18")->Value = AObj->PANL_ON_TIME;
            pADO->Parameters->ParamByName("p19")->Value = AObj->PANL_OFF_TIME;
            pADO->Parameters->ParamByName("p20")->Value = AObj->ISTL_LCTN_ADDR;

			pADO->ExecSQL();

            return MergeFcltInfo(AObj, ADbConn);
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::MergeVms", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::MergeVms", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::MergeFcltInfo(TVmsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_FCLT_INFR L                                         \r\n"
           "USING (SELECT :p01 AS FCLT_ID,                                    \r\n"
           "              :p02 AS FCLT_TYPE,                                  \r\n"
           "              :p03 AS FCLT_LCTN                                   \r\n"
           "         FROM DUAL) M                                             \r\n"
           "        ON (L.FCLT_ID = M.FCLT_ID AND L.FCLT_TYPE = M.FCLT_TYPE)  \r\n"
           "WHEN MATCHED                                                      \r\n"
           "THEN                                                              \r\n"
           "   UPDATE SET L.FCLT_LCTN = M.FCLT_LCTN,                          \r\n"
           "              L.DEL_YN    = 'N'                                   \r\n"
           "WHEN NOT MATCHED                                                  \r\n"
           "THEN                                                              \r\n"
           "   INSERT (  FCLT_ID,   FCLT_TYPE,   FCLT_LCTN)                   \r\n"
           "   VALUES (M.FCLT_ID, M.FCLT_TYPE, M.FCLT_LCTN)                   \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->VMS_CTLR_ID;
			pADO->Parameters->ParamByName("p02")->Value = "VMS";
			pADO->Parameters->ParamByName("p03")->Value = AObj->VMS_NM;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::MergeFcltInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::MergeFcltInfo", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::GetNextVmsId(int &ANmbr, int &AId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(VMS_CTLR_NMBR), 10000) + 1 AS NEWID FROM TB_VMS_CTLR \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            ANmbr = pADO->FieldByName("NEWID")->AsInteger;
            AId = ANmbr / 10000;
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VmsCtlrManager::GetNextVmsId", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VmsCtlrManager::GetNextVmsId", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::UpdateCtlrInfo(TVmsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_VMS_CTLR            \r\n"
           "   SET PANL_ON_TIME   = :p01, \r\n"
           "       PANL_OFF_TIME  = :p02  \r\n"
           " WHERE DEL_YN = 'N'           \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Parameters->ParamByName("p01")->Value = AObj->PANL_ON_TIME;
			pADO->Parameters->ParamByName("p02")->Value = AObj->PANL_OFF_TIME;

			pADO->ExecSQL();
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::UpdateCtlrInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::UpdateCtlrInfo", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::UpdateIfscCngsSpeedInfo(int ACngsSpd, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_VMS_RLTN_IFSC         \r\n"
           "   SET CNGS_CNFM_SPED = :p01    \r\n";
    try
    {
        try
        {
            String sGradCd;
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Parameters->ParamByName("p01")->Value = ACngsSpd;
            pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TVmsCtlrManager::UpdateIfscCngsSpeedInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TVmsCtlrManager::UpdateIfscCngsSpeedInfo", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::LoadIfscRltn(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    FLists.Lock();
    try
    {
        FOR_STL(TVmsCtlr*, pObj, FLists)
        {
            pObj->FIfscRltn.RemoveAll();
        }
    }
    __finally
    {
        FLists.UnLock();
    }

#if 0
    sQry = "SELECT A.VMS_CTLR_NMBR, B.VMS_IFSC_ID, B.DSPL_PRRT, \r\n"
           "       B.CNGS_CNFM_YN, B.CNGS_CNFM_SPED             \r\n"
           "  FROM TB_VMS_CTLR      A,                          \r\n"
           "       TB_VMS_RLTN_IFSC B,                          \r\n"
           "       TB_VMS_IFSC      C                           \r\n"
           " WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR            \r\n"
           "   AND B.VMS_IFSC_ID   = C.VMS_IFSC_ID              \r\n"
           " ORDER BY A.VMS_CTLR_NMBR, B.DSPL_PRRT              \r\n";
#else
    //구미 컬럼 추가하지 않는 경우에는 alias 사용, 테이블 명칭 변경됨
    sQry = "SELECT A.VMS_CTLR_NMBR, B.VMS_IFSC_ID, B.DSPL_PRRT, \r\n"
           "       'N' AS CNGS_CNFM_YN, 5 AS CNGS_CNFM_SPED     \r\n"
           "  FROM TB_VMS_CTLR      A,                          \r\n"
           "       TB_VMS_IFSC_RLTN B,                          \r\n"
           "       TB_VMS_IFSC      C                           \r\n"
           " WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR            \r\n"
           "   AND B.VMS_IFSC_ID   = C.VMS_IFSC_ID              \r\n"
           " ORDER BY A.VMS_CTLR_NMBR, B.DSPL_PRRT              \r\n";
#endif

    FLists.Lock();
    try
    {
        String VMS_CTLR_NMBR;
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                VMS_CTLR_NMBR = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;
				TVmsCtlr *pVms = FLists.Find(String(VMS_CTLR_NMBR));
                if (!pVms) continue;

                TVmsIfscRltn *pObj = new TVmsIfscRltn();
                if (pObj)
                {
                    pObj->VMS_CTLR_NMBR = VMS_CTLR_NMBR;
                    pObj->VMS_IFSC_ID   = pADO->FieldByName("VMS_IFSC_ID")->AsInteger;
                    pObj->DSPL_PRRT     = pADO->FieldByName("DSPL_PRRT")->AsInteger;
                    pObj->CNGS_CNFM_YN  = pADO->FieldByName("CNGS_CNFM_YN")->AsString;
                    pObj->CNGS_CNFM_SPED= pADO->FieldByName("CNGS_CNFM_SPED")->AsInteger;

                    pVms->FIfscRltn.Push(pObj->DSPL_PRRT, pObj);
                }
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::LoadIfscRltn", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::LoadIfscRltn", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::SaveIfscRltn(TVmsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    int ii;

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();

sQry = "DELETE FROM TB_VMS_RLTN_IFSC WHERE VMS_CTLR_NMBR = :p01";
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->VMS_CTLR_NMBR;
			pADO->ExecSQL();

sQry = "INSERT INTO TB_VMS_RLTN_IFSC(VMS_CTLR_NMBR, VMS_IFSC_ID, DSPL_PRRT, CNGS_CNFM_YN, CNGS_CNFM_SPED) VALUES(:p01, :p02, :p03, :p04, :p05)";
            ii = 0;
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            FOR_STL(TVmsIfscRltn *, pLink, AObj->FIfscRltn)
            {
                pADO->Parameters->ParamByName("p01")->Value = AObj->VMS_CTLR_NMBR;
                pADO->Parameters->ParamByName("p02")->Value = pLink->VMS_IFSC_ID;
                pADO->Parameters->ParamByName("p03")->Value = String(ii+1);
                pADO->Parameters->ParamByName("p04")->Value = pLink->CNGS_CNFM_YN;
                pADO->Parameters->ParamByName("p05")->Value = pLink->CNGS_CNFM_SPED;
    			pADO->ExecSQL();
                ii++;
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::SaveIfscRltn", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::SaveIfscRltn", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::LoadVmsMsg(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*,                                                     \r\n"
           "       TO_CHAR(SYSDATE - 8/1440, 'YYYYMMDDHH24MISS') AS SVC_DT  \r\n"
           "  FROM TB_VMS_DSPL_PRST A,                                      \r\n"
           "       (SELECT VMS_CTLR_NMBR, MAX(DSPL_DT) AS DSPL_DT           \r\n"
           "          FROM TB_VMS_DSPL_PRST                                 \r\n"
           "         GROUP BY VMS_CTLR_NMBR) B                              \r\n"
           " WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR                        \r\n"
           "   AND A.DSPL_DT       = B.DSPL_DT                              \r\n"
           " ORDER BY A.VMS_CTLR_NMBR, A.PHASE                              \r\n";

    FLists.Lock();
    try
    {
        FOR_STL(TVmsCtlr*, pTmpObj, FLists)
        {
            pTmpObj->Lock();
            pTmpObj->VmsMsg->Init();
            pTmpObj->UnLock();
        }
    }
    __finally
    {
        FLists.UnLock();
    }

    TVmsCtlr *pObj = NULL;
    String    sOldVmsNmbr = "";
    int       nPhase = 0;
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String sVmsNmbr = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;
                if (sVmsNmbr != sOldVmsNmbr)
                {
                    if (pObj)
                    {
                        pObj->VmsMsg->Total = nPhase;
                        pObj->VmsMsg->Curr  = 0;
                        pObj->UnLock();
                    }
                    pObj = FLists.Find(sVmsNmbr);
                    if (pObj)
                    {
                        pObj->Lock();
                        nPhase = 0;
                        sOldVmsNmbr = sVmsNmbr;
                    }
                    else continue;
                }
                if (!pObj) continue;

                if (nPhase >= INT_VMS_MAX_FORM) continue;

                SAFE_DELETE(FImgStream);
                FImgStream = pADO->CreateBlobStream(pADO->FieldByName("VMS_DSPL_MSG_IMAG"), bmRead);
                if (FImgStream && FImgStream->Size > 0)
                {
                    try
                    {
                        if (!pObj->VmsMsg->PHASE[nPhase].pVmsBmp)
                        {
                            pObj->VmsMsg->PHASE[nPhase].pVmsBmp = new Graphics::TBitmap();
                        }
                        pObj->VmsMsg->PHASE[nPhase].pVmsBmp->FreeImage();
                        pObj->VmsMsg->PHASE[nPhase].pVmsBmp->LoadFromStream(FImgStream);

                        int    PHASE   = pADO->FieldByName("PHASE")->AsInteger;

                        pObj->VmsMsg->PHASE[nPhase].DSPL_HH = pADO->FieldByName("DSPL_HH")->AsInteger;
                        pObj->VmsMsg->PHASE[nPhase].DNLD_YN = pADO->FieldByName("DNLD_YN")->AsString;
                        pObj->VmsMsg->PHASE[nPhase].DSPL_CD = pADO->FieldByName("VMS_FORM_DSPL_MTHD_CD")->AsString;
                        pObj->VmsMsg->PHASE[nPhase].VMS_SCH_FORM_TYPE = pADO->FieldByName("VMS_SCH_FORM_TYPE")->AsInteger;

                        if (pObj->VmsMsg->PHASE[nPhase].VMS_SCH_FORM_TYPE == eSchTp_video  ||
                            pObj->VmsMsg->PHASE[nPhase].VMS_SCH_FORM_TYPE == eSchTp_stream )
                        {
                            pObj->VmsMsg->PHASE[nPhase].VMS_DSPL_MSG_TXT = pADO->FieldByName("VMS_DSPL_MSG_TXT")->AsString.Trim();
                        }

                        if (pObj->VmsMsg->PHASE[nPhase].DSPL_HH <=  0) pObj->VmsMsg->PHASE[nPhase].DSPL_HH = 3;
                        //if (pObj->VmsMsg->PHASE[nPhase].DSPL_HH >= 10) pObj->VmsMsg->PHASE[nPhase].DSPL_HH = 10;

                        String DSPL_DT = pADO->FieldByName("DSPL_DT")->AsString;
                        String SVC_DT  = pADO->FieldByName("SVC_DT")->AsString;

                        pObj->VmsMsg->OFFER_DT = DSPL_DT;
                        if (DSPL_DT >= SVC_DT)
                        {
                             pObj->VmsMsg->OFFER_YN = true;
                        }
                        else
                        {
                             pObj->VmsMsg->OFFER_YN = false;
                        }
                        if (pObj->VmsMsg->PHASE[nPhase].DNLD_YN == "N")
                        {
                            pObj->VmsMsg->OFFER_YN = false;
                        }

                        //TODO
                        if (pObj->STATE.Comm != vms_comm_normal)
                        {
                            pObj->VmsMsg->OFFER_YN = false;
                        }

                        nPhase++;
                    }
                    catch(Exception &e)
                    {
                    }
                }
            }

            if (pObj)
            {
                pObj->VmsMsg->Total = nPhase;
                pObj->VmsMsg->Curr  = 0;
                pObj->UnLock();
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::LoadVmsMsg", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::LoadVmsMsg", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::LoadVmsFormUsage(TADOConnection *ADbConn/*=NULL*/)
{
    InitFormCount();

    String sQry;
    TADOQuery *pADO = NULL;

#if 0
    sQry = "SELECT A.VMS_CTLR_NMBR,                                  \r\n"
           "       A.VMS_SCH_TYPE,                                   \r\n"
           "       A.VMS_SCH_FORM_TYPE,                              \r\n"
           "       A.DSPL_STRT_HH,                                   \r\n"
           "       A.DSPL_END_HH,                                    \r\n"
           "       TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') AS CURR_HH,  \r\n"
           "       A.USE_YN                                          \r\n"
           "  FROM TB_VMS_DSPL_SCH A,                                \r\n"
           "       TB_VMS_FORM     B                                 \r\n"
           " WHERE 1=1                                               \r\n"
//           "   AND B.VALID_YN    = 'Y'                               \r\n"
           "   AND A.VMS_FORM_ID = B.VMS_FORM_ID                     \r\n";
#else
    sQry = "SELECT A.VMS_CTLR_NMBR,                                  \r\n"
           "       A.TRFC_SITU_TYPE_CD AS VMS_SCH_TYPE,              \r\n"
           "       A.TRFC_SITU_TYPE_CD AS VMS_SCH_FORM_TYPE,         \r\n"
           "       A.DSPL_STRT_HH,                                   \r\n"
           "       A.DSPL_END_HH,                                    \r\n"
           "       TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') AS CURR_HH,  \r\n"
           "       'Y' AS USE_YN                                     \r\n"
           "  FROM TB_VMS_TRFC_INFR_DSPL_SCH A,                      \r\n"
           "       TB_VMS_FORM     B                                 \r\n"
           " WHERE 1=1                                               \r\n"
           "   AND A.VMS_FORM_ID = B.VMS_FORM_ID                     \r\n";
#endif

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLOpen(pADO);

            TVmsCtlr *pObj;
            int nSchType, nFormType;

            for( ; !pADO->Eof; pADO->Next())
            {

                String sVmsNmbr = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;

                pObj = FLists.Find(sVmsNmbr);
                if (!pObj) continue;

                String sSchType = pADO->FieldByName("VMS_SCH_TYPE")->AsString.Trim();
                if      (sSchType == "A") nSchType = enScheduleAuto;
                else if (sSchType == "F") nSchType = enScheduleFix;
                else if (sSchType == "B") nSchType = enScheduleBase;
                else continue;

                nFormType = pADO->FieldByName("VMS_SCH_FORM_TYPE")->AsInteger;
                if (nFormType < eSchTp_traffic && nFormType >= eSchTp_max) continue;

                pObj->FormCnt[nSchType].Total[nFormType]++;  //등록되어있는 전체 폼 숫자

                if (pADO->FieldByName("USE_YN")->AsString != 'Y') continue;

                //사용하는 것중에 현재시각이 제공시각안에 들어있는것은 카운트하지 않음.
                String sStrHH = pADO->FieldByName("DSPL_STRT_HH")->AsString;
                String sEndHH = pADO->FieldByName("DSPL_END_HH")->AsString;
                String sCurHH = pADO->FieldByName("CURR_HH")->AsString;
                if (sCurHH >= sStrHH && sCurHH <= sEndHH)
                {
                    pObj->FormCnt[nSchType].Use[nFormType]++;  //사용가능 폼 숫자
                }
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::LoadVmsFormUsage", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::LoadVmsFormUsage", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool TVmsCtlrManager::InsVmsControlHs(String ACtlDt, String AVmsNmbr, String ACtlType, String ACtlVal, String ACtlRes, String AUserId, TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_VMS_CTRL_HS(                \r\n"
           "                          CTRL_DT,         \r\n"
           "                          VMS_CTLR_NMBR,   \r\n"
           "                          CTRL_TYPE,       \r\n"
           "                          CTRL_VAL,        \r\n"
           "                          CTRL_RESULT,     \r\n"
           "                          USER_ID          \r\n"
           "                         )                 \r\n"
           "                   VALUES(                 \r\n"
           "                          :p01,            \r\n"
           "                          :p02,            \r\n"
           "                          :p03,            \r\n"
           "                          :p04,            \r\n"
           "                          :p05,            \r\n"
           "                          :p06             \r\n"
           "                         )                 \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Parameters->ParamByName("p01")->Value = ACtlDt;
            pADO->Parameters->ParamByName("p02")->Value = AVmsNmbr;
            pADO->Parameters->ParamByName("p03")->Value = ACtlType;
            pADO->Parameters->ParamByName("p04")->Value = ACtlVal;
            pADO->Parameters->ParamByName("p05")->Value = ACtlRes;
            pADO->Parameters->ParamByName("p06")->Value = AUserId;
            pADO->ExecSQL();
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVmsCtlrManager::InsVmsControlHs", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVmsCtlrManager::InsVmsControlHs", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------
int VmsGetFormTypeIdx(int AFormType)
{
    int nFormTypeIdx = -1;
    switch(AFormType)
    {
    case eFormTp_traf_1:    //	소통상황(1단)
    case eFormTp_traf_2:    //	소통상황(2단)
    case eFormTp_traf_3:    //	소통상황(3단)
    case eFormTp_traf_4:    //	소통상황(4단)
    case eFormtp_figure:    //	소통상황(도형식배경소통정보)
        nFormTypeIdx = eSchTp_traffic;
        break;
    case eFormTp_congest:    //	정체소통상황
        nFormTypeIdx = eSchTp_congest;
        break;
    case eFormTp_incident:    //	돌발문안
        nFormTypeIdx = eSchTp_incident;
        break;
    case eFormTp_gongsa:    //	공사/행사문안
        nFormTypeIdx = eSchTp_gongsa;
        break;
    case eFormTp_hongbo:    //	홍보문안
        nFormTypeIdx = eSchTp_hongbo;
        break;
    case eFormTp_deture:    //	우회도로
        nFormTypeIdx = eSchTp_deture;
        break;
    case eFormTp_safe:    //	재난안전
        nFormTypeIdx = eSchTp_safe;
        break;
    case eFormTp_video:    //	동영상
        nFormTypeIdx = eSchTp_video;
        break;
    case eFormTp_stream:    //	스트리밍영상
        nFormTypeIdx = eSchTp_stream;
        break;
    case eFormTp_atmp:    //	대기환경
        nFormTypeIdx = eSchTp_atmp;
        break;
    case eFormTp_park:    //	주차정보
        nFormTypeIdx = eSchTp_park;
        break;
    default: break;
    }
    return nFormTypeIdx;
}

