python 如何检查驱动器的电源状态?

polhcujo  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(100)

bounty还有6天到期。回答此问题可获得+50声望奖励。Marus Gradinaru正在寻找这个问题的更详细的答案:奖金将给予向我展示如何执行org.freedesktop.UDisks2.Drive.Ata.PmGetState方法来读取驱动器电源状态的用户。

我尝试在Linux/Raspberry Pi下使用Python读取驱动器电源状态(活动/待机/睡眠)。
我找到了here this answer,它说我可以用udisks2 dbus来做:

you can get around this by invoking a dbus method on the system bus:

Service: org.freedesktop.UDisks2
Object Path: /org/freedesktop/UDisks2/drives/<ID of the hard drive>
Method: org.freedesktop.UDisks2.Drive.Ata.PmGetState

但我不知道如何实施...
我设法编写了这段代码,执行时没有错误,但我不知道如何继续它...你能帮我吗?

from pydbus import SystemBus

def get_drive_power_state(drive_path):
    bus = SystemBus()
    udisks = bus.get(".UDisks2")
    drive_obj = bus.get("org.freedesktop.UDisks2", drive_path)

    return None

drive_path = "/org/freedesktop/UDisks2/drives/WDC_WD20NMVW_11EDZS3_WD_WXV1EA57RT6E"
power_state = get_drive_power_state(drive_path)

print(f"Drive Power State: {power_state}")
ws51t4hk

ws51t4hk1#

在DBus对象上调用方法时,需要指定该方法属于哪个DBus接口。
在本例中,PmGetState方法属于org.freedesktop.UDisks2.Drive.Ata接口,因此这就是我们在方法调用中指定的接口。

import dbus

def get_drive_power_state(drive_path):
    bus = dbus.SystemBus()
    proxy = bus.get_object("org.freedesktop.UDisks2", drive_path)
    iface = dbus.Interface(proxy, "org.freedesktop.UDisks2.Drive.Ata")
    
    power_state = iface.PmGetState({})
    
    return power_state

drive_path = "/org/freedesktop/UDisks2/drives/WDC_WD20NMVW_11EDZS3_WD_WXV1EA57RT6E"
power_state = get_drive_power_state(drive_path)

print(f"Drive Power State: {power_state}")

相关问题