Hello Danijel, please give your advice
I recently bought the RTC SDK because I need to build a windows service to work on the following scenario:
There will be 50 devices sending a string of characters to a fixed IP address and a port, for example:
3-PAL, 12-02-16 14: 17: 46,5916386327, Camilo.The 50 devices are sending 15,000 lines per day, from 3:00 am to 10:00 pm.
That string of characters I'm receiving using the TRtcTCPServer component, in the OnDataReceived event, everything is ok. The problem is that the service is not stable and at some point in the day, although the service manager window says that it is running, the information is not arriving to be recorded to the database.
I would like to know which is the best option to develop a service as described above, as I said I am using:
TRtcTcpServer with MultiThread = True
FireDac for connection and insert into the database.
Delphi XE6
I do not know if i'm doing the best, thanks in advanced and sorry for the long post
The code in the event OnExecute of the service, is as follow:
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
vgad.Controller(CtrlCode);
end;
function Tvgad.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure Tvgad.ServiceExecute(Sender: TService);
begin
Timer1.Enabled := True;
DoConectarDBSistema;
DoConectarDBDatos;
qrSQL.Close;
qrSQL.SQL.Text := 'SELECT IP_SERVER, PUERTO FROM PARAMETRO';
qrSQL.Open();
rtcServer.ServerAddr := qrSQL.FieldByName('IP_SERVER').AsString;
rtcServer.ServerPort := qrSQL.FieldByName('PUERTO').AsString;
rtcServer.Listen();
while not Terminated do
ServiceThread.ProcessRequests(True);
Timer1.Enabled := False;
end;
procedure Tvgad.rtcServerDataReceived(Sender: TRtcConnection);
Var sDato: String;
FF: Text;
sFile: String;
begin
Timer1.Enabled := False;
sDato := Sender.Read;
qrSQL.SQL.Text := ' insert into vw_linea ( ' +
' linea) ' +
' values ( ' +
' :linea) ' ;
qrSQL.ParamByName('linea').AsString := sDato;
Try
qrSQL.ExecSQL;
Timer1.Enabled := True;
Except
on E: Exception do begin
// Log the received string
sFile := 'c:\GADIDX\LOG\LineasX.log';
AssignFile(FF, 'c:\GADIDX\LOG\LineasX.log');
if FileExists(sFile) then begin
Append(FF);
end else begin
Rewrite(FF);
end;
Writeln(FF, sDato);
CloseFile(FF);
// Log the error exception
sFile := 'c:\GADIDX\LOG\Errores.log';
AssignFile(FF, 'c:\GADIDX\LOG\Errores.log');
if FileExists(sFile) then begin
Append(FF);
end else begin
Rewrite(FF);
end;
sDato := FormatDateTime('dd/mm/yyyy hh:mm:ss', Now) +
': ECN = ' + E.ClassName +
' --- ' +
' EM = ' + E.Message;
Writeln(FF, sDato);
CloseFile(FF);
Timer1.Enabled := True;
end;
End;
end;
procedure Tvgad.ServiceDestroy(Sender: TObject);
begin
rtcServer.StopListen;
end;
procedure Tvgad.Timer1Timer(Sender: TObject);
var
FF: Text;
sFile: String;
begin
sFile := 'c:\GADIDX\LOG\GadIdx.log';
AssignFile(FF, 'c:\GADIDX\LOG\GadIdx.log');
if FileExists(sFile) then begin
Rewrite(FF);
end else begin
Rewrite(FF);
end;
Writeln(FF, FormatDateTime('dd/mm/yyyy hh:mm:ss', Now));
CloseFile(FF);
end;
procedure Tvgad.DoConectarDBSistema;
begin
//
fdCia.Connected := False;
with fdCia.Params do
begin
Clear;
Add('DriverID=FB');
Add('Database=' + 'C:\GADIDX\DATOS\SYSTEMS.WAS');
Add('User_Name=SYSDBA');
Add('Password=masterkey');
Add('CharacterSet=UNICODE_FSS');
Add('CreateDatabase=No');
Add('ExtendedMetadata=True');
Add('Server=' + '');
Add('Protocol=' + 'LOCAL');
end;
fdCia.Open();
cnDato.Close;
end;
procedure Tvgad.cnDatoAfterConnect(Sender: TObject);
begin
qrSQL.SQL.Text := 'UPDATE PARAMETRO SET ULTIMA_FECHA_SERVIDOR = :FECHA';
qrSQL.ParamByName('FECHA').AsDateTime := NOW;
qrSQL.ExecSQL;
end;
procedure Tvgad.DoConectarDBDatos;
begin
//
// Vamos buscar la ruta de la base de datos de la primara compania
qrCia.Close;
qrCia.Open;
//
// Vamos asignar la ruta de la base de datos de la compania.
cnDato.Connected := False;
with cnDato.Params do
begin
Clear;
Add('DriverID=FB');
Add('Database=' + qrCia.FieldByName('RUTA_BASE_DATO').AsString);
Add('User_Name=SYSDBA');
Add('Password=masterkey');
Add('Server=' + qrCia.FieldByName('SERVIDOR').AsString);
Add('Protocol=' + qrCia.FieldByName('PROTOCOLO').AsString);
Add('CreateDatabase=No');
Add('ExtendedMetadata=True');
end;
cnDato.Open();
qrCia.Close;
fdCia.Close;
end;
end.