Proyecto

General

Perfil

Descargar (7,52 KB) Estadísticas
| Rama: | Revisión:
a2ba555f Elisa
unit main;

{$mode objfpc}{$H+}

interface

uses
23a384b9 Elisa
IniFiles, HTTPDefs,clrespuesta,Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls, IdTCPClient, Buttons;
a2ba555f Elisa
type

{ TfrmMain }

TfrmMain = class(TForm)
Image1: TImage;
lblPregunta: TLabel;
lblTiempo: TStaticText;
tmrCuestion: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure tmrCuestionTimer(Sender: TObject);
procedure btnOpcionesClick(sender:TObject);
private
procedure CrearMemos;
procedure LeerConf(archivo: string);
function LeerIni(): boolean;
function ObtenerMiIp(): string;
function tiempo(value: integer): string;
{ private declarations }
public
procedure Finalizar;
{ public declarations }
end;

var
frmMain: TfrmMain;
lblOpciones:Array of Tlabel;
btnOpciones:Array of TButton;
f_contador:integer;
f_existe_custion:boolean;


implementation

uses global;
var
23a384b9 Elisa
fInterface:string='eth3';
a2ba555f Elisa
Const
23a384b9 Elisa
DIR_CUSTION:string='%s/recibidos_administrador/.cuestion.ini';
DIR_CONF:string='/etc/adtosaula.conf';
a2ba555f Elisa
procedure TfrmMain.FormCreate(Sender: TObject);
begin
23a384b9 Elisa
try
//setbounds(0,0,screen.width,screen.height);
//lblpregunta.top:=round((lblpregunta.top* (screen.height / 600)));
//lblTiempo.top:=round((lblTiempo.top * (screen.height / 600)));
g_mihost:=ObtenerMiIp();
g_ip_administrador:='';
if FileExists(DIR_CONF) then
LeerConf(DIR_CONF);
g_mihost:=ObtenerMiIp();
g_dirpersonal:=getenvironmentvariable('HOME');
g_usuario:=getenvironmentvariable('USER');
except
WriteLn('No existe el archivo de configuración');
end;
a2ba555f Elisa
end;

function TfrmMain.ObtenerMiIp():string;
var
23a384b9 Elisa
mip:string;
a2ba555f Elisa
begin
23a384b9 Elisa
mip:=trim(HallarDato('/sbin/ifconfig ' +fInterface+ ' | grep "inet" | tr -s " " | sed "s/^[ \t]*//" | cut -f2 -d":" | cut -f1 -d" "'));
result:=mip;
a2ba555f Elisa
end;
23a384b9 Elisa
a2ba555f Elisa
procedure TfrmMain.FormShow(Sender: TObject);
begin
23a384b9 Elisa
if not LeerIni() then
close;
a2ba555f Elisa
end;

procedure TfrmMain.tmrCuestionTimer(Sender: TObject);
var
comando:string;
begin
f_contador:=f_contador-1;
lbltiempo.caption:=Tiempo(f_contador);
if f_contador = 0 then
begin
enabled:=false;
hide;
tmrCuestion.enabled:=false;
g_tiempoempleado:=g_tiempo-f_contador;
comando:='respuestaCuestion '+ HTTPEncode(g_mihost) +' '+HTTPEncode(g_Codigo)+' '+HTTPEncode('0')+' '+HTTPEncode(inttostr(g_tiempoempleado))+' '+HTTPEncode(g_usuario);
23a384b9 Elisa
with ThrRespuesta.create(g_ip_administrador,comando) do resume;
a2ba555f Elisa
end;
end;

procedure Tfrmmain.Finalizar;
begin
close;
end;

function Tfrmmain.tiempo(value:integer):string;
var
minutos,segundos:string;
nsegundos:integer;
begin
minutos:='0'+inttostr(value div 60);
nsegundos:=value mod 60;
if value <10 then
segundos:='0'+inttostr(nsegundos)
else
segundos:=inttostr(nsegundos);
result:=minutos+':'+segundos;
end;
procedure Tfrmmain.LeerConf(archivo:string);
var
FicheroIni:TMemIniFile;
begin
try
FicheroIni:=TMemIniFile.create(archivo);
try
with FicheroIni do
begin
23a384b9 Elisa
g_ip_administrador:=ReadString('Administrador','IP','');
a2ba555f Elisa
fInterface:=ReadString('Red','Interface','eth0');
end;
finally
FicheroIni.free;
end;
except
writeLn('Error al cargar datos');
end;
end;
Function Tfrmmain.LeerIni():boolean;
var
Mifichero:TMemIniFile;
23a384b9 Elisa
dircuestion:string;
a2ba555f Elisa
i:integer;
begin
23a384b9 Elisa
dircuestion:=Format(DIR_CUSTION,[g_dirpersonal]);
a2ba555f Elisa
result:=false;
try
23a384b9 Elisa
if FileExists(dircuestion) then
begin
Mifichero:= TMemIniFile.create(dircuestion);
try
with MiFichero do
begin
g_codigo:=ReadString('TIPO','Codigo','');
g_tipoc:=ReadString('TIPO','Tipo','0');
g_tiempo:=ReadInteger('TIPO','Tiempo',30);
g_num_opciones:=ReadInteger('TIPO','Numero_Opciones',0);
if g_num_opciones>0 then
begin
setlength(g_opciones,g_num_opciones);
setlength(lblOpciones,g_num_opciones);
setlength(btnOpciones,g_num_opciones);
g_pregunta:=ReadString('CUESTION','Pregunta', '');
lblPregunta.caption:=HTTPDecode(g_pregunta);
for i:=0 to high(g_opciones) do
g_opciones[i]:=ReadString('CUESTION','Opcion_'+inttostr(i+1), '');
CrearMemos()
end;
a2ba555f Elisa
end;
23a384b9 Elisa
f_contador:=g_tiempo;
tmrCuestion.enabled:=true;
finally
Mifichero.free;
end;
result:=true;
end ;
a2ba555f Elisa
except
result:=false;
end;
end;

procedure Tfrmmain.CrearMemos;
var i:integer;
begin
lblPregunta.font.size:=14;
for i:=0 to high(lblOpciones) do
begin
lblOpciones[i]:=Tlabel.Create(self);
btnOpciones[i]:=TButton.Create(self);
with btnOpciones[i] do
begin
font.name:='Sans';
font.size:=14;
width:=35;
height:=35;
cursor:=crHandPoint;
Caption:= intToStr(i+1);
Parent:=self;
tag:=i+1;
left:= lblpregunta.left-5;
23a384b9 Elisa
top:= lblpregunta.top + lblpregunta.height + 50 + (i*(10+lblpregunta.height-10));
a2ba555f Elisa
onClick:=@btnOpcionesClick;
end;
with lblOpciones[i] do
begin
font.name:='Sans';
font.size:=14;
width:=lblpregunta.width-btnOpciones[i].width-10;
height:=lblpregunta.height-10;
Parent:=self;
tag:=i+1;
left:= btnOpciones[i].left+btnOpciones[i].width+10;
top:= btnOpciones[i].top+5;
text:=HTTPDecode(g_opciones[i]);
end;
end;
end;

procedure Tfrmmain.btnOpcionesClick(sender:TObject);
var
respuesta:integer;
comando:string;
begin
try
application.ProcessMessages;
tmrCuestion.enabled:=false;
hide;
g_tiempoempleado:=g_tiempo-f_contador;
respuesta:=(sender as Tbutton).tag;
comando:='respuestaCuestion '+ HTTPEncode(g_mihost) +' '+HTTPEncode(g_Codigo)+' '+HTTPEncode(inttostr(respuesta))+' '+HTTPEncode(inttostr(g_tiempoempleado))+' '+HTTPEncode(g_usuario);
23a384b9 Elisa
with ThrRespuesta.create(g_ip_administrador,comando) do resume;
a2ba555f Elisa
except
writeln('error al contestar ');
23a384b9 Elisa
a2ba555f Elisa
end;
end;
initialization
{$I main.lrs}

end.