From 71c0c105ca8a6632c40fbc2575a92c7628f009c2 Mon Sep 17 00:00:00 2001 From: Thiago Rocha Date: Thu, 21 Aug 2014 12:40:52 -0300 Subject: [PATCH] Coleta de hardware no linux iniciada, já salvando informação de cpu e memória no JSON. --- src/cacic_hardware.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/cacic_hardware.h | 6 ++++++ src/ccoleta.cpp | 3 ++- 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/src/cacic_hardware.cpp b/src/cacic_hardware.cpp index 8a350ab..ade6511 100644 --- a/src/cacic_hardware.cpp +++ b/src/cacic_hardware.cpp @@ -10,7 +10,6 @@ void cacic_hardware::iniciaColeta() this->coletaHardware = coletaWin(); #elif defined(Q_OS_LINUX) this->coletaHardware = coletaLinux(); - #endif } @@ -24,5 +23,87 @@ QJsonObject cacic_hardware::coletaWin() QJsonObject cacic_hardware::coletaLinux() { - return QJsonObject(); + + OperatingSystem operatingSystem; + ConsoleObject console; + + QJsonObject hardware; + + QFile lshwFile("lshwJson.json"); + if( lshwFile.exists() ) + lshwFile.remove(); + + console("lshw -json >> lshwJson.json"); + + + QJsonObject lshwJson = oCacic.getJsonFromFile("lshwJson.json")["children"].toArray().first().toObject(); + + if( lshwJson.contains("id") && lshwJson["id"] == "core") { + if ( lshwJson["children"].isArray() ){ + qDebug() << "IS ARRAY!!"; + QJsonArray componentsArray = lshwJson["children"].toArray(); + + foreach(QJsonValue componentValue, componentsArray ) { + QJsonObject component = componentValue.toObject(); + + /* TODO: + * - Formatar direito as quantidades (memória,clock do cpu) + * com unidades mais amigáveis para humanos em todos métodos. + * + * coletaLinuxMem + * coletaLinuxCpu + * coletaLinuxPci - a fazer + */ + + if( component["id"] == "memory" ) { + coletaLinuxMem(hardware,component); + } else if ( component["id"] == "cpu" ) { + coletaLinuxCpu(hardware,component); + } else if ( component["id"] == "pci" ) { + QJsonArray pciArray = component["children"].toArray(); + + foreach(QJsonValue pciValue, pciArray){ + QJsonObject pciObject = pciValue.toObject(); + + coletaLinuxPci(hardware, pciObject); + } + + } + + } + } + + } + + + return hardware; +} + +void cacic_hardware::coletaLinuxMem(QJsonObject &hardware, const QJsonObject &component) +{ + QJsonObject memory; + + memory["size"] = QJsonValue::fromVariant(oCacic.convertDouble(component["size"].toDouble(),0) + " bytes"); + + hardware["memory"] = memory; +} + +void cacic_hardware::coletaLinuxCpu(QJsonObject &hardware, const QJsonObject &component) +{ + QJsonObject cpu; + + cpu["name"] = component["product"]; + cpu["vendor"] = component["vendor"]; + cpu["clock"] = QJsonValue::fromVariant(oCacic.convertDouble(component["capacity"].toDouble(),0) + " Hz"); + + hardware["cpu"] = cpu; +} + +void cacic_hardware::coletaLinuxPci(QJsonObject &hardware, const QJsonObject &pciJson) +{ + +} + +QJsonObject cacic_hardware::toJsonObject() { + return coletaHardware; } diff --git a/src/cacic_hardware.h b/src/cacic_hardware.h index bc4e2c4..b2230e4 100644 --- a/src/cacic_hardware.h +++ b/src/cacic_hardware.h @@ -1,6 +1,9 @@ #ifndef CACIC_HARDWARE_H #define CACIC_HARDWARE_H #include +#include +#include +#include #include #include #include @@ -25,6 +28,9 @@ public: private: QJsonObject coletaWin(); QJsonObject coletaLinux(); + void coletaLinuxMem(QJsonObject &hardware, const QJsonObject &component); + void coletaLinuxCpu(QJsonObject &hardware, const QJsonObject &component); + void coletaLinuxPci(QJsonObject &hardware, const QJsonObject &pciJson); CCacic oCacic; QJsonObject coletaHardware; diff --git a/src/ccoleta.cpp b/src/ccoleta.cpp index 4be5478..3a322fb 100644 --- a/src/ccoleta.cpp +++ b/src/ccoleta.cpp @@ -8,7 +8,7 @@ CColeta::CColeta(QObject *parent) void CColeta::coletaHardware() { qDebug() << "coletaHardware() executado"; -// oHardware.iniciaColeta(); + oHardware.iniciaColeta(); } @@ -55,5 +55,6 @@ QJsonObject CColeta::toJsonObject() QJsonObject coletaJson; coletaJson["computer"] = oComputer.toJsonObject(); coletaJson["software"] = oSoftware.toJsonObject(); + coletaJson["hardware"] = oHardware.toJsonObject(); return coletaJson; } -- libgit2 0.21.2