unit Unit1;

{$mode objfpc}{$H+}

interface

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

type

  { TForm1 }

  TForm1 = class(TForm)
    aoGroupBox: TGroupBox;
    aiGroupBox2: TGroupBox;
    relayName8: TLabel;
    relayName2: TLabel;
    relayName1: TLabel;
    relayName3: TLabel;
    relayName4: TLabel;
    relayName5: TLabel;
    relayName6: TLabel;
    relayName7: TLabel;
    aoSet1: TButton;
    aoSet2: TButton;
    relayValue1: TCheckBox;
    relayValue2: TCheckBox;
    relayValue3: TCheckBox;
    relayValue4: TCheckBox;
    relayValue5: TCheckBox;
    relayValue6: TCheckBox;
    relayValue7: TCheckBox;
    relayValue8: TCheckBox;
    connectButton: TButton;
    aoValue1: TEdit;
    aoValue2: TEdit;
    ipTextBox: TEdit;
    aoName2: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    aoName1: TLabel;
    Label9: TLabel;
    portTextBox: TEdit;
    passwordTextBox: TEdit;
    Image1: TImage;
    statusLabel: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    tiGroupBox: TGroupBox;
    diGroupBox: TGroupBox;
    aiGroupBox: TGroupBox;
    Memo1: TMemo;
    tiNameMemo: TMemo;
    tiValueMemo: TMemo;
    diNameMemo: TMemo;
    diValueMemo: TMemo;
    diCountMemo: TMemo;
    Memo5: TMemo;
    aiNameMemo: TMemo;
    aiValueMemo: TMemo;
    aiCountMemo: TMemo;
    Memo9: TMemo;
    procedure aoGroupBoxClick(Sender: TObject);
    procedure aiGroupBox2Click(Sender: TObject);
    procedure aoSet1Click(Sender: TObject);
    procedure aoSet2Click(Sender: TObject);
    procedure connectButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure getValues();
    procedure connect(parameter:string);
    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;

var
  result_string_list: TStringList;
  url: string;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
end;

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

procedure TForm1.aiGroupBox2Click(Sender: TObject);
begin

end;

procedure TForm1.aoSet1Click(Sender: TObject);
begin
  connect('&AnalogOutput1='+aoValue1.Text);
end;

procedure TForm1.aoSet2Click(Sender: TObject);
begin
  connect('&AnalogOutput2='+aoValue2.Text);
end;

procedure TForm1.aoGroupBoxClick(Sender: TObject);
begin

end;

Function FindCmp(constRef aName:string):TComponent;
var
  i: Integer;
  TmpComponent: TComponent;
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
      TmpComponent := Form1.Components[i];
      if TmpComponent.Name = aName then
      begin
           Result :=  TmpComponent;
      end;
end;

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 FindEdit(constRef aName:string):TEdit;
var
  i: Integer;
  TmpLabel: TEdit;
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 TEdit   then
    begin
      TmpLabel := TEdit(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;

procedure TForm1.getValues();
var
  startPos: SizeInt;
  endPos: SizeInt;
  thisInput: string;
  input: string;
  cntrlName: string;
  i: integer;
  tmpLabel: TLabel;
  tmpEdit: TEdit;
  tmpCheckBox: TCheckBox;
begin
  diNameMemo.Clear();
  diNameMemo.Lines.AddText('Name'#13#10#13#10);
  diValueMemo.Clear();
  diValueMemo.Lines.AddText('Value'#13#10#13#10);
  diCountMemo.Clear();
  diCountMemo.Lines.AddText('Count'#13#10#13#10);
  input := '<DigitalInput%d>'#13#10#9'<Name>';
  for i := 1 to 8 do
  begin
    thisInput := Format(input, [i]);
    startPos := PosEx(thisInput, result_string_list.Text, 1) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    diNameMemo.Lines.AddText(Copy(result_string_list.Text, startPos,
      endPos - startPos) + #13#10);
    thisInput := '<Value>';
    startPos := PosEx(thisInput, result_string_list.Text, endPos) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    diValueMemo.Lines.AddText(Copy(result_string_list.Text, startPos,
      endPos - startPos) + #13#10);
    thisInput := '<Count>';
    startPos := PosEx(thisInput, result_string_list.Text, endPos) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    diCountMemo.Lines.AddText(Copy(result_string_list.Text, startPos,
      endPos - startPos) + #13#10);
  end;

  aiNameMemo.Clear();
  aiNameMemo.Lines.AddText('Name'#13#10#13#10);
  aiValueMemo.Clear();
  aiValueMemo.Lines.AddText('Value'#13#10#13#10);
  aiCountMemo.Clear();
  aiCountMemo.Lines.AddText('Measure'#13#10#13#10);
  input := '<AnalogInput%d>'#13#10#9'<Name>';
  for i := 1 to 4 do
  begin
    thisInput := Format(input, [i]);
    startPos := PosEx(thisInput, result_string_list.Text, 1) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    aiNameMemo.Lines.AddText(Copy(result_string_list.Text, startPos,
      endPos - startPos) + #13#10);
    thisInput := '<Value>';
    startPos := PosEx(thisInput, result_string_list.Text, endPos) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    aiValueMemo.Lines.AddText(Copy(result_string_list.Text, startPos,
      endPos - startPos) + #13#10);
    thisInput := '<Measure>';
    startPos := PosEx(thisInput, result_string_list.Text, endPos) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    aiCountMemo.Lines.AddText(Copy(result_string_list.Text, startPos,
      endPos - startPos) + #13#10);
  end;

  tiNameMemo.Clear();
  tiNameMemo.Lines.AddText('Name'#13#10#13#10);
  tiValueMemo.Clear();
  tiValueMemo.Lines.AddText('Value'#13#10#13#10);
  input := '<AnalogInput%d>'#13#10#9'<Name>';
  for i := 5 to 8 do
  begin
    thisInput := Format(input, [i]);
    startPos := PosEx(thisInput, result_string_list.Text, 1) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    tiNameMemo.Lines.AddText(Copy(result_string_list.Text, startPos,
      endPos - startPos) + #13#10);
    thisInput := '<Measure>';
    startPos := PosEx(thisInput, result_string_list.Text, endPos) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    tiValueMemo.Lines.AddText(Copy(result_string_list.Text, startPos,
      endPos - startPos) + #13#10);
  end;

  input := '<Relay%d>'#13#10#9'<Name>';
  for i := 1 to 8 do
  begin
    thisInput := Format(input, [i]);
    startPos := PosEx(thisInput, result_string_list.Text, 1) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    cntrlName :=    Format('relayName%d',[i]);
    tmpLabel := FindLabel(cntrlName);
    tmpLabel.caption := Copy(result_string_list.Text, startPos, endPos - startPos);

    thisInput := '<Value>';
    startPos := PosEx(thisInput, result_string_list.Text, endPos) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    cntrlName :=    Format('relayValue%d',[i]);
    tmpCheckBox := FindCheckbox(cntrlName);
    thisInput := Copy(result_string_list.Text, startPos, endPos - startPos);
    if thisInput = '1' then
    begin
        tmpCheckBox.Checked:=true;
    end
    else
    begin
        tmpCheckBox.Checked:=false;
    end;
  end;

  input := '<AnalogOutput%d>'#13#10#9'<Name>';
  for i := 1 to 2 do
  begin
    thisInput := Format(input, [i]);
    startPos := PosEx(thisInput, result_string_list.Text, 1) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    cntrlName :=    Format('aoName%d',[i]);
    tmpLabel := FindLabel(cntrlName);
    tmpLabel.caption := Copy(result_string_list.Text, startPos, endPos - startPos);

    thisInput := '<Value>';
    startPos := PosEx(thisInput, result_string_list.Text, endPos) + Length(thisInput);
    endPos := PosEx('</', result_string_list.Text, startPos);
    cntrlName :=    Format('aoValue%d',[i]);
    tmpEdit := FindEdit(cntrlName);
    tmpEdit.Text := Copy(result_string_list.Text, startPos, endPos - startPos);
  end;
end;

procedure TForm1.connect(parameter:string);
begin
  url := 'http://' + ipTextBox.Text + ':' + portTextBox.Text +
    '/current_state.xml?pw=' + passwordTextBox.Text + parameter;
  result_string_list := TStringList.Create;
  try
    if HttpGetText(url, result_string_list) then
    begin
      if Pos('<DigitalInput1>', result_string_list.Text) <> 0 then
      begin
        getValues();
    connectButton.Caption := 'Refresh';
    statusLabel.Caption := 'Connection Established.';
      end
      else if Pos('<LoginKey>', result_string_list.Text) <> 0 then
      begin
    connectButton.Caption := 'Connect';
    statusLabel.Caption := 'Wrong Password.';
      end;
    end
    else
    begin
    connectButton.Caption := 'Connect';
    statusLabel.Caption :=
      'Error connecting to server. Check IP/Port/Password fields again! (Administrator could be logged in.)';
    end
  finally
    result_string_list.Free;
  end;
end;

{procedure TForm1.relayValue1Change(Sender: TObject);
begin
  if relayValue1.Checked then
  begin
  connect('&Relay1=1');
  end
  else
  begin
  connect('&Relay1=0');
  end;
end;    }


procedure TForm1.relayValue1Change(Sender: TObject);
begin
  if relayValue1.Checked then
  begin
  connect('&Relay1=1');
  end
  else
  begin
  connect('&Relay1=0');
  end;
end;

procedure TForm1.relayValue2Change(Sender: TObject);
begin
  if relayValue2.Checked then
  begin
  connect('&Relay2=1');
  end
  else
  begin
  connect('&Relay2=0');
  end;

end;

procedure TForm1.relayValue3Change(Sender: TObject);
begin

  if relayValue3.Checked then
  begin
  connect('&Relay3=1');
  end
  else
  begin
  connect('&Relay3=0');
  end;
end;

procedure TForm1.relayValue4Change(Sender: TObject);
begin

  if relayValue4.Checked then
  begin
  connect('&Relay4=1');
  end
  else
  begin
  connect('&Relay4=0');
  end;
end;

procedure TForm1.relayValue5Change(Sender: TObject);
begin

  if relayValue5.Checked then
  begin
  connect('&Relay5=1');
  end
  else
  begin
  connect('&Relay5=0');
  end;
end;

procedure TForm1.relayValue6Change(Sender: TObject);
begin

  if relayValue6.Checked then
  begin
  connect('&Relay6=1');
  end
  else
  begin
  connect('&Relay6=0');
  end;
end;

procedure TForm1.relayValue7Change(Sender: TObject);
begin

  if relayValue7.Checked then
  begin
  connect('&Relay7=1');
  end
  else
  begin
  connect('&Relay7=0');
  end;
end;

procedure TForm1.relayValue8Change(Sender: TObject);
begin

  if relayValue8.Checked then
  begin
  connect('&Relay8=1');
  end
  else
  begin
  connect('&Relay8=0');
  end;
end;


end.
