【问题标题】:How do I expose a signal to DBUS in Python?如何在 Python 中向 DBUS 公开信号?
【发布时间】:2019-05-16 01:12:17
【问题描述】:

我非常了解如何创建方法 - 但是,我希望使用 Python 公开一些信号,以便可以一次调用多个程序。

class Bildschirm(dbus.service.Object):
    DBUS_NAME = 'hm.retro.Retro'
    DBUS_OBJECT_PATH = '/hm/retro/Retro'
    DBUS_INTERFACE = 'hm.retro.Retro'

    def __init__(self, _Visio):
        self.Visio = _Visio
        self.bus = dbus.SessionBus()
        bus_name = dbus.service.BusName(self.DBUS_NAME, bus=self.bus)

        super().__init__(bus_name, self.DBUS_OBJECT_PATH)


    @dbus.service.method(DBUS_INTERFACE, in_signature='i')
    def delta(self, nr):
        weiter = self.Visio.akt_sender_nr
        weiter = (weiter+nr) % SENDER_AM
        self.Visio.akt_sender_nr = weiter
        self.Visio.update_sender_labels()

        self.Visio.screen.force_update()
        self.Visio.screen.draw_next_frame()

    @dbus.service.signal(DBUS_INTERFACE)
    def config_change_visio(self):
        self.Visio.load_config()
        self.Visio.update_sender_labels()
        self.Visio.update_preset_labels()

        self.Visio.screen.force_update()
        self.Visio.screen.draw_next_frame()

但是,如果我尝试使用 dbus-send 调用信号:

dbus-send --session --print-reply --dest=hm.retro.Retro /hm/retro/Retro hm.retro.Retro.config_change_visio

我收到一条错误消息:

    Error org.freedesktop.DBus.Error.UnknownMethod: Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/dbus/service.py", line 654, in _message_cb
    (candidate_method, parent_method) = _method_lookup(self, method_name, interface_name)
  File "/usr/lib/python3/dist-packages/dbus/service.py", line 246, in _method_lookup
    raise UnknownMethodException('%s is not a valid method of interface %s' % (method_name, dbus_interface))
dbus.exceptions.UnknownMethodException: org.freedesktop.DBus.Error.UnknownMethod: Unknown method: config_change_visio is not a valid method of interface hm.retro.Retro

我做错了什么?

【问题讨论】:

    标签: python-3.x dbus


    【解决方案1】:

    如果您将--print-reply 传递给dbus-send,则意味着您发送的消息类型是方法调用,因为只有方法调用才能得到回复。 dbus-send 手册页说:

       --print-reply
           Block for a reply to the message sent, and print any reply received in
           a human-readable form. It also means the message type (--type=) is
           method_call.
    

    从您的dbus-send 调用中删除--print-reply 参数,并添加--type=signal,它应该可以工作。


    请注意,虽然它不应该影响此错误,但 D-Bus 信号名称以 CamelCase 格式而不是 underscore_case 格式更为传统。 D-Bus specification 说:

    D-Bus 上的成员名称通常由大写字母组成 没有标点符号的单词(“camel-case”)。方法名通常应该 是动词,例如 GetItems,信号名称通常应该是 事件的描述,例如 ItemsChanged。

    所以也许你的信号应该被称为VisioConfigChanged。同样,也许你的方法应该被称为AddDelta

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 2012-06-11
      • 2016-09-16
      • 1970-01-01
      • 2016-07-16
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      相关资源
      最近更新 更多