unit Livetrack;
interface
uses IdFTP, IdHTTP, Classes, DateUtils, SysUtils;
type TFTP =
class(TIDFTP)
end;
type THTTP =
class(TIDHTTP)
end;
type TLivetrack =
class(TObject)
private
{private Vars}
FHTTP : THTTP;
FFTP : TFTP;
FCacheDirectory :
string;
FFTPHost :
String;
FFTPDirectory :
String;
FFTPLoginName :
String;
FFTPPassword :
String;
FHTTPUrl :
String;
FFileName :
String;
FVID :
String;
{Set Procedures}
procedure SetFTPHost(Host :
String);
procedure SetFTPDirectory(Directory :
String);
procedure SetFTPLoginName(LoginName :
String);
procedure SetFTPPassword(Password :
String);
procedure SetVID(VID :
String);
{get Functions}
function GetFTPHost :
String;
function GetFTPLoginName :
String;
function GetFTPPassword :
String;
public
{Functions}
constructor create(
const CacheDirectory :
String;
const FTPHost :
string;
const FTPDirectory :
string;
const FTPLoginName :
string;
const FTPPassword :
string;
const HTTPUrl :
String;
const FileName :
String;
const VID :
string);
overload;
constructor create(
const CacheDirectory :
String;
const FTPHost :
string;
const FTPDirectory :
string;
const FTPLoginName :
string;
const FTPPassword :
string;
const HTTPUrl :
String;
const FileName :
String);
overload;
destructor destroy ;
override;
{Normal Functions}
function download(TimeOut : Integer) : boolean;
function upload(TimeOut : Integer) : boolean;
function refresh (
const RealName :
String;
const ATC_Position :
String;
const Tool :
String): boolean;
{properties}
property CacheDirectory :
String read FCacheDirectory
write FCacheDirectory;
property FTPHost :
String read GetFTPHost
write SetFTPHost;
property FTPDirectory :
String read FFTPDirectory
write SetFTPDirectory;
property FTPLoginName :
String read GetFTPLoginName
write SetFTPLoginName;
property FTPPassword :
String read GetFTPPassword
write SetFTPPassword;
property HTTPUrl :
String read FHTTPUrl
write FHTTPUrl;
property FileName :
String read FFileName
write FFileName;
property VID :
String read FVID
write SetVID;
end;
implementation
{Set Procedures}
procedure TLivetrack.SetFTPHost(Host :
String);
begin
self.FFTP.Host := Host;
end;
procedure TLivetrack.SetFTPDirectory(Directory :
String);
begin
if Directory = '
'
then
exit;
if self.FFTP.Connected
then
self.FFTP.ChangeDir(Directory);
end;
procedure TLivetrack.SetFTPLoginName(LoginName :
String);
begin
if self.FFTP.Connected
then
exit;
self.FFTP.Username := LoginName;
end;
procedure TLivetrack.SetFTPPassword(Password :
String);
begin
if self.FFTP.Connected
then
exit;
self.FFTP.Password := Password;
end;
procedure TLivetrack.SetVID(VID :
String);
begin
self.FVID := VID;
end;
// Ende Set Procedures
////////////////////////////////////////////////////////////////////////////////
{Get Functions}
function TLivetrack.GetFTPHost :
String;
begin
result := self.FFTP.Host;
end;
function TLivetrack.GetFTPLoginName :
String;
begin
result := self.FFTP.Username;
end;
function TLivetrack.GetFTPPassword :
String;
begin
result := self.FFTP.Password;
end;
// Ende Get Functions
////////////////////////////////////////////////////////////////////////////////
{Normal Functions}
function createTimeStamp :
string;
begin
result := inttostr(DateTimeToUnix(now));
end;
// *.csv - Aufbau {VID : TimeStamp : RealName : ATC Position : Tool}
function TLivetrack.refresh(
const RealName :
String;
const ATC_Position :
String;
const Tool :
String) : boolean;
function datainlist(
const TList : TStringlist) : integer;
var i : Integer;
begin
result := -1;
if TList.Count = 0
then
exit;
for i := 0
to TList.Count - 1
do
if self.FVID = copy(TList.Strings[i],1,pos('
:',TList.strings[i]) - 1)
then
begin
result := i;
break;
end;
end;
var TList : Tstringlist;
Index : integer;
FileName :
string;
TimeStamp :
string;
begin
result := false;
TList := TStringlist.Create;
FileName := self.FCacheDirectory + '
\' + self.FFileName;
try
if Fileexists(FileName)
then
Tlist.LoadFromFile(FileName);
Index := datainList(TList);
TimeStamp := createTimeStamp;
if Index = -1
then
TList.add(self.FVID + '
:' + TimeStamp + '
:' + RealName + '
:' + ATC_Position + '
:' + Tool)
else
TList.strings[
Index] := self.FVID + '
:' + TimeStamp + '
:' + RealName + '
:' + ATC_Position + '
:' + Tool;
finally
result := true;
end;
TList.SaveToFile(FileName);
TList.Free;
end;
function TLivetrack.upload(TimeOut : Integer) : boolean;
begin
result := false;
self.FFTP.Port := 21;
self.FFTP.Connect(true,TimeOut);
if self.FFTPDirectory <> '
'
then
self.FFTP.ChangeDir(self.FFTPDirectory);
try
self.FFTP.Put(self.FCacheDirectory + '
\' + self.FFileName , self.FFileName , false);
finally
result := true;
end;
self.FFTP.Disconnect;
end;
function TLivetrack.download(TimeOut : Integer) : boolean;
var FileStream : TFileStream;
begin
result := false;
FileStream := TFileStream.create(self.FCacheDirectory + '
\' + self.FFileName,fmcreate);
self.FHTTP.Connect(TimeOut);
try
self.FHTTP.Get(self.FHTTPUrl + '
/' + self.FFileName , FileStream);
finally
result := true;
end;
FileStream.free;
self.FHTTP.Disconnect;
end;
// Ende Normal Functions
////////////////////////////////////////////////////////////////////////////////
{Constructor}
constructor TLivetrack.create(
const CacheDirectory :
String;
const FTPHost :
string;
const FTPDirectory :
string;
const FTPLoginName :
string;
const FTPPassword :
string;
const HTTPUrl :
String;
const FileName :
String);
begin
inherited create;
self.FFTP := TFTP.Create(
nil);
self.FHTTP := THTTP.Create(
nil);
// Setzen der Parameter für HTTP und FTP
self.CacheDirectory := CacheDirectory;
self.FTPHost := FTPHost;
self.FTPDirectory := FTPDirectory;
self.FTPLoginName := FTPLoginName;
self.FTPPassword := FTPPassword;
self.HTTPUrl := HTTPUrl;
self.FileName := FileName;
self.VID := '
none';
end;
constructor TLivetrack.create(
const CacheDirectory :
String;
const FTPHost :
string;
const FTPDirectory :
string;
const FTPLoginName :
string;
const FTPPassword :
string;
const HTTPUrl :
String;
const FileName :
String;
const VID :
string);
begin
inherited create;
self.FFTP := TFTP.Create(
nil);
self.FHTTP := THTTP.Create(
nil);
// Setzen der Parameter für HTTP und FTP
self.CacheDirectory := CacheDirectory;
self.FTPHost := FTPHost;
self.FTPDirectory := FTPDirectory;
self.FTPLoginName := FTPLoginName;
self.FTPPassword := FTPPassword;
self.HTTPUrl := HTTPUrl;
self.FileName := FileName;
self.VID := VID;
end;
// Ende Constructor
////////////////////////////////////////////////////////////////////////////////
{Destructor}
destructor TLivetrack.destroy;
begin
self.FFTP.Free;
self.FHTTP.free;
inherited destroy;
end;
// Ende Destructor
////////////////////////////////////////////////////////////////////////////////
end.