如何检测我的系统何时通过 DBus 或类似 Python 应用程序从挂起状态唤醒?

如何检测我的系统何时通过 DBus 或类似 Python 应用程序从挂起状态唤醒?

在后台 Python 脚本中,我需要检测系统何时从挂起状态唤醒。有什么好方法可以不依赖于根脚本而是使用诸如 DBus 之类的 Python 模块?

我是 dbus 的新手,所以我确实需要一些示例代码。据我所知,它与

org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Resuming

有人能帮我提供一些将恢复信号与回调连接起来的代码吗?

答案1

下面是一些回答我的问题的示例代码:

#!/usr/bin/python
# This is some example code on howto use dbus to detect when the system returns
#+from hibernation or suspend.

import dbus      # for dbus communication (obviously)
import gobject   # main loop
from dbus.mainloop.glib import DBusGMainLoop # integration into the main loop

def handle_resume_callback():
    print "System just resumed from hibernate or suspend"

DBusGMainLoop(set_as_default=True) # integrate into main loob
bus = dbus.SystemBus()             # connect to dbus system wide
bus.add_signal_receiver(           # defince the signal to listen to
    handle_resume_callback,            # name of callback function
    'Resuming',                        # singal name
    'org.freedesktop.UPower',          # interface
    'org.freedesktop.UPower'           # bus name
)

loop = gobject.MainLoop()          # define mainloop
loop.run()                         # run main loop

查看dbus-python 教程

答案2

login1接口现在提供信号了,修改后的代码如下:

#!/usr/bin/python
# slightly different code for handling suspend resume
# using login1 interface signals
#
import dbus      # for dbus communication (obviously)
import gobject   # main loop
from dbus.mainloop.glib import DBusGMainLoop # integration into the main loop

def handle_sleep_callback(sleeping):
  if sleeping:
    print "System going to hibernate or sleep"
  else:
    print "System just resumed from hibernate or suspend"

DBusGMainLoop(set_as_default=True) # integrate into main loob
bus = dbus.SystemBus()             # connect to dbus system wide
bus.add_signal_receiver(           # defince the signal to listen to
    handle_sleep_callback,            # name of callback function
    'PrepareForSleep',                 # signal name
    'org.freedesktop.login1.Manager',   # interface
    'org.freedesktop.login1'            # bus name
)

loop = gobject.MainLoop()          # define mainloop
loop.run()                         # run main loop

相关内容