簡體   English   中英

用python接收dbus信號

[英]receive dbus signals with python

我正在嘗試制作一個 python 腳本,它通過 dbus 偵聽信號並將它們存儲在一個文件中,因為我需要接收信號的服務發送一個始終在變化的哈希密碼。 我已經閱讀了這些手冊,但由於我對 Python 一無所知,因此我需要任何幫助。

import sys
import traceback
import gobject
import dbus
import dbus.mainloop.glib

def handle_reply(msg):
print msg

def handle_error(e):
print str(e)

def emit_signal():
#call the emitHelloSignal method 
object.emitHelloSignal(dbus_interface="com.example.TestService")
                      #reply_handler = handle_reply, error_handler = handle_error)
# exit after waiting a short time for the signal
gobject.timeout_add(2000, loop.quit)

if sys.argv[1:] == ['--exit-service']:
  object.Exit(dbus_interface='com.example.TestService')

return False

def hello_signal_handler(hello_string):
print ("Received signal (by connecting using remote object) and it says: "
       + hello_string)

def catchall_signal_handler(*args, **kwargs):
print ("Caught signal (in catchall handler) "
       + kwargs['dbus_interface'] + "." + kwargs['member'])
for arg in args:
    print "        " + str(arg)

def catchall_hello_signals_handler(hello_string):
print "Received a hello signal and it says " + hello_string

def catchall_testservice_interface_handler(hello_string, dbus_message):
print "com.example.TestService interface says " + hello_string + " when it    sent signal " + dbus_message.get_member()


if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

bus = dbus.SessionBus()
try:
    object  =  bus.get_object("com.example.TestService","/com/example/TestService/object")

    object.connect_to_signal("HelloSignal", hello_signal_handler,    dbus_interface="com.example.TestService", arg0="Hello")
    except dbus.DBusException:
    traceback.print_exc()
    print usage
    sys.exit(1)

#lets make a catchall
bus.add_signal_receiver(catchall_signal_handler,  interface_keyword='dbus_interface', member_keyword='member')

bus.add_signal_receiver(catchall_hello_signals_handler, dbus_interface =  "com.example.TestService", signal_name = "HelloSignal")

bus.add_signal_receiver(catchall_testservice_interface_handler,   dbus_interface = "com.example.TestService", message_keyword='dbus_message')

# Tell the remote object to emit the signal after a short delay
gobject.timeout_add(2000, emit_signal)

loop = gobject.MainLoop()
loop.run()

任何幫助表示贊賞

您可以使用 dbus-monitor 檢查信號是什么樣的

dbus-monitor --system "type='signal',sender='org.bluez'"

下面是我接收信號的python3示例代碼:

import dbus
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop

def signal_handler(*args, **kwargs):
    for i, arg in enumerate(args):
        print("arg:%d        %s" % (i, str(arg)))
    print('kwargs:')
    print(kwargs)
    print('---end----')

DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
#register your signal callback
bus.add_signal_receiver(signal_handler,
                        bus_name='org.bluez',
                        interface_keyword='interface',
                        member_keyword='member',
                        path_keyword='path',
                        message_keyword='msg')

loop = GLib.MainLoop()
loop.run()

修改 bus_name/sender 以適合您的情況

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM