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

#pragma hdrstop

#include "CDSDsrcF.h"
#include "ITS_OPLibF.h"
#include "CDSFcltF.h"

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

#pragma package(smart_init)

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

//---------------------------------------------------------------------------
TItsDsrcManager *ItsDsrcManager = NULL;
//---------------------------------------------------------------------------

/*
* Dsrc Manager
*/
TItsDsrcManager::TItsDsrcManager()
{
}
//---------------------------------------------------------------------------
TItsDsrcManager::~TItsDsrcManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TItsDsrcManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT ID,              \r\n"
           "       RSE_ID,          \r\n"
           "       ROAD_SPOT_ID,    \r\n"
           "       LOG_CNNC_ID,     \r\n"
           "       LOG_CNNC_PW,     \r\n"
           "       ISTL_LCTN_NM,    \r\n"
           "       LCTN_X,          \r\n"
           "       LCTN_Y,          \r\n"
           "       IP,              \r\n"
           "       PORT,            \r\n"
           "       MNFC_CMPY_CD,    \r\n"
           "       DEL_YN,          \r\n"
           "       CLCT_ABNR_BASI   \r\n"
           "  FROM TB_RSE_MSTR      \r\n";
           " WHERE DEL_YN = 'N'     \r\n";
    try
    {
        FLists.Lock();
        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())
            {
				TItsDsrc *pObj = new TItsDsrc();

				pObj->DSRC_CTLR_NMBR    = pADO->FieldByName("ID")->AsString;
				pObj->DSRC_CTLR_ID      = pADO->FieldByName("RSE_ID")->AsString;
				pObj->ROAD_SPOT_ID      = pADO->FieldByName("ROAD_SPOT_ID")->AsString;
				pObj->LOG_CNNC_ID       = pADO->FieldByName("LOG_CNNC_ID")->AsString;
				pObj->LOG_CNNC_PW       = pADO->FieldByName("LOG_CNNC_PW")->AsString;
				pObj->ISTL_LCTN_NM      = pADO->FieldByName("ISTL_LCTN_NM")->AsString;
				pObj->NAME              = pObj->ISTL_LCTN_NM;
				pObj->X_CRDN            = pADO->FieldByName("LCTN_X")->AsFloat;
				pObj->Y_CRDN            = pADO->FieldByName("LCTN_Y")->AsFloat;
				pObj->CTLR_IP           = pADO->FieldByName("IP")->AsString;
				pObj->CTLR_PORT         = pADO->FieldByName("PORT")->AsInteger;
				pObj->MNFC_CMPY_CD      = pADO->FieldByName("MNFC_CMPY_CD")->AsString;
				pObj->DEL_YN            = pADO->FieldByName("DEL_YN")->AsString;
                pObj->CLCT_ABNR_BASI    = pADO->FieldByName("CLCT_ABNR_BASI")->AsInteger;

				FLists.Push(pObj->DSRC_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("DSRC정보조회", 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("DSRC정보조회", 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 TItsDsrcManager::LoadStatusFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT *                                                        \r\n"
           "  FROM TB_RSE_STTS_PNST A, TB_RSE_MSTR B                        \r\n"
           " WHERE A.ID       = B.ID                                        \r\n"
           "   AND A.CLCT_DT >= TO_CHAR(SYSDATE-2/1440, 'YYYYMMDDHH24MISS') \r\n";

    FLists.Lock();
    try
    {
        TItsDsrc *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("ID")->AsString;
                pObj = FLists.Find(sVmsNmbr);
                if (!pObj) continue;

                String UPDT_DT      = pADO->FieldByName("CLCT_DT")->AsString;
                String CONN_STTS_CD = pADO->FieldByName("CMNC_STTS")->AsString;
                if (CONN_STTS_CD != "0")
                {
                    //통신장애
                    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("TItsDsrcManager::LoadStatusFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TItsDsrcManager::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 TItsDsrcManager::GetNextDsrcId(String &ANewId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT TO_NUMBER(CASE WHEN EXISTS (SELECT 1 FROM TB_RSE_MSTR)          \r\n"
           "                             THEN (SELECT TRIM(MAX(TO_NUMBER(ID)) + 1) \r\n"
           "                                     FROM TB_RSE_MSTR)                 \r\n"
           "                             ELSE '1' END) NEWID                       \r\n"
           " FROM DUAL                                                             \r\n";

    ANewId = "";
    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();
            ANewId = pADO->FieldByName("NEWID")->AsString;
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("DSRC 신규ID조회", 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("DSRC 신규ID조회", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TItsDsrcManager::DeleteDsrc(TItsDsrc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_RSE_MSTR   \r\n"
           "   SET DEL_YN = 'Y'  \r\n"
           " WHERE ID = :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->DSRC_CTLR_NMBR;
			pADO->ExecSQL();

            return TCDSFcltManager::Delete(AObj->DSRC_CTLR_ID);
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("DSRC정보삭제", 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("DSRC정보삭제", 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 TItsDsrcManager::UpdateDsrc(String AOldRseId, TItsDsrc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_RSE_MSTR            \r\n"
           "   SET RSE_ID         = :p02, \r\n"
           "       ROAD_SPOT_ID   = :p03, \r\n"
           "       LOG_CNNC_ID    = :p04, \r\n"
           "       LOG_CNNC_PW    = :p05, \r\n"
           "       ISTL_LCTN_NM   = :p06, \r\n"
           "       LCTN_X         = :p07, \r\n"
           "       LCTN_Y         = :p08, \r\n"
           "       IP             = :p09, \r\n"
           "       PORT           = :p10, \r\n"
           "       MNFC_CMPY_CD   = :p11, \r\n"
           "       DEL_YN         = :p12, \r\n"
           "       CLCT_ABNR_BASI = :p13  \r\n"
           " WHERE ID = :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;

            if (AObj->DEL_YN == "") AObj->DEL_YN = "N";
			pADO->Parameters->ParamByName("p01")->Value = AObj->DSRC_CTLR_NMBR;
			pADO->Parameters->ParamByName("p02")->Value = AObj->DSRC_CTLR_ID;
			pADO->Parameters->ParamByName("p03")->Value = AObj->ROAD_SPOT_ID;
			pADO->Parameters->ParamByName("p04")->Value = AObj->LOG_CNNC_ID;
			pADO->Parameters->ParamByName("p05")->Value = AObj->LOG_CNNC_PW;
			pADO->Parameters->ParamByName("p06")->Value = AObj->ISTL_LCTN_NM;
			pADO->Parameters->ParamByName("p07")->Value = AObj->X_CRDN;
			pADO->Parameters->ParamByName("p08")->Value = AObj->Y_CRDN;
			pADO->Parameters->ParamByName("p09")->Value = AObj->CTLR_IP;
			pADO->Parameters->ParamByName("p10")->Value = AObj->CTLR_PORT;
			pADO->Parameters->ParamByName("p11")->Value = AObj->MNFC_CMPY_CD;
			pADO->Parameters->ParamByName("p12")->Value = AObj->DEL_YN;
			pADO->Parameters->ParamByName("p13")->Value = AObj->CLCT_ABNR_BASI;
			pADO->ExecSQL();

            return TCDSFcltManager::Merge("DSRC", AObj->DSRC_CTLR_ID, AObj->ISTL_LCTN_NM);
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("DSRC정보업데이트", 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("DSRC정보업데이트", 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 TItsDsrcManager::InsertDsrc(TItsDsrc *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_RSE_MSTR(ID,           RSE_ID, ROAD_SPOT_ID, LOG_CNNC_ID, LOG_CNNC_PW,  \r\n"
           "                        ISTL_LCTN_NM, LCTN_X, LCTN_Y,       IP,          PORT,         \r\n"
           "                        MNFC_CMPY_CD, DEL_YN, CLCT_ABNR_BASI)                          \r\n"
           "                 VALUES(:p01,         :p02,   :p03,         :p04,        :p05,         \r\n"
           "                        :p06,         :p07,   :p08,         :p09,        :p10,         \r\n"
           "                        :p11,         :p12,   :p13)                                    \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->DSRC_CTLR_NMBR;
			pADO->Parameters->ParamByName("p02")->Value = AObj->DSRC_CTLR_ID;
			pADO->Parameters->ParamByName("p03")->Value = AObj->ROAD_SPOT_ID;
			pADO->Parameters->ParamByName("p04")->Value = AObj->LOG_CNNC_ID;
			pADO->Parameters->ParamByName("p05")->Value = AObj->LOG_CNNC_PW;
			pADO->Parameters->ParamByName("p06")->Value = AObj->ISTL_LCTN_NM;
			pADO->Parameters->ParamByName("p07")->Value = AObj->X_CRDN;
			pADO->Parameters->ParamByName("p08")->Value = AObj->Y_CRDN;
			pADO->Parameters->ParamByName("p09")->Value = AObj->CTLR_IP;
			pADO->Parameters->ParamByName("p10")->Value = AObj->CTLR_PORT;
			pADO->Parameters->ParamByName("p11")->Value = AObj->MNFC_CMPY_CD;
			pADO->Parameters->ParamByName("p12")->Value = AObj->DEL_YN;
			pADO->Parameters->ParamByName("p13")->Value = AObj->CLCT_ABNR_BASI;
			pADO->ExecSQL();

            return TCDSFcltManager::Merge("DSRC", AObj->DSRC_CTLR_ID, AObj->ISTL_LCTN_NM);
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("DSRC정보입력", 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("DSRC정보입력", 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 TItsDsrcManager::UpdateClctAbnrBasi(int AClctAbnrBasi, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_RSE_MSTR            \r\n"
           "   SET CLCT_ABNR_BASI = :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 = AClctAbnrBasi;
			pADO->ExecSQL();

            FOR_STL(TItsDsrc*, pObj, FLists)
            {
                if (pObj->DEL_YN == "Y") continue;

                pObj->CLCT_ABNR_BASI = AClctAbnrBasi;
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("DSRC수집정보업데이트", 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("DSRC수집정보업데이트", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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


