lua - Lua:将使用 XML dbus 定义发出消息的 Python 代码翻译成 Lua
问题描述
我在对上一个问题的评论中提出了这个问题,但我认为最好将其作为一个新的独立问题移到这里。
我试图弄清楚如何使用 lgi DBus 翻译这个 Python 代码以将 dbus 信号发送到 Lua:
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)
我怀疑(完全不确定)必须向自省接口发送消息,类似于以下内容:
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 设置消息正文。
如果您能提供一些示例或指出我在哪里可以找到它,我将不胜感激!
谢谢!
解决方案
嘿,谷歌只是在查看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()
推荐阅读
- c++ - 按值捕获 lambda
- node.js - 如果我想运行一个每天有一个条目的 Heroku 应用程序,我需要多少个测功机?
- java - IntelliJ 无法打开 cp_init 重新映射的类缓存
- c - if(fp != EOF) -> 警告:指针和整数之间的比较
- python - 如何使用 Cyberduck 凭证通过 Python 访问 WebDAV
- apache-superset - 有没有人尝试过根据 preset.io 博客使用 Superset 制作自定义图表?
- javascript - 用 PHP 检测 javascript window.location.href
- excel - 在 VBA 代码上收到错误以在两个工作簿之间进行计算
- javascript - 独立于浏览器的字符串(DD-MON-YY)到日期转换
- jenkins - 如何仅使用 Jenkins 从存储库中提取特定文件夹?