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

#include <vcl.h>
#pragma hdrstop

#include "FrameHanwhaF.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"
TFrameHanwha *FrameHanwha;
//---------------------------------------------------------------------------
__fastcall TFrameHanwha::TFrameHanwha(TComponent* Owner)
    : TFrame(Owner)
{
    FStrmPlay = false;

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

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

    // 삼성 iPolis
    try
    {
        String sCmdHtml = "";
        TImageBtn *pBtn = (TImageBtn*)Sender;

        switch(pBtn->Tag)
        {
        case  0: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=home&action=control&Channel=0",                FStrmHttp.c_str()); break;  // HOME
        case  1: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=5&Duration=1",  FStrmHttp.c_str()); break;  // Tilt Up
        case  2: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=-5&Duration=1", FStrmHttp.c_str()); break;  // Tilt Down
        case  3: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=5",              FStrmHttp.c_str()); break;  // Pan left
        case  4: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=-5",             FStrmHttp.c_str()); break;  // Pan right
        case  9: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=3",             FStrmHttp.c_str()); break;  // Zoom In
        case 10: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=-3",            FStrmHttp.c_str()); break;  // Zoom Out
        case 12: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Near",         FStrmHttp.c_str()); break;  // Focus In
        case 13: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Far",          FStrmHttp.c_str()); break;  // Focus Out
#if 0
        case  0: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=home&action=control&Channel=0",                FStrmHttp.c_str()); break;  // HOME
        case  1: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=5&Duration=1",  FStrmHttp.c_str()); break;  // Tilt Up
        case  2: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=-5&Duration=1", FStrmHttp.c_str()); break;  // Tilt Down
        case  3: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=5",              FStrmHttp.c_str()); break;  // Pan left
        case  4: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=-5",             FStrmHttp.c_str()); break;  // Pan right
        case  9: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=3",             FStrmHttp.c_str()); break;  // Zoom In
        case 10: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=-3",            FStrmHttp.c_str()); break;  // Zoom Out
        case 12: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Near",         FStrmHttp.c_str()); break;  // Focus In
        case 13: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Far",          FStrmHttp.c_str()); break;  // Focus Out

        case  0: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=home&action=control&Channel=0",                FStrmHttp.c_str()); break;  // HOME
        case  1: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=5&Duration=1",  FStrmHttp.c_str()); break;  // Tilt Up
        case  2: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=-5&Duration=1", FStrmHttp.c_str()); break;  // Tilt Down
        case  3: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=5",              FStrmHttp.c_str()); break;  // Pan left
        case  4: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=-5",             FStrmHttp.c_str()); break;  // Pan right
        case  9: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=3",             FStrmHttp.c_str()); break;  // Zoom In
        case 10: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=-3",            FStrmHttp.c_str()); break;  // Zoom Out
        case 12: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Near",         FStrmHttp.c_str()); break;  // Focus In
        case 13: sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Far",          FStrmHttp.c_str()); break;  // Focus Out

        case  0: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=home&action=control&Channel=0",                FStrmHttp.c_str()); break;  // HOME
        case  1: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=5&Duration=1",  FStrmHttp.c_str()); break;  // Tilt Up
        case  2: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=-5&Duration=1", FStrmHttp.c_str()); break;  // Tilt Down
        case  3: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=5",              FStrmHttp.c_str()); break;  // Pan left
        case  4: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=-5",             FStrmHttp.c_str()); break;  // Pan right
        case  9: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=3",             FStrmHttp.c_str()); break;  // Zoom In
        case 10: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=-3",            FStrmHttp.c_str()); break;  // Zoom Out
        case 12: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Near",         FStrmHttp.c_str()); break;  // Focus In
        case 13: sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Far",          FStrmHttp.c_str()); break;  // Focus Out
#endif
        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 TFrameHanwha::BtnWebCamControlMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    TImageBtn *pBtn = (TImageBtn*)Sender;

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

    try
    {
        String sCmdHtml = "";
        switch(pBtn->Tag)
        {
        case 1: // Tilt Up
        case 2: // Tilt Down
            //sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=0&Duration=1", FStrmHttp.c_str());
            //sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=0&Duration=1", FStrmHttp.c_str());
            sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Tilt=0&Duration=1", FStrmHttp.c_str());
            break;
        case 3: // Pan left
        case 4: // Pan right
            //sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=0", FStrmHttp.c_str());
            //sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=0", FStrmHttp.c_str());
            sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Pan=0", FStrmHttp.c_str());
            break;
        case  9: // Zoom In
        case 10: // Zoom Out
            //sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=0", FStrmHttp.c_str());
            //sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=0", FStrmHttp.c_str());
            sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Zoom=0", FStrmHttp.c_str());
            break;
        case 12: // Focus In
        case 13: // Focus Out
            //sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Stop", FStrmHttp.c_str());
            //sCmdHtml.printf(L"%s/cgi-bin/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Stop", FStrmHttp.c_str());
            sCmdHtml.printf(L"%s/stw-cgi/ptzcontrol.cgi?msubmenu=continuous&action=control&Focus=Stop", FStrmHttp.c_str());
            break;
        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 TFrameHanwha::OpenStream()
{
    FStrmUrl = EdStreamUrl->Text.Trim();
    FStrmUser = EdStreamId->Text.Trim();
    FStrmPswd = EdStreamPswd->Text.Trim();

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

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

void __fastcall TFrameHanwha::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 TFrameHanwha::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 TFrameHanwha::reMsgDblClick(TObject *Sender)
{
    reMsg->Lines->Clear();
}
//---------------------------------------------------------------------------

