Python D-Bus:使用dasbus订阅信号并读取属性

huangapple go评论65阅读模式
英文:

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”/注册回调函数

资源链接:

英文:

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.&lt;property name&gt; works. For your example of NightLightActive it would be:

print(&quot;Night light active?&quot;, proxy.NightLightActive)

For the signal you need to connect to the signal on the proxy so that seems to take the form of proxy.&lt;signal name&gt;.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(
    &quot;org.gnome.SettingsDaemon.Color&quot;,  # bus name
    &quot;/org/gnome/SettingsDaemon/Color&quot;,  # object path
)

print(&quot;Night light active?&quot;, proxy.NightLightActive)
print(&quot;Temperature is set to:&quot;, proxy.Temperature)


def callback(iface, prop_changed, prop_invalidated):
    print(&quot;The notification:&quot;,
          iface, prop_changed, prop_invalidated)


proxy.PropertiesChanged.connect(callback)

loop.run()

huangapple
  • 本文由 发表于 2023年2月8日 16:25:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定