Lua:将使用 XML dbus 定义发出消息的 Python 代码转换为 Lua

我在以前的问题的评论中问了这个问题,但我认为把它移到单独的新问题中会更好。

我正在尝试弄清楚如何使用 lgi DBus 将此 Python 代码转换为 Lua,以发出 dbus 信号:

class DBUSTestInterface(object):
    """
    Server_XML definition.
    Emit / Publish a signal that is a random integer every second
    type='i' for integer.
    """
    dbus = """
    <node>
        <interface name="com.test.device.aaa">
            <signal name="get">
                <arg type='s'/>
                <arg type='s'/>
                <arg type='s'/>
                <arg type='s'/>
                <arg type='s'/>
                <arg type='s'/>
                <arg type='s'/>
                <arg type='i'/>
            </signal>
        </interface>
    </node>
    """
    get = signal()

emit = DBUSTestInterface()
bus.publish("com.test.device.get", emit)

我怀疑(完全不确定)需要向 introspectable 接口发送消息,类似于以下内容:

local object = "/org/freedesktop/DBus"
local interface = "org.freedesktop.DBus.Introspectable"
local method = "Introspect"
local message = Gio.DBusMessage.new_method_call(name, object, interface, method)
message:set_body(GLib.Variant("(aoo)", {{location},session})) -- How do I set the same message as above?

但我不确定,也不知道如何设置与 Python 中使用的 XML 相同的消息正文。

如果您可以提供一些示例或指出我可以找到它的位置,我会非常感激!

谢谢!

点赞
用户436275
用户436275

Heh,我在查看 https://github.com/pavouk/lgi/issues/220的时候被谷歌引导到了这里。

我觉得你的代码示例可能不能直接运行或者不是一些独立的Python代码。因此,我会按照文本中的注释进行操作:

每秒发出/发布一个随机整数的信号

以下是此操作的Lua代码(当然,“随机整数”不包括在内,除非你认为42是随机的):

local lgi = require(“lgi”)
local Gio,GLib,GObject = lgi.Gio,lgi.GLib,lgi.GObject

local conn

GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT,1,function()
    if conn then
        conn:emit_signal(nil,“/your/example/has/no/path”,
            “com.test.device.aaa”,“get”,
            GLib.Variant(”(sssssssi)”,{“what”,“are”,“all”,
            “these”,“strings”,“for”,“?” ,42}))
    end
    return true
end)

local function on_bus_acquire(con)
    conn = con

    local function arg(name,signature)
        return Gio.DBusArgInfo {name = name,signature = signature }
    end
    local interface_info = Gio.DBusInterfaceInfo {
        name =“com.test.device.aaa”,
        signals = {
            Gio.DBusSignalInfo {
                name =“get”,
                args = {
                    arg(“no_name?!?”,“s”),
                    arg(“no_name?!?”,“s”),
                    arg(“no_name?!?”,“s”),
                    arg(“no_name?!?”,“s”),
                    arg(“no_name?!?”,“s”),
                    arg(“no_name?!?”,“s”),
                    arg(“no_name?!?”,“s”),
                    arg(“no_name?!?”,“i”)
                }
            }
        }
    }
    conn:register_object(“/your/example/has/no/path”,interface_info,nil)
end

Gio.bus_own_name(Gio.BusType.SESSION,“com.test.device.get”,Gio.BusNameOwnerFlags.NONE,
    GObject.Closure(on_bus_acquire),nil,nil)

GLib.MainLoop.new():run()
2019-03-11 17:02:45