home *** CD-ROM | disk | FTP | other *** search
/ Exame Informatica 142 / Exame Informatica 142.iso / Programas / Cerberus / CerberusSetup.exe / CerberusNET_Source / ClientMain.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2006-01-29  |  9.1 KB  |  392 lines

  1. {
  2.  (C) Paul Alan Freshney 2006
  3.  (Based on an Indy Client Demo)
  4.  
  5.   Last Modified: January 29th 2006
  6.  
  7.   This code is free to use as you wish. Please do not distribute this code without
  8.   authorisation from myself. We are free to distribute versions of CerberusNET as
  9.   you wish as long as you link to "www.paulalanfreshney.com/cerberus" somewhere
  10.   within the application!
  11.  
  12.   391
  13. }
  14.  
  15. unit ClientMain;
  16.  
  17. interface
  18.  
  19. uses
  20.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  21.   StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, ExtCtrls, Grids, jpeg,
  22.   ComCtrls, ToolWin, ImgList, XPMan;
  23.  
  24. type
  25.   TfrmMain = class(TForm)
  26.       TCPClient: TIdTCPClient;
  27.     timerMain: TTimer;
  28.     Cam1: TImage;
  29.     ImageList1: TImageList;
  30.     CoolBar1: TCoolBar;
  31.     ToolBar1: TToolBar;
  32.     tbSingle: TToolButton;
  33.     tbStream: TToolButton;
  34.     statusMain: TStatusBar;
  35.     CoolBar2: TCoolBar;
  36.     XPManifest1: TXPManifest;
  37.     ToolBar2: TToolBar;
  38.     tbPreferences: TToolButton;
  39.     tbAbout: TToolButton;
  40.     ToolButton2: TToolButton;
  41.     Cam2: TImage;
  42.     Cam3: TImage;
  43.     Cam4: TImage;
  44.     Cam5: TImage;
  45.     Cam6: TImage;
  46.     Cam7: TImage;
  47.     Cam8: TImage;
  48.     Cam9: TImage;
  49.     Cam10: TImage;
  50.       procedure Button1Click(Sender: TObject);
  51.     procedure Button2Click(Sender: TObject);
  52.     procedure timerMainTimer(Sender: TObject);
  53.     procedure FormResize(Sender: TObject);
  54.     procedure FormCreate(Sender: TObject);
  55.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  56.     procedure tbPreferencesClick(Sender: TObject);
  57.     procedure LoadSettings;
  58.     procedure SaveSettings;
  59.     procedure tbAboutClick(Sender: TObject);
  60.     procedure tbStreamClick(Sender: TObject);
  61.     procedure RebuildDisplay;
  62.     procedure TCPClientStatus(ASender: TObject; const AStatus: TIdStatus;
  63.       const AStatusText: String);
  64.     private
  65.     public
  66.     end;
  67.  
  68. var
  69.     frmMain: TfrmMain;
  70.  
  71. implementation
  72.  
  73. {$R *.DFM}
  74.  
  75. uses utility, inifiles, preferences, about;
  76.  
  77. var
  78.  CamCount : integer;
  79.  CamList : array[1..10] of TImage;
  80.  
  81. procedure TfrmMain.FormCreate(Sender: TObject);
  82.  var
  83.   t : integer;
  84.  
  85.  begin
  86.   Caption:='CerberusNET '+CNET_Version+' / '+CNET_Date+' - (c) PAF 2005';
  87.  
  88.   Height:=210;
  89.   Width:=286;
  90.  
  91.   LoadSettings;
  92.  
  93.   timerMain.Interval:=CNETPreferenes.CRefresh*1000;
  94.  
  95.   CamList[1]:=Cam1;
  96.   CamList[2]:=Cam2;
  97.   CamList[3]:=Cam3;
  98.   CamList[4]:=Cam4;
  99.   CamList[5]:=Cam5;
  100.   CamList[6]:=Cam6;
  101.   CamList[7]:=Cam7;
  102.   CamList[8]:=Cam8;
  103.   CamList[9]:=Cam9;
  104.   CamList[10]:=Cam10;
  105.  
  106.   for t:=2 to 10 do
  107.     CamList[t].Picture.Assign(CamList[1].Picture);
  108.  
  109.   CamCount:=1;
  110. end;
  111.  
  112. procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
  113.  begin
  114.   SaveSettings;
  115. end;
  116.  
  117. procedure TfrmMain.LoadSettings;
  118.  var
  119.   config : TINIFile;
  120.  
  121.  begin
  122.   config:=TIniFile.Create('CerberusNET.ini');
  123.   try
  124.     Top   :=config.ReadInteger('Main', 'Top', 50);
  125.     Left  :=config.ReadInteger('Main', 'Left', 50);
  126.  
  127.     with CNETPreferenes do begin
  128.       CHost    :=config.ReadString('Connection', 'Host', '127.0.0.1');
  129.       CPort    :=config.ReadInteger('Connection', 'Port', 8090);
  130.       CRefresh :=config.ReadInteger('Connection', 'Refresh', 3);
  131.     end;
  132.   except
  133.     config.Free;
  134.   end;
  135. end;
  136.  
  137. procedure TfrmMain.SaveSettings;
  138.  var
  139.   config : TINIFile;
  140.  
  141.  begin
  142.   config:=TIniFile.Create('CerberusNET.ini');
  143.   try
  144.     config.WriteInteger('Main', 'Top', Top);
  145.     config.WriteInteger('Main', 'Left', Left);
  146.  
  147.     with CNETPreferenes do begin
  148.       config.WriteString('Connection', 'Host', CHost);
  149.       config.WriteInteger('Connection', 'Port', CPort);
  150.       config.WriteInteger('Connection', 'Refresh', CRefresh);
  151.     end;
  152.   except
  153.     config.Free;
  154.   end;
  155. end;
  156.  
  157. procedure TfrmMain.Button1Click(Sender: TObject);
  158.  
  159. var
  160.     SResponse: String;
  161.   AvailableCams : string;
  162.     AStream: TMemoryStream;
  163.   OldCamCount,t,i : integer;
  164.   brian : TJPEGIMage;
  165.  
  166. begin
  167.   OldCamCount:=0;
  168.   
  169.     with TCPClient do    begin
  170.     try
  171.           Connect;
  172.     except
  173.       on E: Exception do begin
  174.         statusMain.SimpleText:=E.Message;
  175.  
  176.         if timerMain.Enabled then tbStreamClick(Nil);
  177.       end;
  178.     end;
  179.  
  180.         while Connected do
  181.         begin
  182.             AStream := TMemoryStream.Create;
  183.  
  184.             try
  185.                 // banner means the server thread is running
  186.                 SResponse := UpperCase(ReadLn);
  187.                 if Pos('CX', SResponse) = 0 then Break;
  188.  
  189.         AvailableCams:=Copy(SResponse, 4, 10);
  190.  
  191.         OldCamCount:=0;
  192.         for t:=1 to 10 do
  193.           if AvailableCams[t]='A' then inc(OldCamCount);
  194.  
  195.         //Caption:=AvailableCams;
  196.  
  197.         if OldCamCount<>0 then begin
  198.           i:=1;
  199.           for t:=1 to 10 do begin
  200.             if AvailableCams[t]='A' then begin
  201.                        // request OUTLINE data
  202.               AStream := TMemoryStream.Create;
  203.  
  204.                       WriteLn('CAM'+CHR(64+t));
  205.  
  206.                       ReadStream(AStream, -1, True);
  207.                       AStream.Seek(0, soFromBeginning);
  208.  
  209.               brian:=TJPEGImage.Create;
  210.               brian.LoadFromStream(AStream);
  211.               CamList[i].Picture.Graphic.Assign(brian);
  212.  
  213.               inc(i);
  214.  
  215.               // bit of a hack :)
  216.               Disconnect;
  217.               Connect;
  218.               SResponse := UpperCase(ReadLn);
  219.             end;
  220.           end;
  221.         end
  222.         else begin
  223.           statusMain.SimpleText:='No Webcams active on CerberusServer '+CNETPreferenes.CHost;
  224.         end;
  225.             finally
  226.                 Disconnect;
  227.                 AStream.Free;
  228.             end;
  229.         end;
  230.     end;
  231.  
  232.   if OldCamCount<>CamCount then begin
  233.     CamCount:=OldCamCount;
  234.  
  235.     RebuildDisplay;
  236.   end;
  237. end;
  238.  
  239. procedure TfrmMain.Button2Click(Sender: TObject);
  240.  begin
  241.     timerMain.Enabled := not timerMain.Enabled;
  242. end;
  243.  
  244. procedure TfrmMain.timerMainTimer(Sender: TObject);
  245. begin
  246.     try
  247.     Button1Click(Nil);
  248.     except // reset the timer and raise the exception
  249.         begin
  250.  
  251.             raise;
  252.         end;
  253.     end;
  254. end;
  255.  
  256. procedure TfrmMain.FormResize(Sender: TObject);
  257. begin
  258. //    Button1.Width := (Width-26) div 2;
  259. //    Button2.Width :=     Button1.Width;
  260. //    Button2.Left  :=     Button1.Left + Button1.Width + 4;
  261. end;
  262.  
  263. procedure TfrmMain.tbPreferencesClick(Sender: TObject);
  264.  begin
  265.   if DoPreferences=mrOK then begin
  266.     TCPClient.Host:=CNETPreferenes.CHost;
  267.     TCPClient.Port:=CNETPreferenes.CPort;
  268.  
  269.     timerMain.Interval:=CNETPreferenes.CRefresh*1000;
  270.   end;
  271. end;
  272.  
  273. procedure TfrmMain.tbAboutClick(Sender: TObject);
  274.  begin
  275.   frmAbout.ShowModal;
  276. end;
  277.  
  278. procedure TfrmMain.tbStreamClick(Sender: TObject);
  279.  begin
  280.   timerMain.Enabled:=not(timerMain.Enabled);
  281.  
  282.   tbSingle.Enabled      :=not(timerMain.Enabled);
  283.   tbPreferences.Enabled :=not(timerMain.Enabled);
  284.   tbAbout.Enabled       :=not(timerMain.Enabled);
  285. end;
  286.  
  287. procedure TfrmMain.RebuildDisplay;
  288.  var
  289.   x : integer;
  290.  
  291.  begin
  292.   case CamCount of
  293.     0,1 : begin
  294.             Height:=210;
  295.             Width:=286;
  296.           end;
  297.     2   : begin
  298.             Height:=210;
  299.             Width:=469;
  300.           end;
  301.     3   : begin
  302.             Height:=210;
  303.             Width:=653;
  304.           end;
  305.     4   : begin
  306.             Height:=210;
  307.             Width:=837;
  308.  
  309.             for x:=0 to 3 do begin
  310.               CamList[x+1].Left:=96+(x*184);
  311.               CamList[x+1].Top:=8;
  312.             end;
  313.           end;
  314.     5   : begin
  315.             Height:=210;
  316.             Width:=1019;
  317.  
  318.             for x:=0 to 4 do begin
  319.               CamList[x+1].Left:=96+(x*184);
  320.               CamList[x+1].Top:=8;
  321.             end;
  322.           end;
  323.     6   : begin
  324.             Height:=361;
  325.             Width:=653;
  326.  
  327.             for x:=0 to 2 do begin
  328.               CamList[x+1].Left:=96+(x*184);
  329.               CamList[x+1].Top:=8;
  330.  
  331.               CamList[x+4].Left:=96+(x*184);
  332.               CamList[x+4].Top:=160;
  333.             end;
  334.           end;
  335.     7   : begin
  336.             Height:=361;
  337.             Width:=835;
  338.  
  339.             for x:=0 to 3 do begin
  340.               CamList[x+1].Left:=96+(x*184);
  341.               CamList[x+1].Top:=8;
  342.  
  343.               CamList[x+4].Left:=96+(x*184);
  344.               CamList[x+4].Top:=160;
  345.             end;
  346.           end;
  347.     8   : begin
  348.             Height:=361;
  349.             Width:=835;
  350.  
  351.             for x:=0 to 3 do begin
  352.               CamList[x+1].Left:=96+(x*184);
  353.               CamList[x+1].Top:=8;
  354.  
  355.               CamList[x+5].Left:=96+(x*184);
  356.               CamList[x+5].Top:=160;
  357.             end;
  358.           end;
  359.     9   : begin
  360.             Height:=361;
  361.             Width:=1019;
  362.  
  363.             for x:=0 to 4 do begin
  364.               CamList[x+1].Left:=96+(x*184);
  365.               CamList[x+1].Top:=8;
  366.  
  367.               CamList[x+6].Left:=96+(x*184);
  368.               CamList[x+6].Top:=160;
  369.             end;
  370.           end;
  371.     10  : begin
  372.             Height:=361;
  373.             Width:=1019;
  374.  
  375.             for x:=0 to 4 do begin
  376.               CamList[x+1].Left:=96+(x*184);
  377.               CamList[x+1].Top:=8;
  378.  
  379.               CamList[x+6].Left:=96+(x*184);
  380.               CamList[x+6].Top:=160;
  381.             end;
  382.           end;
  383.   end;
  384. end;
  385.  
  386. procedure TfrmMain.TCPClientStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: String);
  387.  begin
  388.   statusMain.SimpleText:=AStatusText;
  389. end;
  390.  
  391. end.
  392.