//---------------------------------------------------------------------------
// For SELMA200, 20180223, 변경사항 없음
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "client.h"
#include "Inspection_Form.h"
#include "Alarm_Form.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TClientForm *ClientForm;
String sMsg, rMsg;

int ConErrFlag = false;
//---------------------------------------------------------------------------
__fastcall TClientForm::TClientForm(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------


boolean __fastcall  TClientForm::SendCmd(int index){
     sMsg =  "alarm," + String(index);
     return Connect();

}

boolean __fastcall  TClientForm::SendCmd(String title, String str){
    sMsg = "push,"+ title + "|" + str;
    return Connect();
}

boolean  __fastcall TClientForm::SendCmd(long s1, long s2, long s3, long s4){
    sMsg = "update," + (String) s1+ "|" + (String)s2 + "|" +  (String)s3 + "|" +  (String) s4;
    return Connect();
}


void __fastcall TClientForm::ClientSocket1Connect(TObject *Sender,
      TCustomWinSocket *Socket)
{
    ConErrFlag = false;
    Memo1->Lines->Clear();
    int len = sMsg.Length();
    //len = 8;
    char *buf = new char[len];
    //int len = sMsg.Length();

    //strcpy(buf,sMsg.c_str());
    memset(buf, 0, len);
    memcpy(buf, sMsg.c_str(), len);


    try{
            Memo1->Lines->Add("[S]"+ sMsg + ConErrFlag);
            ClientSocket1->Socket->SendBuf(buf, len );
            //ShowMessage("2nd");
            //ShowMessage(ConErrFlag);

    }catch(...){
           Memo1->Lines->Add("서버에  접속할 수 없습니다.");
           ShowMessage(" 서버에  접속할 수 없습니다.");
    }
    delete [] buf;

}
//---------------------------------------------------------------------------

void __fastcall TClientForm::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{
    ConErrFlag = false;

    rMsg = Socket->ReceiveText().Trim();
    Memo1->Lines->Add("[R]" + rMsg + ConErrFlag);

    ClientSocket1->Socket->Close();
}
//---------------------------------------------------------------------------

void __fastcall TClientForm::ClientSocket1Error(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{

    ConErrFlag = true;
    ClientSocket1->Socket->Close();
    ErrorCode = 0;
    //ShowMessage("TCP/IP Connection Error 입니다. !!");
    //ShowMessage(ConErrFlag);

}
//---------------------------------------------------------------------------


void __fastcall TClientForm::FormClose(TObject *Sender, TCloseAction &Action)
{
  //If the bacground thread is running
  ClientSocket1->Close();
  Close();

}

boolean __fastcall TClientForm::Connect(){
   boolean flag = false;

        try{

          // ClientSocket1->Close();
           ClientSocket1->Host = "127.0.0.1";
           ClientSocket1->Port = 9005;
           ClientSocket1->Open();
           flag = true;
           ConErrFlag = false;
        }
        catch (Exception &exception)
	{
             Memo1->Lines->Add("예외발생");
             //ShowMessage("TCP/IP Connection 예외 발생 ");
             ClientSocket1->Socket->Close();
	}

   return flag;

}
//---------------------------------------------------------------------------


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

