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

#pragma hdrstop

#include "CDSLprsF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
TLprsCam::TLprsCam()
{
}
//---------------------------------------------------------------------------

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

//---------------------------------------------------------------------------
TLprsManager *LprsManager = NULL;
//---------------------------------------------------------------------------

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

#ifndef USE_LPRS
    return true;
#endif

    sQry = "SELECT A.NMBR_IDNT_CMRA_ID, A.ISTL_LCTN, A.CMRA_IP,    \r\n"
           "       NVL(A.CMRA_X_CRDN, '0') AS Y_CRDN,              \r\n"
           "       NVL(A.CMRA_Y_CRDN, '0') AS X_CRDN               \r\n"
           "  FROM TB_SC_NMBR_IDNT_CMRA_MNGM A                     \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()) {
				TLprsCam *pObj = new TLprsCam();
                pObj->CMRA_ID = pADO->FieldByName("NMBR_IDNT_CMRA_ID")->AsString;
                pObj->CMRA_NM = pADO->FieldByName("ISTL_LCTN")->AsString.Trim();
                pObj->CMRA_IP = pADO->FieldByName("CMRA_IP")->AsString.Trim();
                pObj->RTSP_URL = "";
                pObj->HTTP_URL = "";
                pObj->X_CRDN   = pADO->FieldByName("X_CRDN")->AsFloat;
                pObj->Y_CRDN   = pADO->FieldByName("Y_CRDN")->AsFloat;
                pObj->DEL_YN   = "N";
                pObj->TYPE_CD  = "LPRS";

                pObj->CTLR_NMBR = pObj->CMRA_ID;
                pObj->CTLR_ID   = pObj->CMRA_ID;
                pObj->NAME      = pObj->CMRA_NM;
                pObj->ISTL_LCTN_NM   = pObj->CMRA_NM;
                pObj->ISTL_LCTN_ADDR = pObj->CMRA_NM;

				FLists.Push(pObj->CMRA_ID, pObj);
                pObj->Completed  = true;
            }
        }
        catch(EDatabaseError &E) {
            DBERRORMSG("TLprsManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e) {
            DBERRORMSG("TLprsManager::LoadFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally {
        FLists.UnLock();
        if (pADO) {
            pADO->Close();
            delete pADO;
        }
    }
    return true;
}
//---------------------------------------------------------------------------

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

#ifndef USE_LPRS
    return true;
#endif

    sQry = "SELECT A.*                                                          \r\n"
           "  FROM TB_SC_NMBR_IDNT_CMRA_STTS A,                                 \r\n"
           "       TB_SC_NMBR_IDNT_CMRA_MNGM B                                  \r\n"
           " WHERE A.NMBR_IDNT_CMRA_ID = B.NMBR_IDNT_CMRA_ID                    \r\n";
           //"   AND A.CLCT_DT >= TO_CHAR(SYSDATE-20/1440, 'YYYYMMDDHH24MISS')    \r\n";

    FLists.Lock();
    try
    {
        FOR_STL(TLprsCam*, pTmp, FLists) {
            //pTmp->STATE.Comm = wcam_error;
        }

        TLprsCam *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();
            for( ; !pADO->Eof; pADO->Next()) {
                String CMRA_ID = pADO->FieldByName("NMBR_IDNT_CMRA_ID")->AsString;
                pObj = FLists.Find(CMRA_ID);
                if (!pObj) continue;

                String CLCT_DT = pADO->FieldByName("CLCT_DT")->AsString;
                int CMRA_STTS  = pADO->FieldByName("CMRA_STTS")->AsInteger;
                if (CMRA_STTS != 0) {
                    //Åë½ÅÀå¾Ö
                    continue;
                }
                //pObj->STATE.Comm = (CMNC_STTS_CD == "CMS0") ? wcam_normal : wcam_error;
            }
        }
        catch(EDatabaseError &E) {
            DBERRORLOG("TLprsManager::LoadStatusFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e) {
            DBERRORLOG("TLprsManager::LoadStatusFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally {
        FLists.UnLock();
        if (pADO) {
            pADO->Close();
            delete pADO;
        }
    }

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