AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Procedure in Datenmodul (TIdHTTP Komponente)

Ein Thema von DaSchwoa · begonnen am 18. Nov 2020 · letzter Beitrag vom 18. Nov 2020
Antwort Antwort
Seite 2 von 2     12   
DaSchwoa

Registriert seit: 15. Sep 2020
13 Beiträge
 
#11

AW: Procedure in Datenmodul (TIdHTTP Komponente)

  Alt 18. Nov 2020, 15:19
Hier ist mein Code... Vielleicht kann ihn wer gebrauchen :

Code:
nit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
  Vcl.ExtCtrls, DMUnit;

type
  TEuropeanCentralBankCurrencyService = class(TService)
    Timer1: TTimer;
    procedure ServiceAfterInstall(Sender: TService);
    procedure ServiceContinue(Sender: TService; var Continued: Boolean);
    procedure ServiceCreate(Sender: TObject);

    procedure WriteToLog(destination, Text: string);
    procedure ServiceExecute(Sender: TService);
    procedure ServicePause(Sender: TService; var Paused: Boolean);
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure Timer1Timer(Sender: TObject);
  private
    swLogFile: TStreamWriter;

    outputFileName, outputFilePath, outputFilePathName: String;

    currency: TStringList;

  const
    WEBSITE = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';

  public
    function GetServiceController: TServiceController; override;
    { Public-Deklarationen }
  end;

var
  EuropeanCentralBankCurrencyService: TEuropeanCentralBankCurrencyService;

implementation

{$R *.dfm}
uses
  System.Win.Registry, System.ioutils;

procedure TEuropeanCentralBankCurrencyService.WriteToLog(destination: string; Text: string);
begin
  swLogFile.WriteLine('[' + DateTimeToStr(now) + '] ' + destination + ' schreibt: ' + text);
end;

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  EuropeanCentralBankCurrencyService.Controller(CtrlCode);
end;

function TEuropeanCentralBankCurrencyService.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TEuropeanCentralBankCurrencyService.ServiceAfterInstall(Sender: TService);
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create(KEY_READ or KEY_WRITE);
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('\SYSTEM\CurrentControlSet\Services\' + name, false) then
    begin
      Reg.WriteString('Description', 'Dieses Service Synchronisiert die Daten European Central Bank');
      Reg.CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;

procedure TEuropeanCentralBankCurrencyService.ServiceContinue(Sender: TService;var Continued: Boolean);
begin
  WriteToLog('ServiceContinue','Continued');
  Continued:= true;
end;

procedure TEuropeanCentralBankCurrencyService.ServiceCreate(Sender: TObject);
var
  ExePath, logFilePathName, logFileName, logFilePath: String;
begin
  //Übergebe den Pfad der Exe
  ExePath        := TPath.GetDirectoryName(GetModuleName(HInstance));

  //Namen der Dateien
  logFileName    := 'log_Service.txt';
  outputFileName := 'currency.xls';

  //Weise dem ExePath den jeweiligen Ordner zu
  logFilePath    := TPath.Combine(exePath, 'logFile');
  outputFilePath    := TPath.Combine(exePath, 'OutputFile');

  //Füge zu den Pfaden, die Namen hinzu
  logFilePathName  := TPath.Combine(logFilePath, logFileName);
  outputFilePathName:= TPath.Combine(outputFilePath, outputFileName);

  //Erstelle Path wenn nicht existiert
  if not TDirectory.Exists(logFilePath) then
    TDirectory.CreateDirectory(logFilePath);
  if not TDirectory.Exists(outputFilePath) then
    TDirectory.CreateDirectory(outputFilePath);

  //Erstelle Log Stream Reader
  swLogFile := TStreamWriter.Create(TFileStream.Create(logFilePathName, fmCreate or fmShareDenyWrite));

  currency:= TStringList.Create;
end;

procedure TEuropeanCentralBankCurrencyService.ServiceExecute(Sender: TService);
begin
  while not Terminated do
  begin
    ServiceThread.ProcessRequests(false);
    TThread.Sleep(1000);
  end;
end;

procedure TEuropeanCentralBankCurrencyService.ServicePause(Sender: TService;
  var Paused: Boolean);
begin
  WriteToLog('ServicePause', 'Paused');
  Paused:= True;
end;

procedure TEuropeanCentralBankCurrencyService.ServiceStart(Sender: TService;
  var Started: Boolean);
begin
  WriteToLog('ServiceStart', 'Service Started');
  Started:= true;
  Timer1.Enabled:= true;
end;

procedure TEuropeanCentralBankCurrencyService.ServiceStop(Sender: TService;
  var Stopped: Boolean);
begin
  WriteToLog('ServiceStop','Stopped');
  Stopped:= true;
end;

procedure TEuropeanCentralBankCurrencyService.Timer1Timer(Sender: TObject);
begin
  WriteToLog('Timer1Timer','Start');
  FrmDm.doDownload(WEBSITE, outputFilePathName);
  WriteToLog('Timer1Timer','Finished');
end;

end.
Code:
unit DMUnit;

interface

uses
  System.SysUtils, System.Classes, IdIOHandler, IdIOHandlerSocket,
  IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP;

type
  TFrmDm = class(TDataModule)
    IdHTTP1: TIdHTTP;
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;

    procedure WriteToLog(Destination, Text: String);
    procedure DataModuleCreate(Sender: TObject);
    procedure doDownload(Website, Outputfile: String);

  private
    swLogFile: TStreamWriter;
  public

  end;

var
  FrmDm: TFrmDm;

implementation
  uses System.ioutils;

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

procedure TFrmDm.DataModuleCreate(Sender: TObject);
var
  ExePath, logFilePathName, logFileName, logFilePath: String;
begin
  //Übergebe den Pfad der Exe
  ExePath        := TPath.GetDirectoryName(GetModuleName(HInstance));

  //Namen der Dateien
  logFileName    := 'log_DM.txt';

  //Weise dem ExePath den jeweiligen Ordner zu
  logFilePath    := TPath.Combine(exePath, 'logFile');

  //Füge zu den Pfaden, die Namen hinzu
  logFilePathName := TPath.Combine(logFilePath, logFileName);

  //Erstelle Path wenn nicht existiert
  if not TDirectory.Exists(logFilePath) then
    TDirectory.CreateDirectory(logFilePath);

  //Erstelle Log Stream Reader
  swLogFile := TStreamWriter.Create(TFileStream.Create(logFilePathName, fmCreate or fmShareDenyWrite));
  WriteToLog('DataModuleCreate', 'Created');
end;

procedure TFrmDm.WriteToLog(destination: string; Text: string);
begin
  swLogFile.WriteLine('[' + DateTimeToStr(now) + '] ' + destination + ' schreibt: ' + text);
end;

procedure TFrmDm.doDownload(Website: string; Outputfile: string);
var
  ResponseStream: TFileStream;
begin
  WriteToLog('doDownload', 'Try to read');

  ResponseStream := TFileStream.Create(Outputfile, fmCreate);
  IdHTTP1.IOHandler:= IdSSLIOHandlerSocketOpenSSL1;
  IdHTTP1.HandleRedirects:= true;
  try
    IdHTTP1.Get(Website, ResponseStream);
   WriteToLog('Timer1Timer','HTTP-Status: ' + IntToStr(IdHTTP1.ResponseCode));
  finally
    ResponseStream.Free;
  end;
  WriteToLog('doDownload', 'Download abgeschlossen');
end;

end.
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 2     12   

 

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:26 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz