检测电源更改事件/ UPS状态

时间:2019-01-26 23:04:50

标签: c++ qt winapi

我想检测系统何时打开UPS或仅更改电源状态。

类似的事情:

enter image description here

我尝试使用GetSystemPowerStatus函数检测到它:

代码:

    SYSTEM_POWER_STATUS powerStatus;
    bool powerStatusRes = GetSystemPowerStatus(&powerStatus);

    switch (powerStatus.ACLineStatus) {
        case 0:
            std::cout << "Offline" << std::endl;
            break;
        case 1:
            std::cout << "Online" << std::endl;
            break;
        case 255:
            std::cout << "Unknown status" << std::endl;
        default:
            std::cout << "Failed to detect the status" << std::endl;
    }

    switch (powerStatus.BatteryFlag) {
        case 1:
            std::cout << "High — the battery capacity is at more than 66 percent" << std::endl;
            break;
        case 2:
            std::cout << "Low — the battery capacity is at less than 33 percent" << std::endl;
            break;
        case 4:
            std::cout << "Critical — the battery capacity is at less than five percent" << std::endl;
            break;
        case 8:
            std::cout << "Charging" << std::endl;
            break;
        case 128:
            std::cout << "No system battery" << std::endl;
            break;
        case 255:
            std::cout << "Unknown status—unable to read the battery flag information" << std::endl;
            break;
        default:
            std::cout << "Failed to detect the battery flag" << std::endl;
    }

但是对于powerStatus.ACLineStatusUPSACOnline-powerStatus.BatteryFlag返回No system battery

我还尝试使用UPS函数检测GetPwrCapabilities

代码:

SYSTEM_POWER_CAPABILITIES SysPowerCapabilities = {0};

if (!GetPwrCapabilities(&SysPowerCapabilities)){
    std::cout << "Failed to get System Power information!" << std::endl;
}

if (SysPowerCapabilities.UpsPresent) {
    std::cout << "UPS found" << std::endl;
} else {
    std::cout << "UPS not found" << std::endl;
}

返回-UPS not found(该驱动程序不适用于此UPS模型)。

现在,我正在尝试使用Qt nativeEvent函数来检测功率变化:

bool Test::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    Q_UNUSED(result);
    Q_UNUSED(eventType);
    MSG *msg = static_cast<MSG*>(message);

    if (msg->message == WM_POWERBROADCAST && msg->wParam == PBT_APMPOWERSTATUSCHANGE) {
        qDebug() << "Power changed!!!";
    }

    return false;
}

但是没有任何内容打印到控制台上。有什么想法如何检测它吗?谢谢。

更新时间:2019年3月2日

我发现我需要使用HID API来获取UPS的一些值。来自网络的UPS的某些值。

 0x00840010 = UPS
 0x00840012 = Battery
 0x00840030 = Voltage
 0x00840040 = ConfigVoltage
 0x0084001a = Input
 0x0084005a = AudibleAlarmControl
 0x00840002 = PresentStatus
 0x00850044 = Charging
 0x00850045 = Discharging
 0x008500d0 = ACPresent

更新的代码:

   QString devicePath = "\\\\?\\HID#VID_....";
   HANDLE fileHandle = CreateFileA(devicePath.toStdString().c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);

   if (fileHandle != INVALID_HANDLE_VALUE) {
       std::cout << "Success. Device opened for reading..." << std::endl;
       PHIDP_PREPARSED_DATA preparsedData;
       HIDP_CAPS capabilities;
       HIDD_ATTRIBUTES attributes;
       BOOL hidPreparsedRes = HidD_GetPreparsedData(fileHandle, &preparsedData);

       if (hidPreparsedRes) {
           if (HidD_GetAttributes(fileHandle, &attributes)) {
               std::cout << "Product ID: " << attributes.ProductID << std::endl;
               std::cout << "Size: " << attributes.Size << std::endl;
               std::cout << "Vendor ID: " << attributes.VendorID << std::endl;
               std::cout << "Version number: " << attributes.VersionNumber << std::endl;

              if (HidP_GetCaps(preparsedData, &capabilities) == HIDP_STATUS_SUCCESS) {
                  std::cout << "Caps: " << capabilities.NumberOutputValueCaps << std::endl;
              } else {
                  std::cout << "Failed to return HID capabilities!" << std::endl;
              }
           } else {
              std::cout << "Failed to get HID attributes" << std::endl;
           }

           std::cout << getLastErrorAsString() << std::endl;
       } else {
           std::cout << "Failed to get preparsed data!" << std::endl;
       }

       HidD_FreePreparsedData(preparsedData);
   } else {
       std::cout << "Failed to open device for reading!" << std::endl;
   }

   CloseHandle(fileHandle);

现在,它返回:

成功。设备已打开以供读取... 产品编号:20833 大小:12 供应商ID:1637 版本号:2 上限:1

HIDP_CAPS结构具有很多不同的值,问题是如何将它们转换为UPS值以检查功率变化/ UPS状态?谢谢。

0 个答案:

没有答案