unit DaemonUnit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, DaemonApp;
type
{ TDaemon1 }
TDaemon1 = class(TDaemon)
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
procedure DataModuleExecute(Sender: TCustomDaemon);
procedure DataModuleShutDown(Sender: TCustomDaemon);
procedure DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
procedure DataModuleStop(Sender: TCustomDaemon; var OK: Boolean);
procedure Timer1Timer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
{ TMyThread }
TMyThread = class(TThread)
protected
procedure Execute; override;
end;
var
Daemon1: TDaemon1;
implementation
procedure RegisterDaemon;
begin
RegisterDaemonClass(TDaemon1)
end;
{$R *.lfm}
{ TDaemon1 }
procedure add_to_file(s: ansistring; FileName: string = 'log.log';
NewLine: boolean = True);
var
f: TFileStream;
begin
try
if FileExists(FileName) then
begin
f := TFileStream.Create(FileName, fmOpenReadWrite or fmShareDenyNone);
f.Seek(0, soEnd);
end
else
f := TFileStream.Create(FileName, fmCreate or fmShareDenyNone);
if NewLine then
s := s + #$D#$A;
f.Write(PChar(s)^, Length(s));
f.Free;
except
end;
end;
{ TMyThread }
procedure TMyThread.Execute;
var s : string;
begin
sleep(10000);
s := 'start delay @ ' + DateTimeToStr(Now);
add_to_file(s, '/home/pi/dung.txt');
end;
procedure TDaemon1.DataModuleCreate(Sender: TObject);
begin
end;
procedure TDaemon1.DataModuleDestroy(Sender: TObject);
begin
end;
procedure TDaemon1.DataModuleExecute(Sender: TCustomDaemon);
begin
end;
procedure TDaemon1.DataModuleShutDown(Sender: TCustomDaemon);
begin
end;
procedure TDaemon1.DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
var s : string;
begin
s := 'start at ' + DateTimeToStr(Now);
add_to_file(s, '/home/pi/dung.txt');
TMyThread.Create(False);
end;
procedure TDaemon1.DataModuleStop(Sender: TCustomDaemon; var OK: Boolean);
var s : string;
begin
s := 'finis at ' + DateTimeToStr(Now);
add_to_file(s, '/home/pi/dung.txt');
end;
procedure TDaemon1.Timer1Timer(Sender: TObject);
var s : string;
begin
s := DateTimeToStr(Now);
add_to_file(s, '/home/pi/dung.txt');
end;
initialization
RegisterDaemon;
end.