unit TpTelnetObj;

interface

uses classes, sysutils, rtm_types, IdTelnet, svport, dialogs,
     IdUDPBase, IdUDPClient, IdUDPServer, IdSocketHandle;

type
  TTelnetObj = class(Tobject)
    function GetIpAddrAsStr: String;
    function GetIpAddrAsDWord: cardinal;
  private
    procedure IdTelnet1DataAvailable(Sender: TIdTelnet;
      const Buffer: TArray<System.Byte>); virtual;
    // zde vznika chyba na strane Delphi "Expected '>' but '.' found"
    // tuto chybu ignorovat
    // vice viz: https://forums.embarcadero.com/message.jspa?messageID=435950
  public
    fIdAddress: cardinal;
    fSSID: string;
    fPassword: string;
    fAnswer: ttelegram;
    fHost: string;
    fPort: integer;
// tyto metody se prepisuji
    constructor Create; virtual;
    destructor Destroy; override;
    function Connect: boolean; virtual;
    function DisConnect: boolean; virtual;
    function SendTelegram(ATel: ttelegram; ADelayTel: integer = 300;
      ADelayByte: integer = 1): boolean; virtual;
// obecne parametry, spolecne pro vsechny
    function SetPassword(APassword: string): boolean;
    function SetSSID(ASSID: string): boolean;
    function SetPort(APort: integer): boolean;
    function SetHost(Ahost: string): boolean;
    function ChangeComPortSpeed( ASpeed : integer) :boolean; virtual;

  end;


  TTelnetObjUSB = class(TTelnetObj)
  public
    fSVPort: TSVPort;
    constructor Create;
    function Connect: boolean; override;
    function DisCOnnect: boolean; override;
    function SendTelegram(ATel: ttelegram; ADelayTel: integer = 300;
      ADelayByte: integer = 1): boolean; override;
    procedure SVPortReceiveData(Sender: TObject; DataPtr: Pointer; DataSize: Cardinal);
    function ChangeComPortSpeed( ASpeed : integer) :boolean; override;
  end;

  TTelnetObjWifi = class(TTelnetObj)
  public
    fIdTelnet: TIdTelnet;
    constructor Create; override;
    function Connect: boolean; override;
    function DisCOnnect: boolean; override;
    function SendTelegram(ATel: ttelegram; ADelayTel: integer = 300;
      ADelayByte: integer = 1): boolean; override;
    procedure IdTelnet1DataAvailable(Sender: TIdTelnet;
      const Buffer: TArray<System.Byte>); override;
  end;

  TTelnetObjUDP = class(TTelnetObj)
   public
    fPeerIP: String;    // obsahuje IP adresu
    fPeerPort: Integer;  // obsahuje port, ze ktereho prisly data
    fIdUDPServer1: TIdUDPServer;
    constructor Create; override;
    procedure IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; AData: TArray<System.Byte>; ABinding: TIdSocketHandle);
  end;

implementation

{ TTelnetObj }

function TTelnetObj.Connect: boolean;
begin
end;

constructor TTelnetObj.Create;
begin
end;

destructor TTelnetObj.Destroy;
begin
  disconnect;
end;

function TTelnetObj.DisConnect: boolean;
begin
end;

function TTelnetObj.ChangeComPortSpeed(ASpeed : integer) : boolean;
begin
end;

function TTelnetObj.GetIpAddrAsDWord: cardinal;
begin
  result := fIdAddress;
end;

function TTelnetObj.GetIpAddrAsStr: String;
begin
  result := format('%d.%d.%d.%d', [fIdAddress, fIdAddress, fIdAddress,
    fIdAddress]);
end;

function TTelnetObj.SendTelegram(ATel: ttelegram; ADelayTel: integer = 300;
  ADelayByte: integer = 1): boolean;
begin
  fAnswer.empty;
  result := true;
end;

function TTelnetObj.SetPassword(APassword: string): boolean;
begin
  fPassword := APassword;
end;

function TTelnetObj.SetSSID(ASSID: string): boolean;
begin
  fSSID := ASSID;
end;

function TTelnetObj.SetPort(APort: integer): boolean;
begin
  fPort := APort;
end;

function TTelnetObj.SetHost(Ahost: string): boolean;
begin
  fHost := Ahost;
end;

procedure TTelnetObj.IdTelnet1DataAvailable(Sender: TIdTelnet;
  const Buffer: TArray<System.Byte>);
var
  tel: ttelegram;
begin
  tel.ConvertFromBytes(Buffer);
  fAnswer := fAnswer + tel;
  // memo1.Lines.Add(tel.AsString );
  // memo2.Lines.Add(tel.AsDECString );
end;

/// ////////////////////////////////////////////////////////////////////////////
/// deklarace USB Telnet
///
///
///
constructor TTelnetObjUSB.Create;
begin
  fSVPort := TSVPort.Create(nil);
  fsvport.PortID := 9;
  fsvport.ComPortSpeed := 125000;
  fsvport.ComPortDataBits := db8BITS;
  fsvport.ComPortParity := ptNONE;
  fsvport.OnReceiveData := SVPortReceiveData;
end;


function TTelnetObjUSB.ChangeComPortSpeed(ASpeed : integer) : boolean;
begin
  fsvport.ComPortSpeed := ASpeed;
end;

procedure TTelnetObjUSB.SVPortReceiveData(Sender: TObject; DataPtr: Pointer; DataSize: Cardinal);
var tel : ttelegram;
begin
  tel.AssignBuffer(dataptr, datasize);
  fanswer := fanswer + tel;
end;

function TTelnetObjUSB.Connect: boolean;
begin
  fSVPort.PortID := fPort;
  result := fsvport.Connect;
  sleep(1000);

// puvodni funkcni kod:
//  SVPort1.DISConnect;
//  SVPort1.ComPortSpeed := uart_speed;
//  svport1.PortType := spCOMAsync;
//  SVPort1.Connect;
//  sleep(1000);
end;

function TTelnetObjUSB.DisCOnnect: boolean;
begin
  fsvport.DisConnect;
  result := not fsvport.Connected;
end;

function TTelnetObjUSB.SendTelegram(ATel: ttelegram; ADelayTel: integer = 300;ADelayByte: integer = 1): boolean;
var i:integer;
    test : integer;
begin
  inherited;

  for I := 0 to Atel.size-1 do
  begin
    fsvport.SendData(@Atel.value[i],1);
    sleep(ADelayByte);
  end;
  sleep(ADelayTel);
end;

/// /////////////////////////////////////////////////////////////////////////////
/// deklarace Wifi Telnet
///
///
///

// vytvoreni objektu
  constructor TTelnetObjWifi.Create;
  begin
    fIdTelnet := TIdTelnet.Create();
    fIdTelnet.OnDataAvailable := IdTelnet1DataAvailable;
  end;


  function TTelnetObjWifi.Connect: boolean;
  begin
    // nastavit potrebne parametry
    fIdTelnet.Host := fHost;
    fIdTelnet.Port := fPort;
    fIdTelnet.Connect;
    sleep(100);
    result := fIdTelnet.Connected;

    // a ted si muzeme vycist parametry
{    if result then
    begin
      // napr. IP adresu
    end;}
  end;

  function TTelnetObjWifi.DisCOnnect: boolean;
  begin
    fIdTelnet.DisConnect;
    result := not fIdTelnet.Connected;
  end;

  function TTelnetObjWifi.SendTelegram(ATel: ttelegram; ADelayTel: integer = 300; ADelayByte: integer = 1): boolean;
  var
    i: integer;
  begin
    fAnswer.empty;
    result := false;

    for i := 0 to ATel.size-1 do
    begin
      fIdTelnet.SendCh(char(ATel.Value[i]));
      sleep(ADelayByte);
    end;
    sleep(ADelayTel);
  end;

  procedure TTelnetObjWifi.IdTelnet1DataAvailable(Sender: TIdTelnet;
    const Buffer: TArray<System.Byte>);
  var
    tel: ttelegram;
  begin
    tel.ConvertFromBytes(Buffer);
    fAnswer := fAnswer + tel;
  end;



/// /////////////////////////////////////////////////////////////////////////////
/// deklarace UDP
///
///
///

// vytvoreni objektu
constructor TTelnetObjUDP.Create;
begin
  inherited;
  fIdUDPServer1 := TIdUDPServer.Create();
  fIdUDPServer1.BroadcastEnabled := True;
  fIdUDPServer1.DefaultPort:= 55555;
  fIdUDPServer1.OnUDPRead := IdUDPServer1UDPRead;
  fIdUDPServer1.Active := True;   // active musi probehnout az po nastaveni
end;

procedure TTelnetObjUDP.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; AData: TArray<System.Byte>; ABinding: TIdSocketHandle);
var
  tel: TTelegram;
begin
  tel.ConvertFromBytes(AData);
  fAnswer := fanswer + tel;       // obsah UDP paketu

  fPeerIP := ABinding.PeerIP;       // IP adresa ze ktere prisel paket
  fPeerPort:= ABinding.PeerPort;    // obsahuje port odkud prisel paket
end;



end.
