英文:
Python D-Bus: Subscribe to signal and read property with dasbus
问题
使用Python和dasbus如何通过D-Bus监视和读取Ubuntu的“Night Light”状态?我无法弄清楚API文档中如何读取属性或订阅信号。
可能的候选项:
以下内容改编自基本示例,并打印对象的接口和属性/信号:
#!/usr/bin/env python3
from dasbus.connection import SessionMessageBus
bus = SessionMessageBus()
# dasbus.client.proxy.ObjectProxy
proxy = bus.get_proxy(
"org.gnome.SettingsDaemon.Color", # 总线名称
"/org/gnome/SettingsDaemon/Color", # 对象路径
)
print(proxy.Introspect())
# 从接口“org.gnome.SettingsDaemon.Color”中读取并打印属性“NightLightActive”和“Temperature”(回调)函数
# 订阅接口“org.freedesktop.DBus.Properties”的信号“PropertiesChanged”/注册回调函数
资源链接:
- https://pypi.org/project/dbus-python/
- https://stackoverflow.com/questions/58639602/what-is-recommended-to-use-pydbus-or-dbus-python-and-what-are-the-differences
- https://wiki.python.org/moin/DbusExamples
- https://stackoverflow.com/questions/54113389/migration-from-dbus-to-gdbus-in-python-3
英文:
How to monitor and read Ubuntu's "Night Light" status via D-Bus using Python with dasbus? I can't figure out the API docs on how to read a property or subscribe to a signal.
Likely candidates:
The following is adapted from the basic examples and prints the interfaces and properties/signals of the object:
#!/usr/bin/env python3
from dasbus.connection import SessionMessageBus
bus = SessionMessageBus()
# dasbus.client.proxy.ObjectProxy
proxy = bus.get_proxy(
"org.gnome.SettingsDaemon.Color", # bus name
"/org/gnome/SettingsDaemon/Color", # object path
)
print(proxy.Introspect())
# read and print properties "NightLightActive" and "Temperature" from interface "org.gnome.SettingsDaemon.Color" in (callback) function
# subscribe to signal "PropertiesChanged" in interface "org.freedesktop.DBus.Properties" / register callback function
<hr>
Resources
答案1
得分: 1
这是您提供的代码的中文翻译部分:
# 从[1]中的示例和内省数据来看,似乎要获取 dasbus 的属性,需要使用 Python 风格的 `proxy.<属性名>`。
# 对于您提到的 `NightLightActive` 示例,可以这样使用:
print("是否开启夜灯?", proxy.NightLightActive)
# 要连接信号,需要在代理上连接信号,形式如下: `proxy.<信号名>.connect`,例如:
proxy.PropertiesChanged.connect(callback)
# 这需要运行一个 `EventLoop`。
# 我的整个测试示例如下:
from dasbus.connection import SessionMessageBus
from dasbus.loop import EventLoop
bus = SessionMessageBus()
loop = EventLoop()
# 获取 dasbus.client.proxy.ObjectProxy
proxy = bus.get_proxy(
"org.gnome.SettingsDaemon.Color", # 总线名称
"/org/gnome/SettingsDaemon/Color", # 对象路径
)
print("是否开启夜灯?", proxy.NightLightActive)
print("温度设置为:", proxy.Temperature)
def callback(iface, prop_changed, prop_invalidated):
print("通知:", iface, prop_changed, prop_invalidated)
proxy.PropertiesChanged.connect(callback)
loop.run()
英文:
Looking at the dasbus examples and the Introspection data it looks like to get the property the dasbus is pythonic so proxy.<property name>
works. For your example of NightLightActive
it would be:
print("Night light active?", proxy.NightLightActive)
For the signal you need to connect to the signal on the proxy so that seems to take the form of proxy.<signal name>.connect
so for example:
proxy.PropertiesChanged.connect(callback)
And this will need to have an EventLoop
running.
My entire test was:
from dasbus.connection import SessionMessageBus
from dasbus.loop import EventLoop
bus = SessionMessageBus()
loop = EventLoop()
# dasbus.client.proxy.ObjectProxy
proxy = bus.get_proxy(
"org.gnome.SettingsDaemon.Color", # bus name
"/org/gnome/SettingsDaemon/Color", # object path
)
print("Night light active?", proxy.NightLightActive)
print("Temperature is set to:", proxy.Temperature)
def callback(iface, prop_changed, prop_invalidated):
print("The notification:",
iface, prop_changed, prop_invalidated)
proxy.PropertiesChanged.connect(callback)
loop.run()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论