共计 3922 个字符,预计需要花费 10 分钟才能阅读完成。
1、写在后面
前篇咱们曾经实现了一个 Monitor
所须要大部分的内容。
然而,硬件资源的监控相对来说比拟麻烦。
不过,咱们能够借助 Qt,实际上它也提供了不少相干的 Api。
2、注释开始
当初咱们须要监控机器的硬件资源。
所谓硬件资源,次要分为三种:
1、获取磁盘应用:
Qt 中获取磁盘信息的类为 QStorageInfo
。
QStorageInfo
容许检索无关卷空间的信息,其装入点,标签和文件系统名称。您能够通过将卷的挂载点作为构造函数参数传递给函数的挂载点来创立 QStorageInfo 的实例,或者您能够应用 setPath() 办法设置它。动态挂载 volumes() 办法可用于获取所有已装置文件系统的列表。QStorageInfo 始终高速缓存检索到的信息,但能够调用 refresh() 来使缓存有效。
当初咱们应用他实现一个获取磁盘用量的通用接口:
QVariantList MonitorApi::getDrives()
{
QVariantList drives;
for (const QStorageInfo &storage: QStorageInfo::mountedVolumes()) {if (storage.isValid() && storage.isReady()) {if (!storage.isReadOnly()) {
QVariantMap drive;
uint driveType;
QString driveName = storage.name();
#ifdef Q_OS_WIN
driveType = GetDriveType(LPCWSTR(storage.rootPath().utf16()));
switch (driveType) {
case 2:
driveName = storage.name().isEmpty() ? QStringLiteral("U 盘") : storage.name();
break;
case 3:
driveName = storage.name().isEmpty() ? QStringLiteral("本地磁盘") : storage.name();
break;
case 4:
driveName = storage.name().isEmpty() ? QStringLiteral("网络磁盘") : storage.name();
break;
case 5:
driveName = storage.name().isEmpty() ? QStringLiteral("CD-ROM 驱动器") : storage.name();
break;
case 6:
driveName = storage.name().isEmpty() ? QStringLiteral("RAM 磁盘") : storage.name();
break;
default:
driveName = storage.name().isEmpty() ? QStringLiteral("磁盘") : storage.name();
break;
}
#endif
drive["drivePath"] = storage.rootPath();
drive["driveName"] = driveName;
drive["usedSize"] = storage.bytesTotal() - storage.bytesFree();
drive["totalSize"] = storage.bytesTotal();
drives.append(drive);
}
}
}
return drives;
}
当然,Windows
平台的磁盘名稍有不同。
2、获取 CPU 利用率:
qreal MonitorApi::getCpuUsageRate()
{
qreal usageRate = 0.0;
#ifdef Q_OS_WIN
static FILETIME preidleTime;
static FILETIME prekernelTime;
static FILETIME preuserTime;
FILETIME idleTime;
FILETIME kernelTime;
FILETIME userTime;
GetSystemTimes(&idleTime, &kernelTime, &userTime);
quint64 a, b;
quint64 idle, kernel, user;
a = (quint64(preidleTime.dwHighDateTime) << 32) | preidleTime.dwLowDateTime;
b = (quint64(idleTime.dwHighDateTime) << 32) | idleTime.dwLowDateTime;
idle = b -a;
a = (quint64(prekernelTime.dwHighDateTime) << 32) | prekernelTime.dwLowDateTime;
b = (quint64(kernelTime.dwHighDateTime) << 32) | kernelTime.dwLowDateTime;
kernel = b - a;
a = (quint64(preuserTime.dwHighDateTime) << 32) | preuserTime.dwLowDateTime;
b = (quint64(userTime.dwHighDateTime) << 32) | userTime.dwLowDateTime;
user = b - a;
usageRate = qreal(kernel + user - idle) / qreal(kernel + user);
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime;
#endif
return usageRate > 1.0 ? 1.0 : usageRate;
}
3、获取内存使用率:
QVariant MonitorApi::getMemoryStatus()
{
QVariantMap map;
#ifdef Q_OS_WIN
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx(&statex);
quint64 memoryAll = statex.ullTotalPhys;
quint64 memoryFree = statex.ullAvailPhys;
quint64 memoryUsed = memoryAll - memoryFree;
map["usedSize"] = memoryUsed;
map["totalSize"] = memoryAll;
#endif
return map;
}
4、获取网络流量:
QVariant MonitorApi::getNetworkStatus()
{
QVariantMap map;
#ifdef Q_OS_WIN
static bool initialized = false;
static quint64 preRecvTotal = 0;
static quint64 preSendTotal = 0;
quint64 recvTotal = 0;
quint64 sendTotal = 0;
MIB_IF_ROW2 *ifRow;
PMIB_IF_TABLE2 ifTable;
if (GetIfTable2(&ifTable) == NO_ERROR) {
QSet<QString> addressSet;
for (DWORD i = 0; i < ifTable->NumEntries; i++) {ifRow = (MIB_IF_ROW2 *)&ifTable->Table[i];
if (ifRow->Type == IF_TYPE_ETHERNET_CSMACD) {
QString addr;
for (ULONG i = 0; i < ifRow->PhysicalAddressLength; i++) {addr += QString::number(ifRow->PhysicalAddress[i], 16).rightJustified(2, '0');
}
if (!addressSet.contains(addr)) {addressSet.insert(addr);
recvTotal += ifRow->InOctets;
sendTotal += ifRow->OutOctets;
//qDebug() << i << addr << ifRow->InOctets << ifRow->OutOctets;}
}
}
FreeMibTable(ifTable);
}
if (!initialized) {
preRecvTotal = recvTotal;
preSendTotal = sendTotal;
initialized = true;
}
map["downloadSize"] = recvTotal - preRecvTotal;
map["uploadSize"] = sendTotal - preSendTotal;
map["trafficSize"] = (recvTotal + sendTotal) - (preRecvTotal + preSendTotal);
preRecvTotal = recvTotal;
preSendTotal = sendTotal;
#endif
return map;
}
对于 SkeyeARS
SkeyeARS 全景 AR 加强监视系统,是视开科技开发的一款基于宽场景多路视频无缝拼接、视频实时加强、监督指标加强显示、指标主动跟踪、视频存储回放、近程数据传输和多通道全景视频同步显示等性能的综合视频 AR 加强监视系统,广泛应用于智慧交通、智慧城市、智慧机场等大场景智能监控畛域。
具体阐明:http://www.openskeye.cn/web/product/ars
正文完