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

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

Python D-Bus: Subscribe to signal and read property with dasbus

问题

使用Python和dasbus如何通过D-Bus监视和读取Ubuntu的“Night Light”状态?我无法弄清楚API文档中如何读取属性或订阅信号。

可能的候选项:

以下内容改编自基本示例,并打印对象的接口和属性/信号:

  1. #!/usr/bin/env python3
  2. from dasbus.connection import SessionMessageBus
  3. bus = SessionMessageBus()
  4. # dasbus.client.proxy.ObjectProxy
  5. proxy = bus.get_proxy(
  6. "org.gnome.SettingsDaemon.Color", # 总线名称
  7. "/org/gnome/SettingsDaemon/Color", # 对象路径
  8. )
  9. print(proxy.Introspect())
  10. # 从接口“org.gnome.SettingsDaemon.Color”中读取并打印属性“NightLightActive”和“Temperature”(回调)函数
  11. # 订阅接口“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:

  1. #!/usr/bin/env python3
  2. from dasbus.connection import SessionMessageBus
  3. bus = SessionMessageBus()
  4. # dasbus.client.proxy.ObjectProxy
  5. proxy = bus.get_proxy(
  6. "org.gnome.SettingsDaemon.Color", # bus name
  7. "/org/gnome/SettingsDaemon/Color", # object path
  8. )
  9. print(proxy.Introspect())
  10. # read and print properties "NightLightActive" and "Temperature" from interface "org.gnome.SettingsDaemon.Color" in (callback) function
  11. # subscribe to signal "PropertiesChanged" in interface "org.freedesktop.DBus.Properties" / register callback function

<hr>
Resources

答案1

得分: 1

这是您提供的代码的中文翻译部分:

  1. # 从[1]中的示例和内省数据来看,似乎要获取 dasbus 的属性,需要使用 Python 风格的 `proxy.<属性名>`。
  2. # 对于您提到的 `NightLightActive` 示例,可以这样使用:
  3. print("是否开启夜灯?", proxy.NightLightActive)
  4. # 要连接信号,需要在代理上连接信号,形式如下: `proxy.<信号名>.connect`,例如:
  5. proxy.PropertiesChanged.connect(callback)
  6. # 这需要运行一个 `EventLoop`。
  7. # 我的整个测试示例如下:
  8. from dasbus.connection import SessionMessageBus
  9. from dasbus.loop import EventLoop
  10. bus = SessionMessageBus()
  11. loop = EventLoop()
  12. # 获取 dasbus.client.proxy.ObjectProxy
  13. proxy = bus.get_proxy(
  14. "org.gnome.SettingsDaemon.Color", # 总线名称
  15. "/org/gnome/SettingsDaemon/Color", # 对象路径
  16. )
  17. print("是否开启夜灯?", proxy.NightLightActive)
  18. print("温度设置为:", proxy.Temperature)
  19. def callback(iface, prop_changed, prop_invalidated):
  20. print("通知:", iface, prop_changed, prop_invalidated)
  21. proxy.PropertiesChanged.connect(callback)
  22. 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:

  1. 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:

  1. proxy.PropertiesChanged.connect(callback)

And this will need to have an EventLoop running.

My entire test was:

  1. from dasbus.connection import SessionMessageBus
  2. from dasbus.loop import EventLoop
  3. bus = SessionMessageBus()
  4. loop = EventLoop()
  5. # dasbus.client.proxy.ObjectProxy
  6. proxy = bus.get_proxy(
  7. &quot;org.gnome.SettingsDaemon.Color&quot;, # bus name
  8. &quot;/org/gnome/SettingsDaemon/Color&quot;, # object path
  9. )
  10. print(&quot;Night light active?&quot;, proxy.NightLightActive)
  11. print(&quot;Temperature is set to:&quot;, proxy.Temperature)
  12. def callback(iface, prop_changed, prop_invalidated):
  13. print(&quot;The notification:&quot;,
  14. iface, prop_changed, prop_invalidated)
  15. proxy.PropertiesChanged.connect(callback)
  16. 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:

确定