2018/6/14

dbus an simple service example in python

ref: Register a “Hello World” DBus service, object and method using Python

service 的 source code 就是..
import gobject
import dbus
import dbus.service

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)


OPATH = "/com/example/HelloHell"
IFACE = "com.example.HelloHell"
BUS_NAME = "com.example.HelloHell"


class Example(dbus.service.Object):
        def __init__(self):
                bus = dbus.SessionBus()
                bus.request_name(BUS_NAME)
                bus_name = dbus.service.BusName(BUS_NAME, bus=bus)
                dbus.service.Object.__init__(self, bus_name, OPATH)

        @dbus.service.method(dbus_interface=IFACE + ".SayHello",
                        in_signature="", out_signature="")
        def SayHello(self):
                print "hello, world"


if __name__ == "__main__":
        a = Example()
        loop = gobject.MainLoop()
        loop.run()
用 python run 起來後,用 dbus-send 送message 給'他',就會print Hello 出來..
dbus-send --session --print-reply --type=method_call --dest=com.example.HelloHell /com/example/HelloHell com.example.HelloHell.SayHello.SayHello

沒有留言:

張貼留言