desarrollosconsejeria/siaticControl/3.3-13/Siatic_d/siaticsessionmanager.cpp @ 29401061
29401061 | Elisa | #include "siaticsessionmanager.h"
|
|
SiaticSessionManager::SiaticSessionManager(char argChar, QObject *parent) :
|
|||
QObject(parent)
|
|||
{
|
|||
this->argChar = argChar;
|
|||
sesion = new DLinkPasswordGen();
|
|||
connect(&Conectando,SIGNAL(timeout()),this,SLOT(IntentarConectar()));
|
|||
}
|
|||
void SiaticSessionManager::init(){
|
|||
opciones.cargaVariablesEntorno();
|
|||
printf("Procesando opciones\n");
|
|||
switch (argChar){
|
|||
case '1':
|
|||
estadoProyector(opciones.proyectorInicio());
|
|||
estadoRele(opciones.sonidoInicio());
|
|||
break;
|
|||
case '0':
|
|||
estadoProyector(opciones.proyectorFin());
|
|||
estadoRele(opciones.sonidoFin());
|
|||
break;
|
|||
default:
|
|||
printf("Argumento no reconocido");
|
|||
emit finished();
|
|||
return;
|
|||
}
|
|||
if (opciones.getPuntoAccesoActivo()=="-1"){
|
|||
qDebug()<<"Cerrando aplicación";
|
|||
exit(1);
|
|||
return;
|
|||
}
|
|||
sesion->PWD_DEFAULT=opciones.claveRoot();
|
|||
sesion->HOST_ROUTER=opciones.IP();
|
|||
connect(sesion, SIGNAL(onLoginSuccess()), this, SLOT(onLoginSuccess()));
|
|||
sesion->ConnectRouter();
|
|||
Conectando.start(ROUTER_CONNECT_TIMEOUT);
|
|||
}
|
|||
void SiaticSessionManager::onLoginSuccess(){
|
|||
//qDebug() << "INICIANDO PROGRAMA " << endl;
|
|||
if(opciones.ActualizarClaveWifi()==1){
|
|||
srand(time(NULL));
|
|||
char rndChar = (rand()%('z'-'a'))+'a';
|
|||
int rndNum = rand()%999;
|
|||
char *key = (char*) malloc(10);
|
|||
sprintf(key, "%c%03d%c%03d",rndChar,rndNum,rndChar,rndNum);
|
|||
//Obtengo la clave del fichero de texto
|
|||
printf("%s\n", key);
|
|||
ofstream out;
|
|||
out.open("/tmp/clave.txt");
|
|||
out<<key;
|
|||
out.close();
|
|||
}
|
|||
switch (argChar){
|
|||
case '1':
|
|||
procesarOpciones(false);
|
|||
break;
|
|||
case '0':
|
|||
procesarOpciones(true);
|
|||
break;
|
|||
}
|
|||
}
|
|||
void SiaticSessionManager::IntentarConectar(){
|
|||
if(sesion->Ready){
|
|||
//qDebug()<<"Conectado";
|
|||
Conectando.stop();
|
|||
}else{
|
|||
//qDebug()<<"Reintentando";
|
|||
disconnect(sesion, SIGNAL(onLoginSuccess()), this, SLOT(onLoginSuccess()));
|
|||
delete sesion;
|
|||
REINTENTOS++;
|
|||
if(REINTENTOS>6){
|
|||
Conectando.stop();
|
|||
sesion->DisconnectRouter();
|
|||
//qDebug() << "No se intentara mas por WiFi";
|
|||
exit(1);
|
|||
}
|
|||
sesion = new DLinkPasswordGen();
|
|||
sesion->HOST_ROUTER=opciones.IP();
|
|||
sesion->PWD_DEFAULT=opciones.claveRoot();
|
|||
connect(sesion, SIGNAL(onLoginSuccess()), this, SLOT(onLoginSuccess()));
|
|||
sesion->ConnectRouter();
|
|||
}
|
|||
}
|
|||
//@param estado: 1=Encender ; 0=Apagar ; 2=No hacer Nada ;
|
|||
void SiaticSessionManager::estadoWifi(int estado, bool fin){
|
|||
qDebug()<<"Wifi"<<estado;
|
|||
//printf("%s\n", "INICIANDO ESTADO WIFI");
|
|||
QString sshCommand;
|
|||
if(estado==1){
|
|||
sshCommand += "ifconfig ra0 up";
|
|||
}
|
|||
if (estado==0){
|
|||
sshCommand += "ifconfig ra0 down";
|
|||
}
|
|||
if(opciones.ActualizarClaveWifi()==1 && fin){
|
|||
//printf("%s\n", "Almacenando Clave");
|
|||
sesion->GeneratePWD(opciones.SuperUser(), sshCommand, opciones.claveRoot());
|
|||
//printf("%s\n", "Fin Clave");
|
|||
}else{
|
|||
if(estado==1){
|
|||
//printf("%s\n", "ConectarWIFI");
|
|||
sesion->ConectarWIFI();
|
|||
}
|
|||
if (estado==0){
|
|||
//printf("%s\n", "DesconectarWIFI");
|
|||
sesion->DesconectarWIFI();
|
|||
}
|
|||
}
|
|||
sleep(3);
|
|||
return;
|
|||
}
|
|||
//@param estado: 1=Encender ; 0=Apagar ; 2=No hacer Nada ;
|
|||
void SiaticSessionManager::estadoRele(int estado){
|
|||
//printf("Estado rele\n");
|
|||
if(estado==2){return;}
|
|||
//qDebug()<<"Rele"<<estado;
|
|||
RedRelayLibrary rele;
|
|||
if(rele.ConnectRelay()){
|
|||
if(estado==1){
|
|||
if(rele.GetRelayState(PORT_THREE)==OFF){
|
|||
rele.SetRelayState(ON,PORT_THREE);
|
|||
}
|
|||
}else{
|
|||
if(rele.GetRelayState(PORT_THREE)==ON){
|
|||
rele.SetRelayState(OFF,PORT_THREE);
|
|||
}
|
|||
}
|
|||
rele.DisconnectRelay();
|
|||
}
|
|||
return;
|
|||
}
|
|||
//@param estado: 1=Encender ; 0=Apagar ; 2=No hacer Nada ;
|
|||
void SiaticSessionManager::estadoProyector(int estado){
|
|||
//printf("Estado proyector\n");
|
|||
if(estado==2){return;}
|
|||
qDebug()<<"Proyector"<<estado;
|
|||
int cport_nr=0, // /dev/ttyS0 (COM1 on windows)
|
|||
bdrate=9600; // 9600 baud
|
|||
char mode[]={'8','N','1',0},
|
|||
str[2][512];
|
|||
strcpy(str[0], "\x7E\x30\x30\x30\x30\x20\x31\x0D"); //ENCIENDE
|
|||
strcpy(str[1], "\x7E\x30\x30\x30\x30\x20\x32\x0D"); //APAGA
|
|||
RS232_OpenComport(cport_nr, bdrate, mode);
|
|||
if(estado==1){
|
|||
RS232_cputs(cport_nr, str[0]);
|
|||
}else{
|
|||
RS232_cputs(cport_nr, str[1]);
|
|||
}
|
|||
RS232_CloseComport(cport_nr);
|
|||
}
|
|||
//@param: fin puede ser cierto o falso, si en falso procesa las opciones para arranque, sino para cierre
|
|||
void SiaticSessionManager::procesarOpciones(bool fin){
|
|||
if(fin){ //Opciones de Cierre
|
|||
estadoWifi(opciones.wifiFin(), fin);
|
|||
}else{ //Opciones de inicio
|
|||
estadoWifi(opciones.wifiInicio(), fin);
|
|||
}
|
|||
emit finished();
|
|||
}
|