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

#include <vcl.h>
#pragma hdrstop

#include "FrmControllerInfoF.h"
#include "FrmCommLogF.h"
#include "FrmMainF.h"
#include "AppGlobalF.h"
#include "PacketHandllingF.h"
#include "FrmSetTimeF.h"
#include "FrmSetBrghF.h"
#include "FrmSetConfigF.h"
#include "VmsProtocolF.h"
#include "CDSCtlrF.h"
#include "ClientSessionF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxCalc"
#pragma link "cxClasses"
#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 "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "dxGDIPlusClasses"
#pragma link "cxSpinEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"
TFrmControllerInfo *FrmControllerInfo = NULL;

String VmsModuleType(TCDSCtlr *AObj)
{
#if 0
    if (AObj->VmsType == "VUTN") return "문자식";
    return "도형식";
#else
return "";
#endif
}
//---------------------------------------------------------------------------

String VmsOperMode(TCDSCtlr *AObj)
{
    if (AObj->OPER_MODE == "A") return "자동";
    return "고정";
}
//---------------------------------------------------------------------------

String VmsComm(TCDSCtlr *AObj)
{
    if (AObj->Client.commState == comm_open) return "정상";
    return "장애";
}
//---------------------------------------------------------------------------

String VmsModuleState(TCDSCtlr *AObj)
{
    if (AObj->Client.commState != comm_open) return "-";
    if (AObj->RSTATE.ModuleState == vms_comm_normal) return "정상";
    return "장애";
}
//---------------------------------------------------------------------------

String VmsModulePower(TCDSCtlr *AObj)
{
    if (AObj->Client.commState != comm_open) return "-";
    if (AObj->RSTATE.ModulePowerStatus == vms_module_power_on) return "켜짐";
    return "꺼짐";
}
//---------------------------------------------------------------------------

String VmsModuleLuminance(TCDSCtlr *AObj)
{
    if (AObj->Client.commState != comm_open) return "-";
    int nLuminance = AObj->RSTATE.LuminanceStatus;
    return String(nLuminance);
}
//---------------------------------------------------------------------------

String VmsModuleDoor(TCDSCtlr *AObj)
{
    if (AObj->Client.commState != comm_open) return "-";
    if (AObj->RSTATE.DoorStatus == vms_door_open) return "열림";
    return "닫힘";
}
//---------------------------------------------------------------------------


String VmsModuleFan(TCDSCtlr *AObj)
{
    if (AObj->Client.commState != comm_open) return "-";
    if (AObj->RSTATE.FanStatus == vms_fan_on) return "동작";
    return "정지";
}
//---------------------------------------------------------------------------


String VmsModuleHeater(TCDSCtlr *AObj)
{
    if (AObj->Client.commState != comm_open) return "-";
    if (AObj->RSTATE.HeaterStatus == vms_heater_on) return "가동";
    return "중지";
}
//---------------------------------------------------------------------------

String VmsModuleTemp(TCDSCtlr *AObj)
{
    if (AObj->Client.commState != comm_open) return "-";
    int nTemp = AObj->RSTATE.BodyTemp;
    return String(nTemp);//+"℃";
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
__fastcall TFrmControllerInfo::TFrmControllerInfo(TComponent* Owner)
    : TForm(Owner)
{
    FControllerCnt = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::UpdateCtlrList()
{
    int nRow;
    TcxDataController *pGDC = TvList->DataController;
    try
    {
        TvList->BeginUpdate();
        pGDC->RecordCount = 0;

        CtlrItr it;
        for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
        {
            TCDSCtlr *pObj = (TCDSCtlr*)it->second;

            nRow = pGDC->AppendRecord();

            pGDC->Values[nRow][Col00->Index] = pObj->Server.commState;
            pGDC->Values[nRow][Col01->Index] = "";
            pGDC->Values[nRow][Col02->Index] = pObj->CTLR_NMBR;
            pGDC->Values[nRow][Col03->Index] = pObj->CTLR_ID;
            pGDC->Values[nRow][Col04->Index] = pObj->NM;
            pGDC->Values[nRow][Col05->Index] = pObj->LOGIN_IPADDR;//CTLR_IP;
            pGDC->Values[nRow][Col06->Index] = "오류";

            pGDC->Values[nRow][Col07->Index] = pObj->OPER_MODE;
            pGDC->Values[nRow][Col08->Index] = pObj->dwConnectCnt;
            pGDC->Values[nRow][Col09->Index] = String(pObj->CTLR_LOCAL_NO);
            pGDC->Values[nRow][Col10->Index] = (pObj->PROTOCOL_VER == 1) ? "T" : "F";
            pGDC->Values[nRow][Col11->Index] = "-";
            pGDC->Values[nRow][Col12->Index] = "-";
            pGDC->Values[nRow][Col13->Index] = "-";
            pGDC->Values[nRow][Col14->Index] = pObj->Server.ConnectTm;
            pGDC->Values[nRow][Col15->Index] = pObj->Server.DisconnectTm;

            pGDC->Values[nRow][Col99->Index] = (int)pObj;
        }
    }
    __finally
    {
        TvList->EndUpdate();
        FControllerCnt = pGDC->RecordCount;
    }

    CDSCtlrManager->Total  = CDSCtlrManager->Count;
    CDSCtlrManager->Error  = CDSCtlrManager->Count;
    CDSCtlrManager->Normal = 0;
    CDSCtlrManager->Module = 0;

    plTot->Caption = String(CDSCtlrManager->Count);
    plErr->Caption = String(CDSCtlrManager->Count);
    plNor->Caption = "0";
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::UpdateServerState(int AObj)
{
    TcxDataController *pGDC = TvList->DataController;
    try
    {
        TvList->BeginUpdate();

        try
        {
            int          nErrCnt = 0;
            int          nMemPtr;
            TCDSCtlr *pObj;
            for (int nRow = 0; nRow < FControllerCnt; nRow++)
            {
                nMemPtr = pGDC->Values[nRow][Col99->Index];
                pObj = (TCDSCtlr*)nMemPtr;
                if (!pObj) continue;

                pGDC->Values[nRow][Col00->Index] = pObj->Server.commState;
                if (pObj->Server.commState != comm_open)
                {
                    nErrCnt++;
                }
                pGDC->Values[nRow][Col05->Index] = pObj->LOGIN_IPADDR;//CTLR_IP;
                pGDC->Values[nRow][Col14->Index] = pObj->Server.ConnectTm;
                pGDC->Values[nRow][Col08->Index] = pObj->dwConnectCnt;
                pGDC->Values[nRow][Col15->Index] = pObj->Server.DisconnectTm;
            }

            CDSCtlrManager->Error  = nErrCnt;
            CDSCtlrManager->Normal = FControllerCnt-nErrCnt;
            plTot->Caption = String(FControllerCnt);
            plErr->Caption = String(nErrCnt);
            plNor->Caption = String(FControllerCnt-nErrCnt);
        }
        catch(Exception &e)
        {
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::UpdateClientState(int AObj)
{
    String sNormal = "정상";
    String sError  = "오류";

    TcxDataController *pGDC = TvList->DataController;
    try
    {
        TvList->BeginUpdate();

        try
        {
            int          nMemPtr;
            TCDSCtlr *pObj;
            for(int nRow = 0; nRow < FControllerCnt; nRow++)
            {
                nMemPtr = pGDC->Values[nRow][Col99->Index];
                pObj = (TCDSCtlr*)nMemPtr;
                if (!pObj) continue;

                if (pObj->Server.commState == comm_open)
                    pGDC->Values[nRow][Col06->Index] = sNormal;
                else
                    pGDC->Values[nRow][Col06->Index] = sError;

                pGDC->Values[nRow][Col07->Index] = pObj->OPER_MODE;
            }
        }
        catch(Exception &e)
        {
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::Col01CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo->GridRecord->RecordIndex < 0) return;

    Graphics::TBitmap *pBmp = NULL;
    pBmp = ImgState1->Picture->Bitmap;
	if (!VarIsNull(AViewInfo->GridRecord->DisplayTexts[Col00->Index]))
    {
        String sCode = AViewInfo->GridRecord->DisplayTexts[Col00->Index];
        int    nCode = sCode.ToIntDef(2);

        if      (nCode == comm_open ) pBmp = ImgState0->Picture->Bitmap;
        else if (nCode == comm_close) pBmp = ImgState1->Picture->Bitmap;
        else                          pBmp = ImgState3->Picture->Bitmap;
    }

    if (!pBmp) return;
    pBmp->Transparent = true;

    int nW, nH;
    nW = nH = 21;
    try
    {
        TColor bBrush = ACanvas->Brush->Color;
        if (AViewInfo->GridRecord->Selected)
        {
            if (AViewInfo->GridRecord->Index % 2 == 0)
            {
                bBrush = Sender->LookAndFeelPainter->DefaultContentEvenColor();
            }
            else
            {
                bBrush = Sender->LookAndFeelPainter->DefaultContentOddColor();
            }
        }
        ACanvas->Brush->Color = bBrush;

        TRect rc;
        TRect rcOrg = AViewInfo->Bounds;
        ACanvas->FillRect(rcOrg);

        nW = rcOrg.Bottom - rcOrg.Top - 4;
        rc.Top    = rcOrg.Top+2;
        rc.Bottom = rcOrg.Bottom - 2;
        rc.Left   = rcOrg.Left + (rcOrg.Width() - nW) / 2;
        rc.Right  = rc.Left + nW;

        ACanvas->Canvas->StretchDraw(rc, pBmp);
        //ACanvas->Draw(AViewInfo->ContentBounds.Left, AViewInfo->ContentBounds.Top, pBmp);
        ADone = true;
    }
    catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift,
          bool &AHandled)
{
    if (!ACellViewInfo) return;
    try {
        TcxDataController *pGDC = TvList->DataController;
        int nRow = pGDC->FocusedRecordIndex;
        if (nRow < 0) return;

        int nMemPtr = pGDC->Values[nRow][Col99->Index];
        if (!nMemPtr) return;

        if (!FrmCommLog->Showing)
        {
            FrmCommLog->Top = VmsCommMain->Top;
            //FrmCommLog->Left= VmsCommMain->Left+VmsCommMain->Width;
            FrmCommLog->Show();
        }
        else
        {
            FrmCommLog->BringToFront();
        }
        FrmCommLog->ResetDisplayLog(nMemPtr);
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::TvListCellClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift,
          bool &AHandled)
{
	if (AButton != mbRight) return;
    if (!ACellViewInfo) return;

    try {
        TcxDataController *pGDC = TvList->DataController;
        int nRow = pGDC->FocusedRecordIndex;
        if (nRow < 0) return;

        FSelControllerNo = VarToStr(pGDC->Values[nRow][Col02->Index]);
        FSelControllerNm = VarToStr(pGDC->Values[nRow][Col04->Index]);

        TCDSCtlr *pObj = CDSCtlrManager->FLists.Find(FSelControllerNo);
        if (!pObj) return;

        if (pObj->Server.commState != comm_open)
        {
            return;
        }

        POINT p;
        GetCursorPos(&p);

        pmuController->Popup(p.x, p.y);
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::pmuControllerPopup(TObject *Sender)
{
    mnuControllerId->Caption = FSelControllerNm;
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::mnuDlndScenarioClick(TObject *Sender)
{
    //시나리오 다운로드
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
    pMsg->Type   = eVmsScenarioDownload;
    pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
    g_jobQ.PushBlocking((DWORD)pMsg);
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::mnuStatusClick(TObject *Sender)
{
    //상태정보 요청
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
    pMsg->Type   = eVmsStatusReq;
    pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
    g_jobQ.PushBlocking((DWORD)pMsg);
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::mnuReqParamClick(TObject *Sender)
{
    //파라미터 요청
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
    pMsg->Type   = eVmsParamReq;
    pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
    g_jobQ.PushBlocking((DWORD)pMsg);
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::LEDOnOffClick(TObject *Sender)
{
    //LED On/Off
    TMenuItem *pItem = (TMenuItem*)Sender;
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
    pMsg->Type    = eVmsStatusControl;
    pMsg->ObjPtr  = (DWORD)pCDSCtlr->FSession;
    pMsg->Buff[0] = 0x01;
    pMsg->Buff[1] = (BYTE)pItem->Tag;
    pMsg->Len     = 2;
    g_jobQ.PushBlocking((DWORD)pMsg);
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::FANOnOffClick(TObject *Sender)
{
    //FAN On/Off
    TMenuItem *pItem = (TMenuItem*)Sender;
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
    pMsg->Type    = eVmsStatusControl;
    pMsg->ObjPtr  = (DWORD)pCDSCtlr->FSession;
    pMsg->Buff[0] = 0x07;
    pMsg->Buff[1] = (BYTE)pItem->Tag;
    pMsg->Len     = 2;
    g_jobQ.PushBlocking((DWORD)pMsg);
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::mnuSetBrghClick(TObject *Sender)
{
    //휘도설정
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    FrmSetBrgh = new TFrmSetBrgh(this);
    try
    {
        try
        {
            FrmSetBrgh->EdMode->Text     = String(pCDSCtlr->BRGH_MODE);
            FrmSetBrgh->EdCurrStep->Text = String(pCDSCtlr->BRGH_CURR_STEP);
            FrmSetBrgh->EdWeekStep->Text = String(pCDSCtlr->BRGH_WEEK_STEP);
            FrmSetBrgh->EdMghtStep->Text = String(pCDSCtlr->BRGH_NGHT_STEP);
            FrmSetBrgh->ShowModal();
            if (FrmSetBrgh->FUpdate)
            {
                if (!pCDSCtlr) return;
                if (!pCDSCtlr->FSession) return;

                SIGNT_BRGH_SET set;
                set.VMS_CTLR_NMBR = pCDSCtlr->CTLR_NMBR.ToIntDef(0);
                set.BrghMode      = (BYTE)FrmSetBrgh->EdMode->Text.Trim().ToIntDef(30);
                set.BrghCurrStep  = (BYTE)FrmSetBrgh->EdCurrStep->Text.Trim().ToIntDef(30);
                set.BrghWeekStep  = (BYTE)FrmSetBrgh->EdWeekStep->Text.Trim().ToIntDef(30);
                set.BrghNghtStep  = (BYTE)FrmSetBrgh->EdMghtStep->Text.Trim().ToIntDef(30);

#if 0
                IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
                pMsg->Type    = eVmsBrghSet;
                pMsg->ObjPtr  = (DWORD)pCDSCtlr->FSession;
                memcpy(pMsg->Buff, (char*)&set, sizeof(SIGNT_BRGH_SET));
                g_jobQ.PushBlocking((DWORD)pMsg);
#else
                BYTE cmdVal = 0x00;
                switch(set.BrghMode)
                {
                case 0x00:  //주간
                    cmdVal = set.BrghWeekStep;
                    break;
                case 0x01:  //야간
                    cmdVal = set.BrghNghtStep;
                    break;
                case 0x02:  //자동
                    cmdVal = 0x00;
                    break;
                default: return;
                }
                IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
                pMsg->Type    = eVmsStatusControl;
                pMsg->ObjPtr  = (DWORD)pCDSCtlr->FSession;
                pMsg->Buff[0] = 0x06;
                pMsg->Buff[1] = (BYTE)set.BrghMode;
                pMsg->Buff[2] = cmdVal;
                pMsg->Len     = 3;
                g_jobQ.PushBlocking((DWORD)pMsg);
#endif
            }
        }
        catch(Exception &e)
        {
        }
    }
    __finally
    {
        SAFE_DELETE(FrmSetBrgh);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::mnuSetConfigClick(TObject *Sender)
{
    //환경설정
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    FrmSetConfig = new TFrmSetConfig(this);
    try
    {
        try
        {
            FrmSetConfig->EdFanTmpr->Text  = String(pCDSCtlr->FAN_RUN_TMPR);
            FrmSetConfig->EdHetrTmpr->Text = String(pCDSCtlr->HETR_RUN_TMPR);
            FrmSetConfig->EdStrTm->Text    = pCDSCtlr->PANL_ON_TIME;
            FrmSetConfig->EdEndTm->Text    = pCDSCtlr->PANL_OFF_TIME;
            FrmSetConfig->ShowModal();
            if (FrmSetConfig->FUpdate)
            {
                if (!pCDSCtlr) return;
                if (!pCDSCtlr->FSession) return;

                SIGNT_CONFIG_SET set;
                set.VMS_CTLR_NMBR = pCDSCtlr->CTLR_NMBR.ToIntDef(0);
                set.FanRunTmpr    = (BYTE)FrmSetConfig->EdFanTmpr->Text.Trim().ToIntDef(30);
                set.HetrRunTmpr   = (BYTE)FrmSetConfig->EdHetrTmpr->Text.Trim().ToIntDef(0);
                memcpy(set.PanlOnTime,  AnsiString(FrmSetConfig->EdStrTm->Text.Trim()).c_str(), 4);
                memcpy(set.PanlOffTime, AnsiString(FrmSetConfig->EdEndTm->Text.Trim()).c_str(), 4);

                IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
                pMsg->Type    = eVmsConfigSet;
                pMsg->ObjPtr  = (DWORD)pCDSCtlr->FSession;
                memcpy(pMsg->Buff, (char*)&set, sizeof(SIGNT_CONFIG_SET));
                g_jobQ.PushBlocking((DWORD)pMsg);
            }
        }
        catch(Exception &e)
        {
        }
    }
    __finally
    {
        SAFE_DELETE(FrmSetConfig);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::mnuSetTimeClick(TObject *Sender)
{
    //시간설정
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
    pMsg->Type    = eVmsStatusControl;
    pMsg->ObjPtr  = (DWORD)pCDSCtlr->FSession;
    pMsg->Buff[0] = 0x04;
    memcpy(&pMsg->Buff[1], AnsiString(Now().FormatString("yyyymmddhhnnss")).c_str(), 14);
    pMsg->Len     = 1+14;
    g_jobQ.PushBlocking((DWORD)pMsg);
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::mnuDisconnectClick(TObject *Sender)
{
    //연결종료
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    pCDSCtlr->FSession->Disconnect();
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::mnuReset1Click(TObject *Sender)
{
    //제어기리셋
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    TMenuItem *pMenu = (TMenuItem*)Sender;
    IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
    pMsg->Type    = eVmsStatusControl;
    pMsg->ObjPtr  = (DWORD)pCDSCtlr->FSession;
    pMsg->Buff[0] = 0x02;
    pMsg->Len     = 1;
    g_jobQ.PushBlocking((DWORD)pMsg);
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::mnuResetClick(TObject *Sender)
{
#if 0
    //제어기리셋
    TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(FSelControllerNo);
    if (!pCDSCtlr)
    {
        MERROR("%s Not Found", AnsiString(FSelControllerNo).c_str());
        return;
    }
    if (!pCDSCtlr->FSession)
    {
        MERROR("%s Session NULL", AnsiString(FSelControllerNo).c_str());
        return;
    }

    TMenuItem *pMenu = (TMenuItem*)Sender;
    IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
    pMsg->Type    = eVmsReset;
    pMsg->ObjPtr  = (DWORD)pCDSCtlr->FSession;
    pMsg->Buff[0] = pMenu->Tag;//1;  //하드웨어 리셋
    g_jobQ.PushBlocking((DWORD)pMsg);
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::mnuRefreshClick(TObject *Sender)
{
    UpdateServerState(0);
    UpdateClientState(0);
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::Panel6DblClick(TObject *Sender)
{
    UpdateServerState(0);
    UpdateClientState(0);
}
//---------------------------------------------------------------------------

void __fastcall TFrmControllerInfo::Panel2DblClick(TObject *Sender)
{
    CtlrItr it;
    for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
    {
        TCDSCtlr *pObj = (TCDSCtlr*)it->second;

        pObj->dwConnectCnt = pObj->Server.commState == comm_open ? 1 : 0;
    }

    UpdateServerState(0);
    UpdateClientState(0);
}
//---------------------------------------------------------------------------

