unit fFrmMain;

interface

uses
  Windows, SysUtils, Forms,
  SVPort, ExtCtrls, RTM_Types, Controls, Classes, StdCtrls, Sockets, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinsDefaultPainters, cxLabel, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTelnet, tptelnetobj, dialogs,
  IniFiles, Grids;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit2: TEdit;
    ComboBox1: TComboBox;
    Button5: TButton;
    Button6: TButton;
    Button8: TButton;
    Button9: TButton;
    Memo2: TMemo;
    Button1: TButton;
    Button14: TButton;
    Button15: TButton;
    Edit1: TEdit;
    Edit4: TEdit;
    Button18: TButton;
    Label1: TLabel;
    Edit6: TEdit;
    Edit7: TEdit;
    Button2: TButton;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    cxLabel1: TcxLabel;
    Button23: TButton;
    Button24: TButton;
    Edit10: TEdit;
    Edit11: TEdit;
    cxLabel2: TcxLabel;
    cxLabel3: TcxLabel;
    GroupBox3: TGroupBox;
    cxLabel4: TcxLabel;
    cxLabel5: TcxLabel;
    cxLabel6: TcxLabel;
    cxLabel7: TcxLabel;
    cxLabel8: TcxLabel;
    cxLabel9: TcxLabel;
    Edit12: TEdit;
    Edit13: TEdit;
    Edit14: TEdit;
    cxLabel10: TcxLabel;
    RadioGroup1: TRadioGroup;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    GroupBox4: TGroupBox;
    Button11: TButton;
    cxLabel11: TcxLabel;
    cxLabel12: TcxLabel;
    cxLabel13: TcxLabel;
    cxLabel14: TcxLabel;
    cxLabel15: TcxLabel;
    Edit8: TEdit;
    Edit9: TEdit;
    Edit15: TEdit;
    Edit16: TEdit;
    Edit17: TEdit;
    SVPortTestPort: TSVPort;
    Timer1: TTimer;
    Edit3: TEdit;
    cxLabel18: TcxLabel;
    GroupBox5: TGroupBox;
    GroupBox6: TGroupBox;
    Edit5: TEdit;
    Button4: TButton;
    cxLabel17: TcxLabel;
    Button3: TButton;
    Button7: TButton;
    cxLabel16: TcxLabel;
    Edit18: TEdit;
    RadioGroup2: TRadioGroup;
    procedure FormCreate(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button9Click(Sender: TObject);
    procedure SVPort1ReceiveData(Sender: TObject; DataPtr: Pointer; DataSize: Cardinal);
    procedure Button1Click(Sender: TObject);
    procedure SendTelegram(s: string; ADelayTel : integer = 300; ADelayByte : integer = 1);
    procedure Button14Click(Sender: TObject);
    procedure Button15Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
    procedure Button23Click(Sender: TObject);
    procedure Button24Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button18Click(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure Button4Click(Sender: TObject);
    procedure RadioButton4Click(Sender: TObject);
    procedure RadioButton3Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);

  private
    { Private declarations }
    fTelneObj : TtelnetObj;        // obsahuje objekty pro komunikaci pres USB a Telnet
    fTestTelneObj : TtelnetObj;    // ten samy objekt, ale jen pro otestovani ...
    fUDPObj : TTelnetObjUDP;
    procedure EvaluateUDPPacket(ABuf: ttelegram; AIP: string; Aport: integer);
    procedure overeni_pripojeni(ABuf: ttelegram; AIP: string; Aport: integer);
    procedure test_telnet_connect(AIP:String; APort:integer);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  fbuffer : ttelegram;
  prikazovy_rezim : Boolean;
  butt_overeni_pripojeni : Boolean;
  UkonciTestTelnetObjCounter : integer;
  UkonciTestTelnetObj : boolean;
  DataFile: TIniFile; // *.ini s konfigurací wlan modulu
   testconnection: boolean;

implementation

{$R *.dfm}

// disconnect USB
procedure TForm1.Button11Click(Sender: TObject);
begin
  fTelneObj.DisConnect; // odpojeni od portu
  fTelneObj.Free;       // uvolneni objektu
  fTelneObj := nil;
end;


// uvede modul do prikazoveho rezimu, pak je mozno konfigurovat
// do te doby funguje jako tzv datapipe - preposila vse co mu prijde
procedure TForm1.Button14Click(Sender: TObject);
begin
  SendTelegram ('$$$');
end;


// prikaz pro ulozeni konfigurace
procedure TForm1.Button15Click(Sender: TObject);
begin
    SendTelegram('save' +#13#10  ,300,0);
end;


// overeni konfigurace modulu
procedure TForm1.Button4Click(Sender: TObject);
begin
  memo1.Lines.Add('Testuji pripojeni k adapteru....');
   testconnection := true;
end;


// ulozeni konfigurace z formulare do wlan modulu
procedure TForm1.Button18Click(Sender: TObject);
begin

  if RadioGroup1.ItemIndex = -1 then    // pokud neni oznacena zadna polozka (vybrana ADHOC nebo FIREMNI SIT)
  begin
    ShowMessage('Vyber Adhoc nebo Mistni sit');
    exit;
  end;

  if RadioGroup2.ItemIndex = -1 then    // pokud neni oznacena zadna polozka (vybrana ADHOC nebo FIREMNI SIT)
  begin
    ShowMessage('Vyber 802.11b nebo 802.11g');
    exit;
  end;

  if  prikazovy_rezim = false then      // pokud neni v prikazovem rezimu tak ho do nej uvest
  begin
    SendTelegram ('$$$');
    prikazovy_rezim := true;
  end;

// rozdilne polozky dle nastaveni ADHOC/FIREMNI SIT
  if RadioGroup1.ItemIndex = 0 then       // ADHOC
  begin
    SendTelegram('set wlan join 4'   +#13#10 ,300,0);     // set wlan join 4 -> ad-hoc sit
    SendTelegram('set ip dhcp 0'     +#13#10 ,300,0);     // automaticky vypnout DHCP - u ADHOC neni podporovano
    SendTelegram('set ip protocol 8' +#13#10 ,300,0);
  end

  else if RadioGroup1.ItemIndex = 1 then  // FIREMNI SIT
  begin
    SendTelegram('set wlan join 1'    +#13#10 ,300,0);     // set wlan join 1 -> firemni sit - pripojeni k AP
    SendTelegram('set wlan pass '   +edit14.Text +#13#10  ,300,0);  // PASS = heslo pro WPA i WEP
    SendTelegram('set wlan phrase ' +edit14.Text +#13#10  ,300,0);  // PHRASE = heslo pro WPA/WPA2, radeji pouzit oba
    SendTelegram('set ip protocol 3'             +#13#10  ,300,0);

    if RadioButton3.Checked = true then
      begin
        SendTelegram('set ip dhcp 0' +#13#10 ,300,0);
        SendTelegram('set ip address '  +edit6.text    +#13#10 ,300,0);   // IP ADR poslat pouze pokud je DHCP OFF
        SendTelegram('set ip netmask '  +edit7.text    +#13#10 ,300,0);   // MASKU SITE poslat pouze pokud je DHCP OFF
      end;
    if RadioButton4.Checked = true then
      begin
        SendTelegram('set ip dhcp 1' +#13#10 ,300,0);
      end;
  end;

// stejne polozky pro ADHOC i pro FIREMNI SIT
    if RadioGroup2.ItemIndex = 0 then       // 802.11b
      begin
        SendTelegram('set wlan rate 1' +#13#10 ,300,0); // nastaveni 802.11b - 2Mbit/s
      end
    else  // 802.11g
      begin   // ikdyby nastala chyba a "RadioGroup2.ItemIndex = -1" muze byt nastavena defaultni hodnota 802.11g 24 Mb/s
        SendTelegram('set wlan rate 12' +#13#10 ,300,0); // nastaveni 802.11g - 24Mbit/s
      end;

    SendTelegram('set wlan ssid '   +edit1.text    +#13#10 ,300,0);
    SendTelegram('set ip localport '+edit13.text   +#13#10 ,300,0);
    SendTelegram('set wlan chan '   +edit4.text    +#13#10 ,300,0);
    SendTelegram('set ip gateway '  +edit18.text   +#13#10 ,300,0);
    SendTelegram('set opt deviceid SuperVAG_'+edit12.text   +#13#10 ,300,0);

    SendTelegram('save'                            +#13#10 ,300,0);
end;

// smaze memo1 a memo2
procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Clear;
  Memo2.Clear;
end;



// Telnet
procedure TForm1.Button23Click(Sender: TObject);
begin
  if fTelneObj = nil then // neni-li objekt vytvoren, pak ho vytvorime
    ftelneobj := TTelnetObjWifi.Create; // vytvareni je napr. pri zmene rychlosti
  ftelneobj.SetHost ( edit10.Text );
  ftelneobj.Setport ( StrToInt(edit11.Text) );

  if fTelneObj.Connect
    then
      begin
        memo1.Lines.Add('connected at ' + fTelneObj.fHost );
        Button23.Enabled := false;  // DISABLE button connect
        Button24.Enabled := true;   // ENABLE button Disconnect
      end
    else memo1.Lines.Add('not connected at ' + fTelneObj.fHost );
end;


procedure TForm1.Button24Click(Sender: TObject);
begin
  if fTelneObj.Disconnect
    then
      begin
      memo1.Lines.Add('DISconnected at ' + ftelneobj.fHost );
      Button23.Enabled := true;  // DISABLE button connect
      Button24.Enabled := false;   // ENABLE button Disconnect
      end
    else memo1.Lines.Add('not DISconnected at ' + ftelneobj.fHost );
  ftelneobj.Free;
  fTelneObj := nil;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  //zde doplnit vyhledavani volneho kanalu
  SendTelegram('scan'   +#13#10  ,300,0);

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  // pokud existuje wlan_cfg.ini pouze otevre, nevytvari znovu
  // hleda v adresari kde je umistena tato aplikace
  DataFile := TIniFile.Create(ExtractFilePath(Application.ExeName)+'wlan_cfg.ini');

  // Zapíu do INI souboru do 'Nastaveni parametru WLAN modulu' pod poloku 'Typ site' hodnotu RadioGroup1.ItemIndex ktera rika jestli je nastavena Adhoc nebo Mistni sit
  DataFile.WriteInteger('Nastaveni parametru WLAN modulu','Typ site',RadioGroup1.ItemIndex);
  DataFile.Writebool('Nastaveni parametru WLAN modulu','DHCP ON',RadioButton3.Checked);
  DataFile.WriteString('Nastaveni parametru WLAN modulu','SSID',Edit1.Text);
  DataFile.WriteString('Nastaveni parametru WLAN modulu','Heslo',Edit14.Text);
  DataFile.WriteString('Nastaveni parametru WLAN modulu','IP adresa',Edit6.Text);
  DataFile.WriteString('Nastaveni parametru WLAN modulu','Maska',Edit7.Text);
  DataFile.WriteString('Nastaveni parametru WLAN modulu','Brana',Edit18.Text);
  DataFile.WriteString('Nastaveni parametru WLAN modulu','Cislo adapteru',Edit12.Text);
  DataFile.WriteString('Nastaveni parametru WLAN modulu','Port',Edit13.Text);
  DataFile.WriteString('Nastaveni parametru WLAN modulu','Kanal',Edit4.Text);

  // uvolneni souboru
  DataFile.Free;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
  DataFile := TIniFile.Create(ExtractFilePath(Application.ExeName)+'wlan_cfg.ini');

  RadioGroup1.ItemIndex :=  DataFile.ReadInteger('Nastaveni parametru WLAN modulu','Typ site',-1);
  DataFile.ReadBool('Nastaveni parametru WLAN modulu','DHCP ON',RadioButton3.Checked);
  Edit1.Text  := DataFile.ReadString('Nastaveni parametru WLAN modulu','SSID','');
  Edit14.Text := DataFile.ReadString('Nastaveni parametru WLAN modulu','Heslo','');
  Edit6.Text  := DataFile.ReadString('Nastaveni parametru WLAN modulu','IP adresa','');
  Edit7.Text  := DataFile.ReadString('Nastaveni parametru WLAN modulu','Maska','');
  Edit18.Text := DataFile.ReadString('Nastaveni parametru WLAN modulu','Brana','');
  Edit12.Text := DataFile.ReadString('Nastaveni parametru WLAN modulu','Cislo adapteru','');
  Edit13.Text := DataFile.ReadString('Nastaveni parametru WLAN modulu','Port','');
  Edit4.Text  := DataFile.ReadString('Nastaveni parametru WLAN modulu','Kanal','');

  DataFile.Free;
end;

// odesila jednotlive prikazy
procedure TForm1.Button5Click(Sender: TObject);
begin
    sendtelegram(Edit2.Text+#10#13);     // zde nechat poradi #10#13 - funguje lepe jak #13#10
end;

// PORT CONNECT - pripojeni k USB portu
procedure TForm1.Button6Click(Sender: TObject);
begin
  fbuffer.Empty;
  if fTelneObj = nil then // neni-li objekt vytvoren, pak ho vytvorime
    fTelneObj := TTelnetObjUSB.Create; // vytvareni je napr. pri zmene rychlosti
  ftelneobj.SetPort(strtoint(ComboBox1.text));    // nastav port, ktery je vybrany v komponente
  fTelneObj.Connect;
end;


// odeslani prikazu pro reset WLAN modulu
procedure TForm1.Button8Click(Sender: TObject);
begin
  sendtelegram('reboot'#13);
  prikazovy_rezim := false;
end;

// provede exit z prikazoveho (konfiguracniho) rezimu WLAN modulu
procedure TForm1.Button9Click(Sender: TObject);
begin
  sendtelegram('exit'#13#10);
  prikazovy_rezim := false;
end;


// zde se uvolni pouzivane objekty
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if fTelneObj <> nil  then
    ftelneobj.free;

  fUDPObj.free;      // vytvari se s formem, uvolnit vzdy

  if fTestTelneObj <> nil  then // uz se uvolnuje v TIMER, ale pro jistotu
    fTestTelneObj.free;
end;

// vytvoreni formu
procedure TForm1.FormCreate(Sender: TObject);
var
  Lportlist  : tbytearray;
  LPortCount : byte;
  i : integer;
begin
  fTelneObj := nil;
  fTestTelneObj := nil;
  UkonciTestTelnetObj := false;
  testconnection  := false;

  // Detekce a vypis portu
  SVPortTestPort.EnumCOMPorts( LPortlist, LPortCount );
  if LPortCount <> 0 then begin    // pokud jsou nejake porty dostupne
    for I := 0 to LPortCount-1 do begin
      ComboBox1.Items.Add( IntToStr( LPortlist[I] ) );     // pridani polozek do Items
    end;
  end;
  if ComboBox1.ItemIndex = -1 then
      ComboBox1.ItemIndex := 0;

  fUDPObj := TTelnetObjUDP.Create();

  RadioGroup1.ItemIndex := 1;    // Nastaveni parametru wifi - firemni sit

  button11.Enabled := false;  // zakazani tlacitka Disconnect port
  button24.Enabled := false;  // zakazani tlacitka Disconnect from Telnet
end;

// nastaveni rychlosti UARTu, 9600 je default po resetu vsech nastaveni
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  if fTelneObj = nil
    then fTelneObj := TTelnetObjUSB.Create();
  fTelneObj.ChangeComPortSpeed (9600);
end;

// nastaveni rychlosti UARTu, 125000 je nase standardni rychlost
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  if fTelneObj = nil
    then fTelneObj := TTelnetObjUSB.Create();
  fTelneObj.ChangeComPortSpeed (125000);
end;

// if DHCP ON
procedure TForm1.RadioButton3Click(Sender: TObject);
begin
  edit6.Enabled    := true;     // pri DHCP ON posilat IP adresu
  edit7.Enabled    := true;     // pri DHCP ON posilat MASKU SITE
  edit18.Enabled   := true;     // pri DHCP ON posilat BRANU
  cxLabel4.Enabled := true;     // label IP ADRESA zapnout
  cxLabel5.Enabled := true;     // label MASKA SITE zapnout
  cxLabel16.Enabled := true;    // label BRANA zapnout
end;

// if DHCP OFF
procedure TForm1.RadioButton4Click(Sender: TObject);
begin
  if RadioGroup1.ItemIndex = 1 then   // pouze pokud je nastavena ADHOC sit
  begin
    edit6.Enabled := false;       // pri DHCP OFF neposilat IP adresu
    edit7.Enabled := false;       // pri DHCP OFF neposilat MASKU SITE
    edit18.Enabled := false;      // pri DHCP OFF neposilat BRANU
    cxLabel4.Enabled := false;    // label IP ADRESA vypnout
    cxLabel5.Enabled := false;    // label MASKA SITE vypnout
    cxLabel16.Enabled := false;   // label BRANA vypnout
  end;
end;

// v rezimu ADHOC vypnout/zamcit tlacitka s volbou DHCP - DHCP pri ADHOC neni podporovan
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
  if RadioGroup1.ItemIndex = 0 then   // pokud je nastavena adhoc
  begin
    RadioButton4.Checked := true;     // nastavit na TRUE = DHCP OFF
    RadioButton3.Enabled := false;    // u ADHOC neni podporovano DHCP, proto vypnout
    RadioButton4.Enabled := false;    // pristup k tlacitkum
    edit14.Enabled := false;          // u ADHOC neni mozne heslo, vypnout edit pro zadavani hesla
    cxLabel10.Enabled := false;       // label PASSWORD vypnout
    cxLabel7.Enabled := false;        // label DHCP vypnout
      Edit6.Enabled := true;        // zapnout edit IP ADRESA
      Edit7.Enabled := true;        // zapnout edit MASKA SITE
      Edit18.Enabled := true;       // zapnout edit BRANA
      cxLabel4.Enabled := true;     // label IP ADRESA zapnout
      cxLabel5.Enabled := true;     // label MASKA SITE zapnout
      cxLabel16.Enabled:= true;     // label BRANA zapnout
  end
  else
  begin
    RadioButton3.Enabled := true;    // u ADHOC neni podporovano DHCP, proto vypnout
    RadioButton4.Enabled := true;    // pristup k tlacitkum
    RadioButton3.Checked := true;    // nastavit na TRUE = DHCP ON - pravdepodobnejsi varianta
    edit14.Enabled := true;          // v rezimu FIREMNI SIT zapnout edit pro zadavani hesla
    cxLabel10.Enabled := true;       // label PASSWORD zapnout
    cxLabel7.Enabled  := true;       // label DHCP zapnout
  end;
end;

procedure TForm1.SendTelegram(s: string; ADelayTel : integer = 300; ADelayByte : integer = 1);
begin
  fTelneObj.SendTelegram(s);
end;

// Event editu - provede se pri zmacknuti klavesy ENTER
procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
  if Ord(Key) = VK_RETURN then
    begin
    sendtelegram(Edit2.Text+#13 ); // pri zmacknuti klavesy ENTER se prida znak #10, nepridavat ho za prikaz
    edit2.Text := '';
    end;
end;


// funkce na otestovani pripojeni k telnetu
procedure TForm1.test_telnet_connect(AIP:String; APort:integer);
begin
  UkonciTestTelnetObj := false;
  if fTestTelneObj = nil then // neni-li objekt vytvoren, pak ho vytvorime
    fTesttelneobj := TTelnetObjWifi.Create;
  fTesttelneobj.SetHost ( AIP );      // nastaveni IP a PORTu
  fTesttelneobj.Setport ( APort );

  if fTesttelneobj.Connect then
  begin
    memo1.Lines.Add(' Connected at ' + AIP + ':' + IntToStr(APort) );
    memo1.Lines.Add(' Pripojeni probehlo v poradku');
  end
  else memo1.Lines.Add('NOT connected at ' + AIP + ':' + IntToStr(APort) );
end;

procedure TForm1.overeni_pripojeni(ABuf:ttelegram; AIP:String; APort:integer);
var
tmp : TTelegram;
nazev_zarizeni_txt : string;  // text obsahujici hodnotu "SuperVAG_"
nazev_zarizeni_num : string;  // text obsahuje cislo adapteru
orig_zarizeni_num : string;     // cislo adapteru, ktery se overuje
begin
  orig_zarizeni_num := edit5.Text;    // orig cislo vyctu z formu
  tmp := ABuf;
  nazev_zarizeni_num := tmp.copy(69,5).asstring;
  nazev_zarizeni_txt := tmp.copy(60,9).asstring;

  if (nazev_zarizeni_txt = 'SuperVAG_') then
  begin
    if (nazev_zarizeni_num = orig_zarizeni_num)  then
      begin
        memo1.Lines.Add('Cislo adapteru se shoduje');
        memo1.Lines.Add('');
        memo1.Lines.Add('Testuji pripojeni pres Telnet...');
        test_telnet_connect(AIP, APort);  // volam fci na test telnetu
      end
    else
      begin
        memo1.Lines.Add('Cislo adapteru se neshoduje!');
        memo1.Lines.Add('Tento adapter neni pripojen do site!');
      end;
  end
  else
  begin
    memo1.Lines.Add('Nazev "SuperVAG_" se neshoduje!');
  end;
butt_overeni_pripojeni := false;  // vypnout flag na overovani
end;


procedure TForm1.EvaluateUDPPacket(ABuf:ttelegram; AIP:String; APort:integer);
var
i : integer;
rssi : integer;
begin
  rssi := -1 * ABuf.ByteArray[7];
  edit8.text  := abuf.copy(60,32).asstring;        // Nazev zarizeni _ pozice: 60B-91B, max velikost: 32B
  edit9.text  := abuf.copy(0,6).ashexstring;       // MAC ADR AP     _ pozice: 0B-5B, velikost: 6B
  edit15.text := IntToStr(rssi)+ 'dBm / ' +        // RSSI           _ pozice: 7B, velikost: 1B
                      IntToStr( 110+rssi ) + '%';
  edit16.text := abuf.copy(6,1).asdecstring;       // Kanal wifi     _ pozice: 6B, velikost: 1B
  edit17.text := abuf.copy(32,26).asstring;        // Verze firmware _ pozice: 32B-59B, max velikost velikost: 26B
  edit3.text := AIP + ':' +IntToStr(APort);
end;


procedure TForm1.SVPort1ReceiveData(Sender: TObject; DataPtr: Pointer;  DataSize: Cardinal);
var
  msg : TTelegram;
begin
  msg.AssignBuffer(DataPtr, DataSize);
  memo1.Lines.Add(msg.AsString);
  memo2.Lines.Add(msg.AsDecString);
end;

// vypis prichozi zprav
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if fTelneObj <> nil then    // objekt obsahujici zpravy prichazejici pres Telnet
  begin
    if ftelneobj.fanswer.size > 0 then  // pokud je nejaka zprava
    begin
      memo1.Lines.Add(ftelneobj.fanswer.asstring);  // vypsani prichozi zpravy do okna
      memo2.Lines.Add(ftelneobj.fanswer.AsDECString);   // vypsani zpravy jako DEC string - kvuli netisknutelnym znakum
      ftelneobj.fanswer.empty;
    end;
  end;

  if fTestTelneObj <> nil then    // objekt obsahujici zpravy prichazejici pres TESTTelnet
  begin
    if fTestTelneObj.fanswer.size > 0 then  // pokud je nejaka zprava
    begin
      if fTestTelneObj.fanswer.Pos('*HELLO*')>-1 then
      begin
        fTestTelneObj.fanswer.empty;
        memo1.Lines.Add(' Test "HELLO" probehl OK');
        UkonciTestTelnetObj := true;
      end;
    end;
  end;

  if UkonciTestTelnetObj then
  begin
    if fTesttelneobj <> nil then
    begin
      memo1.Lines.Add(' Ukoncuji testovaci pripojeni');
      if UkonciTestTelnetObjCounter > 3 then // ukoncujeme test, protoze dobehl citac
        memo1.Lines.Add(' Test pravdepodobne neprobehl OK');
      if fTesttelneobj.Disconnect then  begin     // odpojeni
        memo1.Lines.Add(' DISconnected at '  + fTestTelneObj.fHost + ':' + IntToStr(fTestTelneObj.fPort) ); end
      else memo1.Lines.Add(' NOT DISconnected at '  + fTestTelneObj.fHost + ':' + IntToStr(fTestTelneObj.fPort) );
      fTestTelneObj.Free;
      fTestTelneObj := nil;
    end;
  end
  else begin
    if fTestTelneObj <> nil then
      UkonciTestTelnetObjCounter := UkonciTestTelnetObjCounter + 1;
  end;

  if UkonciTestTelnetObjCounter > 3 then
  begin
    if fTestTelneObj <> nil then
    begin
      memo1.Lines.Add(' Vyprsel timeout, ukoncuji test');
      UkonciTestTelnetObj := true;
    end;
  end;


  if fUDPObj <> nil then      // objekt obsahujici zpravy prichazejici pres UDP broadcast
  begin
    if fUDPObj.fanswer.size > 0 then  // pokud je nejaka zprava v objektu
    begin
      if fUDPObj.fanswer.size=110 then     // delka paketu je 110B a je vzdy konstantni
      begin
        EvaluateUDPPacket(fUDPObj.fanswer, fUDPObj.fPeerIP, fUDPObj.fPeerPort);         // rozhozeni paketu do formulare
        if testconnection then  // pokud je volana funkce test pripojeni
        begin
          overeni_pripojeni(fUDPObj.fanswer, fUDPObj.fPeerIP, fUDPObj.fPeerPort);
          testconnection := false;
        end;
        fUDPObj.fanswer.empty;
        fUDPObj.fPeerIP := '';
        fUDPObj.fPeerPort := 0;
      end;
    end;
  end;

end;


end.
