CAP/cap_control/TPropiedades.cpp
2021-09-01 18:53:06 +02:00

404 lines
14 KiB
C++

//---------------------------------------------------------------------------
#include <registry.hpp>
#include <vcl.h>
#include <inifiles.hpp>
#include <stdio.h>
#pragma hdrstop
#include "TPropiedades.h"
#include "LEGO_COMM.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "DigitNum"
#pragma link "LM2000"
#pragma resource "*.dfm"
TPropiedades *Propiedades;
//---------------------------------------------------------------------------
__fastcall TPropiedades::TPropiedades(TComponent* Owner)
: TForm(Owner)
{
// Recuperar configuracion
// Recuperamos la configuración
TIniFile *ini;
ini = new TIniFile( ExtractFileDir( Application->ExeName ) + "\\CAP.INI" );
// General
ComboBox1->ItemIndex = ini->ReadInteger( "CAP_c", "PuertoLego", 0 );
DireccionesE->Text = ini->ReadString( "CAP_c", "IDsLegoE", "1;" );
DireccionesS->Text = ini->ReadString( "CAP_c", "IDsLegoS", "2;" );
Historial->Checked = ini->ReadBool( "CAP_c", "historial", true );
Capacidad->Text = ini->ReadInteger( "parking", "Capacidad", 1000 );
MsgCapacidad->Text = ini->ReadString( "parking", "MsgParking_lleno", "El parking esta lleno" );
ComboBox2->ItemIndex = ini->ReadInteger( "parking", "DiasDeCortesia", 0 );
DB_host->Text = ini->ReadString( "db", "host", "." );
DB_login->Text = ini->ReadString( "db", "login", "NO_ES_SQL" );
DB_pwd->Text = ini->ReadString( "db", "pwd", "NO_ES_SQL" );
/* Sobrecarga de franjas horarias */
CheckBox1->Checked = ini->ReadBool( "horas", "sobrecargar", false );
verL->Checked = ini->ReadBool( "horas", "verL", false );
desdeL->Text = ini->ReadString( "horas", "desdeL", "00:00" );
hastaL->Text = ini->ReadString( "horas", "hastaL", "00:00" );
verM->Checked = ini->ReadBool( "horas", "verM", false );
desdeM->Text = ini->ReadString( "horas", "desdeM", "00:00" );
hastaM->Text = ini->ReadString( "horas", "hastaM", "00:00" );
verX->Checked = ini->ReadBool( "horas", "verX", false );
desdeX->Text = ini->ReadString( "horas", "desdeX", "00:00" );
hastaX->Text = ini->ReadString( "horas", "hastaX", "00:00" );
verJ->Checked = ini->ReadBool( "horas", "verJ", false );
desdeJ->Text = ini->ReadString( "horas", "desdeJ", "00:00" );
hastaJ->Text = ini->ReadString( "horas", "hastaJ", "00:00" );
verV->Checked = ini->ReadBool( "horas", "verV", false );
desdeV->Text = ini->ReadString( "horas", "desdeV", "00:00" );
hastaV->Text = ini->ReadString( "horas", "hastaV", "00:00" );
verS->Checked = ini->ReadBool( "horas", "verS", false );
desdeS->Text = ini->ReadString( "horas", "desdeS", "00:00" );
hastaS->Text = ini->ReadString( "horas", "hastaS", "00:00" );
verD->Checked = ini->ReadBool( "horas", "verD", false );
desdeD->Text = ini->ReadString( "horas", "desdeD", "00:00" );
hastaD->Text = ini->ReadString( "horas", "hastaD", "00:00" );
delete ini;
StartEventRecorder("Historial.log");
// Hacer efectivos los cambios
bAplicarClick(0);
LEGO_COMM = new CheckLEGOs(true);
LEGO_COMM -> SetPuerto( ComboBox1->Items->Strings[ComboBox1->ItemIndex] );
LEGO_COMM -> SetDB( DB_host->Text, DB_login->Text, DB_pwd->Text );
LEGO_COMM -> SetDiasCortesia( ComboBox2->ItemIndex );
LEGO_COMM -> SetLegoID( nLEGOSe, IDe, nLEGOSs, IDs );
try {
LEGO_COMM -> maxDENTRO = Capacidad->Text.ToInt();
}catch(...){
LEGO_COMM -> maxDENTRO = 2000;
}
LEGO_COMM -> Resume();
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::StartEventRecorder(AnsiString opLOG)
{
try {
// Cargamos todos los eventos anteriores
mHistorial->Lines->LoadFromFile( opLOG );
// Si hay demasiados registros
if ( mHistorial->Lines->Count > 700 )
{
for ( int i=0; i<100; i++ ) // Eliminamos las 100 últimas entradas
mHistorial->Lines->Delete( 3 );
}
} catch(...) {
mHistorial->Lines->Clear(); // En caso de error, las eliminamos todas
}
//Fecha y hora del inicio de sesión
mHistorial->Lines->Insert( 0, (Now()).DateTimeString() + " :: Inicio de la sesión" );
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::BorrarHistorialClick(TObject *Sender)
{
mHistorial -> Lines -> Clear();
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::FormDestroy(TObject *Sender)
{
if ( Historial -> Checked )
{
mHistorial -> Lines -> Insert( 0, TDateTime::CurrentDateTime().DateTimeString() + " :: Fin de sesión ");
mHistorial -> Lines -> SaveToFile( "Historial.log" );
}
/*
// Desactivamos el HILO de ejecución y esperamos hasta que acabe
LEGO_COMM->Terminate();
LEGO_COMM->WaitFor();
*/
delete IDe;
delete IDs;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::mHistorialChange(TObject *Sender)
{
/*
try {
if ( Historial -> Checked )
mHistorial -> Lines -> SaveToFile("Historial.log");
} catch(...) {
// do nothing
}
*/
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::ActivarAplicar(TObject *Sender)
{
if ( ! bDesBlock->Visible )
{
bAplicar->Enabled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::bDesBlockClick(TObject *Sender)
{
ComboBox2->Enabled = true;
ComboBox1->Enabled = true;
DireccionesE->Enabled = true;
DireccionesS->Enabled = true;
Capacidad->Enabled = true;
MsgCapacidad->Enabled = true;
DB_host->Enabled = true;
DB_login->Enabled = true;
DB_pwd->Enabled = true;
bDesBlock->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::bAplicarClick(TObject *Sender)
{
if ( Sender )
{
// Guardamos los cambios...
TIniFile *ini;
ini = new TIniFile( ExtractFileDir( Application->ExeName ) + "\\CAP.INI" );
// General
ini->WriteInteger( "CAP_c", "PuertoLego", ComboBox1->ItemIndex );
ini->WriteString( "CAP_c", "IDsLegoE", DireccionesE->Text );
ini->WriteString( "CAP_c", "IDsLegoS", DireccionesS->Text );
ini->WriteBool( "CAP_c", "historial", Historial->Checked );
try {
ini->WriteInteger( "parking", "Capacidad", Capacidad->Text.ToInt() );
} catch(...) {
ini->WriteInteger( "parking", "Capacidad", 1000 );
}
ini->WriteString( "parking", "MsgParking_lleno", MsgCapacidad->Text );
ini->ReadInteger( "parking", "DiasDeCortesia", ComboBox2->ItemIndex );
ini->WriteString( "db", "host", DB_host->Text );
ini->WriteString( "db", "login", DB_login->Text );
ini->WriteString( "db", "pwd", DB_pwd->Text );
/* Sobrecarga de franjas horarias */
ini->WriteBool( "horas", "sobrecargar", CheckBox1->Checked );
ini->WriteBool( "horas", "verL", verL->Checked );
ini->WriteString( "horas", "desdeL", desdeL->Text );
ini->WriteString( "horas", "hastaL", hastaL->Text );
ini->WriteBool( "horas", "verM", verM->Checked );
ini->WriteString( "horas", "desdeM", desdeX->Text );
ini->WriteString( "horas", "hastaM", hastaX->Text );
ini->WriteBool( "horas", "verX", verX->Checked );
ini->WriteString( "horas", "desdeX", desdeX->Text );
ini->WriteString( "horas", "hastaX", hastaX->Text );
ini->WriteBool( "horas", "verJ", verJ->Checked );
ini->WriteString( "horas", "desdeJ", desdeJ->Text );
ini->WriteString( "horas", "hastaJ", hastaJ->Text );
ini->WriteBool( "horas", "verV", verV->Checked );
ini->WriteString( "horas", "desdeV", desdeV->Text );
ini->WriteString( "horas", "hastaV", hastaV->Text );
ini->WriteBool( "horas", "verS", verS->Checked );
ini->WriteString( "horas", "desdeS", desdeS->Text );
ini->WriteString( "horas", "hastaS", hastaS->Text );
ini->WriteBool( "horas", "verD", verD->Checked );
ini->WriteString( "horas", "desdeD", desdeD->Text );
ini->WriteString( "horas", "hastaD", hastaD->Text );
if ( LEGO_COMM )
{
LEGO_COMM -> SetPuerto( ComboBox1->Items->Strings[ComboBox1->ItemIndex] );
LEGO_COMM -> SetDB( DB_host->Text, DB_login->Text, DB_pwd->Text );
LEGO_COMM -> SetDiasCortesia( ComboBox2->ItemIndex );
try {
LEGO_COMM -> maxDENTRO = Capacidad->Text.ToInt();
}catch(...){
LEGO_COMM -> maxDENTRO = 2000;
}
}
ini->UpdateFile();
delete ini;
}
// Hacemos efectivos los cambios...
ObtenDirLEGOS( DireccionesE->Text, nLEGOSe, &IDe );
ObtenDirLEGOS( DireccionesS->Text, nLEGOSs, &IDs );
if ( LEGO_COMM )
{
LEGO_COMM -> SetLegoID( nLEGOSe, IDe, nLEGOSs, IDs );
}
////////// HAY QUE FINALIZAR EL HILO Y COMENZAR CON OTRO NUEVO
bDesBlock->Visible = true;
ComboBox2->Enabled = false;
ComboBox1->Enabled = false;
DireccionesE->Enabled = false;
DireccionesS->Enabled = false;
Capacidad->Enabled = false;
MsgCapacidad->Enabled = false;
DB_host->Enabled = false;
DB_login->Enabled = false;
DB_pwd->Enabled = false;
bAplicar->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::ObtenDirLEGOS(AnsiString cadena, int &nLEGOS, int **ID)
{
char *pDir, buff[80], *pbuff;
nLEGOS = 0; int i = 1, j=0;
while ( i <= cadena.Length() )
{
if ( cadena[i] == ';' ) nLEGOS++;
i++;
}
(*ID) = new int[nLEGOS];
i = 1; pbuff = buff;
while ( i <= cadena.Length() )
{
if ( cadena[i] == ';' )
{
*pbuff = '\0';
try {
(*ID)[j++] = AnsiString( buff ).ToInt();
} catch(...) {
nLEGOS--;
ShowMessage( "La configuración de direcciones LEGO'S es incorrecta" );
}
pbuff = buff; i++;
}else{
*(pbuff++) = cadena[i];
i++;
}
}
}
//---------------------------------------------------------------------------
bool __fastcall TPropiedades::entreHora(AnsiString hLo, AnsiString hHi)
{
bool dev = false;
try {
TDateTime hora;
hora = EncodeTime( (hLo.SubString(1,2)).ToInt(), (hLo.SubString(4,2)).ToInt(), 0, 0 );
if ( TDateTime::CurrentTime() >= hora )
{
hora = EncodeTime( (hHi.SubString(1,2)).ToInt(), (hHi.SubString(4,2)).ToInt(), 0, 0 );
dev = ( TDateTime::CurrentTime() <= hora );
}
} catch (...) {
return false;
}
return dev;
}
//---------------------------------------------------------------------------
bool __fastcall TPropiedades::CheckHorario(void)
{
bool dev = false;
if ( CheckBox1->Checked )
{
switch ( TDateTime::CurrentDate().DayOfWeek() )
{
// Domingo
case 1:
if ( verD->Checked )
dev = entreHora( desdeD->Text, hastaD->Text );
break;
// Lunes
case 2:
if ( verL->Checked )
dev = entreHora( desdeL->Text, hastaL->Text );
break;
// Martes
case 3:
if ( verM->Checked )
dev = entreHora( desdeM->Text, hastaM->Text );
break;
// Miercoles
case 4:
if ( verX->Checked )
dev = entreHora( desdeX->Text, hastaX->Text );
break;
// Jueves
case 5:
if ( verJ->Checked )
dev = entreHora( desdeJ->Text, hastaJ->Text );
break;
// Viernes
case 6:
if ( verV->Checked )
dev = entreHora( desdeV->Text, hastaV->Text );
break;
// Sabado
case 7:
if ( verS->Checked )
dev = entreHora( desdeS->Text, hastaS->Text );
break;
}
}
return dev;
}
void __fastcall TPropiedades::Timer1Timer(TObject *Sender)
{
static bool horaVista = false, firstTime = true, cH;
bool nH;
nH = CheckHorario();
if ( firstTime ) { horaVista=false; firstTime = false; cH = nH; }
if ( cH != nH ) { horaVista = false; cH = nH; }
if ( !horaVista && cH ) // Si no hemos visto este cambio
{
horaVista = true;
sndPlaySound("snd\\fuera_de_hora.wav", SND_ASYNC | SND_FILENAME);
LEGO_COMM -> RetiraPermisoSalida();
}
}
//---------------------------------------------------------------------------
void __fastcall TPropiedades::Label1DblClick(TObject *Sender)
{
AnsiString cantidad;
// Cambio del aforo
if ( InputQuery( "Forzar lleno de aforo", "Nuevo aforo:" , cantidad ) )
{
try {
Ocupacion->Value = cantidad.ToInt();
LEGO_COMM -> crdDENTRO = Ocupacion->Value;
} catch(...){
//nothing
}
}
}
//---------------------------------------------------------------------------