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

#pragma hdrstop

#include "CDSVdsCtlrF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)


TVdsDtctLink::TVdsDtctLink()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
}
//---------------------------------------------------------------------------
TVdsDtctLink::~TVdsDtctLink()
{
}
//---------------------------------------------------------------------------

TVdsDtct::TVdsDtct()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
TVdsDtct::~TVdsDtct()
{
}
//---------------------------------------------------------------------------

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

//---------------------------------------------------------------------------
TVdsCtlrManager *VdsCtlrManager = NULL;
//---------------------------------------------------------------------------

/*
* Vds Manager
*/
TVdsCtlrManager::TVdsCtlrManager()
{
}
//---------------------------------------------------------------------------
TVdsCtlrManager::~TVdsCtlrManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TVdsCtlrManager::LoadFromDbVdsLink(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.LINK_ID, C.NODE_NAME AS STR_NAME, D.NODE_NAME AS END_NAME,         \r\n"
           "       F.VDS_CTLR_NMBR, F.VDS_NM                                            \r\n"
           "  FROM TB_VDS_DTCT_RLTN_LINK A,                                             \r\n"
           "       TB_LINK               B,                                             \r\n"
           "       TB_NODE               C,                                             \r\n"
           "       TB_NODE               D,                                             \r\n"
           "       TB_VDS_DTCT           E,                                             \r\n"
           "       TB_VDS_CTLR           F                                              \r\n"
           " WHERE A.LINK_ID   = B.LINK_ID                                              \r\n"
           "   AND B.F_NODE_ID = C.NODE_ID                                              \r\n"
           "   AND B.T_NODE_ID = D.NODE_ID                                              \r\n"
           "   AND A.VDS_DTCT_NMBR = E.VDS_DTCT_NMBR                                    \r\n"
           "   AND E.VDS_CTLR_NMBR = F.VDS_CTLR_NMBR                                    \r\n"
           "   AND F.VDS_TYPE_CD   = 'I'                                                \r\n"
           " GROUP BY A.LINK_ID, C.NODE_NAME, D.NODE_NAME, F.VDS_CTLR_NMBR, F.VDS_NM    \r\n";
//           " ORDER BY F.VDS_CTLR_NMBR, A.LINK_ID                                        \r\n";
    try
    {
        FLinkSelCnt = 0;
        FLinkLists.Lock();
        FLinkLists.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())
            {
				TVdsLink *pObj = new TVdsLink();

                pObj->VDS_CTLR_NMBR = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                pObj->VDS_NM        = pADO->FieldByName("VDS_NM")->AsString;
                pObj->LINK_ID       = pADO->FieldByName("LINK_ID")->AsString;
                pObj->STR_NAME      = pADO->FieldByName("STR_NAME")->AsString;
                pObj->END_NAME      = pADO->FieldByName("END_NAME")->AsString;

                pObj->IsSelected = true;
                FLinkSelCnt++;
				FLinkLists.Push(pObj->LINK_ID, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAtrdManager::LoadFromDbAtrdName", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsAtrdManager::LoadFromDbAtrdName", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLinkLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------
bool TVdsCtlrManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT *            \r\n"
           "  FROM TB_VDS_CTLR  \r\n"
           " WHERE 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())
            {
				TVdsCtlr *pObj = new TVdsCtlr();

                pObj->VDS_CTLR_NMBR     = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                pObj->VDS_CTLR_ID       = pADO->FieldByName("VDS_CTLR_ID")->AsString;
                pObj->VDS_NM            = pADO->FieldByName("VDS_NM")->AsString;
                pObj->VDS_CTLR_IP       = pADO->FieldByName("VDS_CTLR_IP")->AsString;
                pObj->VDS_CTLR_PORT     = pADO->FieldByName("VDS_CTLR_PORT")->AsInteger;
                pObj->VDS_CTLR_LOCAL_NO = pADO->FieldByName("VDS_CTLR_LOCAL_NO")->AsInteger;
                pObj->VDS_TYPE_CD       = pADO->FieldByName("VDS_TYPE_CD")->AsString;
                pObj->ISTL_LCTN_NM      = pADO->FieldByName("ISTL_LCTN_NM")->AsString;
                pObj->X_CRDN            = pADO->FieldByName("X_CRDN")->AsFloat;
                pObj->Y_CRDN            = pADO->FieldByName("Y_CRDN")->AsFloat;
                pObj->DEL_YN            = pADO->FieldByName("DEL_YN")->AsString;

                pObj->GROUP_NO          = pADO->FieldByName("GROUP_NO")->AsInteger;
                pObj->FAN_MODE          = pADO->FieldByName("FAN_MODE")->AsInteger;
                pObj->FAN_RUN_TMPR      = pADO->FieldByName("FAN_RUN_TMPR")->AsInteger;
                pObj->HETR_RUN_TMPR     = pADO->FieldByName("HETR_RUN_TMPR")->AsInteger;
                pObj->DETECT_LANES      = pADO->FieldByName("DETECT_LANES")->AsInteger;
                pObj->TRAF_CLCT_CYCL    = pADO->FieldByName("TRAF_CLCT_CYCL")->AsInteger;
                pObj->STTS_CLCT_CYCL    = pADO->FieldByName("STTS_CLCT_CYCL")->AsInteger;
                pObj->ISTL_LCTN_ADDR    = pADO->FieldByName("ISTL_LCTN_ADDR")->AsString;

                pObj->MEM_DEL           = "N";

				FLists.Push(pObj->VDS_CTLR_NMBR, pObj);

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::LoadFromDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return LoadFromCmraDb(ADbConn);
}
//---------------------------------------------------------------------------

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

    sQry = "SELECT *            \r\n"
           "  FROM TB_VDS_CMRA  \r\n"
           " WHERE DEL_YN = 'N' \r\n";

    TVdsCtlr *pVds;
    FLists.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())
            {
                String VDS_CTLR_NMBR = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                pVds = FLists.Find(VDS_CTLR_NMBR);
                if (!pVds) continue;

				TVdsCmra *pObj = new TVdsCmra();

                pObj->VDS_CTLR_NMBR  = VDS_CTLR_NMBR;
                pObj->VDS_CMRA_NO    = pADO->FieldByName("VDS_CMRA_NO")->AsInteger;
                pObj->VDS_CMRA_NM    = pADO->FieldByName("VDS_CMRA_NM")->AsString.Trim();
                pObj->DETECT_LANE    = pADO->FieldByName("DETECT_LANE")->AsInteger;
                pObj->CMRA_DRCT_CD   = pADO->FieldByName("CMRA_DRCT_CD")->AsString.Trim();
                pObj->DEL_YN         = pADO->FieldByName("DEL_YN")->AsString.Trim();
                pObj->CCAM_CTLR_NMBR = pADO->FieldByName("CCAM_CTLR_NMBR")->AsString.Trim();
                pObj->ROAD_NAME      = pADO->FieldByName("ROAD_NAME")->AsString.Trim();
                pObj->CROSS_NAME     = pADO->FieldByName("CROSS_NAME")->AsString.Trim();
                pObj->DIR_NAME       = pADO->FieldByName("DIR_NAME")->AsString.Trim();
                pObj->VDS_CMRA_IP    = pADO->FieldByName("VDS_CMRA_IP")->AsString.Trim();

				pVds->FCmras.Push(pObj->VDS_CMRA_NO, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::LoadFromCmraDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::LoadFromCmraDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

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

    sQry = "SELECT *            \r\n"
           "  FROM TB_VDS_DTCT  \r\n";

    TVdsCtlr *pVds;
    FLists.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())
            {
                String VDS_CTLR_NMBR = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                pVds = FLists.Find(VDS_CTLR_NMBR);
                if (!pVds) continue;

				TVdsDtct *pObj = new TVdsDtct();

                pObj->VDS_DTCT_NMBR = pADO->FieldByName("VDS_DTCT_NMBR")->AsString;
                pObj->VDS_DTCT_ID   = pADO->FieldByName("VDS_DTCT_ID")->AsString;
                pObj->VDS_DTCT_NM   = pADO->FieldByName("VDS_DTCT_NM")->AsString;
                pObj->VDS_CTLR_NMBR = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                pObj->VDS_CMRA_NO   = pADO->FieldByName("VDS_CMRA_NO")->AsInteger;
                pObj->DTCT_TYPE     = pADO->FieldByName("DTCT_TYPE")->AsString;
                pObj->DTCT_DIR      = pADO->FieldByName("DTCT_DIR")->AsString;
                pObj->DETECT_LANE   = pADO->FieldByName("DETECT_LANE")->AsInteger;
                pObj->ISTL_LANE     = pADO->FieldByName("ISTL_LANE")->AsInteger;
                pObj->SPED_CLCT_YN  = pADO->FieldByName("SPED_CLCT_YN")->AsString;
                pObj->TFVL_CLCT_YN  = pADO->FieldByName("TFVL_CLCT_YN")->AsString;
                pObj->OCPY_CLCT_YN  = pADO->FieldByName("OCPY_CLCT_YN")->AsString;
                pObj->MAX_TFVL      = pADO->FieldByName("MAX_TFVL")->AsInteger;
                pObj->MIN_TFVL      = pADO->FieldByName("MIN_TFVL")->AsInteger;
                pObj->MAX_OCPY_RATE = pADO->FieldByName("MAX_OCPY_RATE")->AsFloat;
                pObj->MIN_OCPY_RATE = pADO->FieldByName("MIN_OCPY_RATE")->AsFloat;
                pObj->MAX_SPED      = pADO->FieldByName("MAX_SPED")->AsInteger;
                pObj->MIN_SPED      = pADO->FieldByName("MIN_SPED")->AsInteger;
                pObj->USE_YN        = pADO->FieldByName("USE_YN")->AsString;
                pObj->DEL_YN        = pADO->FieldByName("DEL_YN")->AsString;

				pVds->FDtcts.Push(pObj->VDS_DTCT_NMBR, pObj);

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::LoadFromDtctDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::LoadFromDtctDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

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

    sQry = "SELECT A.VDS_CTLR_NMBR,                  \r\n"
           "       A.VDS_DTCT_NMBR,                  \r\n"
           "       B.LINK_ID                         \r\n"
           "  FROM TB_VDS_DTCT           A,          \r\n"
           "       TB_VDS_DTCT_RLTN_LINK B           \r\n"
           " WHERE A.VDS_DTCT_NMBR = B.VDS_DTCT_NMBR \r\n"
           " ORDER BY VDS_CTLR_NMBR                  \r\n";

    TVdsCtlr *pVds;
    TVdsDtct *pVdsDtct;

    FLists.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())
            {
                String VDS_CTLR_NMBR = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                pVds = FLists.Find(VDS_CTLR_NMBR);
                if (!pVds) continue;

                String VDS_DTCT_NMBR = pADO->FieldByName("VDS_DTCT_NMBR")->AsString;
                pVdsDtct = pVds->FDtcts.Find(VDS_DTCT_NMBR);
                if (!pVdsDtct) continue;

                TVdsDtctLink *pObj = new TVdsDtctLink();

                pObj->VDS_CTLR_NMBR = VDS_CTLR_NMBR;
                pObj->VDS_DTCT_NMBR = VDS_DTCT_NMBR;
                pObj->LINK_ID       = pADO->FieldByName("LINK_ID")->AsString;

				pVdsDtct->FLinks.Push(pObj->LINK_ID, pObj);

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::LoadFromDtctLinkDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::LoadFromDtctLinkDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

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

    sQry = "SELECT *                                                        \r\n"
           "  FROM TB_VDS_CTLR_STTS A, TB_VDS_CTLR B                        \r\n"
           " WHERE A.VDS_CTLR_NMBR = B.VDS_CTLR_NMBR                        \r\n"
           "   AND A.UPDT_DT >= TO_CHAR(SYSDATE-2/1440, 'YYYYMMDDHH24MISS') \r\n";

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

        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();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
                String sVmsNmbr = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                pObj = FLists.Find(sVmsNmbr);
                if (!pObj) continue;

                String UPDT_DT      = pADO->FieldByName("UPDT_DT")->AsString;
                String CONN_STTS_CD = pADO->FieldByName("CMNC_STTS_CD")->AsString;
                if (CONN_STTS_CD != "CMS0")
                {
                    //통신장애
                    continue;
                }
#if 0
                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;
#endif
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVdsCtlrManager::LoadStatusFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVdsCtlrManager::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 TVdsCtlrManager::GetNextVdsId(int &ANmbr, int &AId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(VDS_CTLR_NMBR), 10000) + 1 AS NEWID FROM TB_VDS_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)
        {
            DBERRORLOG("TVdsCtlrManager::GetNextVdsId", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TVdsCtlrManager::GetNextVdsId", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

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

    sQry = "SELECT NVL(MAX(VDS_DTCT_NMBR), 10000) + 1 AS NEWID FROM TB_VDS_DTCT \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)
        {
            DBERRORLOG("TVdsCtlrManager::GetNextVdsId", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TVdsCtlrManager::GetNextVdsId", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TVdsCtlrManager::DeleteVdsDtctSubTable(TVdsDtct *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

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

        sQry = "DELETE TB_VDS_DTCT_RLTN_LINK \r\n"
               " WHERE VDS_DTCT_NMBR = :p01  \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->VDS_DTCT_NMBR;
			pADO->ExecSQL();
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::DeleteVdsDtctSubTable", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::DeleteVdsDtctSubTable", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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


bool TVdsCtlrManager::DeleteVdsDtct(TVdsDtct *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_VDS_DTCT          \r\n"
           "   SET DEL_YN = 'Y'         \r\n"
           " WHERE VDS_DTCT_NMBR = :p01 \r\n";
    try
    {
        DeleteVdsDtctSubTable(AObj, ADbConn);
        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->VDS_DTCT_NMBR;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::DeleteVdsDtct", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::DeleteVdsDtct", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TVdsCtlrManager::SaveVdsDtct(TVdsDtct *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_VDS_DTCT L                            \r\n"
           "USING (SELECT :p01 AS VDS_DTCT_NMBR,                \r\n"
           "              :p02 AS VDS_DTCT_ID,                  \r\n"
           "              :p03 AS VDS_DTCT_NM,                  \r\n"
           "              :p04 AS VDS_CTLR_NMBR,                \r\n"
           "              :p05 AS VDS_CMRA_NO,                  \r\n"
           "              :p06 AS DTCT_TYPE,                    \r\n"
           "              :p07 AS DTCT_DIR,                     \r\n"
           "              :p08 AS DETECT_LANE,                  \r\n"
           "              :p09 AS ISTL_LANE,                    \r\n"
           "              :p10 AS SPED_CLCT_YN,                 \r\n"
           "              :p11 AS TFVL_CLCT_YN,                 \r\n"
           "              :p12 AS OCPY_CLCT_YN,                 \r\n"
           "              :p13 AS MAX_TFVL,                     \r\n"
           "              :p14 AS MIN_TFVL,                     \r\n"
           "              :p15 AS MAX_OCPY_RATE,                \r\n"
           "              :p16 AS MIN_OCPY_RATE,                \r\n"
           "              :p17 AS MAX_SPED,                     \r\n"
           "              :p18 AS MIN_SPED,                     \r\n"
           "              :p19 AS USE_YN,                       \r\n"
           "              :p20 AS DEL_YN                        \r\n"
           "         FROM DUAL) M                               \r\n"
           "        ON (L.VDS_DTCT_NMBR = M.VDS_DTCT_NMBR)      \r\n"
           "WHEN MATCHED THEN                                   \r\n"
           "   UPDATE SET L.VDS_DTCT_ID     = M.VDS_DTCT_ID,    \r\n"
           "              L.VDS_DTCT_NM     = M.VDS_DTCT_NM,    \r\n"
           "              L.VDS_CTLR_NMBR   = M.VDS_CTLR_NMBR,  \r\n"
           "              L.VDS_CMRA_NO     = M.VDS_CMRA_NO,    \r\n"
           "              L.DTCT_TYPE       = M.DTCT_TYPE,      \r\n"
           "              L.DTCT_DIR        = M.DTCT_DIR,       \r\n"
           "              L.DETECT_LANE     = M.DETECT_LANE,    \r\n"
           "              L.ISTL_LANE       = M.ISTL_LANE,      \r\n"
           "              L.SPED_CLCT_YN    = M.SPED_CLCT_YN,   \r\n"
           "              L.TFVL_CLCT_YN    = M.TFVL_CLCT_YN,   \r\n"
           "              L.OCPY_CLCT_YN    = M.OCPY_CLCT_YN,   \r\n"
           "              L.MAX_TFVL        = M.MAX_TFVL,       \r\n"
           "              L.MIN_TFVL        = M.MIN_TFVL,       \r\n"
           "              L.MAX_OCPY_RATE   = M.MAX_OCPY_RATE,  \r\n"
           "              L.MIN_OCPY_RATE   = M.MIN_OCPY_RATE,  \r\n"
           "              L.MAX_SPED        = M.MAX_SPED,       \r\n"
           "              L.MIN_SPED        = M.MIN_SPED,       \r\n"
           "              L.USE_YN          = M.USE_YN,         \r\n"
           "              L.DEL_YN          = M.DEL_YN          \r\n"
           "WHEN NOT MATCHED THEN                               \r\n"
           "   INSERT (VDS_DTCT_NMBR,                           \r\n"
           "           VDS_DTCT_ID,                             \r\n"
           "           VDS_DTCT_NM,                             \r\n"
           "           VDS_CTLR_NMBR,                           \r\n"
           "           VDS_CMRA_NO,                             \r\n"
           "           DTCT_TYPE,                               \r\n"
           "           DTCT_DIR,                                \r\n"
           "           DETECT_LANE,                             \r\n"
           "           ISTL_LANE,                               \r\n"
           "           SPED_CLCT_YN,                            \r\n"
           "           TFVL_CLCT_YN,                            \r\n"
           "           OCPY_CLCT_YN,                            \r\n"
           "           MAX_TFVL,                                \r\n"
           "           MIN_TFVL,                                \r\n"
           "           MAX_OCPY_RATE,                           \r\n"
           "           MIN_OCPY_RATE,                           \r\n"
           "           MAX_SPED,                                \r\n"
           "           MIN_SPED,                                \r\n"
           "           USE_YN,                                  \r\n"
           "           DEL_YN )                                 \r\n"
           "   VALUES (M.VDS_DTCT_NMBR,                         \r\n"
           "           M.VDS_DTCT_ID,                           \r\n"
           "           M.VDS_DTCT_NM,                           \r\n"
           "           M.VDS_CTLR_NMBR,                         \r\n"
           "           M.VDS_CMRA_NO,                           \r\n"
           "           M.DTCT_TYPE,                             \r\n"
           "           M.DTCT_DIR,                              \r\n"
           "           M.DETECT_LANE,                           \r\n"
           "           M.ISTL_LANE,                             \r\n"
           "           M.SPED_CLCT_YN,                          \r\n"
           "           M.TFVL_CLCT_YN,                          \r\n"
           "           M.OCPY_CLCT_YN,                          \r\n"
           "           M.MAX_TFVL,                              \r\n"
           "           M.MIN_TFVL,                              \r\n"
           "           M.MAX_OCPY_RATE,                         \r\n"
           "           M.MIN_OCPY_RATE,                         \r\n"
           "           M.MAX_SPED,                              \r\n"
           "           M.MIN_SPED,                              \r\n"
           "           M.USE_YN,                                \r\n"
           "           M.DEL_YN )                               \r\n";

    try
    {
        //관련정보를 모두 삭제
        DeleteVdsDtctSubTable(AObj, ADbConn);
        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->VDS_DTCT_NMBR;
            pADO->Parameters->ParamByName("p02")->Value = AObj->VDS_DTCT_ID;
            pADO->Parameters->ParamByName("p03")->Value = AObj->VDS_DTCT_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->VDS_CTLR_NMBR;
            pADO->Parameters->ParamByName("p05")->Value = AObj->VDS_CMRA_NO;
            pADO->Parameters->ParamByName("p06")->Value = AObj->DTCT_TYPE;
            pADO->Parameters->ParamByName("p07")->Value = AObj->DTCT_DIR;
            pADO->Parameters->ParamByName("p08")->Value = AObj->DETECT_LANE;
            pADO->Parameters->ParamByName("p09")->Value = AObj->ISTL_LANE;
            pADO->Parameters->ParamByName("p10")->Value = AObj->SPED_CLCT_YN;
            pADO->Parameters->ParamByName("p11")->Value = AObj->TFVL_CLCT_YN;
            pADO->Parameters->ParamByName("p12")->Value = AObj->OCPY_CLCT_YN;
            pADO->Parameters->ParamByName("p13")->Value = AObj->MAX_TFVL;
            pADO->Parameters->ParamByName("p14")->Value = AObj->MIN_TFVL;
            pADO->Parameters->ParamByName("p15")->Value = String(AObj->MAX_OCPY_RATE);
            pADO->Parameters->ParamByName("p16")->Value = String(AObj->MIN_OCPY_RATE);
            pADO->Parameters->ParamByName("p17")->Value = AObj->MAX_SPED;
            pADO->Parameters->ParamByName("p18")->Value = AObj->MIN_SPED;
            pADO->Parameters->ParamByName("p19")->Value = AObj->USE_YN;
            pADO->Parameters->ParamByName("p20")->Value = AObj->DEL_YN;

			pADO->ExecSQL();

    //링크구성정보 입력
    sQry = "INSERT INTO TB_VDS_DTCT_RLTN_LINK (VDS_DTCT_NMBR, LINK_ID)  \r\n"
           "                           VALUES (:p01,          :p02   )  \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            FOR_STL(TVdsDtctLink *, pObj, AObj->FLinks)
            {
                pADO->Parameters->ParamByName("p01")->Value = AObj->VDS_DTCT_NMBR;
                pADO->Parameters->ParamByName("p02")->Value = pObj->LINK_ID;
                pADO->ExecSQL();
            }

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::SaveVdsDtct", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::SaveVdsDtct", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TVdsCtlrManager::DeleteVds(TVdsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_VDS_CTLR           \r\n"
           "   SET DEL_YN = 'Y'          \r\n"
           " WHERE VDS_CTLR_NMBR = :p01  \r\n";
    try
    {
        FLists.Lock();
        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->VDS_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->VDS_CTLR_ID;
			pADO->ExecSQL();

            DeleteVdsCmra(AObj, ADbConn);

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::DeleteVds", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::DeleteVds", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

bool TVdsCtlrManager::UpdateVds(TVdsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    return MergeVds(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TVdsCtlrManager::MergeVds(TVdsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_VDS_CTLR L                   \r\n"
           "USING (SELECT :p01 AS VDS_CTLR_NMBR,       \r\n"
           "              :p02 AS VDS_CTLR_ID,         \r\n"
           "              :p03 AS VDS_NM,              \r\n"
           "              :p04 AS VDS_CTLR_IP,         \r\n"
           "              :p05 AS VDS_CTLR_PORT,       \r\n"
           "              :p06 AS VDS_CTLR_LOCAL_NO,   \r\n"
           "              :p07 AS VDS_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 GROUP_NO,            \r\n"
           "              :p13 AS FAN_MODE,            \r\n"
           "              :p14 AS FAN_RUN_TMPR,        \r\n"
           "              :p15 AS HETR_MODE,           \r\n"
           "              :p16 AS HETR_RUN_TMPR,       \r\n"
           "              :p17 AS DETECT_LANES,        \r\n"
           "              :p18 AS TRAF_CLCT_CYCL,      \r\n"
           "              :p19 AS STTS_CLCT_CYCL,      \r\n"
           "              :p20 AS ISTL_LCTN_ADDR       \r\n"
           "         FROM DUAL) M                                     \r\n"
           "        ON (L.VDS_CTLR_NMBR = M.VDS_CTLR_NMBR)            \r\n"
           "WHEN MATCHED THEN                                         \r\n"
           "   UPDATE SET L.VDS_CTLR_ID       = M.VDS_CTLR_ID,        \r\n"
           "              L.VDS_NM            = M.VDS_NM,             \r\n"
           "              L.VDS_CTLR_IP       = M.VDS_CTLR_IP,        \r\n"
           "              L.VDS_CTLR_PORT     = M.VDS_CTLR_PORT,      \r\n"
           "              L.VDS_CTLR_LOCAL_NO = M.VDS_CTLR_LOCAL_NO,  \r\n"
           "              L.VDS_TYPE_CD       = M.VDS_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.GROUP_NO          = M.GROUP_NO,           \r\n"
           "              L.FAN_MODE          = M.FAN_MODE,           \r\n"
           "              L.FAN_RUN_TMPR      = M.FAN_RUN_TMPR,       \r\n"
           "              L.HETR_MODE         = M.HETR_MODE,          \r\n"
           "              L.HETR_RUN_TMPR     = M.HETR_RUN_TMPR,      \r\n"
           "              L.DETECT_LANES      = M.DETECT_LANES,       \r\n"
           "              L.TRAF_CLCT_CYCL    = M.TRAF_CLCT_CYCL,     \r\n"
           "              L.STTS_CLCT_CYCL    = M.STTS_CLCT_CYCL,     \r\n"
           "              L.ISTL_LCTN_ADDR    = M.ISTL_LCTN_ADDR      \r\n"
           "WHEN NOT MATCHED THEN                                     \r\n"
           "   INSERT (                     \r\n"
           "           VDS_CTLR_NMBR,       \r\n"
           "           VDS_CTLR_ID,         \r\n"
           "           VDS_NM,              \r\n"
           "           VDS_CTLR_IP,         \r\n"
           "           VDS_CTLR_PORT,       \r\n"
           "           VDS_CTLR_LOCAL_NO,   \r\n"
           "           VDS_TYPE_CD,         \r\n"
           "           ISTL_LCTN_NM,        \r\n"
           "           X_CRDN,              \r\n"
           "           Y_CRDN,              \r\n"
           "           DEL_YN,              \r\n"
           "           GROUP_NO,            \r\n"
           "           FAN_MODE,            \r\n"
           "           FAN_RUN_TMPR,        \r\n"
           "           HETR_MODE,           \r\n"
           "           HETR_RUN_TMPR,       \r\n"
           "           DETECT_LANES,        \r\n"
           "           TRAF_CLCT_CYCL,      \r\n"
           "           STTS_CLCT_CYCL,      \r\n"
           "           ISTL_LCTN_ADDR       \r\n"
           "          )                     \r\n"
           "   VALUES (                     \r\n"
           "           M.VDS_CTLR_NMBR,     \r\n"
           "           M.VDS_CTLR_ID,       \r\n"
           "           M.VDS_NM,            \r\n"
           "           M.VDS_CTLR_IP,       \r\n"
           "           M.VDS_CTLR_PORT,     \r\n"
           "           M.VDS_CTLR_LOCAL_NO, \r\n"
           "           M.VDS_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.GROUP_NO,          \r\n"
           "           M.FAN_MODE,          \r\n"
           "           M.FAN_RUN_TMPR,      \r\n"
           "           M.HETR_MODE,         \r\n"
           "           M.HETR_RUN_TMPR,     \r\n"
           "           M.DETECT_LANES,      \r\n"
           "           M.TRAF_CLCT_CYCL,    \r\n"
           "           M.STTS_CLCT_CYCL,    \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;

            AObj->FAN_MODE      = 2;
            AObj->FAN_RUN_TMPR  = 30;
            AObj->HETR_MODE     = 2;
            AObj->HETR_RUN_TMPR = 0;

            pADO->Parameters->ParamByName("p01")->Value = AObj->VDS_CTLR_NMBR;
            pADO->Parameters->ParamByName("p02")->Value = AObj->VDS_CTLR_ID;
            pADO->Parameters->ParamByName("p03")->Value = AObj->VDS_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->VDS_CTLR_IP;
            pADO->Parameters->ParamByName("p05")->Value = AObj->VDS_CTLR_PORT;
            pADO->Parameters->ParamByName("p06")->Value = AObj->VDS_CTLR_LOCAL_NO;
            pADO->Parameters->ParamByName("p07")->Value = AObj->VDS_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->GROUP_NO;
            pADO->Parameters->ParamByName("p13")->Value = AObj->FAN_MODE;
            pADO->Parameters->ParamByName("p14")->Value = AObj->FAN_RUN_TMPR;
            pADO->Parameters->ParamByName("p15")->Value = AObj->HETR_MODE;
            pADO->Parameters->ParamByName("p16")->Value = AObj->HETR_RUN_TMPR;
            pADO->Parameters->ParamByName("p17")->Value = AObj->DETECT_LANES;
            pADO->Parameters->ParamByName("p18")->Value = AObj->TRAF_CLCT_CYCL;
            pADO->Parameters->ParamByName("p19")->Value = AObj->STTS_CLCT_CYCL;
            pADO->Parameters->ParamByName("p20")->Value = AObj->ISTL_LCTN_ADDR;

			pADO->ExecSQL();

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

    return InsertVdsCmra(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TVdsCtlrManager::InsertVds(TVdsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_VDS_CTLR (       \r\n"
           "            VDS_CTLR_NMBR     , \r\n"
           "            VDS_CTLR_ID       , \r\n"
           "            VDS_NM            , \r\n"
           "            VDS_CTLR_IP       , \r\n"
           "            VDS_CTLR_PORT     , \r\n"
           "            VDS_CTLR_LOCAL_NO , \r\n"
           "            VDS_TYPE_CD       , \r\n"
           "            ISTL_LCTN_NM      , \r\n"
           "            X_CRDN            , \r\n"
           "            Y_CRDN            , \r\n"
           "            DEL_YN            , \r\n"
           "            GROUP_NO          , \r\n"
           "            FAN_MODE          , \r\n"
           "            FAN_RUN_TMPR      , \r\n"
           "            HETR_MODE         , \r\n"
           "            HETR_RUN_TMPR     , \r\n"
           "            DETECT_LANES      , \r\n"
           "            TRAF_CLCT_CYCL    , \r\n"
           "            STTS_CLCT_CYCL    , \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";

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

            AObj->FAN_MODE      = 2;
            AObj->FAN_RUN_TMPR  = 30;
            AObj->HETR_MODE     = 2;
            AObj->HETR_RUN_TMPR = 0;

            pADO->Parameters->ParamByName("p01")->Value = AObj->VDS_CTLR_NMBR;
            pADO->Parameters->ParamByName("p02")->Value = AObj->VDS_CTLR_ID;
            pADO->Parameters->ParamByName("p03")->Value = AObj->VDS_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->VDS_CTLR_IP;
            pADO->Parameters->ParamByName("p05")->Value = AObj->VDS_CTLR_PORT;
            pADO->Parameters->ParamByName("p06")->Value = AObj->VDS_CTLR_LOCAL_NO;
            pADO->Parameters->ParamByName("p07")->Value = AObj->VDS_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->GROUP_NO;
            pADO->Parameters->ParamByName("p13")->Value = AObj->FAN_MODE;
            pADO->Parameters->ParamByName("p14")->Value = AObj->FAN_RUN_TMPR;
            pADO->Parameters->ParamByName("p15")->Value = AObj->HETR_MODE;
            pADO->Parameters->ParamByName("p16")->Value = AObj->HETR_RUN_TMPR;
            pADO->Parameters->ParamByName("p17")->Value = AObj->DETECT_LANES;
            pADO->Parameters->ParamByName("p18")->Value = AObj->TRAF_CLCT_CYCL;
            pADO->Parameters->ParamByName("p19")->Value = AObj->STTS_CLCT_CYCL;
            pADO->Parameters->ParamByName("p20")->Value = AObj->ISTL_LCTN_ADDR;

			pADO->ExecSQL();

            MergeFcltInfo(AObj, ADbConn);
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::InsertVds", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::InsertVds", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return InsertVdsCmra(AObj, ADbConn);
}
//---------------------------------------------------------------------------
bool TVdsCtlrManager::MergeFcltInfo(TVdsCtlr *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->VDS_CTLR_ID;
			pADO->Parameters->ParamByName("p02")->Value = "VDS";
			pADO->Parameters->ParamByName("p03")->Value = AObj->VDS_NM;
			pADO->ExecSQL();

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

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

bool TVdsCtlrManager::DeleteVdsCmra(TVdsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "DELETE TB_VDS_CMRA          \r\n"
           " WHERE VDS_CTLR_NMBR = :p01 \r\n";

    FLists.Lock();
    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->VDS_CTLR_NMBR;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::DeleteVdsCmra", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::DeleteVdsCmra", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------
bool TVdsCtlrManager::InsertVdsCmra(TVdsCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_VDS_CMRA(VDS_CTLR_NMBR, VDS_CMRA_NO, VDS_CMRA_NM, DETECT_LANE, CMRA_DRCT_CD, DEL_YN, CCAM_CTLR_NMBR, ROAD_NAME, CROSS_NAME, DIR_NAME, VDS_CMRA_IP) \r\n"
           "                 VALUES(:p01,          :p02,        :p03,        :p04,        :p05,         'N',    :p06,           :p07,      :p08,       :p09,     :p10       ) \r\n";

    FLists.Lock();
    try
    {
        try
        {
            DeleteVdsCmra(AObj, ADbConn);

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

            FOR_STL(TVdsCmra*, pCmra, AObj->FCmras)
            {
                if (pCmra->DEL_YN == "Y") continue;

                pADO->Parameters->ParamByName("p01")->Value = pCmra->VDS_CTLR_NMBR;
                pADO->Parameters->ParamByName("p02")->Value = pCmra->VDS_CMRA_NO;
                pADO->Parameters->ParamByName("p03")->Value = pCmra->VDS_CMRA_NM;
                pADO->Parameters->ParamByName("p04")->Value = pCmra->DETECT_LANE;
                pADO->Parameters->ParamByName("p05")->Value = pCmra->CMRA_DRCT_CD;
                pADO->Parameters->ParamByName("p06")->Value = pCmra->CCAM_CTLR_NMBR;
                pADO->Parameters->ParamByName("p07")->Value = pCmra->ROAD_NAME;
                pADO->Parameters->ParamByName("p08")->Value = pCmra->CROSS_NAME;
                pADO->Parameters->ParamByName("p09")->Value = pCmra->DIR_NAME;
                pADO->Parameters->ParamByName("p10")->Value = pCmra->VDS_CMRA_IP;

                pADO->ExecSQL();
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::InsertVdsCmra", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TVdsCtlrManager::InsertVdsCmra", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

