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

#include <vcl.h>
#include "AppGlobalF.h"
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSLangTransF.h"
#include "FrmInitializeF.h"
#include "DMDbF.h"
#pragma hdrstop

#include "FrmCctvCamCtrlF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridBandedTableView"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdIcmpClient"
#pragma link "IdRawBase"
#pragma link "IdRawClient"
#pragma link "ImageBtn"
#pragma link "dxSkinBlack"
#pragma link "dxSkinsForm"
#pragma link "dxSkinMcSkin"
#pragma link "IdHTTP"
#pragma link "IdTCPClient"
#pragma link "IdTCPConnection"
#pragma link "AcesTechXPlayer2Lib_OCX"
#pragma link "FRAME_CctvStateListF"
#pragma resource "*.dfm"
TFrmCctvCamCtrl *FrmCctvCamCtrl = NULL;

//{*      Information Bar    *}
#define CAP_CAMERA_NAME				0x01
#define CAP_TIME					0x02
#define CAP_CHANNEL					0x04
#define CAP_SIZE					0x08
#define CAP_EVENT					0x10
#define CAP_FPSCHANGED				0x20

//{*  Pan/tilt/Zoom and Transparent controls *}
#define CTL_FOCUS_CONT				0x2001
#define CTL_PTZ_CONT				0x2002
#define CTL_TRANSPARENT				0x7000
#define CTL_TRANSPARENT_RS232		0x7001

//{*      OCXState for NVSMPEG4OCX    *}
#define GRAPH_NONE					0x00000000
#define GRAPH_PREVIEW				0x00000001
#define GRAPH_CAPTURE				0x00000002
#define GRAPH_PREVIEWCAPTURE		0x00000003
#define GRAPH_AUDIO_LISTEN			0x00000100
#define GRAPH_AUDIO_CAPTURE			0x00000200
#define GRAPH_AUDIO_LISTENCAPTURE	0x00000400
#define GRAPH_STATE_RUNNING			0x80000000


#define SB_PAN_LEFT					0x000000C4
#define SB_PAN_RIGHT				0x0000003C
#define SB_TILT_UP					0x00003C00
#define SB_TILT_DOWN				0x0000C400
#define SB_ZOOM_IN					0x003C0000
#define SB_ZOOM_OUT					0x00C40000
#define SB_FOCUS_IN					0x3C
#define SB_FOCUS_OUT				0xC4

enum { E_MJPG=0, E_MP4S, E_DX50, E_MPEG2, E_MPEG1, E_H264 };
enum { BR_32K=0, BR_64K, BR_128K, BR_256K, BR_384K, BR_500K, BR_750K, BR_1M, BR_1_5M, BR_2M, BR_4M, BR_5M, BR_6M, BR_7M, BR_8M };
enum { S_1080P=0, S_720P, S_D1, S_4CIF, S_VGA, S_HD1, S_H4CIF, S_HVGA, S_CIF, S_QVGA, S_QCIF, S_QQVGA };

#define PRESET_SET                  0x1020 // Unsigned 1 byte
#define PRESET_MOVE                 0x1021 // Unsigned 1 byte
#define PRESET_CLEAR                0x1022 // Unsigned 1 byte

//---------------------------------------------------------------------------
__fastcall TFrmCctvCamCtrl::TFrmCctvCamCtrl(TComponent* Owner)
    : TForm(Owner)
{
    //::CoInitialize(NULL);
    ITSSkin_Initialize(Application, "kr", true, true);
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    LoadLocalSkin();

    //CMM_LoadForm(g_sFormsDir, this);

    FSelCtlr = NULL;

    FRAMECctvStateList1->PnlTop->Visible    = false;
    FRAMECctvStateList1->PnlBottom->Visible = false;
    FRAMECctvStateList1->TvList->OptionsView->Footer = false;

    FSelIds = new TStringList();

    FTitle = Caption;

    //TabPreset->Visible = false;
    //TabPreset->TabVisible = false;

    FStrmPlay = false;
    FStrmHandle = 0;

    PgCam->Properties->HideTabs = true;

    PnlCamera->ParentColor = false;
    FTmrShow = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::CommClose()
{
    try
    {
        SAFE_DELETE(FSelIds);
        CMM_SaveForm(g_sFormsDir, this);

        CloseRtsp();

        //::CoUninitialize();
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::FormShow(TObject *Sender)
{
    Refresh();
    TmrShow->Enabled = FTmrShow;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::FormActivate(TObject *Sender)
{
    ActivateForm(true);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::FormDeactivate(TObject *Sender)
{
    ActivateForm(false);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::LoadLocalSkin()
{
    PnlCamera->ParentColor = false;
    PnlCamera->Color = clBlack;
    Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

    if (!CctvCtlrManager)
    {
        CctvCtlrManager = new TCctvCtlrManager();
    }

    FrmInitialize = new TFrmInitialize(this);
    FrmInitialize->ShowModal();
    FrmInitialize = NULL;

    FRAMECctvStateList1->UpdateList();
    FRAMECctvStateList1->RefreshStatus();
    FRAMECctvStateList1->CxList->Enabled = false;
    UpdatePingList();
    FRAMECctvStateList1->CxList->Enabled = true;

    BtnStatRefresh->Enabled = true;
    TmrRefresh->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::FormClose(TObject *Sender, TCloseAction &Action)
{
    try {
        CommClose();

        FrmCctvCamCtrl = NULL;
        Action = caFree;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::OnMainFormMessage(TMessage &Msg)
{
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::OnCctvStateRefreshMessage(TMessage &Msg)
{
    FRAMECctvStateList1->RefreshStatus();
    RefreshPingList();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::ActivateForm(bool AActivate)
{
    if (AActivate) FRAMECctvStateList1->RefreshStatus();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::InitForm()
{
    ClearPreset();

    EdCtlrId->Text = "";
	EdCtlrNm->Text = "";

    EdCtlrIp->Text = "";
    Se_CCTV_CTLR_PORT->Value = 0;
    EdTypeNm->Text = "";

    Ed_STRM_SESN_NM->Text   = "";

    Ed_STRM_RTSP_ADDR->Text = "";
    Ed_STRM_RTMP_ADDR->Text = "";
	Ed_STRM_HTTP_ADDR->Text = "";
    Ed_INTR_RTMP_ADDR->Text = "";

    EdCmraId->Text   = "";
    EdCmraPswd->Text = "";

    BtnWebCamUrl->Enabled = false;
    PnlCamConrtol->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::DisplayInfo(TCctvCtlr* AObj)
{
    InitForm();

    FSelCtlr = AObj;
    if (FSelCtlr == NULL)
    {
        return;
    }

    EdCtlrId->Text = AObj->CCTV_CTLR_ID;
	EdCtlrNm->Text = AObj->CCTV_NM;

    EdCtlrIp->Text = AObj->CCTV_CTLR_IP;
    Se_CCTV_CTLR_PORT->Value = AObj->CCTV_CTLR_PORT;

    EdTypeNm->Text = AObj->CCTV_TYPE_NM;

    Ed_STRM_SESN_NM->Text   = AObj->STRM_SESN_NM;

    Ed_STRM_RTSP_ADDR->Text = AObj->STRM_RTSP_ADDR;
    Ed_STRM_RTMP_ADDR->Text = AObj->STRM_RTMP_ADDR;
	Ed_STRM_HTTP_ADDR->Text = AObj->STRM_HTTP_ADDR;
    Ed_INTR_RTMP_ADDR->Text = AObj->STRM_STOR_ADDR;

    EdCmraId->Text   = AObj->CMRA_ID;
    EdCmraPswd->Text = AObj->CMRA_PSWD;

    if (AObj->CCTV_TYPE == 1)
    {
        // 다기능 제어기
        BtnWebCamUrl->Enabled = false;
        PnlCamConrtol->Enabled = false;
        FStrmHttp = "http://" + FSelCtlr->CCTV_CTLR_IP;
    }
    else
    {
        BtnWebCamUrl->Enabled = true;
        PnlCamConrtol->Enabled = true;
        FStrmHttp = "http://" + FSelCtlr->CCTV_CTLR_IP;
        if (FSelCtlr->CCTV_CTLR_PORT != 0 && FSelCtlr->CCTV_CTLR_PORT != 80)
        {
            FStrmHttp = FStrmHttp + ":" + String(FSelCtlr->CCTV_CTLR_PORT);
        }
    }

    try
    {
        OpenRtsp();
        InitPreset();
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------
bool __fastcall TFrmCctvCamCtrl::OpenRtsp()
{
    if (!FSelCtlr) return false;

    try
    {
        reMsg->Lines->Clear();

        CloseRtsp();

        FStrmAddr = FSelCtlr->STRM_SESN_NM.Trim();
        if (FStrmAddr != "")
        {
            AcesTechXPlayer21->URL = FStrmAddr;
            FStrmHandle = AcesTechXPlayer21->ConnectAsync();
            if (FStrmHandle > 0)
            {
                PnlCamConrtol->Enabled = true;
            }

            FStrmUser = FSelCtlr->CMRA_ID;
            FStrmPswd = FSelCtlr->CMRA_PSWD;
            FStrmHttp = "http://" + FSelCtlr->CCTV_CTLR_IP;

            FStrmPlay = true;
        }
    }
    catch(Exception &e)
    {
    }
    return true;
}
//---------------------------------------------------------------------------
void __fastcall TFrmCctvCamCtrl::CloseRtsp()
{
    if (!FStrmPlay) return;

    try
    {
        if (FStrmHandle > 0)
        {
            AcesTechXPlayer21->Close();
        }
    }
    catch(Exception &e)
    {
    }
    PnlCamConrtol->Enabled = false;
    FStrmPlay = false;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::ClearCheckList()
{
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::FRAMECctvStateList1TvListCellDblClick(TcxCustomGridTableView *Sender,
          TcxGridTableDataCellViewInfo *ACellViewInfo, TMouseButton AButton,
          TShiftState AShift, bool &AHandled)
{
	if (!ACellViewInfo) return;

    ClearCheckList();

    TcxGridBandedTableView *TvList = FRAMECctvStateList1->TvList;
    TcxDataController *pGDC = TvList->DataController;
    int nSelIdx = CMM_GetRecordIndex(TvList);
	if (nSelIdx < 0) return;
    pGDC->Values[nSelIdx][FRAMECctvStateList1->ColumnSel->Index] = true;

    int nMemPtr = (int)pGDC->Values[nSelIdx][FRAMECctvStateList1->Col99->Index];
    FSelCtlr = (TCctvCtlr*)nMemPtr;
    if (!FSelCtlr) return;

    DisplayInfo(FSelCtlr);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::FRAMECctvStateList1TvList2CellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
	if (!ACellViewInfo) return;

    ClearCheckList();

    TcxGridBandedTableView *TvList = FRAMECctvStateList1->TvList2;
    TcxDataController *pGDC = TvList->DataController;
    int nSelIdx = CMM_GetRecordIndex(TvList);
	if (nSelIdx < 0) return;
    pGDC->Values[nSelIdx][FRAMECctvStateList1->ColumnSel2->Index] = true;

    int nMemPtr = (int)pGDC->Values[nSelIdx][FRAMECctvStateList1->Column99->Index];
    FSelCtlr = (TCctvCtlr*)nMemPtr;
    if (!FSelCtlr) return;

    DisplayInfo(FSelCtlr);
}
//---------------------------------------------------------------------------

int __fastcall TFrmCctvCamCtrl::GetSelListItems(bool ACommCheck/*=true*/)
{
    return 0;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::BtnWebCamUrlClick(TObject *Sender)
{
    if (FSelCtlr)
    {
        if (FStrmHttp != "")
        {
            ShellExecute(NULL, NULL, FStrmHttp.c_str(), NULL, NULL, 0);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::UpdatePingList()
{
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::RefreshPingList(bool AInit/*=false*/)
{
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::PingNetwork(String AIp, int APort)
{
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::BtnSaveImgFileClick(TObject *Sender)
{
#if 0
	String sSaveFileName;

	if (!FSelCtlr || XNVTViewerCtrl1->OCXState == GRAPH_NONE)
	{
        String sMsg = cxLabel1->Caption;
        Application->MessageBox(sMsg.c_str(), L"Save Snap Shot", MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}

	sSaveFileName.printf(L"%s\\%d_%s.bmp", g_sTempDir.c_str(), FSelCtlr->WCAM_CTLR_NMBR, Now().FormatString("yyyymmddhhnnss").c_str());

	if (XNVTViewerCtrl1->FileSnapShot(sSaveFileName.c_str()))
    {
        Application->MessageBox(sSaveFileName.c_str(), L"Save Snap Shot Success", MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
	}
    else
    {
        Application->MessageBox(cxLabel2->Caption.c_str(), L"Save Snap Shot Fail", MB_OK|MB_ICONERROR|MB_APPLMODAL);
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::ClearPreset()
{
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::InitPreset()
{
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::reMsgDblClick(TObject *Sender)
{
    reMsg->Lines->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::BtnStatRefreshClick(TObject *Sender)
{
    TmrRefresh->Enabled = false;
    BtnStatRefresh->Enabled = false;
    if (!DMDb->Connect())
    {
        if (g_AppCfg.sLang != "kr")
        {
            Application->MessageBox(L"Database connection failed.\r\n\r\nPlease restart the program.",
                                    L"Database connection error !!!",
                                    MB_OK|MB_ICONERROR);
        }
        else
        {
            Application->MessageBox(L"데이터베이스 연결에 실패하였습니다.\r\n\r\n프로그램을 다시 시작하십시요.",
                                    L"데이터베이스 연결 오류!!!",
                                    MB_OK|MB_ICONERROR);
        }
        BtnStatRefresh->Enabled = true;
        TmrRefresh->Enabled = true;
        return;
    }

    CctvCtlrManager->LoadStatusFromDb(DMDb->GetConnection());
    FRAMECctvStateList1->RefreshStatus();
    RefreshPingList();

    DMDb->Close();;
    BtnStatRefresh->Enabled = true;
    TmrRefresh->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::TmrRefreshTimer(TObject *Sender)
{
    BtnStatRefreshClick(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::IdHTTP1Authorization(TObject *Sender, TIdAuthentication *Authentication, bool &Handled)
{
#if 1
    IdHTTP1->Request->Username = FStrmUser;
    IdHTTP1->Request->Password = FStrmPswd;
#endif

    Authentication->Username = FStrmUser;
    Authentication->Password = FStrmPswd;

    TIdDigestAuthentication *pAuth1 = (TIdDigestAuthentication*)IdHTTP1->Request->Authentication;
    TIdDigestAuthentication *pAuth2 = (TIdDigestAuthentication*)Authentication;
    pAuth1->Uri = IdHTTP1->Request->URL;
    pAuth2->Method = "GET";

    Handled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::BtnUpMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    // PANControl
    if (!FStrmPlay) return;
    if (!FSelCtlr) return;
    if (FSelCtlr->CCTV_CTLR_IP.Trim() == "") return;

    String sPTZSpeed = String(PTZSpeed->Position);
    try
    {
        String sCmdHtml = "";
        TImageBtn *pBtn = (TImageBtn*)Sender;

        if (FSelCtlr->CCTV_TYPE == 2)
        {
            // Truen
            //        url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MOVE=";

            switch(pBtn->Tag)
            {
            case  0: sCmdHtml.printf(L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=home,%s&PTZ_TIMEOUT=5000",      FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // HOME
            case  1: sCmdHtml.printf(L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=up,%s&PTZ_TIMEOUT=5000",        FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Tilt Up
            case  2: sCmdHtml.printf(L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=down,%s&PTZ_TIMEOUT=5000",      FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Tilt Down
            case  3: sCmdHtml.printf(L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=left,%s&PTZ_TIMEOUT=5000",      FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Pan left
            case  4: sCmdHtml.printf(L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=right,%s&PTZ_TIMEOUT=5000",     FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Pan right
            case  9: sCmdHtml.printf(L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=zoomin,%s&PTZ_TIMEOUT=5000",    FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Zoom In
            case 10: sCmdHtml.printf(L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=zoomout,%s&PTZ_TIMEOUT=5000",   FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Zoom Out
            case 12: sCmdHtml.printf(L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=focusnear,%s&PTZ_TIMEOUT=5000", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Focus In
            case 13: sCmdHtml.printf(L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=focusfar,%s&PTZ_TIMEOUT=5000",  FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Focus Out
            default: return;
            }
        }
        else
        if (FSelCtlr->CCTV_TYPE == 3)
        {
            // Pelco
        }
        else
        if (FSelCtlr->CCTV_TYPE == 4)
        {
            // ESCA
            switch(pBtn->Tag)
            {
            case  0: sCmdHtml.printf(L"%s/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=home,%s&PTZ_TIMEOUT=5000",      FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // HOME
            case  1: sCmdHtml.printf(L"%s/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=up,%s&PTZ_TIMEOUT=5000",        FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Tilt Up
            case  2: sCmdHtml.printf(L"%s/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=down,%s&PTZ_TIMEOUT=5000",      FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Tilt Down
            case  3: sCmdHtml.printf(L"%s/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=left,%s&PTZ_TIMEOUT=5000",      FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Pan left
            case  4: sCmdHtml.printf(L"%s/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=right,%s&PTZ_TIMEOUT=5000",     FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Pan right
            case  9: sCmdHtml.printf(L"%s/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=zoomin,%s&PTZ_TIMEOUT=5000",    FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Zoom In
            case 10: sCmdHtml.printf(L"%s/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=zoomout,%s&PTZ_TIMEOUT=5000",   FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Zoom Out
            case 12: sCmdHtml.printf(L"%s/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=focusnear,%s&PTZ_TIMEOUT=5000", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Focus In
            case 13: sCmdHtml.printf(L"%s/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=focusfar,%s&PTZ_TIMEOUT=5000",  FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Focus Out
            default: return;
            }
        }

        if (sCmdHtml == "") return;

        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions >> hoInProcessAuth;
        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions << hoInProcessAuth;
        String sResult = IdHTTP1->Get(sCmdHtml);
        reMsg->Lines->Add(sResult.Trim());
        IdHTTP1->Disconnect();
    }
    catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::BtnUpMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    // PANControl Stop
    if (!FStrmPlay) return;
    if (!FSelCtlr) return;
    if (FSelCtlr->CCTV_CTLR_IP.Trim() == "") return;

    String sPTZSpeed = String(PTZSpeed->Position);
    try
    {
        String sCmdHtml = "";
        TImageBtn *pBtn = (TImageBtn*)Sender;

        if (FSelCtlr->CCTV_TYPE == 2)
        {
            // Truen
            sCmdHtml.printf(L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=stop,%s&PTZ_TIMEOUT=5000", FStrmHttp.c_str(), sPTZSpeed.c_str());    // stop
        }
        else
        if (FSelCtlr->CCTV_TYPE == 3)
        {
            // Pelco
        }
        else
        if (FSelCtlr->CCTV_TYPE == 4)
        {
            // ESCA
            sCmdHtml.printf(L"%s/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=stop,%s&PTZ_TIMEOUT=5000", FStrmHttp.c_str(), sPTZSpeed.c_str());    // stop
        }
        if (sCmdHtml == "") return;

        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions >> hoInProcessAuth;
        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions << hoInProcessAuth;
        String sResult = IdHTTP1->Get(sCmdHtml);
        reMsg->Lines->Add(sResult.Trim());
        IdHTTP1->Disconnect();
    }
    catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCamCtrl::EdPresetNameKeyPress(TObject *Sender, wchar_t &Key)
{
    if (Key == VK_SPACE)
    {
        Key = 0;
    }
}
//---------------------------------------------------------------------------

