cacic_hardware.cpp
5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "cacic_hardware.h"
cacic_hardware::cacic_hardware()
{
}
void cacic_hardware::iniciaColeta()
{
#ifdef Q_OS_WIN
this->coletaHardware = coletaWin();
#elif defined(Q_OS_LINUX)
this->coletaHardware = coletaLinux();
#endif
}
#ifdef Q_OS_WIN
QJsonObject cacic_hardware::coletaWin()
{
QAxObject *objIWbemLocator = new QAxObject("WbemScripting.SWbemLocator");
QAxObject *objWMIService = objIWbemLocator->querySubObject("ConnectServer(QString&,QString&)",QString("."),QString("root\\cimv2"));
if (objWMIService->isNull())
{
return QJsonObject();
}
QAxObject *returnList = objWMIService->querySubObject("ExecQuery(QString&)",QString("select SystemType from win32_computersystem"));
QAxObject *enum1 = returnList->querySubObject("_NewEnum");
IEnumVARIANT* enumInterface; //to get this, include <windows.h>
enum1->queryInterface(IID_IEnumVARIANT, (void**)&enumInterface);
enumInterface->Reset(); //start at the beginning of the list.
for (int i=0;i<returnList->dynamicCall("Count").toInt();i++){
VARIANT *theItem;
enumInterface->Next(1,theItem,NULL);
QAxObject *item = new QAxObject((IUnknown *)theItem->punkVal);
qDebug() << item->dynamicCall("Caption");
}
return QJsonObject();
}
#elif Q_OS_LINUX
QJsonObject cacic_hardware::coletaLinux()
{
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"] == QJsonValue::fromVariant(QString("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
*/
if( component["id"] == QJsonValue::fromVariant(QString("memory")) ) {
coletaLinuxMem(hardware,component);
} else if ( component["id"] == QJsonValue::fromVariant(QString("cpu")) ) {
coletaLinuxCpu(hardware,component);
} else if ( component["id"] == QJsonValue::fromVariant(QString("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 pciMember;
if ( pciJson["id"] == QJsonValue::fromVariant(QString("multimedia")) ) {
pciMember["description"] = pciJson["description"];
pciMember["product"] = pciJson["product"];
pciMember["vendor"] = pciJson["vendor"];
hardware["multimedia"] = pciMember;
} else if (pciJson["id"].toString().contains("pci:") ) {
QJsonArray pciChildren = pciJson["children"].toArray();
foreach( QJsonValue pciChild, pciChildren ) {
QJsonObject pciChildJson = pciChild.toObject();
if( pciChildJson["id"] == QJsonValue::fromVariant(QString("network")) &&
( pciChildJson["description"].toString().contains("Wireless") ||
pciChildJson["product"].toString().contains("Wireless") )) {
pciMember["description"] = pciChildJson["description"];
pciMember["product"] = pciChildJson["product"];
pciMember["vendor"] = pciChildJson["vendor"];
pciMember["logicalname"] = pciChildJson["logicalname"];
pciMember["serial"] = pciChildJson["serial"];
pciMember["firmware"] = pciChildJson["configuration"].toObject()["firmware"];
hardware["wireless_card"] = pciMember;
} else if( pciChildJson["id"] == QJsonValue::fromVariant(QString("network")) ) {
pciMember["description"] = pciChildJson["description"];
pciMember["product"] = pciChildJson["product"];
pciMember["vendor"] = pciChildJson["vendor"];
pciMember["logicalname"] = pciChildJson["logicalname"];
pciMember["serial"] = pciChildJson["serial"];
pciMember["capacity"] = QJsonValue::fromVariant(
oCacic.convertDouble(pciChildJson["capacity"].toDouble(), 0) +
" bits/s" );
hardware["ethernet_card"] = pciMember;
}
}
}
}
#endif
QJsonObject cacic_hardware::toJsonObject() {
return coletaHardware;
}