unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Menus, httpsend, strutils, snmpsend;

type

  { TForm1 }

  TForm1 = class(TForm)
    aiMeasureMemo: TMemo;
    aiGroupBox: TGroupBox;
    aiGroupBox2: TGroupBox;
    aiNameMemo: TMemo;
    aiValueMemo: TMemo;
    aoGroupBox: TGroupBox;
    aoName1: TLabel;
    aoName2: TLabel;
    aoSet1: TButton;
    aoSet2: TButton;
    aoValue1: TEdit;
    aoValue2: TEdit;
    connectButton: TButton;
    diCountMemo: TMemo;
    diGroupBox: TGroupBox;
    diNameMemo: TMemo;
    diValueMemo: TMemo;
    Image1: TImage;
    ipTextBox: TEdit;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Memo1: TMemo;
    Memo5: TMemo;
    Memo9: TMemo;
    passwordTextBox: TEdit;
    portTextBox: TEdit;
    relayName1: TLabel;
    relayName2: TLabel;
    relayName3: TLabel;
    relayName4: TLabel;
    relayName5: TLabel;
    relayName6: TLabel;
    relayName7: TLabel;
    relayName8: TLabel;
    relayValue1: TCheckBox;
    relayValue2: TCheckBox;
    relayValue3: TCheckBox;
    relayValue4: TCheckBox;
    relayValue5: TCheckBox;
    relayValue6: TCheckBox;
    relayValue7: TCheckBox;
    relayValue8: TCheckBox;
    statusLabel: TLabel;
    tiGroupBox: TGroupBox;
    tiNameMemo: TMemo;
    tiValueMemo: TMemo;
    procedure aoSet1Click(Sender: TObject);
    procedure aoSet2Click(Sender: TObject);
    procedure aoValue1Change(Sender: TObject);
    procedure connectButtonClick(Sender: TObject);
    function getSnmpData(oid: string): string;
    procedure connect();
    procedure relayValue1Change(Sender: TObject);
    procedure relayValue2Change(Sender: TObject);
    procedure relayValue3Change(Sender: TObject);
    procedure relayValue4Change(Sender: TObject);
    procedure relayValue5Change(Sender: TObject);
    procedure relayValue6Change(Sender: TObject);
    procedure relayValue7Change(Sender: TObject);
    procedure relayValue8Change(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }


Function FindLabel(constRef aName:string):TLabel;
var
  i: Integer;
  TmpLabel: TLabel;
begin
  { This example loops through all of the components on the form, and toggles the
    Visible property of each panel to the value that is opposite of what it has (IOW,
    if it's True it's switched to False, if it's False it's switched to True). }
  Result := Nil;
  for i := 0 to Form1.ComponentCount - 1 do
    if Form1.Components[i] is TLabel   then
    begin
      TmpLabel := TLabel(Form1.Components[i]);
      if TmpLabel.Name = aName then
      begin
           Result :=  TmpLabel;
      end;
    end;
end;

Function FindCheckbox(constRef aName:string):TCheckBox;
var
  i: Integer;
  TmpLabel: TCheckBox;
begin
  { This example loops through all of the components on the form, and toggles the
    Visible property of each panel to the value that is opposite of what it has (IOW,
    if it's True it's switched to False, if it's False it's switched to True). }
  Result := Nil;
  for i := 0 to Form1.ComponentCount - 1 do
    if Form1.Components[i] is TCheckBox   then
    begin
      TmpLabel := TCheckBox(Form1.Components[i]);
      if TmpLabel.Name = aName then
      begin
           Result :=  TmpLabel;
      end;
    end;
end;

function IsNumeric(s:String): Boolean;
var num: Integer;
begin
  IsNumeric:=True;
  try
    num:= StrToInt(s)
  except
      IsNumeric:=False;
  end;
end;

procedure TForm1.connectButtonClick(Sender: TObject);
begin
  connect();
end;

procedure TForm1.aoSet1Click(Sender: TObject);
begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.4.1.3.0',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,aoValue1.Text,2);
end;

procedure TForm1.aoSet2Click(Sender: TObject);
begin
               SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.4.1.3.1',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,aoValue2.Text,2);
end;

procedure TForm1.aoValue1Change(Sender: TObject);
begin
  if IsNumeric(aoValue1.Text) then
  begin
    if (StrToInt(aoValue1.Text) < 0) or (StrToInt(aoValue1.Text) > 1023) then
    begin
       aoValue1.Clear;
    end;
  end;
end;

function TForm1.getSnmpData(oid: string): string;
var
  snmpResult: string;
begin
  if SNMPDenkoviGet(oid, passwordTextBox.Text, ipTextBox.Text,portTextBox.Text, snmpResult) then
  begin
    Result := snmpResult;
  end
  else
  begin
    Result := 'NULL';
  end;
end;

procedure TForm1.connect();
var
  i: integer;
  error: integer;
  res: string;
  thisString: string;
  temp_oid: string;
  tmpLabel: TLabel;
  tmpCheckbox: TCheckBox;
  cntrlName: String;
begin
  error := 0;
  temp_oid := '1.3.6.1.4.1.42505.8.2.1.1.2.%d';
  diNameMemo.Clear();
  diNameMemo.Lines.AddText('Name'#13#10#13#10);
  for i := 0 to 8 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
      diNameMemo.Lines.AddText(res);
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  temp_oid := '1.3.6.1.4.1.42505.8.2.1.1.7.%d';
  diValueMemo.Clear();
  diValueMemo.Lines.AddText('Value'#13#10#13#10);
  for i := 0 to 15 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
      diValueMemo.Lines.AddText(res);
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  temp_oid := '1.3.6.1.4.1.42505.8.2.1.1.3.%d';
  diCountMemo.Clear();
  diCountMemo.Lines.AddText('Count'#13#10#13#10);
  for i := 0 to 15 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
      diCountMemo.Lines.AddText(res);
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  temp_oid := '1.3.6.1.4.1.42505.8.2.2.1.2.%d';
  aiNameMemo.Clear();
  aiNameMemo.Lines.AddText('Name'#13#10#13#10);
  for i := 0 to 3 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
      aiNameMemo.Lines.AddText(res);
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  temp_oid := '1.3.6.1.4.1.42505.8.2.2.1.6.%d';
  aiValueMemo.Clear();
  aiValueMemo.Lines.AddText('Value'#13#10#13#10);
  for i := 0 to 3 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
      aiValueMemo.Lines.AddText(res);
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  temp_oid := '1.3.6.1.4.1.42505.8.2.2.1.10.%d';
  aiMeasureMemo.Clear();
  aiMeasureMemo.Lines.AddText('Measure'#13#10#13#10);
  for i := 0 to 3 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
      aiMeasureMemo.Lines.AddText(res);
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  temp_oid := '1.3.6.1.4.1.42505.8.2.2.1.2.%d';
  tiNameMemo.Clear();
  tiNameMemo.Lines.AddText('Name'#13#10#13#10);
  for i := 4 to 7 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
      tiNameMemo.Lines.AddText(res);
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  temp_oid := '1.3.6.1.4.1.42505.8.2.2.1.10.%d';
  tiValueMemo.Clear();
  tiValueMemo.Lines.AddText('Measure'#13#10#13#10);
  for i := 4 to 7 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
      tiValueMemo.Lines.AddText(res);
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  res := getSnmpData('1.3.6.1.4.1.42505.8.2.4.1.2.0');
  if (res <> 'NULL') then
  begin
    aoName1.Caption:=res;
    connectButton.Caption := 'Refresh';
    statusLabel.Caption := 'Connection Established.';
  end
  else
  begin
    error := error + 1;
  end;

  res := getSnmpData('1.3.6.1.4.1.42505.8.2.4.1.2.1');
  if (res <> 'NULL') then
  begin
    aoName2.Caption:=res;
    connectButton.Caption := 'Refresh';
    statusLabel.Caption := 'Connection Established.';
  end
  else
  begin
    error := error + 1;
  end;

  res := getSnmpData('1.3.6.1.4.1.42505.8.2.4.1.3.0');
  if (res <> 'NULL') then
  begin
    aoValue1.Text:=res;
    connectButton.Caption := 'Refresh';
    statusLabel.Caption := 'Connection Established.';
  end
  else
  begin
    error := error + 1;
  end;

  res := getSnmpData('1.3.6.1.4.1.42505.8.2.4.1.3.1');
  if (res <> 'NULL') then
  begin
    aoValue2.Text:=res;
    connectButton.Caption := 'Refresh';
    statusLabel.Caption := 'Connection Established.';
  end
  else
  begin
    error := error + 1;
  end;

  temp_oid := '1.3.6.1.4.1.42505.8.2.3.1.2.%d';
  for i := 0 to 7 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
    cntrlName :=    Format('relayName%d',[i+1]);
    tmpLabel := FindLabel(cntrlName);
    tmpLabel.caption := res;
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  temp_oid := '1.3.6.1.4.1.42505.8.2.3.1.13.%d';
  for i := 0 to 7 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
    cntrlName :=    Format('relayValue%d',[i+1]);
    tmpCheckbox := FindCheckbox(cntrlName);
    if res = '1' then
    begin
        tmpCheckbox.Checked:=true;
    end
    else
    begin
        tmpCheckbox.Checked:=false;
    end;
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;
end;

procedure TForm1.relayValue1Change(Sender: TObject);
begin
  if relayValue1.Checked = True then
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.0',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'1',2);
  end
  else
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.0',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'0',2);
  end;
end;

procedure TForm1.relayValue2Change(Sender: TObject);
begin

  if relayValue2.Checked = True then
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.1',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'1',2);
  end
  else
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.1',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'0',2);
  end;
end;

procedure TForm1.relayValue3Change(Sender: TObject);
begin

  if relayValue3.Checked = True then
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.2',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'1',2);
  end
  else
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.2',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'0',2);
  end;
end;

procedure TForm1.relayValue4Change(Sender: TObject);
begin

  if relayValue4.Checked = True then
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.3',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'1',2);
  end
  else
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.3',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'0',2);
  end;
end;

procedure TForm1.relayValue5Change(Sender: TObject);
begin

  if relayValue5.Checked = True then
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.4',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'1',2);
  end
  else
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.4',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'0',2);
  end;
end;

procedure TForm1.relayValue6Change(Sender: TObject);
begin

  if relayValue6.Checked = True then
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.5',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'1',2);
  end
  else
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.5',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'0',2);
  end;
end;

procedure TForm1.relayValue7Change(Sender: TObject);
begin

  if relayValue7.Checked = True then
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.6',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'1',2);
  end
  else
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.6',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'0',2);
  end;
end;

procedure TForm1.relayValue8Change(Sender: TObject);
begin

  if relayValue8.Checked = True then
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.7',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'1',2);
  end
  else
  begin
       SNMPDenkoviSet('1.3.6.1.4.1.42505.8.2.3.1.13.7',passwordTextBox.Text,ipTextBox.Text, portTextBox.Text,'0',2);
  end;
end;


  {temp_oid := '1.3.6.1.4.1.42505.7.2.3.1.2.%d';
  tiNameMemo.Clear();
  tiNameMemo.Lines.AddText('Name'#13#10#13#10);
  for i := 0 to 7 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
      tiNameMemo.Lines.AddText(res);
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  temp_oid := '1.3.6.1.4.1.42505.7.2.3.1.7.%d';
  tiValueMemo.Clear();
  tiValueMemo.Lines.AddText('Value'#13#10#13#10);
  for i := 0 to 7 do
  begin
    thisString := Format(temp_oid, [i]);
    res := getSnmpData(thisString);
    if (res <> 'NULL') then
    begin
      tiValueMemo.Lines.AddText(res);
      connectButton.Caption := 'Refresh';
      statusLabel.Caption := 'Connection Established.';
    end
    else
    begin
      error := error + 1;
      Break;
    end;
  end;

  if error = 8 then
  begin
    connectButton.Caption := 'Connect';
    statusLabel.Caption := 'Failed to Connect. Check IP/Password or Internet Connection.';
  end
  else if error <> 0 then
  begin
    connectButton.Caption := 'Connect';
    statusLabel.Caption := 'Some fields failed to load.';
  end; }

end.
