TScriptEngineThread =
class(TThread)
private
FDevice:
string;
FModul:
string;
FParam:
string;
FData:
string;
CS: TCriticalSection;
CA: TComAdapter;
PS: TPSScript;
FQry: TZQuery;
FQueryList: TStringList;
FWriteLn: TOnWriteLn;
procedure PSCompile(Sender: TPSScript);
procedure ClearQueryList;
function AddQueryObject(
Name:
string): Boolean;
function RemoveQueryObject(
Name:
string): Boolean;
function GetQuery(
Name:
string;
var Qry: TZQuery): Boolean;
function FindQueryInList(
Name:
string;
var Index: Integer): Boolean;
// Script Methods
function PS_GetDevice():
string;
function PS_GetModul():
string;
function PS_GetParam():
string;
function PS_GetData():
string;
procedure PS_WriteToModul(Device:
string; Modul:
string; Param:
string; Data:
string);
function PS_QueryAdd(
Name:
string): Boolean;
function PS_QueryRem(
Name:
string): Boolean;
procedure PS_Query(
Name:
string;
SQL:
string);
function PS_QueryRowsAffected(
Name:
string): Integer;
function PS_QueryRecordCount(
Name:
string): Integer;
procedure PS_QueryFirst(
Name:
string);
procedure PS_QueryLast(
Name:
string);
procedure PS_QueryNext(
Name:
string);
procedure PS_QueryPrior(
Name:
string);
function PS_QueryEof(
Name:
string): Boolean;
function PS_QueryReadsInt(
Name:
string; Fieldname:
string): Integer;
function PS_QueryReadAsStr(
Name:
string; Fieldname:
string):
string;
function PS_QueryReadAsFloat(
Name:
string; Fieldname:
string): Double;
function PS_GetDBData(Device:
string; Modul:
string; Param:
string):
string;
procedure PS_AddWebAlert(S:
string);
procedure PS_AddWebNotification(S:
string);
procedure PS_AddWebLog(S:
string);
procedure PS_WriteLn(S:
string);
function PS_Now(): Double;
function PS_DateTimeToStr(DateTime: Double):
string;
protected
procedure Execute;
override;
public
constructor Create(ComAdapter: TComAdapter);
destructor Destroy;
override;
procedure Run(Content:
string);
overload;
procedure Run(Device, Modul, Param, Data, Content:
string);
overload;
property OnWriteLn: TOnWriteLn
read FWriteLn
write FWriteLn;
end;
...
procedure TScriptEngineBase.PSCompile(Sender: TPSScript);
begin
// Generic and Debug
Sender.AddMethod(Self, @TScriptEngineBase.PS_WriteLn, '
procedure WriteLn(S: string);');
Sender.AddMethod(Self, @TScriptEngineBase.PS_DateTimeToStr, '
function DateTimeToStr(const DateTime: Double): string;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_Now, '
function Now: Double;');
// Get and Set Event Data
Sender.AddMethod(Self, @TScriptEngineBase.PS_GetDevice, '
function GetDevice(): string;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_GetModul, '
function GetModul(): string;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_GetParam, '
function GetParam(): string;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_GetData, '
function GetData(): string;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_WriteToModul, '
procedure WriteToModul(Device: string; Modul: string; Param: string; Data: string);');
// SQL Communication
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryAdd, '
function QueryAdd(Name: string): Boolean);');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryRem, '
function QueryRem(Name: string): Boolean);');
Sender.AddMethod(Self, @TScriptEngineBase.PS_Query, '
procedure Query(Name: string; SQL: string);');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryRowsAffected, '
function QueryRowsAffected(Name: string): Integer;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryRecordCount, '
function QueryRecordCount(Name: string): Integer;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryFirst, '
procedure QueryFirst(Name: string);');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryLast, '
procedure QueryLast(Name: string);');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryNext, '
procedure QueryNext(Name: string);');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryPrior, '
procedure QueryPrior(Name: string);');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryEof, '
function QueryEof(Name: string): Boolean;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryReadsInt, '
function QueryReadsInt(Name: string; Fieldname: string): Integer;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryReadAsStr, '
function QueryReadAsStr(Name: string; Fieldname: string): string;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_QueryReadAsFloat, '
function QueryReadAsFloat(Name: string; Fieldname: string): Double;');
Sender.AddMethod(Self, @TScriptEngineBase.PS_GetDBData, '
function DBData(Device: string; Modul: string; Param: string; Data: string): string;');
// Alert and Log
Sender.AddMethod(Self, @TScriptEngineBase.PS_AddWebNotification, '
procedure AddWebNotification(S: string);');
Sender.AddMethod(Self, @TScriptEngineBase.PS_AddWebLog, '
procedure AddWebLog(S: string);');
Sender.AddFunction(@ShowMessage, '
procedure ShowMessage(const Msg: string)');
Sender.AddFunction(@ExtractFileExt,'
function ExtractFileExt(const FileName: string): string;');
Sender.AddFunction(@Sleep, '
procedure Sleep(milliseconds: Cardinal);');
end;
...
procedure TScriptEngineThread.Execute;
var
Compiled: Boolean;
Messages:
string;
I: Integer;
begin
Compiled := PS.Compile;
for I := 0
to PS.CompilerMessageCount -1
do
Messages := Messages + PS.CompilerMessages[I].MessageToString + #13#10;
PS_WriteLn(Messages);
if Compiled
then
begin
try
PS.Execute;
except
on E:
Exception do
PS_WriteLn(E.
Message);
end;
end;
PS_WriteLn('
PROC::DONE');
end;
...
procedure TScriptEngineThread.PS_WriteLn(S:
string);
begin
CS.Acquire;
try
if Assigned(FWriteLn)
then
FWriteLn(S);
finally
CS.Release;
end;
end;