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

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

#include "FRAME_VmsStateF.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 "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "cxGridBandedTableView"
#pragma link "cxGridCardView"
#pragma link "cxGridCustomLayoutView"
#pragma link "cxGroupBox"
#pragma link "cxRadioGroup"
#pragma link "cxLabel"
#pragma resource "*.dfm"
//TFRAMEVmsStateList *FRAMEVmsStateList;
//---------------------------------------------------------------------------
__fastcall TFRAMEVmsState::TFRAMEVmsState(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    FSelVmsObj = NULL;

    TcxDataController *pGDC = TvVmsInfo->DataController;
    try {
        pGDC->BeginUpdate();
        while(pGDC->RecordCount > 0) {
            pGDC->DeleteRecord(0);
        }
        pGDC->AppendRecord();
    }
    __finally {
        pGDC->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEVmsState::UpdateList()
{
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEVmsState::SetVmsObject(TVmsCtlr *ASelVmsObj)
{
    FSelVmsObj = ASelVmsObj;
    RefreshStatus();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEVmsState::RefreshStatus()
{
    CxVmsInfo->LayoutChanged();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEVmsState::OnCloseQuery(bool &CanClose)
{
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEVmsState::ColVmsIdGetDataText(TcxCustomGridTableItem *Sender,
          int ARecordIndex, UnicodeString &AText)
{
    if (!FSelVmsObj)
    {
        AText = "";
        return;
    }

    TVmsCtlr *pObj = FSelVmsObj;
    switch(Sender->Tag)
    {
    case 0: AText = pObj->VMS_CTLR_ID;
        break;
    case 1: AText = pObj->VMS_TYPE_NM;
        break;
    case 2: AText = pObj->CTLR_NMBR + "-" + pObj->VMS_NM;
        break;
    case 3: //운영모드
        AText = VmsOperMode(pObj);
        break;
    case 4: //통신
        AText = VmsComm(pObj);
        break;
    case 5: //모듈상태
        //AText = VmsModuleState(pObj);
        AText = VmsModulePower(pObj);
        break;
    case 6: //모듈전원상태
        AText = VmsModulePower(pObj);
        break;
    case 7: //모듈휘도값
        AText = VmsModuleLuminance(pObj);
        break;
    case 8: //도어상태
        AText = VmsModuleDoor(pObj);
        break;
    case 9: //팬상태
        AText = VmsModuleFan(pObj);
        break;
    case 10: //히터상태
        AText = VmsModuleHeater(pObj);
        break;
    case 11: //온도값
        AText = VmsModuleTemp(pObj);
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEVmsState::BtnControl0Click(TObject *Sender)
{
    //전광판제어
    String sTitle = lblText1->Caption;//"전광판/제어기 제어";
    String sMsg   = "";

    int nOkCnt  = 0;
    if (!FSelVmsObj)
    {
        sMsg = lblText2->Caption;//"VMS가 선택되지 않았습니다.\r\nVMS를 선택하십시요.";
        Application->MessageBox(sMsg.c_str(), sTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        CxVmsInfo->SetFocus();
        return;
    }
    if (FSelVmsObj->STATE.Comm != vms_comm_normal)
    {
        sMsg = lblText3->Caption;//"현재 통신이 비정상인 VMS는 선택하실 수 없습니다.\r\n통신상태가 정상인 VMS를 선택하십시요.";
        Application->MessageBox(sMsg.c_str(), sTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        CxVmsInfo->SetFocus();
        return;
    }

    int nCmdIdx;
    if (RdbOn->Checked)
    {
        nCmdIdx = vms_req_board_power_on;
        sMsg = lblText4->Caption + "\r\n" + cxLabel9->Caption;//"선택하신 VMS에 VMS 전광판 전원 ON 명령을 전송하시겠습니까?\r\n(통신상태에 따라 명령이 실행되지 않을 수 있습니다.)";
    }
    else
    if (RdbOff->Checked)
    {
        nCmdIdx = vms_req_board_power_off;
        sMsg = lblText5->Caption + "\r\n" + cxLabel9->Caption;//"선택하신 VMS에 VMS 전광판 전원 OFF 명령을 전송하시겠습니까?\r\n(통신상태에 따라 명령이 실행되지 않을 수 있습니다.)";
    }
    else
    if (RdbReset->Checked)
    {
        nCmdIdx = 2;
        sMsg = lblText6->Caption + "\r\n" + cxLabel9->Caption;//"선택하신 VMS에 VMS 제어기 리셋 명령을 전송하시겠습니까?\r\n(통신상태에 따라 명령이 실행되지 않을 수 있습니다.)";
    }
    if (Application->MessageBox(sMsg.c_str(), sTitle.c_str(), MB_YESNO|MB_ICONWARNING|MB_APPLMODAL) != IDYES) return;

    String sCmdUser = g_pLOGIN->sUserId;
    String sCmdTime = Now().FormatString("yyyymmddhhnnss");

    int result;
    String sVmsNmbr = String(FSelVmsObj->VMS_CTLR_NMBR);

    if (nCmdIdx == vms_req_board_power_on || nCmdIdx == vms_req_board_power_off)
    {
        // 전광판 On(=0)/Off(=1)
        INT_VMS_POWER_CTL_REQ VmsPowerCtl;
        BYTE                  MsgSeq;

        memset(&VmsPowerCtl, 0x00, sizeof(VmsPowerCtl));

        MsgSeq = (BYTE)0x10;                                                            // 제어명령 일련번호-결과값을 받아서 처리할때 필요함

        memcpy(VmsPowerCtl.CmdTime, AnsiString(sCmdTime).c_str(), INT_VMS_MAX_DATETIME); /* 명령수행시각 */
        memcpy(VmsPowerCtl.OperId,  AnsiString(sCmdUser).c_str(), INT_VMS_MAX_OPER_ID);  /* 운영자 ID */
        VmsPowerCtl.VMS_CTLR_NMBR = sVmsNmbr.ToIntDef(0);
        VmsPowerCtl.Command = (BYTE)nCmdIdx;                                            /* 제어 구분, 0:켜짐 1:꺼짐 */

        String sOprCode  = (nCmdIdx == vms_req_board_power_on) ? "V01" : "V02";
        String sCmdValue = (nCmdIdx == vms_req_board_power_on) ? "On"  : "Off";

        result = CComm_VmsPowerControlReq(&VmsPowerCtl, MsgSeq);
        if (VERR_NONE == result)
        {
            VmsCtlrManager->InsVmsControlHs(sCmdTime, sVmsNmbr, sOprCode, sCmdValue, "1", sCmdUser);
            nOkCnt++;
        }
        else
        {
            VmsCtlrManager->InsVmsControlHs(sCmdTime, sVmsNmbr, sOprCode, sCmdValue, "0", sCmdUser);
        }
    }
    else
    {
        // 제어기 리셋
        INT_VMS_RESET_REQ VmsReset;
        BYTE              MsgSeq;

        memset(&VmsReset, 0x00, sizeof(VmsReset));

        MsgSeq = (BYTE)0x20;
        memcpy(VmsReset.CmdTime, AnsiString(sCmdTime).c_str(), INT_VMS_MAX_DATETIME); /* 명령수행시각 */
        memcpy(VmsReset.OperId,  AnsiString(sCmdUser).c_str(), INT_VMS_MAX_OPER_ID);  /* 운영자 ID */
        VmsReset.VMS_CTLR_NMBR = sVmsNmbr.ToIntDef(0);

        result = CComm_VmsResetReq(&VmsReset, MsgSeq);
        if (VERR_NONE == result)
        {
            VmsCtlrManager->InsVmsControlHs(sCmdTime, sVmsNmbr, "V03", "Reset", "1", sCmdUser);
            nOkCnt++;
        }
        else
        {
            VmsCtlrManager->InsVmsControlHs(sCmdTime, sVmsNmbr, "V03", "Reset", "0", sCmdUser);
        }
    }

    sMsg = lblText7->Caption;//"VMS로 전광판/제어기 제어 명령을 요청하였습니다.";
    Application->MessageBox(sMsg.c_str(), sTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
}
//---------------------------------------------------------------------------

