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

#include <vcl.h>
#pragma hdrstop

#include "FrameTruenActiveXF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxGraphics"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinsCore"
#pragma link "ImageBtn"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxLabel"
#pragma link "AcesTechXPlayer2Lib_OCX"
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdHTTP"
#pragma link "IdTCPClient"
#pragma link "IdTCPConnection"
#pragma link "cxTextEdit"
#pragma resource "*.dfm"
TFrameTruenActiveX *FrameTruenActiveX;
//---------------------------------------------------------------------------
__fastcall TFrameTruenActiveX::TFrameTruenActiveX(TComponent* Owner)
    : TFrame(Owner)
{
    FStrmPlay = false;

    reMsg->Lines->Clear();
    FStrmHandle = 0;
}
//---------------------------------------------------------------------------
void __fastcall TFrameTruenActiveX::BtnWebCamControlMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    TImageBtn *pBtn = (TImageBtn*)Sender;

    String sPTZSpeed = String(PTZSpeed->Position);
    FStrmHttp = EdHttpUrl->Text.Trim();
    if (FStrmHttp == "")
    {
        return;
    }

#if 0
    http://<server>/httpapi/<api_group_name>?action=<action_name>&<parameter>=<value>[&<parameter>=<value>...]
    http://<server>/httpapi/SendPTZ?action=<action_name>&<parameter>=<value>[&<parameter>=<value>...]

    PTZ_TIMEOUT <int> 10 ~ 5000

Speed: 1 ~ 10 (10: the fastest)

stop
continue
home
irisopen
irisclose

left
right
up
down
zoomin
zoomout
focusnear
focusfar

To pan-left the camera for the first video channel for 500ms with speed 4
http://<server>/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=left,4&PTZ_TIMEOUT=500

L"%s/httpapi/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MOVE=left,%s&PTZ_TIMEOUT=5000"


#endif
    // 자체적인 타임아웃을 사용하고 있기 때문에 명령을 내릴때 타임아웃값을 함께 내리도록 한다.
    try
    {
        String sCmdHtml = "";
        TImageBtn *pBtn = (TImageBtn*)Sender;

        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  4: 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  3: 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;
        }

        if (sCmdHtml == "") return;

        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions >> hoInProcessAuth;
        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions << hoInProcessAuth;
        String sResult = IdHTTP1->Get(sCmdHtml);
        reMsg->Lines->Add(sResult);
        IdHTTP1->Disconnect();
    }
    catch(...) {}
}
//---------------------------------------------------------------------------
void __fastcall TFrameTruenActiveX::BtnWebCamControlMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    TImageBtn *pBtn = (TImageBtn*)Sender;

    String sPTZSpeed = String(PTZSpeed->Position);
    FStrmHttp = EdHttpUrl->Text.Trim();
    if (FStrmHttp == "")
    {
        return;
    }

    try
    {
        String sCmdHtml = "";
        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

        if (sCmdHtml == "") return;

        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions >> hoInProcessAuth;
        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions << hoInProcessAuth;
        String sResult = IdHTTP1->Get(sCmdHtml);
        reMsg->Lines->Add(sResult);
        IdHTTP1->Disconnect();
    }
    catch(...) {}
}
//---------------------------------------------------------------------------
void __fastcall TFrameTruenActiveX::OpenStream()
{
    FStrmUrl = EdStreamUrl->Text.Trim();
    FStrmUser = EdStreamId->Text.Trim();
    FStrmPswd = EdStreamPswd->Text.Trim();

    AcesTechXPlayer21->URL = FStrmAddr;
    FStrmHandle = AcesTechXPlayer21->ConnectAsync();
    if (FStrmHandle > 0)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrameTruenActiveX::CloseStream()
{
    if (FStrmHandle > 0)
    {
        AcesTechXPlayer21->Close();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrameTruenActiveX::BtnCameraConDisconnClick(TObject *Sender)
{
    TcxButton *btn = (TcxButton*)Sender;
    if (btn->Tag == 1) {
        // 연결
        btn->Tag = 0;
        btn->Caption = "영상 연결 해제";
        OpenStream();
    }
    else {
        // 연결 종료
        btn->Tag = 1;
        btn->Caption = "영상 연결";
        CloseStream();
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrameTruenActiveX::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 TFrameTruenActiveX::reMsgDblClick(TObject *Sender)
{
    reMsg->Lines->Clear();
}
//---------------------------------------------------------------------------

