unit Unit1;

{$mode objfpc}{$H+}

interface

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

type

  { TDAEnetIP4 }

  TDAEnetIP4 = class(TForm)
    Button1: TButton;
    DOcombo1: TComboBox;
    DOcombo5: TComboBox;
    DOcombo7: TComboBox;
    ComboBox12: TComboBox;
    DOcombo4: TComboBox;
    DOcombo3: TComboBox;
    ComboBox15: TComboBox;
    ComboBox16: TComboBox;
    DOcombo10: TComboBox;
    DOcombo11: TComboBox;
    DOcombo12: TComboBox;
    ComboBox2: TComboBox;
    DOcombo13: TComboBox;
    DOcombo14: TComboBox;
    DOcombo15: TComboBox;
    DOcombo16: TComboBox;
    ComboBox3: TComboBox;
    DOcombo2: TComboBox;
    DOcombo6: TComboBox;
    ComboBox6: TComboBox;
    ComboBox7: TComboBox;
    DOcombo8: TComboBox;
    DOcombo9: TComboBox;
    DIedit10: TEdit;
    DIedit11: TEdit;
    DIedit12: TEdit;
    DIedit13: TEdit;
    DIedit14: TEdit;
    DIedit15: TEdit;
    DIedit16: TEdit;
    DIedit7: TEdit;
    DIedit8: TEdit;
    DIedit9: TEdit;
    AIedit2: TEdit;
    AIedit3: TEdit;
    AIedit4: TEdit;
    AIedit5: TEdit;
    AIedit6: TEdit;
    AIedit7: TEdit;
    AIedit8: TEdit;
    DIedit2: TEdit;
    DIedit3: TEdit;
    DIedit4: TEdit;
    DIedit5: TEdit;
    DIedit6: TEdit;
    ip: TEdit;
    AIedit1: TEdit;
    DI1: TLabel;
    DI2: TLabel;
    DI3: TLabel;
    DI4: TLabel;
    DI5: TLabel;
    DI6: TLabel;
    DI7: TLabel;
    DI8: TLabel;
    AI2: TLabel;
    AI3: TLabel;
    AI4: TLabel;
    AI5: TLabel;
    AI6: TLabel;
    AI7: TLabel;
    AI8: TLabel;
    port: TEdit;
    password: TEdit;
    DIedit1: TEdit;
    networksettings: TGroupBox;
    digitaloutputs18: TGroupBox;
    digitaloutputs916: TGroupBox;
    digitalinputs: TGroupBox;
    analoginputs: TGroupBox;
    Image1: TImage;
    Label1: TLabel;
    DO6: TLabel;
    DO7: TLabel;
    DO8: TLabel;
    DO9: TLabel;
    DO10: TLabel;
    DO11: TLabel;
    DO12: TLabel;
    DO13: TLabel;
    DO14: TLabel;
    DO15: TLabel;
    Label2: TLabel;
    DO16: TLabel;
    Label3: TLabel;
    DO1: TLabel;
    AI1: TLabel;
    DO2: TLabel;
    DO3: TLabel;
    DO4: TLabel;
    DO5: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure DOcombo10Change(Sender: TObject);
    procedure DOcombo11Change(Sender: TObject);
    procedure DOcombo12Change(Sender: TObject);
    procedure DOcombo13Change(Sender: TObject);
    procedure DOcombo14Change(Sender: TObject);
    procedure DOcombo15Change(Sender: TObject);
    procedure DOcombo16Change(Sender: TObject);
    procedure DOcombo1Change(Sender: TObject);
    procedure DOcombo2Change(Sender: TObject);
    procedure DOcombo3Change(Sender: TObject);
    procedure DOcombo4Change(Sender: TObject);
    procedure DOcombo5Change(Sender: TObject);
    procedure DOcombo6Change(Sender: TObject);
    procedure DOcombo7Change(Sender: TObject);
    procedure DOcombo8Change(Sender: TObject);
    procedure DOcombo9Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure getfieldsdata(result: TStringList);
    procedure updateform();
    procedure setoutput(state: String);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  DAEnetIP4: TDAEnetIP4;

implementation

{$R *.lfm}

{ TDAEnetIP4 }
var
  result : TStringList;
  url : String;

procedure TDAEnetIP4.Button1Click(Sender: TObject);
begin
  Button1.Caption:='Connecting...';
  url := 'http://' + ip.Text  + ':' + port.Text + '/current_state.xml?pw=' + password.Text;
  //ShowMessage(url);
  result := TStringList.Create;
  try
    if HttpGetText(url,result) then
       begin
       Button1.Caption:='Refresh';
       //ShowMessage(result.text);
       if Pos('</Name>'#13#10'  <Value>',result.Text) <> 0 then
          begin
          getfieldsdata(result);
          updateform();
          end
       else
       begin
       DAEnetIP4.FormCreate(self);
       Button1.Caption:='Connect';
       ShowMessage('Wrong password!');
       end;
       end
    else
    begin
       DAEnetIP4.FormCreate(self);
        ShowMessage('Error connecting to server. Check IP and Port fields again!');
        Button1.Caption:='Connect';
        end
  finally
    result.Free;
  end;
end;

procedure TDAEnetIP4.FormCreate(Sender: TObject);
begin
  digitalinputs.Visible:=False;
  digitaloutputs18.Visible:=False;
  digitaloutputs916.Visible:=False;
  analoginputs.Visible:=False;
  Button1.Caption:='Connect';
end;


procedure TDAEnetIP4.updateform();
begin

  digitalinputs.Visible:=True;
  digitaloutputs18.Visible:=True;
  digitaloutputs916.Visible:=True;
  analoginputs.Visible:=True;
end;

procedure TDAEnetIP4.getfieldsdata(result: TStringList);
var
  collect_char: String;
  collect_data: String;
  data_index: Integer;
  loop_var: Integer;
  current_name: String;
  current_label: TLabel;
  current_combo: TComboBox;
  current_edit: TEdit;
  current_count_edit: TEdit;
  isComb0: Boolean;
  isEdit: Boolean;
  hasCount: Boolean;
const
  Count_value = '</Value>'#13#10'  <Count>';
  ANY_value = '</Name>'#13#10'  <Value>';
  DI1_name = '<DigitalInput1>'#13#10'  <Name>';
  DI2_name = '<DigitalInput2>'#13#10'  <Name>';
  DI3_name = '<DigitalInput3>'#13#10'  <Name>';
  DI4_name = '<DigitalInput4>'#13#10'  <Name>';
  DI5_name = '<DigitalInput5>'#13#10'  <Name>';
  DI6_name = '<DigitalInput6>'#13#10'  <Name>';
  DI7_name = '<DigitalInput7>'#13#10'  <Name>';
  DI8_name = '<DigitalInput8>'#13#10'  <Name>';
  AI1_name = '<AnalogInput1>'#13#10'  <Name>';
  AI2_name = '<AnalogInput2>'#13#10'  <Name>';
  AI3_name = '<AnalogInput3>'#13#10'  <Name>';
  AI4_name = '<AnalogInput4>'#13#10'  <Name>';
  AI5_name = '<AnalogInput5>'#13#10'  <Name>';
  AI6_name = '<AnalogInput6>'#13#10'  <Name>';
  AI7_name = '<AnalogInput7>'#13#10'  <Name>';
  AI8_name = '<AnalogInput8>'#13#10'  <Name>';
  DO1_name = '<Output1>'#13#10'  <Name>';
  DO2_name = '<Output2>'#13#10'  <Name>';
  DO3_name = '<Output3>'#13#10'  <Name>';
  DO4_name = '<Output4>'#13#10'  <Name>';
  DO5_name = '<Output5>'#13#10'  <Name>';
  DO6_name = '<Output6>'#13#10'  <Name>';
  DO7_name = '<Output7>'#13#10'  <Name>';
  DO8_name = '<Output8>'#13#10'  <Name>';
  DO9_name = '<Output9>'#13#10'  <Name>';
  DO10_name = '<Output10>'#13#10'  <Name>';
  DO11_name = '<Output11>'#13#10'  <Name>';
  DO12_name = '<Output12>'#13#10'  <Name>';
  DO13_name = '<Output13>'#13#10'  <Name>';
  DO14_name = '<Output14>'#13#10'  <Name>';
  DO15_name = '<Output15>'#13#10'  <Name>';
  DO16_name = '<Output16>'#13#10'  <Name>';
begin
  //ShowMessage(result.Text);
  for loop_var := 1 to 32 do
     begin
     isComb0 := False;
     isEdit := False;
     hasCount := False;
       case loop_var of
       1:
         begin
           current_name := DI1_name;
           current_label := DI1;
           isEdit := True;
           hasCount := True;
           current_edit := DIedit1;
           current_count_edit := DIedit9;
           end;
       2:
         begin
           current_name := DI2_name;
           current_label := DI2;
           isEdit := True;
           hasCount := True;
           current_edit := DIedit2;
           current_count_edit := DIedit10;
           end;
       3:
         begin
           current_name := DI3_name;
           current_label := DI3;
           isEdit := True;
           hasCount := True;
           current_edit := DIedit3;
           current_count_edit := DIedit11;
           end;
       4:
         begin
           current_name := DI4_name;
           current_label := DI4;
           isEdit := True;
           hasCount := True;
           current_edit := DIedit4;
           current_count_edit := DIedit12;
           end;
       5:
         begin
           current_name := DI5_name;
           current_label := DI5;
           isEdit := True;
           hasCount := True;
           current_edit := DIedit5;
           current_count_edit := DIedit13;
           end;
       6:
         begin
           current_name := DI6_name;
           current_label := DI6;
           isEdit := True;
           hasCount := True;
           current_edit := DIedit6;
           current_count_edit := DIedit14;
           end;
       7:
         begin
           current_name := DI7_name;
           current_label := DI7;
           isEdit := True;
           hasCount := True;
           current_edit := DIedit7;
           current_count_edit := DIedit15;
           end;
       8:
         begin
           current_name := DI8_name;
           current_label := DI8;
           isEdit := True;
           hasCount := True;
           current_edit := DIedit8;
           current_count_edit := DIedit16;
           end;
       9:
         begin
           current_name := AI1_name;
           current_label := AI1;
           isEdit := True;
           current_edit := AIedit1;
           end;
       10:
         begin
           current_name := AI2_name;
           current_label := AI2;
           isEdit := True;
           current_edit := AIedit2;
           end;
       11:
         begin
           current_name := AI3_name;
           current_label := AI3;
           isEdit := True;
           current_edit := AIedit3;
           end;
       12:
         begin
           current_name := AI4_name;
           current_label := AI4;
           isEdit := True;
           current_edit := AIedit4;
           end;
       13:
         begin
           current_name := AI5_name;
           current_label := AI5;
           isEdit := True;
           current_edit := AIedit5;
           end;
       14:
         begin
           current_name := AI6_name;
           current_label := AI6;
           isEdit := True;
           current_edit := AIedit6;
           end;
       15:
         begin
           current_name := AI7_name;
           current_label := AI7;
           isEdit := True;
           current_edit := AIedit7;
           end;
       16:
         begin
           current_name := AI8_name;
           current_label := AI8;
           isEdit := True;
           current_edit := AIedit8;
           end;
       17:
         begin
           current_name := DO1_name;
           current_label := DO1;
           isComb0 := True;
           current_combo := DOcombo1;
           end;
       18:
         begin
           current_name := DO2_name;
           current_label := DO2;
           isComb0 := True;
           current_combo := DOcombo2;
           end;
       19:
         begin
           current_name := DO3_name;
           current_label := DO3;
           isComb0 := True;
           current_combo := DOcombo3;
           end;
       20:
         begin
           current_name := DO4_name;
           current_label := DO4;
           isComb0 := True;
           current_combo := DOcombo4;
           end;
       21:
         begin
           current_name := DO5_name;
           current_label := DO5;
           isComb0 := True;
           current_combo := DOcombo5;
           end;

       22:
         begin
           current_name := DO6_name;
           current_label := DO6;
           isComb0 := True;
           current_combo := DOcombo6;
           end;
       23:
         begin
           current_name := DO7_name;
           current_label := DO7;
           isComb0 := True;
           current_combo := DOcombo7;
           end;
       24:
         begin
           current_name := DO8_name;
           current_label := DO8;
           isComb0 := True;
           current_combo := DOcombo8;
           end;
       25:
         begin
           current_name := DO9_name;
           current_label := DO9;
           isComb0 := True;
           current_combo := DOcombo9;
           end;
       26:
         begin
           current_name := DO10_name;
           current_label := DO10;
           isComb0 := True;
           current_combo := DOcombo10;
           end;
       27:
         begin
           current_name := DO11_name;
           current_label := DO11;
           isComb0 := True;
           current_combo := DOcombo11;
           end;
       28:
         begin
           current_name := DO12_name;
           current_label := DO12;
           isComb0 := True;
           current_combo := DOcombo12;
           end;
       29:
         begin
           current_name := DO13_name;
           current_label := DO13;
           isComb0 := True;
           current_combo := DOcombo13;
           end;
       30:
         begin
           current_name := DO14_name;
           current_label := DO14;
           isComb0 := True;
           current_combo := DOcombo14;
           end;
       31:
         begin
           current_name := DO15_name;
           current_label := DO15;
           isComb0 := True;
           current_combo := DOcombo15;
           end;
       32:
         begin
           current_name := DO16_name;
           current_label := DO16;
           isComb0 := True;
           current_combo := DOcombo16;
           end;
       end;
     collect_char := '';
     collect_data := '';
     data_index := Pos(current_name,result.Text);
     data_index += Length(current_name);
     while collect_char <> '<' do
     begin
       collect_data += collect_char;
       collect_char := '';
       collect_char := Copy(result.Text,data_index,1);
       data_index += 1;
     end;
     current_label.Caption:=collect_data;

     data_index := Pos(current_label.Caption + ANY_value,result.Text);
     data_index += Length(current_label.Caption + ANY_value);
     collect_char := '';
     collect_data := '';
     while collect_char <> '<' do
     begin
       collect_data += collect_char;
       collect_char := '';
       collect_char := Copy(result.Text,data_index,1);
       data_index += 1;
     end;

     if isComb0 = True then
        begin
        if collect_data = '1' then
           begin
           current_combo.ItemIndex:=0;
           end
        else begin
           current_combo.ItemIndex:=1;
        end;
     end;

     if isEdit = True then
        begin
        current_edit.Text:=collect_data;
        end;

     if hasCount = True then
        begin
         data_index := Pos(current_label.Caption + ANY_value + current_edit.Text + Count_value,result.Text);
         data_index += Length(current_label.Caption + ANY_value + current_edit.Text + Count_value);
         collect_char := '';
         collect_data := '';
         while collect_char <> '<' do
         begin
         collect_data += collect_char;
         collect_char := '';
         collect_char := Copy(result.Text,data_index,1);
         data_index += 1;
         end;
         current_count_edit.Text:=collect_data;
        end;
  end;
end;

procedure TDAEnetIP4.DOcombo1Change(Sender: TObject);
var state: String;
begin
  if DOcombo1.ItemIndex = 0 then state := '&Output1=1' else state := '&Output1=0';
  setoutput(state);
  end;

procedure TDAEnetIP4.DOcombo2Change(Sender: TObject);
var state: String;
begin
  if DOcombo2.ItemIndex = 0 then state := '&Output2=1' else state := '&Output2=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo3Change(Sender: TObject);
var state: String;
begin
  if DOcombo3.ItemIndex = 0 then state := '&Output3=1' else state := '&Output3=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo4Change(Sender: TObject);
var state: String;
begin
  if DOcombo4.ItemIndex = 0 then state := '&Output4=1' else state := '&Output4=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo5Change(Sender: TObject);
var state: String;
begin
  if DOcombo5.ItemIndex = 0 then state := '&Output5=1' else state := '&Output5=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo6Change(Sender: TObject);
var state: String;
begin
  if DOcombo6.ItemIndex = 0 then state := '&Output6=1' else state := '&Output6=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo7Change(Sender: TObject);
var state: String;
begin
  if DOcombo7.ItemIndex = 0 then state := '&Output7=1' else state := '&Output7=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo8Change(Sender: TObject);
var state: String;
begin
  if DOcombo8.ItemIndex = 0 then state := '&Output8=1' else state := '&Output8=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo9Change(Sender: TObject);
var state: String;
begin
  if DOcombo9.ItemIndex = 0 then state := '&Output9=1' else state := '&Output9=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo10Change(Sender: TObject);
var state: String;
begin
  if DOcombo10.ItemIndex = 0 then state := '&Output10=1' else state := '&Output10=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo11Change(Sender: TObject);
var state: String;
begin
  if DOcombo11.ItemIndex = 0 then state := '&Output11=1' else state := '&Output11=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo12Change(Sender: TObject);
var state: String;
begin
  if DOcombo12.ItemIndex = 0 then state := '&Output12=1' else state := '&Output12=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo13Change(Sender: TObject);
var state: String;
begin
  if DOcombo13.ItemIndex = 0 then state := '&Output13=1' else state := '&Output13=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo14Change(Sender: TObject);
var state: String;
begin
  if DOcombo14.ItemIndex = 0 then state := '&Output14=1' else state := '&Output14=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo15Change(Sender: TObject);
var state: String;
begin
  if DOcombo15.ItemIndex = 0 then state := '&Output15=1' else state := '&Output15=0';
  setoutput(state);
end;

procedure TDAEnetIP4.DOcombo16Change(Sender: TObject);
var state: String;
begin
  if DOcombo16.ItemIndex = 0 then state := '&Output16=1' else state := '&Output16=0';
  setoutput(state);
end;

procedure TDAEnetIP4.setoutput(state: String);
begin
  Button1.Caption:='Updating...';
  url := 'http://' + ip.Text  + ':' + port.Text + '/current_state.xml?pw=' + password.Text + state;
  result := TStringList.Create;
  try
    if HttpGetText(url,result) then
       begin
       if Pos('</Name>'#13#10'  <Value>',result.Text) <> 0 then
          begin
          getfieldsdata(result);
          updateform();
          Button1.Caption:='Refresh';
          end
       else
       begin
       DAEnetIP4.FormCreate(self);
       Button1.Caption:='Connect';
       ShowMessage('Wrong password!');
       end;
       end
    else
    begin
    DAEnetIP4.FormCreate(self);
        ShowMessage('Error connecting to server. Check IP and Port fields again!');
        Button1.Caption:='Connect';
        end
  finally
    result.Free;
end;
end;

end.

