hi everybody, i make a program for get data to weather station, this aplication downloada a txt and show info in form, but some times get error List index(0) out of bounds, and i dont know why. attached my code
unit Main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ComCtrls, ExtCtrls, Grids, Buttons, ftpsend, httpsend;
type
{ TForm1 }
TForm1 = class(TForm)
ButtonLeerTXT: TButton;
ButtonDescargar: TButton;
Image1: TImage;
ImageBanner: TImage;
LabelDirViento: TLabel;
LabelUVientos: TLabel;
LabelWspeed: TLabel;
LabelPresion2: TLabel;
LabelVer: TLabel;
LabelH: TLabel;
LabelUPresion: TLabel;
LabelULluvia: TLabel;
LabelUT: TLabel;
LabelDatoTemp: TLabel;
LabelDatoPresion: TLabel;
BannerFecha: TLabel;
Label4: TLabel;
Label5: TLabel;
LabelDatoLluvia: TLabel;
LabelPresion1: TLabel;
LabelUST: TLabel;
LabelUT1: TLabel;
LabelUT2: TLabel;
LabelVersion: TLabel;
LabelHoraDatos: TLabel;
LabelFechadeDatos: TLabel;
LabelDatoHum: TLabel;
LabelDatoST: TLabel;
LabelHum: TLabel;
LabelPresion: TLabel;
LabelTemp: TLabel;
LabelST: TLabel;
StatusBarEstado: TStatusBar;
TimerLeer: TTimer;
TimerDownload: TTimer;
procedure BorrarFichero(Fichero: String);
procedure ButtonDescargarClick(Sender: TObject);
procedure ButtonLeerTXTClick(Sender: TObject);
function DiasdesdeLanzamiento(StrHoy:string): Integer;
function DownloadHTTP(URL, TargetFile: string): Boolean;
function CalculaDirVientos(Dir:String): String;
procedure LeerDatos;
procedure FormCreate(Sender: TObject);
procedure TimerDownloadTimer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
const FechaActivacion='20/07/15';
const DiasLicencia=30;
var
Form1: TForm1;
lineaNovedades:string;
lineaDatos:string;
ArrayDatos: TStringList;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.BorrarFichero(Fichero: String);
begin
if FileExists(fichero) then
DeleteFile(fichero);
end;
function TForm1.DiasdesdeLanzamiento(StrHoy:string): Integer;
var
FechaHoy,FechaLanzamiento:TDate;
begin
FechaHoy:=StrToDate(StrHoy);
FechaLanzamiento:=StrToDate(FechaActivacion);
Result := Trunc (FechaHoy - FechaLanzamiento);
end;
procedure TForm1.ButtonDescargarClick(Sender: TObject);
var
Resultadoinfo,ResultadoNews,ResultadoBanner:Boolean;
begin
//ButtonDescargar.Visible:=False;
TimerLeer.Enabled:=False;
//BorrarFichero('C:\eclima\realtime.txt');
ArrayDatos.Clear;
Resultadoinfo:=False;
ResultadoNews:=False;
ResultadoBanner:=False;
LineaNovedades:='';
StatusBarEstado.SimpleText:='Descargando información...';
DownloadHTTP('http://www.climaesperanza.com.ar/realtime.txt','C:\eclima\realtime.txt');
Resultadoinfo:=DownloadHTTP('http://www.climaesperanza.com.ar/realtime.txt','C:\eclima\realtime.txt');
if(Resultadoinfo=False) then StatusBarEstado.SimpleText:='Error descargando informacion...'
else
begin
StatusBarEstado.SimpleText:='Información descargada con exito!';
//LeerDatos;
TimerLeer.Enabled:=True; ///Habilito despues de descargar
end;
//StatusBarEstado.SimpleText:='Descargando novedades...';
//DownloadHTTP('http://www.climaesperanza.com.ar/novedades.txt','C:\eclima\novedades.txt');
//ResultadoNews:=DownloadHTTP('http://www.climaesperanza.com.ar/novedades.txt','C:\eclima\novedades.txt');
//if(ResultadoNews=False) then StatusBarEstado.SimpleText:='Error descargando novedades...'
//else
// begin
//StatusBarEstado.SimpleText:='Novedades descargadas con exito!';
//LeerNovedades;
///MemoNews.Caption:=LineaNovedades;
//end;
DownloadHTTP('http://www.climaesperanza.com.ar/images/banner.gif','C:\eclima\banner.gif');
ResultadoBanner:=DownloadHTTP('http://www.climaesperanza.com.ar/images/banner.gif','C:\eclima\banner.gif');
if(ResultadoBanner=False) then StatusBarEstado.SimpleText:='Error descargando Banner...'
else
begin
StatusBarEstado.SimpleText:='Descarga de Banner exitosa...';
ImageBanner.Picture.LoadFromFile('C:\eclima\banner.gif');
end;
if ((ResultadoBanner=False)or(Resultadoinfo=False))then StatusBarEstado.SimpleText:='Error Descargando archivos...'
else StatusBarEstado.SimpleText:='Descarga exitosa...';
end;
procedure TForm1.ButtonLeerTXTClick(Sender: TObject);
begin
if FileExists('C:\eclima\realtime.txt') then
LeerDatos;
end;
function TForm1.DownloadHTTP(URL, TargetFile: string): Boolean;
// Download file; retry if necessary.
// Could use Synapse HttpGetBinary, but that doesn't deal
// with result codes (i.e. it happily downloads a 404 error document)
const
MaxRetries = 3;
var
HTTPGetResult: Boolean;
HTTPSender: THTTPSend;
RetryAttempt: Integer;
begin
Result := False;
RetryAttempt := 1;
HTTPSender := THTTPSend.Create;
try
try
// Try to get the file
HTTPGetResult := HTTPSender.HTTPMethod('GET', URL);
while (HTTPGetResult = False) and (RetryAttempt < MaxRetries) do
begin
Sleep(500 * RetryAttempt);
HTTPGetResult := HTTPSender.HTTPMethod('GET', URL);
RetryAttempt := RetryAttempt + 1;
end;
// If we have an answer from the server, check if the file
// was sent to us.
case HTTPSender.Resultcode of
100..299:
begin
HTTPSender.Document.SaveToFile(TargetFile);
Result := True;
end; //informational, success
300..399: Result := False; // redirection. Not implemented, but could be.
400..499: Result := False; // client error; 404 not found etc
500..599: Result := False; // internal server error
else Result := False; // unknown code
end;
except
// We don't care for the reason for this error; the download failed.
Result := False;
end;
finally
HTTPSender.Free;
end;
end;
function TForm1.CalculaDirVientos(Dir:String):String;
begin
if (Dir='NW')then Result:='NO' else
if (Dir='NNW')then Result:='NNO' else
if (Dir='WNW')then Result:='0NO' else
if (Dir='SW')then Result:='SO' else
if (Dir='SSW')then Result:='SSO' else
if (Dir='WSW')then Result:='OSO' else
if (Dir='W')then Result:='O' else
if (Dir='E')then Result:='E' else
if (Dir='N')then Result:='N' else
if (Dir='S')then Result:='S' else
if (Dir='NNE')then Result:='NNE' else
if (Dir='NE')then Result:='NE' else
if (Dir='ENE')then Result:='ENE' else
if (Dir='ESE')then Result:='ESE' else
if (Dir='SSE')then Result:='SSE' else
if (Dir='SE')then Result:='SE' else
Result:=Dir;
end;
procedure TForm1.LeerDatos;
var f : TextFile;
DiasPasados:integer;
begin
AssignFile(f,'c:\eClima\realtime.txt');
Reset(f);
Readln(f, lineaDatos);
Closefile(f);
ExtractStrings([' '],[],PChar(lineaDatos),ArrayDatos);
DiasPasados:=DiasdesdeLanzamiento(ArrayDatos[0]);
//Label1.Caption:=IntToStr(ArrayDatos.Count);
if(DiasPasados<=DiasLicencia)then
begin
LabelFechadeDatos.Caption:=ArrayDatos[0];
LabelHoraDatos.Caption:=ArrayDatos[1];
LabelDatoTemp.Caption:=ArrayDatos[2];
LabelUT.Caption:=ArrayDatos[14];
LabelDatoHum.Caption:=ArrayDatos[3];
LabelDatoPresion.Caption:=ArrayDatos[10];
LabelUPresion.Caption:=ArrayDatos[15];
LabelDatoST.Caption:=ArrayDatos[24];
LabelUST.Caption:=ArrayDatos[14];
LabelDatoLluvia.Caption:=ArrayDatos[9];
LabelULluvia.Caption:=ArrayDatos[16];
LabelWspeed.Caption:=ArrayDatos[5];
LabelUVientos.Caption:=ArrayDatos[13];
LabelDirViento.Caption:=CalculaDirVientos(ArrayDatos[51]);
/// ButtonDescargar.Visible:=True;
end
else
begin
ShowMessage('Debe descargar la version actualizada de eClima...');
TimerDownload.Enabled:=False;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Resultadoinfo,ResultadoNews,ResultadoBanner:Boolean;
begin
ArrayDatos:=TstringList.Create;
Resultadoinfo:=False;
ResultadoNews:=False;
ResultadoBanner:=False;
LineaNovedades:='';
CreateDir('C:\eClima');
StatusBarEstado.SimpleText:='Descargando información...';
DownloadHTTP('http://www.climaesperanza.com.ar/realtime.txt','C:\eclima\realtime.txt');
Resultadoinfo:=DownloadHTTP('http://www.climaesperanza.com.ar/realtime.txt','C:\eclima\realtime.txt');
if(Resultadoinfo=False) then StatusBarEstado.SimpleText:='Error descargando informacion...'
else
begin
StatusBarEstado.SimpleText:='Información descargada con exito!';
LeerDatos;
end;
//StatusBarEstado.SimpleText:='Descargando novedades...';
//DownloadHTTP('http://www.climaesperanza.com.ar/novedades.txt','C:\eclima\novedades.txt');
//ResultadoNews:=DownloadHTTP('http://www.climaesperanza.com.ar/novedades.txt','C:\eclima\novedades.txt');
//if(ResultadoNews=False) then StatusBarEstado.SimpleText:='Error descargando novedades...'
//else
// begin
// StatusBarEstado.SimpleText:='Novedades descargadas con exito!';
//LeerNovedades;
//MemoNews.Caption :=LineaNovedades;
//end;
DownloadHTTP('http://www.climaesperanza.com.ar/images/banner.gif','C:\eclima\banner.gif');
ResultadoBanner:=DownloadHTTP('http://www.climaesperanza.com.ar/images/banner.gif','C:\eclima\banner.gif');
if(ResultadoBanner=False) then StatusBarEstado.SimpleText:='Error descargando Banner...'
else
begin
StatusBarEstado.SimpleText:='Descarga de Banner exitosa...';
ImageBanner.Picture.LoadFromFile('C:\eclima\banner.gif');
end;
if ((ResultadoBanner=False)or(Resultadoinfo=False)) then StatusBarEstado.SimpleText:='Error Descargando archivos...'
else StatusBarEstado.SimpleText:='Descarga exitosa...';
end;
procedure TForm1.TimerDownloadTimer(Sender: TObject);
begin
end;
end.