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.