Commit b3f29df01cdfed37cc4d1e4e973ce8795228350d
1 parent
48bd8e5a
Exists in
master
refactoring
Showing
2 changed files
with
71 additions
and
83 deletions
Show diff stats
cacic-daemon/cacicD/cacictimer.cpp
| ... | ... | @@ -22,93 +22,14 @@ void CacicTimer::iniciarTimer(int x) |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | void CacicTimer::mslot(){ |
| 25 | - QLogger::QLog_Info("Cacic Daemon (Timer)", QString("mslot();")); | |
| 26 | 25 | cMutex->lock(); |
| 27 | 26 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Semáforo fechado.")); |
| 28 | - | |
| 29 | 27 | if(getTest()){ |
| 30 | 28 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getTeste() success.")); |
| 31 | - | |
| 32 | 29 | if(getConfig()){ |
| 33 | 30 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getConfig() success.")); |
| 34 | - | |
| 35 | - // Compara o novo arquivo de configuração com um antigo e se forem diferentes | |
| 36 | - // mantem o mais recente; caso iguais simplesmente apaga o novo. | |
| 37 | - QFile *fileOld; | |
| 38 | - QFile *fileNew; | |
| 39 | - | |
| 40 | - fileOld = new QFile(getApplicationDirPath().append("/getConfig.json")); | |
| 41 | - fileNew = new QFile(getApplicationDirPath().append("/getConfigNew.json")); | |
| 42 | - if( fileOld->exists() && fileNew->exists() ){ | |
| 43 | - if( Md5IsEqual(QVariant::fromValue(fileOld), QVariant::fromValue(fileNew)) ) { | |
| 44 | - fileNew->remove(); | |
| 45 | - } else { | |
| 46 | - // Renomeia getConfigNew.json para getConfig.json | |
| 47 | - fileOld->remove(); | |
| 48 | - fileNew->rename("getConfigNew.json","getConfig.json"); | |
| 49 | - } | |
| 50 | - jsonConfig = ccacic->getJsonFromFile(getApplicationDirPath().append("/getConfig.json")); | |
| 51 | - } else if( fileOld->exists() ){ | |
| 52 | - jsonConfig = ccacic->getJsonFromFile(getApplicationDirPath().append("/getConfig.json")); | |
| 53 | - } else { | |
| 54 | - QLogger::QLog_Error("Cacic Daemon (Timer)", "Arquivo de configuração não criado."); | |
| 55 | - } | |
| 56 | - delete fileOld; | |
| 57 | - delete fileNew; | |
| 58 | - | |
| 59 | - lerArquivoConfig(jsonConfig); | |
| 60 | - | |
| 61 | - int countExecNotFound = 0; | |
| 62 | - QMap<QString, QString>::const_iterator mapIterator = moduleMap.constBegin(); | |
| 63 | - while (mapIterator != moduleMap.constEnd()) { | |
| 64 | - QString nomeModulo = mapIterator.value(); | |
| 65 | - QString hashModulo = mapIterator.key(); | |
| 66 | - | |
| 67 | - // Calcula hash do binario atual | |
| 68 | -#if defined(Q_OS_WIN) | |
| 69 | - fileOld = new QFile(getApplicationDirPath().append("/").append(modulo).append(".exe")); | |
| 70 | -#else | |
| 71 | - fileOld = new QFile(getApplicationDirPath().append("/").append(nomeModulo)); | |
| 72 | -#endif | |
| 73 | - if(!fileOld->exists()) { | |
| 74 | - QLogger::QLog_Error("Cacic Daemon (Timer)", QString("Módulo ").append(nomeModulo).append(" não encontrado.")); | |
| 75 | - | |
| 76 | - countExecNotFound++; | |
| 77 | - if( countExecNotFound == moduleMap.size() ) { | |
| 78 | - QLogger::QLog_Error("Cacic Daemon (Timer)", "Não foi possível encontrar nenhum módulo executável!"); | |
| 79 | - return; | |
| 80 | - } | |
| 81 | - continue; | |
| 82 | - } | |
| 83 | - | |
| 84 | - QString oldMd5 = QString(QCryptographicHash::hash(fileOld->readAll(),QCryptographicHash::Md5).toHex()); | |
| 85 | - | |
| 86 | - if ( oldMd5 != hashModulo ) { | |
| 87 | - | |
| 88 | - fileOld->rename(getApplicationDirPath().append("/").append(nomeModulo), | |
| 89 | - getApplicationDirPath().append("/").append("nomeModulo").append("Old") ); | |
| 90 | - | |
| 91 | - // Download nova versão do executável | |
| 92 | - QList<QMap<QString,QString> >::const_iterator metodosIterator = metodosDownload.constBegin(); | |
| 93 | - bool downloadSucess = false; | |
| 94 | - while ( !downloadSucess && metodosIterator != metodosDownload.constEnd() ) { | |
| 95 | - | |
| 96 | - if( metodosIterator->value("tipo") == "ftp" || metodosIterator->value("tipo") == "" ) { | |
| 97 | - if ( OCacicComm->ftpDownload( metodosIterator->value("url"), metodosIterator->value("path") ) ) | |
| 98 | - downloadSucess = true; | |
| 99 | - | |
| 100 | - } else if ( metodosIterator->value("tipo") == "http" ) { | |
| 101 | - if( OCacicComm->httpDownload( metodosIterator->value("url"), metodosIterator->value("path") ) ) | |
| 102 | - downloadSucess = true; | |
| 103 | - } | |
| 104 | - metodosIterator++; | |
| 105 | - } | |
| 106 | - fileOld->remove(); | |
| 107 | - delete fileOld; | |
| 108 | - } | |
| 109 | - | |
| 110 | - mapIterator++; | |
| 111 | - } | |
| 31 | + verificarModulos(); | |
| 32 | + iniciarGercols(); | |
| 112 | 33 | }else{ |
| 113 | 34 | qDebug() << "getConfig() failed. - " + QDateTime::currentDateTime().toLocalTime().toString(); |
| 114 | 35 | QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na obtenção do arquivo de configuração."); |
| ... | ... | @@ -117,7 +38,6 @@ void CacicTimer::mslot(){ |
| 117 | 38 | qDebug() << "getTest() failed. - " + QDateTime::currentDateTime().toLocalTime().toString(); |
| 118 | 39 | QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na execução do getTest()."); |
| 119 | 40 | } |
| 120 | - | |
| 121 | 41 | cMutex->unlock(); |
| 122 | 42 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Semáforo aberto.")); |
| 123 | 43 | } |
| ... | ... | @@ -277,3 +197,71 @@ void CacicTimer::definirDirGercols(QString appDirPath){ |
| 277 | 197 | setDirProgram(appDirPath + "/cacic-gercols"); |
| 278 | 198 | #endif |
| 279 | 199 | } |
| 200 | + | |
| 201 | +void CacicTimer::verificarModulos(){ | |
| 202 | + // Compara o novo arquivo de configuração com um antigo e se forem diferentes | |
| 203 | + // mantem o mais recente; caso iguais simplesmente apaga o novo. | |
| 204 | + QFile *fileOld; | |
| 205 | + QFile *fileNew; | |
| 206 | + fileOld = new QFile(this->applicationDirPath + "/getConfig.json"); | |
| 207 | + fileNew = new QFile(this->applicationDirPath + "/getConfigNew.json"); | |
| 208 | + if( fileOld->exists() && fileNew->exists() ){ | |
| 209 | + if( Md5IsEqual(QVariant::fromValue(fileOld), QVariant::fromValue(fileNew)) ) { | |
| 210 | + fileNew->remove(); | |
| 211 | + } else { | |
| 212 | + // Renomeia getConfigNew.json para getConfig.json | |
| 213 | + fileOld->remove(); | |
| 214 | + fileNew->rename("getConfigNew.json","getConfig.json"); | |
| 215 | + } | |
| 216 | + jsonConfig = ccacic->getJsonFromFile(this->applicationDirPath + "/getConfig.json"); | |
| 217 | + } else if( fileOld->exists() ){ | |
| 218 | + jsonConfig = ccacic->getJsonFromFile(this->applicationDirPath + "/getConfig.json"); | |
| 219 | + } else { | |
| 220 | + QLogger::QLog_Error("Cacic Daemon (Timer)", "Arquivo de configuração não criado."); | |
| 221 | + } | |
| 222 | + delete fileOld; | |
| 223 | + delete fileNew; | |
| 224 | + lerArquivoConfig(jsonConfig); | |
| 225 | + int countExecNotFound = 0; | |
| 226 | + QMap<QString, QString>::const_iterator mapIterator = moduleMap.constBegin(); | |
| 227 | + while (mapIterator != moduleMap.constEnd()) { | |
| 228 | + QString nomeModulo = mapIterator.value(); | |
| 229 | + QString hashModulo = mapIterator.key(); | |
| 230 | + // Calcula hash do binario atual | |
| 231 | +#if defined(Q_OS_WIN) | |
| 232 | + fileOld = new QFile(this->applicationDirPath + "/" + modulo + ".exe"); | |
| 233 | +#else | |
| 234 | + fileOld = new QFile(this->applicationDirPath + "/" + nomeModulo); | |
| 235 | +#endif | |
| 236 | + if(!fileOld->exists()) { | |
| 237 | + QLogger::QLog_Error("Cacic Daemon (Timer)", QString("Módulo ").append(nomeModulo).append(" não encontrado.")); | |
| 238 | + countExecNotFound++; | |
| 239 | + if( countExecNotFound == moduleMap.size() ) { | |
| 240 | + QLogger::QLog_Error("Cacic Daemon (Timer)", "Não foi possível encontrar nenhum módulo executável!"); | |
| 241 | + return; | |
| 242 | + } | |
| 243 | + continue; | |
| 244 | + } | |
| 245 | + QString oldMd5 = QString(QCryptographicHash::hash(fileOld->readAll(),QCryptographicHash::Md5).toHex()); | |
| 246 | + if ( oldMd5 != hashModulo ) { | |
| 247 | + fileOld->rename(this->applicationDirPath + "/" + nomeModulo, | |
| 248 | + this->applicationDirPath + "/" + nomeModulo + "Old"); | |
| 249 | + // Download nova versão do executável | |
| 250 | + QList<QMap<QString,QString> >::const_iterator metodosIterator = metodosDownload.constBegin(); | |
| 251 | + bool downloadSucess = false; | |
| 252 | + while ( !downloadSucess && metodosIterator != metodosDownload.constEnd() ) { | |
| 253 | + if( metodosIterator->value("tipo") == "ftp" || metodosIterator->value("tipo") == "" ) { | |
| 254 | + if ( OCacicComm->ftpDownload( metodosIterator->value("url"), metodosIterator->value("path") ) ) | |
| 255 | + downloadSucess = true; | |
| 256 | + } else if ( metodosIterator->value("tipo") == "http" ) { | |
| 257 | + if( OCacicComm->httpDownload( metodosIterator->value("url"), metodosIterator->value("path") ) ) | |
| 258 | + downloadSucess = true; | |
| 259 | + } | |
| 260 | + metodosIterator++; | |
| 261 | + } | |
| 262 | + fileOld->remove(); | |
| 263 | + delete fileOld; | |
| 264 | + } | |
| 265 | + mapIterator++; | |
| 266 | + } | |
| 267 | +} | ... | ... |
cacic-daemon/cacicD/cacictimer.h
| ... | ... | @@ -36,9 +36,9 @@ public: |
| 36 | 36 | private: |
| 37 | 37 | void registraFimColeta(QString msg); |
| 38 | 38 | void registraInicioColeta(); |
| 39 | + void verificarModulos(); | |
| 39 | 40 | QLogger::QLoggerManager *logManager; |
| 40 | 41 | QString dirProgram; |
| 41 | - //QProcess *myProcess; | |
| 42 | 42 | QString applicationDirPath; |
| 43 | 43 | QString getDirProgram() const; |
| 44 | 44 | void iniciarGercols(); | ... | ... |